@atmo-dev/kit 0.21.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/components/RichText.svelte +17 -9
- package/dist/components/RichTextEditor.svelte +67 -11
- package/dist/components/RichTextEditor.svelte.d.ts +0 -2
- package/dist/components/StreamplacePicker.svelte +2 -2
- package/dist/components/StreamplacePicker.svelte.d.ts +1 -1
- package/dist/components/composer/EmojiPanel.svelte +55 -0
- package/dist/components/composer/EmojiPanel.svelte.d.ts +6 -0
- package/dist/components/composer/EmojiPicker.svelte +2 -38
- package/dist/components/data/DataLabel.svelte +120 -0
- package/dist/components/data/DataLabel.svelte.d.ts +10 -0
- package/dist/components/data/issues.d.ts +5 -0
- package/dist/components/data/issues.js +52 -0
- package/dist/components/data/types.d.ts +28 -0
- package/dist/components/data/types.js +1 -0
- package/dist/components/data/visibility.d.ts +7 -0
- package/dist/components/data/visibility.js +15 -0
- package/dist/components/legal/links.js +1 -0
- package/dist/components/legal/types.d.ts +1 -1
- package/dist/components/notifications/NotificationList.svelte +137 -0
- package/dist/components/notifications/NotificationList.svelte.d.ts +19 -0
- package/dist/components/notifications/NotificationPushToggle.svelte +76 -0
- package/dist/components/notifications/NotificationPushToggle.svelte.d.ts +19 -0
- package/dist/components/notifications/NotificationSettings.svelte +125 -0
- package/dist/components/notifications/NotificationSettings.svelte.d.ts +16 -0
- package/dist/components/notifications/types.d.ts +37 -0
- package/dist/components/notifications/types.js +1 -0
- package/dist/editor/ImageDialog.svelte +87 -0
- package/dist/editor/ImageDialog.svelte.d.ts +16 -0
- package/dist/editor/image-node.d.ts +6 -2
- package/dist/editor/image-node.js +36 -37
- package/dist/events/EventCard.svelte +6 -1
- package/dist/events/VodPlayer.svelte +1 -1
- package/dist/events/atmo-rsvp.d.ts +6 -0
- package/dist/events/atmo-rsvp.js +34 -0
- package/dist/events/atproto-helpers.d.ts +1 -0
- package/dist/events/atproto-helpers.js +3 -0
- package/dist/events/event-link-embed.d.ts +7 -3
- package/dist/events/event-link-embed.js +11 -5
- package/dist/events/event-view/StreamPlacePlayer.svelte +14 -108
- package/dist/events/linked-records.d.ts +1 -1
- package/dist/events/linked-records.js +2 -2
- package/dist/events-card.d.ts +9 -0
- package/dist/events-card.js +7 -0
- package/dist/events-data.d.ts +1 -0
- package/dist/events-data.js +1 -0
- package/dist/grain/xrpc.js +11 -4
- package/dist/index.d.ts +13 -1
- package/dist/index.js +7 -1
- package/dist/legal.d.ts +3 -0
- package/dist/legal.js +2 -0
- package/dist/richtext/serialize.js +13 -8
- package/dist/streamplace/StreamCard.svelte +184 -0
- package/dist/streamplace/StreamCard.svelte.d.ts +24 -0
- package/dist/streamplace/StreamLinkEmbed.svelte +59 -0
- package/dist/streamplace/StreamLinkEmbed.svelte.d.ts +5 -0
- package/dist/streamplace/StreamPlayer.svelte +539 -0
- package/dist/streamplace/StreamPlayer.svelte.d.ts +42 -0
- package/dist/streamplace/VideoCard.svelte +159 -0
- package/dist/streamplace/VideoCard.svelte.d.ts +17 -0
- package/dist/streamplace/api.d.ts +74 -0
- package/dist/streamplace/api.js +182 -0
- package/dist/streamplace/broadcast.d.ts +3 -0
- package/dist/streamplace/broadcast.js +110 -0
- package/dist/streamplace/channel.svelte.d.ts +74 -0
- package/dist/streamplace/channel.svelte.js +515 -0
- package/dist/streamplace/chat/Composer.svelte +232 -0
- package/dist/streamplace/chat/Composer.svelte.d.ts +15 -0
- package/dist/streamplace/chat/Embeds.svelte +135 -0
- package/dist/streamplace/chat/Embeds.svelte.d.ts +13 -0
- package/dist/streamplace/chat/Message.svelte +303 -0
- package/dist/streamplace/chat/Message.svelte.d.ts +25 -0
- package/dist/streamplace/chat/StreamChat.svelte +463 -0
- package/dist/streamplace/chat/StreamChat.svelte.d.ts +33 -0
- package/dist/streamplace/chat/StreamChatPopups.svelte +151 -0
- package/dist/streamplace/chat/StreamChatPopups.svelte.d.ts +15 -0
- package/dist/streamplace/chat/UserCard.svelte +76 -0
- package/dist/streamplace/chat/UserCard.svelte.d.ts +12 -0
- package/dist/streamplace/chat/color.d.ts +7 -0
- package/dist/streamplace/chat/color.js +33 -0
- package/dist/streamplace/chat/links.d.ts +10 -0
- package/dist/streamplace/chat/links.js +29 -0
- package/dist/streamplace/facets.d.ts +15 -0
- package/dist/streamplace/facets.js +112 -0
- package/dist/streamplace/link-embed.d.ts +13 -0
- package/dist/streamplace/link-embed.js +16 -0
- package/dist/streamplace/links.d.ts +6 -0
- package/dist/streamplace/links.js +1 -0
- package/dist/streamplace/reactions.d.ts +21 -0
- package/dist/streamplace/reactions.js +46 -0
- package/dist/streamplace/types.d.ts +200 -0
- package/dist/streamplace/types.js +1 -0
- package/dist/streamplace-data.d.ts +11 -0
- package/dist/streamplace-data.js +10 -0
- package/dist/streamplace.d.ts +14 -6
- package/dist/streamplace.js +11 -1
- package/dist/{events/event-view → video}/Player.svelte +69 -21
- package/dist/video/Player.svelte.d.ts +38 -0
- package/dist/video/engines.d.ts +59 -0
- package/dist/video/engines.js +647 -0
- package/package.json +11 -1
- package/dist/events/event-view/Player.svelte.d.ts +0 -24
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/* eslint-disable svelte/prefer-svelte-reactivity --
|
|
2
|
+
The plain collections here are bookkeeping that must not re-render anything; what the UI
|
|
3
|
+
reads is published as whole new values in `$state.raw` fields. */
|
|
4
|
+
import { SvelteMap } from 'svelte/reactivity';
|
|
5
|
+
import { appviewUrl } from '../events/atproto-helpers.js';
|
|
6
|
+
import { NODE_WS } from './api.js';
|
|
7
|
+
import { detectFacets } from './facets.js';
|
|
8
|
+
import { groupReactions, reactionTarget } from './reactions.js';
|
|
9
|
+
const MAX_MESSAGES = 500;
|
|
10
|
+
/** A stream counts as live while segments keep arriving. */
|
|
11
|
+
const LIVE_WINDOW = 15_000;
|
|
12
|
+
const timeOf = (message) => {
|
|
13
|
+
const created = Date.parse(message.record.createdAt);
|
|
14
|
+
const indexed = Date.parse(message.indexedAt);
|
|
15
|
+
// A client clock in the future must not pin a message to the bottom.
|
|
16
|
+
if (Number.isNaN(created))
|
|
17
|
+
return indexed || 0;
|
|
18
|
+
return Number.isNaN(indexed) ? created : Math.min(created, indexed);
|
|
19
|
+
};
|
|
20
|
+
/** One streamer's live channel: stream state, viewers and chat over a single socket. */
|
|
21
|
+
export class Channel {
|
|
22
|
+
user;
|
|
23
|
+
connected = $state(false);
|
|
24
|
+
/** Whether it is known yet if the stream is live. */
|
|
25
|
+
settled = $state(false);
|
|
26
|
+
profile = $state.raw(null);
|
|
27
|
+
livestream = $state.raw(null);
|
|
28
|
+
viewers = $state(null);
|
|
29
|
+
viewTotal = $state(null);
|
|
30
|
+
segment = $state.raw(null);
|
|
31
|
+
renditions = $state.raw([]);
|
|
32
|
+
problems = $state.raw([]);
|
|
33
|
+
messages = $state.raw([]);
|
|
34
|
+
/** Reactions by the message they are on. They are replies of one emoji, kept out of `messages`. */
|
|
35
|
+
reactions = $state.raw(new Map());
|
|
36
|
+
pinned = $state.raw(null);
|
|
37
|
+
teleport = $state.raw(null);
|
|
38
|
+
/** Everyone seen in chat, for mentions and name colours. */
|
|
39
|
+
authors = new SvelteMap();
|
|
40
|
+
#lastSegmentAt = $state(-Infinity);
|
|
41
|
+
#now = $state(Date.now());
|
|
42
|
+
live = $derived(this.#now - this.#lastSegmentAt < LIVE_WINDOW);
|
|
43
|
+
ended = $derived(!!this.livestream?.record.endedAt);
|
|
44
|
+
#lastSegmentStart = 0;
|
|
45
|
+
#lastSegmentReceivedAt = 0;
|
|
46
|
+
#write;
|
|
47
|
+
#socket;
|
|
48
|
+
#closed = true;
|
|
49
|
+
#settle;
|
|
50
|
+
#attempt = 0;
|
|
51
|
+
#retry;
|
|
52
|
+
#clock;
|
|
53
|
+
#queue = [];
|
|
54
|
+
#flush;
|
|
55
|
+
#index = new Map();
|
|
56
|
+
#pin = null;
|
|
57
|
+
#pendingHides = new Map();
|
|
58
|
+
#hidden = new Set();
|
|
59
|
+
#blocked = new Set();
|
|
60
|
+
#canceled = new Set();
|
|
61
|
+
/** The published copy of each reply, reused while its quote is unchanged so rows stay put. */
|
|
62
|
+
#quoted = new WeakMap();
|
|
63
|
+
constructor(user, initial) {
|
|
64
|
+
this.user = user;
|
|
65
|
+
this.#write = initial?.write;
|
|
66
|
+
if (initial?.livestream) {
|
|
67
|
+
this.livestream = initial.livestream;
|
|
68
|
+
this.profile = initial.livestream.author;
|
|
69
|
+
this.viewers = initial.livestream.viewerCount?.count ?? null;
|
|
70
|
+
}
|
|
71
|
+
if (initial?.live !== undefined) {
|
|
72
|
+
// The server's answer holds until the socket reports segments itself.
|
|
73
|
+
if (initial.live)
|
|
74
|
+
this.#lastSegmentAt = Date.now();
|
|
75
|
+
this.settled = true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Opens the socket. Does nothing while already connected, connecting or waiting to retry. */
|
|
79
|
+
connect() {
|
|
80
|
+
if (!this.#closed)
|
|
81
|
+
return;
|
|
82
|
+
this.#closed = false;
|
|
83
|
+
this.#clock = setInterval(() => (this.#now = Date.now()), 2000);
|
|
84
|
+
this.#open();
|
|
85
|
+
}
|
|
86
|
+
/** Closes the socket and drops anything not yet applied. `connect()` starts again. */
|
|
87
|
+
close() {
|
|
88
|
+
this.#closed = true;
|
|
89
|
+
clearTimeout(this.#retry);
|
|
90
|
+
clearTimeout(this.#settle);
|
|
91
|
+
clearTimeout(this.#flush);
|
|
92
|
+
this.#flush = undefined;
|
|
93
|
+
this.#queue = [];
|
|
94
|
+
clearInterval(this.#clock);
|
|
95
|
+
this.#clock = undefined;
|
|
96
|
+
const socket = this.#socket;
|
|
97
|
+
this.#socket = undefined;
|
|
98
|
+
socket?.close();
|
|
99
|
+
this.connected = false;
|
|
100
|
+
}
|
|
101
|
+
#open() {
|
|
102
|
+
const socket = new WebSocket(`${NODE_WS}/api/websocket/${encodeURIComponent(this.user)}`);
|
|
103
|
+
this.#socket = socket;
|
|
104
|
+
socket.onopen = () => {
|
|
105
|
+
if (this.#socket !== socket)
|
|
106
|
+
return;
|
|
107
|
+
// The snapshot only includes an active pin or teleport. Absence means none.
|
|
108
|
+
this.#pin = this.pinned = null;
|
|
109
|
+
this.teleport = null;
|
|
110
|
+
this.#attempt = 0;
|
|
111
|
+
this.connected = true;
|
|
112
|
+
// Channels that never streamed send no segment at all.
|
|
113
|
+
clearTimeout(this.#settle);
|
|
114
|
+
this.#settle = setTimeout(() => (this.settled = true), 2500);
|
|
115
|
+
};
|
|
116
|
+
socket.onmessage = (event) => {
|
|
117
|
+
if (this.#socket !== socket)
|
|
118
|
+
return;
|
|
119
|
+
try {
|
|
120
|
+
this.#queue.push(JSON.parse(event.data));
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// Bursts (history on connect, busy chats) become one update.
|
|
126
|
+
this.#flush ??= setTimeout(() => {
|
|
127
|
+
this.#flush = undefined;
|
|
128
|
+
const batch = this.#queue;
|
|
129
|
+
this.#queue = [];
|
|
130
|
+
this.#apply(batch);
|
|
131
|
+
}, 60);
|
|
132
|
+
};
|
|
133
|
+
socket.onclose = () => {
|
|
134
|
+
if (this.#socket !== socket)
|
|
135
|
+
return;
|
|
136
|
+
this.connected = false;
|
|
137
|
+
if (this.#closed)
|
|
138
|
+
return;
|
|
139
|
+
this.#attempt += 1;
|
|
140
|
+
this.#retry = setTimeout(() => this.#open(), Math.min(10_000, 500 * 2 ** this.#attempt));
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Applies events as if the node had sent them: history an app loaded itself, or fixtures
|
|
145
|
+
* in tests and examples. The socket is not needed for this.
|
|
146
|
+
*/
|
|
147
|
+
receive(events) {
|
|
148
|
+
this.#apply(events);
|
|
149
|
+
}
|
|
150
|
+
#apply(batch) {
|
|
151
|
+
let chatChanged = false;
|
|
152
|
+
for (const data of batch) {
|
|
153
|
+
switch (data.$type) {
|
|
154
|
+
case 'place.stream.livestream#livestreamView':
|
|
155
|
+
this.livestream = data;
|
|
156
|
+
break;
|
|
157
|
+
case 'place.stream.livestream#viewerCount':
|
|
158
|
+
this.viewers = data.count;
|
|
159
|
+
if (typeof data.total === 'number')
|
|
160
|
+
this.viewTotal = data.total;
|
|
161
|
+
break;
|
|
162
|
+
case 'app.bsky.actor.defs#profileViewBasic':
|
|
163
|
+
this.profile = data;
|
|
164
|
+
break;
|
|
165
|
+
case 'place.stream.segment': {
|
|
166
|
+
const segment = data;
|
|
167
|
+
this.segment = segment;
|
|
168
|
+
const start = Date.parse(segment.startTime) || 0;
|
|
169
|
+
const end = start + (segment.duration ?? 0) / 1e6;
|
|
170
|
+
const now = Date.now();
|
|
171
|
+
// A strictly newer segment arriving in a later batch proves the stream is live
|
|
172
|
+
// even when this device's clock is ahead of the server's. On the initial burst,
|
|
173
|
+
// fall back to the segment's timestamp so a past stream never looks live.
|
|
174
|
+
const advancing = this.#lastSegmentStart > 0 &&
|
|
175
|
+
start > this.#lastSegmentStart &&
|
|
176
|
+
now - this.#lastSegmentReceivedAt >= 200;
|
|
177
|
+
if (start > this.#lastSegmentStart)
|
|
178
|
+
this.#lastSegmentStart = start;
|
|
179
|
+
this.#lastSegmentReceivedAt = now;
|
|
180
|
+
this.#lastSegmentAt = Math.max(this.#lastSegmentAt, advancing ? now : Math.min(now, end || 0));
|
|
181
|
+
this.#now = now;
|
|
182
|
+
this.settled = true;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case 'place.stream.defs#renditions':
|
|
186
|
+
this.renditions = (data.renditions ?? []).map((r) => r.name);
|
|
187
|
+
break;
|
|
188
|
+
case 'place.stream.ingest.defs#problems':
|
|
189
|
+
this.problems = data.problems ?? [];
|
|
190
|
+
break;
|
|
191
|
+
case 'place.stream.chat.defs#messageView':
|
|
192
|
+
chatChanged = this.#message(data) || chatChanged;
|
|
193
|
+
break;
|
|
194
|
+
case 'place.stream.defs#blockView': {
|
|
195
|
+
const subject = data.record?.subject;
|
|
196
|
+
if (!subject)
|
|
197
|
+
break;
|
|
198
|
+
this.#blocked.add(subject);
|
|
199
|
+
for (const [uri, message] of this.#index) {
|
|
200
|
+
if (message.author.did === subject)
|
|
201
|
+
this.#index.delete(uri);
|
|
202
|
+
}
|
|
203
|
+
chatChanged = true;
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
case 'place.stream.chat.gate': {
|
|
207
|
+
const uri = data.hiddenMessage;
|
|
208
|
+
if (typeof uri !== 'string' || !uri)
|
|
209
|
+
break;
|
|
210
|
+
this.#hidden.add(uri);
|
|
211
|
+
this.#index.delete(uri);
|
|
212
|
+
// Even when the message itself is not loaded, a quote or the pin may show it.
|
|
213
|
+
chatChanged = true;
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
case 'place.stream.chat.defs#pinnedRecordView': {
|
|
217
|
+
this.#pin = data;
|
|
218
|
+
this.#publishPin();
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
case 'place.stream.chat.pinnedRecord':
|
|
222
|
+
if (data.deleted)
|
|
223
|
+
this.#pin = this.pinned = null;
|
|
224
|
+
break;
|
|
225
|
+
case 'place.stream.live.teleport': {
|
|
226
|
+
const version = `${data.uri}#${data.cid}`;
|
|
227
|
+
if (this.#canceled.has(version) || this.#canceled.has(String(data.uri)))
|
|
228
|
+
break;
|
|
229
|
+
this.teleport = data;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
case 'place.stream.livestream#teleportCanceled': {
|
|
233
|
+
const uri = String(data.teleportUri);
|
|
234
|
+
this.#canceled.add(data.cid ? `${uri}#${data.cid}` : uri);
|
|
235
|
+
const active = this.teleport;
|
|
236
|
+
if (active &&
|
|
237
|
+
(!active.uri || active.uri === uri) &&
|
|
238
|
+
(!data.cid || !active.cid || active.cid === data.cid))
|
|
239
|
+
this.teleport = null;
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case 'place.stream.livestream#teleportArrival': {
|
|
243
|
+
const source = data.source;
|
|
244
|
+
if (!source?.did || typeof data.teleportUri !== 'string')
|
|
245
|
+
break;
|
|
246
|
+
const uri = `system:${data.teleportUri}`;
|
|
247
|
+
if (this.#index.has(uri))
|
|
248
|
+
break;
|
|
249
|
+
const count = data.viewerCount ?? 0;
|
|
250
|
+
this.#index.set(uri, {
|
|
251
|
+
uri,
|
|
252
|
+
cid: '',
|
|
253
|
+
author: source,
|
|
254
|
+
chatProfile: data.chatProfile,
|
|
255
|
+
indexedAt: data.startsAt,
|
|
256
|
+
system: 'teleport',
|
|
257
|
+
record: {
|
|
258
|
+
$type: 'place.stream.chat.message',
|
|
259
|
+
text: `teleported ${count} ${count === 1 ? 'viewer' : 'viewers'} here`,
|
|
260
|
+
createdAt: data.startsAt,
|
|
261
|
+
streamer: ''
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
chatChanged = true;
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (chatChanged)
|
|
270
|
+
this.#publish();
|
|
271
|
+
}
|
|
272
|
+
#message(message) {
|
|
273
|
+
if (!message?.uri || !message.author?.did || !message.record)
|
|
274
|
+
return false;
|
|
275
|
+
if (message.deleted) {
|
|
276
|
+
// Remembered, so its text also goes from quotes and the pin, and it cannot come back.
|
|
277
|
+
this.#hidden.add(message.uri);
|
|
278
|
+
this.#index.delete(message.uri);
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
if (this.#hidden.has(message.uri) || this.#blocked.has(message.author.did))
|
|
282
|
+
return false;
|
|
283
|
+
// Only this tab's own unconfirmed copy is replaced; anything else is a repeat.
|
|
284
|
+
const existing = this.#index.get(message.uri);
|
|
285
|
+
if (existing && !existing.pending)
|
|
286
|
+
return false;
|
|
287
|
+
this.authors.set(message.author.did, {
|
|
288
|
+
handle: message.author.handle,
|
|
289
|
+
chatProfile: message.chatProfile
|
|
290
|
+
});
|
|
291
|
+
this.#index.set(message.uri, message);
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
#publish() {
|
|
295
|
+
let all = [...this.#index.values()]
|
|
296
|
+
.filter((message) => !this.#gone(message.uri, message.author.did))
|
|
297
|
+
.sort((a, b) => timeOf(a) - timeOf(b));
|
|
298
|
+
let list = all.filter((message) => !reactionTarget(message));
|
|
299
|
+
if (list.length > MAX_MESSAGES) {
|
|
300
|
+
const dropped = new Set(list.slice(0, list.length - MAX_MESSAGES).map((old) => old.uri));
|
|
301
|
+
// Reactions go with the message they are on.
|
|
302
|
+
list = list.slice(-MAX_MESSAGES);
|
|
303
|
+
const oldest = timeOf(list[0]);
|
|
304
|
+
for (const message of all) {
|
|
305
|
+
const target = reactionTarget(message);
|
|
306
|
+
if (dropped.has(message.uri) ||
|
|
307
|
+
// Also reactions to messages that were never loaded and are older than the list.
|
|
308
|
+
(target &&
|
|
309
|
+
(dropped.has(target) || (!this.#index.has(target) && timeOf(message) < oldest)))) {
|
|
310
|
+
this.#index.delete(message.uri);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
all = all.filter((message) => this.#index.has(message.uri));
|
|
314
|
+
}
|
|
315
|
+
// A reply quotes the message it answers. The quote is taken from what is on screen now,
|
|
316
|
+
// so a hidden, deleted or blocked message does not live on inside its replies.
|
|
317
|
+
this.messages = list.map((message) => {
|
|
318
|
+
const parent = message.record.reply?.parent.uri;
|
|
319
|
+
if (!parent)
|
|
320
|
+
return message;
|
|
321
|
+
const quoted = this.#index.get(parent) ?? message.replyTo;
|
|
322
|
+
const replyTo = quoted && !this.#gone(parent, quoted.author.did) ? quoted : undefined;
|
|
323
|
+
if (replyTo === message.replyTo)
|
|
324
|
+
return message;
|
|
325
|
+
const previous = this.#quoted.get(message);
|
|
326
|
+
if (previous && previous.replyTo === replyTo)
|
|
327
|
+
return previous;
|
|
328
|
+
const next = { ...message, replyTo };
|
|
329
|
+
this.#quoted.set(message, next);
|
|
330
|
+
return next;
|
|
331
|
+
});
|
|
332
|
+
this.reactions = groupReactions(all);
|
|
333
|
+
this.#publishPin();
|
|
334
|
+
}
|
|
335
|
+
#publishPin() {
|
|
336
|
+
const pin = this.#pin;
|
|
337
|
+
this.pinned =
|
|
338
|
+
pin && !this.#gone(pin.record.pinnedMessage, pin.message?.author.did) ? pin : null;
|
|
339
|
+
}
|
|
340
|
+
/** Whether a message must not be shown anywhere: hidden, deleted, or from a blocked account. */
|
|
341
|
+
#gone(uri, author) {
|
|
342
|
+
return (this.#pendingHides.has(uri) ||
|
|
343
|
+
this.#hidden.has(uri) ||
|
|
344
|
+
(!!author && this.#blocked.has(author)));
|
|
345
|
+
}
|
|
346
|
+
/** Resolves a mentioned handle from chat first, then from the network. */
|
|
347
|
+
async resolveHandle(handle) {
|
|
348
|
+
const needle = handle.toLowerCase();
|
|
349
|
+
for (const [did, author] of this.authors) {
|
|
350
|
+
if (author.handle.toLowerCase() === needle)
|
|
351
|
+
return did;
|
|
352
|
+
}
|
|
353
|
+
if (this.profile?.handle.toLowerCase() === needle)
|
|
354
|
+
return this.profile.did;
|
|
355
|
+
const response = await fetch(`${appviewUrl()}/xrpc/com.atproto.identity.resolveHandle?handle=${encodeURIComponent(needle)}`, { signal: AbortSignal.timeout(2500) });
|
|
356
|
+
return response.ok ? (await response.json()).did : undefined;
|
|
357
|
+
}
|
|
358
|
+
#writer() {
|
|
359
|
+
if (!this.#write)
|
|
360
|
+
throw new Error('Sign in to continue');
|
|
361
|
+
return this.#write;
|
|
362
|
+
}
|
|
363
|
+
/** Shows the message at once and writes it to the sender's repository. */
|
|
364
|
+
async send(text, viewer, replyTo) {
|
|
365
|
+
const write = this.#writer();
|
|
366
|
+
const streamer = this.profile?.did ?? this.livestream?.author.did;
|
|
367
|
+
if (!streamer)
|
|
368
|
+
throw new Error('The chat is still connecting');
|
|
369
|
+
const draft = {
|
|
370
|
+
$type: 'place.stream.chat.message',
|
|
371
|
+
text,
|
|
372
|
+
createdAt: new Date().toISOString(),
|
|
373
|
+
streamer,
|
|
374
|
+
...(replyTo
|
|
375
|
+
? {
|
|
376
|
+
reply: {
|
|
377
|
+
root: replyTo.record.reply?.root ?? { uri: replyTo.uri, cid: replyTo.cid },
|
|
378
|
+
parent: { uri: replyTo.uri, cid: replyTo.cid }
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
: {})
|
|
382
|
+
};
|
|
383
|
+
// On screen before anything is looked up: resolving a mention can take a moment.
|
|
384
|
+
let local = {
|
|
385
|
+
uri: `local:${crypto.randomUUID()}`,
|
|
386
|
+
cid: '',
|
|
387
|
+
author: {
|
|
388
|
+
did: viewer.did,
|
|
389
|
+
handle: viewer.handle,
|
|
390
|
+
displayName: viewer.displayName,
|
|
391
|
+
avatar: viewer.avatar
|
|
392
|
+
},
|
|
393
|
+
chatProfile: this.authors.get(viewer.did)?.chatProfile,
|
|
394
|
+
record: draft,
|
|
395
|
+
indexedAt: draft.createdAt,
|
|
396
|
+
replyTo: replyTo ?? undefined,
|
|
397
|
+
pending: true
|
|
398
|
+
};
|
|
399
|
+
this.#index.set(local.uri, local);
|
|
400
|
+
this.#publish();
|
|
401
|
+
let created;
|
|
402
|
+
try {
|
|
403
|
+
const facets = await detectFacets(text, (handle) => this.resolveHandle(handle));
|
|
404
|
+
if (facets.length) {
|
|
405
|
+
// A new object, so the row on screen picks up its links and mentions.
|
|
406
|
+
local = { ...local, record: { ...draft, facets } };
|
|
407
|
+
this.#index.set(local.uri, local);
|
|
408
|
+
this.#publish();
|
|
409
|
+
}
|
|
410
|
+
created = await write.create('place.stream.chat.message', local.record);
|
|
411
|
+
}
|
|
412
|
+
catch (cause) {
|
|
413
|
+
this.#index.delete(local.uri);
|
|
414
|
+
this.#publish();
|
|
415
|
+
throw cause;
|
|
416
|
+
}
|
|
417
|
+
// From here the record's own URI identifies it. The copy from the node replaces this one
|
|
418
|
+
// when it arrives; if it already has, or the writer named no URI, this one just goes.
|
|
419
|
+
this.#index.delete(local.uri);
|
|
420
|
+
const uri = created?.uri;
|
|
421
|
+
if (uri && !this.#index.has(uri) && !this.#hidden.has(uri)) {
|
|
422
|
+
this.#index.set(uri, { ...local, uri, cid: created?.cid ?? '' });
|
|
423
|
+
}
|
|
424
|
+
this.#publish();
|
|
425
|
+
}
|
|
426
|
+
/** Adds the viewer's reaction to a message, or takes it back if it is already there. */
|
|
427
|
+
async react(target, emoji, viewer) {
|
|
428
|
+
const mine = this.reactions
|
|
429
|
+
.get(target.uri)
|
|
430
|
+
?.find((reaction) => reaction.emoji === emoji)
|
|
431
|
+
?.by.find((person) => person.did === viewer.did);
|
|
432
|
+
if (!mine)
|
|
433
|
+
return this.send(emoji, viewer, target);
|
|
434
|
+
// Still being written: there is no record to remove yet.
|
|
435
|
+
if (mine.uri.startsWith('local:'))
|
|
436
|
+
return;
|
|
437
|
+
await this.remove(mine.uri);
|
|
438
|
+
}
|
|
439
|
+
/** Takes a message off screen for a write, and puts it back if the write fails. */
|
|
440
|
+
async #withoutMessage(uri, write) {
|
|
441
|
+
// Keep the optimistic hide out of the node's state: a failed write only removes
|
|
442
|
+
// its own hide, never a gate, deletion or block received while it was pending.
|
|
443
|
+
this.#pendingHides.set(uri, (this.#pendingHides.get(uri) ?? 0) + 1);
|
|
444
|
+
this.#publish();
|
|
445
|
+
try {
|
|
446
|
+
await write();
|
|
447
|
+
this.#hidden.add(uri);
|
|
448
|
+
this.#index.delete(uri);
|
|
449
|
+
}
|
|
450
|
+
finally {
|
|
451
|
+
const remaining = this.#pendingHides.get(uri) - 1;
|
|
452
|
+
if (remaining)
|
|
453
|
+
this.#pendingHides.set(uri, remaining);
|
|
454
|
+
else
|
|
455
|
+
this.#pendingHides.delete(uri);
|
|
456
|
+
this.#publish();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/** Deletes one of the viewer's own messages. It comes back if that fails. */
|
|
460
|
+
async remove(uri) {
|
|
461
|
+
const write = this.#writer();
|
|
462
|
+
await this.#withoutMessage(uri, () => write.delete(uri));
|
|
463
|
+
}
|
|
464
|
+
/** Streamer: hides someone's message from this chat. It comes back if that fails. */
|
|
465
|
+
async hide(uri) {
|
|
466
|
+
const write = this.#writer();
|
|
467
|
+
await this.#withoutMessage(uri, () => write.create('place.stream.chat.gate', {
|
|
468
|
+
$type: 'place.stream.chat.gate',
|
|
469
|
+
hiddenMessage: uri
|
|
470
|
+
}));
|
|
471
|
+
}
|
|
472
|
+
/** Streamer: pins a message above the chat. */
|
|
473
|
+
async pin(uri) {
|
|
474
|
+
await this.#writer().create('place.stream.chat.pinnedRecord', {
|
|
475
|
+
$type: 'place.stream.chat.pinnedRecord',
|
|
476
|
+
pinnedMessage: uri,
|
|
477
|
+
createdAt: new Date().toISOString()
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
/** Streamer: removes the pinned message. */
|
|
481
|
+
async unpin() {
|
|
482
|
+
const pin = this.pinned;
|
|
483
|
+
if (!pin)
|
|
484
|
+
return;
|
|
485
|
+
await this.#writer().delete(pin.uri);
|
|
486
|
+
if (this.#pin?.uri === pin.uri && this.#pin.cid === pin.cid)
|
|
487
|
+
this.#pin = this.pinned = null;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Streamer: bans an account from this chat. This blocks the account. Their messages go
|
|
491
|
+
* once the block is written, so a failed ban changes nothing on screen.
|
|
492
|
+
*/
|
|
493
|
+
async ban(did) {
|
|
494
|
+
await this.#writer().create('app.bsky.graph.block', {
|
|
495
|
+
$type: 'app.bsky.graph.block',
|
|
496
|
+
subject: did,
|
|
497
|
+
createdAt: new Date().toISOString()
|
|
498
|
+
});
|
|
499
|
+
this.banLocally(did);
|
|
500
|
+
}
|
|
501
|
+
/** Removes a message from this tab right away; the network confirms later. */
|
|
502
|
+
hideLocally(uri) {
|
|
503
|
+
this.#hidden.add(uri);
|
|
504
|
+
this.#index.delete(uri);
|
|
505
|
+
this.#publish();
|
|
506
|
+
}
|
|
507
|
+
banLocally(did) {
|
|
508
|
+
this.#blocked.add(did);
|
|
509
|
+
for (const [uri, message] of this.#index) {
|
|
510
|
+
if (message.author.did === did)
|
|
511
|
+
this.#index.delete(uri);
|
|
512
|
+
}
|
|
513
|
+
this.#publish();
|
|
514
|
+
}
|
|
515
|
+
}
|