@alexsun-top/opencode-chat-room 0.2.3 → 0.3.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/CHANGELOG.md +12 -0
- package/README.md +23 -7
- package/README.zh-CN.md +23 -7
- package/package.json +5 -2
- package/plugin.ts +6 -5
- package/server/assets/DOMPURIFY-LICENSE +202 -0
- package/server/assets/MARKED-LICENSE +44 -0
- package/server/assets/NOTICE.txt +6 -0
- package/server/assets/base.css +225 -0
- package/server/assets/chat/events.js +146 -0
- package/server/assets/chat/index.js +12 -0
- package/server/assets/chat/markdown.js +65 -0
- package/server/assets/chat/members.js +95 -0
- package/server/assets/chat/mentions.js +131 -0
- package/server/assets/chat/messages.js +417 -0
- package/server/assets/chat/rooms.js +443 -0
- package/server/assets/chat/state.js +316 -0
- package/server/assets/conversation.css +350 -0
- package/server/assets/dompurify-3.4.7.js +3 -0
- package/server/assets/marked-18.0.4.js +79 -0
- package/server/assets/responsive.css +119 -0
- package/server/chat-server.ts +34 -10
- package/server/chat.html +27 -1984
- package/server/history-route.ts +2 -1
- package/server/inbox.ts +1 -1
- package/server/newrooms.ts +90 -0
- package/server/wake.ts +13 -14
- package/src/background.ts +70 -25
- package/src/center-client.ts +3 -1
- package/src/center-inbox.ts +71 -37
- package/src/center-wake.ts +43 -9
- package/src/mentions.ts +7 -0
- package/src/plugin-notifications.ts +69 -23
- package/src/queue-transport.ts +17 -1
- package/src/queue.ts +36 -2
- package/src/receiver-state.ts +58 -1
- package/src/room-service.ts +19 -1
- package/src/sqlite-state.ts +24 -9
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import { createRandomId, LS } from "./state.js";
|
|
2
|
+
import { renderMarkdown } from "./markdown.js";
|
|
3
|
+
|
|
4
|
+
export function initMessages(app) {
|
|
5
|
+
const $ = app.$;
|
|
6
|
+
let pollDelay = 2000;
|
|
7
|
+
|
|
8
|
+
app.autoGrow = (textarea) => {
|
|
9
|
+
textarea.style.height = "auto";
|
|
10
|
+
textarea.style.height = `${Math.min(textarea.scrollHeight, 132)}px`;
|
|
11
|
+
};
|
|
12
|
+
app.updateSendState = () => { $("#btnSend").disabled = !$("#msgInput").value.trim(); };
|
|
13
|
+
app.updateDraftCount = () => app.setText($("#draftCount"), "messageCount", { count: $("#msgInput").value.length });
|
|
14
|
+
app.renderFailedMessages = () => {
|
|
15
|
+
const composer = $("#msgInput").closest(".composer");
|
|
16
|
+
let panel = composer.querySelector(".failed-messages");
|
|
17
|
+
if (!panel) {
|
|
18
|
+
panel = document.createElement("section");
|
|
19
|
+
panel.className = "failed-messages";
|
|
20
|
+
panel.setAttribute("aria-live", "polite");
|
|
21
|
+
composer.insertBefore(panel, $(".composer-row"));
|
|
22
|
+
}
|
|
23
|
+
const roomId = app.state.room?.id, items = roomId && app.state.failedMessages.get(roomId) || [];
|
|
24
|
+
panel.textContent = "";
|
|
25
|
+
panel.hidden = !items.length;
|
|
26
|
+
if (!items.length) return;
|
|
27
|
+
const heading = document.createElement("h3");
|
|
28
|
+
heading.textContent = app.t("failedMessages");
|
|
29
|
+
panel.append(heading);
|
|
30
|
+
for (const item of items) {
|
|
31
|
+
const row = document.createElement("article"), text = document.createElement("p");
|
|
32
|
+
row.className = "failed-message";
|
|
33
|
+
text.textContent = item.text;
|
|
34
|
+
row.append(text, failedAction("retryFailed", roomId, item.id, app), failedAction("discardFailed", roomId, item.id, app));
|
|
35
|
+
panel.append(row);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
app.discardFailedMessage = (roomId, failedId) => {
|
|
39
|
+
app.saveFailedMessages(roomId, (app.state.failedMessages.get(roomId) || []).filter((item) => item.id !== failedId));
|
|
40
|
+
app.renderFailedMessages();
|
|
41
|
+
};
|
|
42
|
+
app.retryFailedMessage = async (roomId, failedId) => {
|
|
43
|
+
const item = app.state.failedMessages.get(roomId)?.find((message) => message.id === failedId);
|
|
44
|
+
if (!item || app.state.retryingFailedIds.has(failedId)) return;
|
|
45
|
+
app.state.retryingFailedIds.add(failedId);
|
|
46
|
+
app.renderFailedMessages();
|
|
47
|
+
try {
|
|
48
|
+
const updated = await app.api(`/rooms/${encodeURIComponent(roomId)}/messages`, {
|
|
49
|
+
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text: item.text }),
|
|
50
|
+
});
|
|
51
|
+
const entry = app.state.rooms.get(roomId);
|
|
52
|
+
if (entry) {
|
|
53
|
+
entry.room = mergeRoom(entry.room, updated);
|
|
54
|
+
const latest = entry.room.messages.at(-1);
|
|
55
|
+
if (latest && latest.createdAt >= entry.previewAt) { entry.preview = latest.text; entry.previewAt = latest.createdAt; }
|
|
56
|
+
}
|
|
57
|
+
app.discardFailedMessage(roomId, failedId);
|
|
58
|
+
if (app.state.room?.id === roomId && entry) {
|
|
59
|
+
app.state.room = entry.room;
|
|
60
|
+
app.renderMessages(true);
|
|
61
|
+
app.renderMembers();
|
|
62
|
+
app.markActiveRoomRead();
|
|
63
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
64
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
65
|
+
}
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (app.state.room?.id === roomId) app.showSendError(error.status === 401 ? "sendAuth" : error.network ? "sendNetwork" : "sendFailed", { message: error.message });
|
|
68
|
+
} finally {
|
|
69
|
+
app.state.retryingFailedIds.delete(failedId);
|
|
70
|
+
app.renderFailedMessages();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
app.jumpToBottom = () => {
|
|
74
|
+
$("#thread").scrollTop = $("#thread").scrollHeight;
|
|
75
|
+
const entry = app.state.room && app.state.rooms.get(app.state.room.id);
|
|
76
|
+
app.state.unread = entry ? entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0) : 0;
|
|
77
|
+
app.updateBadge();
|
|
78
|
+
app.updateJump();
|
|
79
|
+
void app.markActiveRoomRead();
|
|
80
|
+
};
|
|
81
|
+
app.updateJump = () => {
|
|
82
|
+
const thread = $("#thread");
|
|
83
|
+
const away = thread.scrollHeight - thread.scrollTop - thread.clientHeight > 160;
|
|
84
|
+
$("#btnJump").hidden = !(away || app.state.unread > 0);
|
|
85
|
+
};
|
|
86
|
+
app.updateBadge = () => {
|
|
87
|
+
const badge = $("#jumpBadge");
|
|
88
|
+
badge.textContent = app.state.unread > 99 ? "99+" : String(app.state.unread);
|
|
89
|
+
badge.hidden = app.state.unread === 0;
|
|
90
|
+
app.updateTitle();
|
|
91
|
+
};
|
|
92
|
+
app.markActiveRoomRead = async () => {
|
|
93
|
+
const room = app.state.room, entry = room && app.state.rooms.get(room.id);
|
|
94
|
+
const generation = app.state.pollGeneration;
|
|
95
|
+
const thread = $("#thread");
|
|
96
|
+
if (!room || !entry || document.hidden || !document.hasFocus()
|
|
97
|
+
|| thread.scrollHeight - thread.scrollTop - thread.clientHeight >= 80) return;
|
|
98
|
+
const timestamp = room.messages.reduce((latest, message) => Math.max(latest, message.createdAt), entry.readTs);
|
|
99
|
+
if (!timestamp || timestamp <= entry.readTs) return;
|
|
100
|
+
try {
|
|
101
|
+
await app.api("/read", {
|
|
102
|
+
method: "POST", body: JSON.stringify({ roomId: room.id, sessionID: LS.sessionID, ts: timestamp }),
|
|
103
|
+
});
|
|
104
|
+
if (app.state.room?.id !== room.id || app.state.pollGeneration !== generation) return;
|
|
105
|
+
entry.readTs = timestamp;
|
|
106
|
+
entry.unreadOverflow = false;
|
|
107
|
+
for (const [id, message] of entry.unreadIds) if (message.createdAt <= timestamp) entry.unreadIds.delete(id);
|
|
108
|
+
app.state.unread = entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0);
|
|
109
|
+
app.updateBadge();
|
|
110
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
111
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
112
|
+
} catch { /* The next active poll will retry the read watermark. */ }
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
app.renderMessage = (message, live) => {
|
|
116
|
+
const row = document.createElement("li");
|
|
117
|
+
row.dataset.sender = message.senderId;
|
|
118
|
+
row.dataset.ts = message.createdAt;
|
|
119
|
+
row.dataset.msgId = message.id;
|
|
120
|
+
if (message.senderId === "system") {
|
|
121
|
+
row.className = "sys";
|
|
122
|
+
const text = document.createElement("span");
|
|
123
|
+
text.textContent = systemMessage(message.text, app);
|
|
124
|
+
row.append(text);
|
|
125
|
+
} else {
|
|
126
|
+
const direct = app.state.room.kind === "direct";
|
|
127
|
+
const sameName = app.state.memberSessions.get(app.state.room.id)?.filter((member) => member.name === message.senderId);
|
|
128
|
+
const own = direct ? message.senderId === LS.sessionID
|
|
129
|
+
: message.senderSessionID ? message.senderSessionID === LS.sessionID
|
|
130
|
+
: message.senderId === app.state.myName && sameName?.length === 1 && sameName[0].sessionID === LS.sessionID;
|
|
131
|
+
const sender = direct
|
|
132
|
+
? app.state.room.participants.find((person) => person.id === message.senderId)?.name || app.t("guest")
|
|
133
|
+
: message.senderId;
|
|
134
|
+
row.className = `msg${own ? " own" : ""}`;
|
|
135
|
+
const meta = document.createElement("div"), name = document.createElement("span"), time = document.createElement("span");
|
|
136
|
+
meta.className = "msg-meta";
|
|
137
|
+
name.className = "msg-name";
|
|
138
|
+
name.textContent = own ? app.t("you") : sender;
|
|
139
|
+
time.className = "msg-time";
|
|
140
|
+
time.textContent = app.timeHM(message.createdAt);
|
|
141
|
+
meta.append(name, time);
|
|
142
|
+
const body = document.createElement("div");
|
|
143
|
+
body.className = "msg-text";
|
|
144
|
+
renderMarkdown(body, message.text, app);
|
|
145
|
+
row.append(meta, body);
|
|
146
|
+
}
|
|
147
|
+
if (live) row.classList.add("in");
|
|
148
|
+
return row;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
app.renderMessages = (live = false) => {
|
|
152
|
+
const thread = $("#thread"), list = $("#msgList"), messages = app.state.room.messages;
|
|
153
|
+
const nearBottom = thread.scrollHeight - thread.scrollTop - thread.clientHeight < 80;
|
|
154
|
+
let last = list.lastElementChild, skipped = 0;
|
|
155
|
+
let anchorId = null;
|
|
156
|
+
let renderedIds = new Set([...list.children].map((node) => node.dataset.msgId).filter(Boolean));
|
|
157
|
+
if (messages.length && app.firstMessageId && messages[0].id !== app.firstMessageId) {
|
|
158
|
+
anchorId = [...list.children].find((node) => node.classList.contains("msg"))?.dataset.msgId || null;
|
|
159
|
+
list.textContent = "";
|
|
160
|
+
app.rendered = 0;
|
|
161
|
+
last = null;
|
|
162
|
+
renderedIds = new Set();
|
|
163
|
+
}
|
|
164
|
+
for (let index = app.rendered; index < messages.length; index++) {
|
|
165
|
+
const message = messages[index];
|
|
166
|
+
if (renderedIds.has(message.id)) continue;
|
|
167
|
+
try {
|
|
168
|
+
const day = new Date(message.createdAt).toDateString();
|
|
169
|
+
if (!last || last.dataset.day !== day) {
|
|
170
|
+
const separator = document.createElement("li");
|
|
171
|
+
separator.className = "day-sep";
|
|
172
|
+
separator.dataset.day = day;
|
|
173
|
+
separator.textContent = app.dayText(message.createdAt);
|
|
174
|
+
list.append(separator);
|
|
175
|
+
last = separator;
|
|
176
|
+
}
|
|
177
|
+
const row = app.renderMessage(message, live);
|
|
178
|
+
row.dataset.day = day;
|
|
179
|
+
if (last.classList.contains("msg") && last.dataset.sender === message.senderId
|
|
180
|
+
&& message.createdAt - Number(last.dataset.ts) < 8 * 60 * 1000) row.classList.add("repeat");
|
|
181
|
+
list.append(row);
|
|
182
|
+
last = row;
|
|
183
|
+
renderedIds.add(message.id);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
console.error("[chat] Message could not render", error);
|
|
186
|
+
skipped++;
|
|
187
|
+
app.failedRenderIds.add(message.id);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
app.rendered = messages.length;
|
|
191
|
+
app.firstMessageId = messages[0]?.id || app.firstMessageId;
|
|
192
|
+
$("#msgEmpty").hidden = messages.length > 0;
|
|
193
|
+
if (anchorId) {
|
|
194
|
+
const anchor = list.querySelector(`[data-msg-id="${CSS.escape(anchorId)}"]`);
|
|
195
|
+
if (anchor) thread.scrollTop += anchor.getBoundingClientRect().top - thread.getBoundingClientRect().top - 40;
|
|
196
|
+
else thread.scrollTop = thread.scrollHeight;
|
|
197
|
+
} else if (!live || nearBottom) thread.scrollTop = thread.scrollHeight;
|
|
198
|
+
app.updateJump();
|
|
199
|
+
trimMessages(list, app);
|
|
200
|
+
return { skipped };
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
app.rerenderMessages = () => {
|
|
204
|
+
if (!app.state.room) return { skipped: 0 };
|
|
205
|
+
const thread = $("#thread"), list = $("#msgList"), oldTop = thread.scrollTop;
|
|
206
|
+
const atBottom = thread.scrollHeight - oldTop - thread.clientHeight < 80;
|
|
207
|
+
const edge = thread.getBoundingClientRect();
|
|
208
|
+
const anchor = [...list.querySelectorAll("[data-msg-id]")].find((node) => node.getBoundingClientRect().bottom > edge.top);
|
|
209
|
+
const anchorId = anchor?.dataset.msgId;
|
|
210
|
+
const anchorOffset = anchor ? anchor.getBoundingClientRect().top - edge.top : 0;
|
|
211
|
+
$("#msgList").textContent = "";
|
|
212
|
+
app.rendered = 0;
|
|
213
|
+
app.firstMessageId = null;
|
|
214
|
+
app.failedRenderIds.clear();
|
|
215
|
+
const result = app.renderMessages(false);
|
|
216
|
+
const nextAnchor = anchorId && list.querySelector(`[data-msg-id="${CSS.escape(anchorId)}"]`);
|
|
217
|
+
if (atBottom) thread.scrollTop = thread.scrollHeight;
|
|
218
|
+
else if (nextAnchor) thread.scrollTop += nextAnchor.getBoundingClientRect().top - edge.top - anchorOffset;
|
|
219
|
+
else thread.scrollTop = oldTop;
|
|
220
|
+
return result;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
app.send = async () => {
|
|
224
|
+
const input = $("#msgInput"), text = input.value.trim(), room = app.state.room;
|
|
225
|
+
if (!text || !room) return;
|
|
226
|
+
const revision = app.state.draftRevision.get(room.id) || 0;
|
|
227
|
+
app.closeMention();
|
|
228
|
+
input.value = "";
|
|
229
|
+
app.clearDraft(room.id);
|
|
230
|
+
app.autoGrow(input);
|
|
231
|
+
app.updateSendState();
|
|
232
|
+
app.updateDraftCount();
|
|
233
|
+
app.hideSendError();
|
|
234
|
+
try {
|
|
235
|
+
const updated = await app.api(`/rooms/${encodeURIComponent(room.id)}/messages`, {
|
|
236
|
+
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text }),
|
|
237
|
+
});
|
|
238
|
+
if (app.state.room?.id !== room.id) return;
|
|
239
|
+
app.state.room = updated;
|
|
240
|
+
const entry = app.state.rooms.get(room.id);
|
|
241
|
+
if (entry) {
|
|
242
|
+
entry.room = updated;
|
|
243
|
+
const latest = updated.messages.at(-1);
|
|
244
|
+
if (latest) { entry.preview = latest.text; entry.previewAt = latest.createdAt; }
|
|
245
|
+
}
|
|
246
|
+
app.renderMessages(true);
|
|
247
|
+
$("#thread").scrollTop = $("#thread").scrollHeight;
|
|
248
|
+
app.renderMembers();
|
|
249
|
+
app.state.unread = entry ? entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0) : 0;
|
|
250
|
+
app.updateBadge();
|
|
251
|
+
void app.markActiveRoomRead();
|
|
252
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
253
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
254
|
+
} catch (error) {
|
|
255
|
+
const unchanged = (app.state.draftRevision.get(room.id) || 0) === revision;
|
|
256
|
+
const failures = app.state.failedMessages.get(room.id) || [];
|
|
257
|
+
app.saveFailedMessages(room.id, [...failures, { id: createRandomId(), text }]);
|
|
258
|
+
app.renderFailedMessages();
|
|
259
|
+
if (app.state.room?.id !== room.id) return;
|
|
260
|
+
if (unchanged) app.clearDraft(room.id);
|
|
261
|
+
app.autoGrow(input);
|
|
262
|
+
app.updateSendState();
|
|
263
|
+
app.updateDraftCount();
|
|
264
|
+
if (error.status === 401) { app.openChatSettings(); app.showSendError("sendAuth"); }
|
|
265
|
+
else app.showSendError(error.network ? "sendNetwork" : "sendFailed", { message: error.message });
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
function isCurrentPoll(roomId, generation) {
|
|
270
|
+
return app.state.polling && app.state.pollGeneration === generation && app.state.room?.id === roomId;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
app.startPolling = () => {
|
|
274
|
+
app.stopPolling();
|
|
275
|
+
const generation = ++app.state.pollGeneration;
|
|
276
|
+
const controller = new AbortController();
|
|
277
|
+
app.state.pollController = controller;
|
|
278
|
+
app.state.polling = true;
|
|
279
|
+
pollDelay = 2000;
|
|
280
|
+
void pollLoop(generation, controller.signal);
|
|
281
|
+
};
|
|
282
|
+
app.stopPolling = () => {
|
|
283
|
+
app.state.polling = false;
|
|
284
|
+
app.state.pollGeneration++;
|
|
285
|
+
app.state.pollController?.abort();
|
|
286
|
+
app.state.pollController = null;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
async function pollLoop(generation, signal) {
|
|
290
|
+
while (app.state.polling && app.state.room && generation === app.state.pollGeneration) {
|
|
291
|
+
await poll(generation, signal).catch(() => {});
|
|
292
|
+
if (!app.state.polling || generation !== app.state.pollGeneration) break;
|
|
293
|
+
await new Promise((resolve) => setTimeout(resolve, pollDelay));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async function poll(generation, signal) {
|
|
298
|
+
const roomId = app.state.room?.id;
|
|
299
|
+
if (!roomId || !isCurrentPoll(roomId, generation)) return;
|
|
300
|
+
try {
|
|
301
|
+
const query = new URLSearchParams({ sessionID: LS.sessionID, roomId, excludeSelf: "0", timeout: "25000" });
|
|
302
|
+
const data = await app.api(`/inbox?${query}`, { signal });
|
|
303
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
304
|
+
if (!data.member) {
|
|
305
|
+
void app.removeJoinedRoom(roomId, { reason: true });
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const items = Array.isArray(data.items) ? data.items : [];
|
|
309
|
+
const entry = app.state.rooms.get(roomId), messages = app.state.room.messages;
|
|
310
|
+
const known = new Set(messages.map((message) => message.id));
|
|
311
|
+
let sawSystem = false, added = 0;
|
|
312
|
+
for (const item of items) {
|
|
313
|
+
if (item.senderId === "system") sawSystem = true;
|
|
314
|
+
if (known.has(item.id)) continue;
|
|
315
|
+
messages.push({ ...item, roomId });
|
|
316
|
+
known.add(item.id);
|
|
317
|
+
entry?.unreadIds.set(item.id, item);
|
|
318
|
+
if (entry && item.createdAt >= entry.previewAt) { entry.preview = item.text; entry.previewAt = item.createdAt; }
|
|
319
|
+
added++;
|
|
320
|
+
}
|
|
321
|
+
if (entry) entry.unreadOverflow = Boolean(data.hasMore);
|
|
322
|
+
if (messages.length > 500) messages.splice(0, messages.length - 500);
|
|
323
|
+
const rendered = app.renderMessages(true);
|
|
324
|
+
if (sawSystem) {
|
|
325
|
+
await app.refreshRoomMeta(roomId, generation);
|
|
326
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
327
|
+
}
|
|
328
|
+
if (added) {
|
|
329
|
+
app.state.unread = entry ? entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0) : app.state.unread + added;
|
|
330
|
+
app.updateBadge();
|
|
331
|
+
}
|
|
332
|
+
if (rendered.skipped === 0) void app.markActiveRoomRead();
|
|
333
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
334
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
335
|
+
if (!app.lastReconcileAt || Date.now() - app.lastReconcileAt > 120000) {
|
|
336
|
+
app.lastReconcileAt = Date.now();
|
|
337
|
+
void app.reconcileRoom(roomId, generation);
|
|
338
|
+
}
|
|
339
|
+
app.setOnline(true);
|
|
340
|
+
pollDelay = 2000;
|
|
341
|
+
} catch (error) {
|
|
342
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
343
|
+
if (error.status === 401) {
|
|
344
|
+
app.setOnline(false);
|
|
345
|
+
if ($("#chatSettings").hidden) app.openChatSettings();
|
|
346
|
+
pollDelay = Math.min(pollDelay * 2, 30000);
|
|
347
|
+
} else if (error.status === 404) {
|
|
348
|
+
void app.removeJoinedRoom(roomId, { reason: true });
|
|
349
|
+
} else {
|
|
350
|
+
app.setOnline(false);
|
|
351
|
+
pollDelay = 2000;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
app.refreshRoomMeta = async (roomId = app.state.room?.id, generation = app.state.pollGeneration) => {
|
|
357
|
+
if (!roomId) return;
|
|
358
|
+
try {
|
|
359
|
+
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
360
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
361
|
+
app.state.room.participants = room.participants;
|
|
362
|
+
app.renderMembers();
|
|
363
|
+
} catch { /* The next room event will retry. */ }
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
app.reconcileRoom = async (roomId, generation = app.state.pollGeneration) => {
|
|
367
|
+
try {
|
|
368
|
+
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
369
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
370
|
+
const current = app.state.room.messages;
|
|
371
|
+
const ids = new Set(current.map((message) => message.id));
|
|
372
|
+
const missing = room.messages.filter((message) => !ids.has(message.id));
|
|
373
|
+
if (!missing.length && !app.failedRenderIds.size) return;
|
|
374
|
+
for (const message of missing) {
|
|
375
|
+
const index = current.findIndex((item) => item.createdAt > message.createdAt);
|
|
376
|
+
current.splice(index < 0 ? current.length : index, 0, message);
|
|
377
|
+
}
|
|
378
|
+
if (current.length > 500) current.splice(0, current.length - 500);
|
|
379
|
+
const result = app.rerenderMessages();
|
|
380
|
+
if (result.skipped === 0) void app.markActiveRoomRead();
|
|
381
|
+
} catch { /* Try again during the next reconciliation. */ }
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
function failedAction(key, roomId, failedId, app) {
|
|
385
|
+
const button = document.createElement("button");
|
|
386
|
+
button.type = "button";
|
|
387
|
+
button.textContent = app.t(key);
|
|
388
|
+
button.dataset.failedAction = key === "retryFailed" ? "retry" : "discard";
|
|
389
|
+
button.dataset.failedRoom = roomId;
|
|
390
|
+
button.dataset.failedId = failedId;
|
|
391
|
+
button.disabled = app.state.retryingFailedIds.has(failedId);
|
|
392
|
+
return button;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function mergeRoom(current, updated) {
|
|
396
|
+
const messages = new Map([...(current?.messages || []), ...updated.messages].map((message) => [message.id, message]));
|
|
397
|
+
return { ...updated, messages: [...messages.values()].sort((a, b) => a.createdAt - b.createdAt).slice(-500) };
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function trimMessages(list, app) {
|
|
401
|
+
if (list.children.length <= 400) return;
|
|
402
|
+
while (list.children.length > 300) list.removeChild(list.firstChild);
|
|
403
|
+
if (list.firstElementChild && !list.firstElementChild.classList.contains("gap")) {
|
|
404
|
+
const gap = document.createElement("li");
|
|
405
|
+
gap.className = "gap";
|
|
406
|
+
app.setText(gap, "earlierMessages");
|
|
407
|
+
list.insertBefore(gap, list.firstChild);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function systemMessage(text, app) {
|
|
412
|
+
const joined = /^(.*?)\s+joined the room$/.exec(String(text));
|
|
413
|
+
if (joined) return app.t("systemJoined", { name: joined[1] });
|
|
414
|
+
const left = /^(.*?)\s+left the room$/.exec(String(text));
|
|
415
|
+
return left ? app.t("systemLeft", { name: left[1] }) : String(text);
|
|
416
|
+
}
|
|
417
|
+
}
|