@alexsun-top/opencode-chat-room 0.8.2 → 0.8.4
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 +18 -62
- package/README.zh-CN.md +25 -69
- package/dist/cli.js +466 -188
- package/package.json +11 -5
- package/packages/chat-room-channeld/README.md +25 -3
- package/packages/chat-room-channeld/dist/mock-adapter.js +5 -5
- package/packages/chat-room-channeld/src/adapter.ts +5 -0
- package/packages/chat-room-channeld/src/cli.ts +72 -5
- package/packages/chat-room-channeld/src/host.ts +17 -2
- package/packages/chat-room-channeld/src/pg/database.ts +187 -0
- package/packages/chat-room-channeld/src/pg/migrate.ts +150 -0
- package/packages/chat-room-channeld/src/pg/schema.ts +60 -0
- package/packages/chat-room-channeld/src/pg/sql.mjs +175 -0
- package/packages/chat-room-channeld/src/pg/sqlite.ts +100 -0
- package/packages/chat-room-channeld/src/pg/worker.mjs +133 -0
- package/packages/chat-room-channeld/src/protocol.ts +1 -0
- package/packages/chat-room-channeld/src/state.ts +73 -97
- package/packages/chat-room-channeld/src/stats.ts +140 -109
- package/server/assets/base.css +3 -1
- package/server/assets/chat/admin-badge.js +32 -30
- package/server/assets/chat/admin.js +201 -161
- package/server/assets/chat/announcements.js +302 -208
- package/server/assets/chat/attachment-store.js +116 -76
- package/server/assets/chat/attachments.js +701 -441
- package/server/assets/chat/chat-background.js +179 -156
- package/server/assets/chat/clipboard.js +45 -25
- package/server/assets/chat/confirm-dialog.js +38 -35
- package/server/assets/chat/copy-action.js +40 -41
- package/server/assets/chat/create-room-dialog.js +50 -47
- package/server/assets/chat/diagram-viewer.js +293 -236
- package/server/assets/chat/doc-preview.js +516 -443
- package/server/assets/chat/editor.js +446 -336
- package/server/assets/chat/events.js +650 -510
- package/server/assets/chat/files.js +505 -431
- package/server/assets/chat/history-loader.js +81 -73
- package/server/assets/chat/icons.js +9 -11
- package/server/assets/chat/image-zoom.js +246 -179
- package/server/assets/chat/index.js +2 -3
- package/server/assets/chat/install-prompt.js +90 -69
- package/server/assets/chat/integration-account-schema.js +91 -87
- package/server/assets/chat/integration-editor.js +162 -155
- package/server/assets/chat/integration-values.js +116 -101
- package/server/assets/chat/integrations.js +750 -626
- package/server/assets/chat/join-requests.js +415 -358
- package/server/assets/chat/locales/de.js +193 -194
- package/server/assets/chat/locales/es.js +193 -194
- package/server/assets/chat/locales/fr.js +193 -194
- package/server/assets/chat/locales/index.js +1 -2
- package/server/assets/chat/locales/it.js +193 -194
- package/server/assets/chat/locales/ja.js +193 -193
- package/server/assets/chat/locales/ko.js +193 -193
- package/server/assets/chat/markdown-footnotes.js +134 -103
- package/server/assets/chat/markdown.js +557 -404
- package/server/assets/chat/members.js +259 -197
- package/server/assets/chat/mentions.js +181 -133
- package/server/assets/chat/message-cache.js +40 -33
- package/server/assets/chat/message-polling.js +299 -227
- package/server/assets/chat/message-window.js +78 -55
- package/server/assets/chat/messages.js +562 -480
- package/server/assets/chat/office-preview.js +46 -45
- package/server/assets/chat/palette.js +464 -417
- package/server/assets/chat/panels.js +94 -79
- package/server/assets/chat/pwa.js +147 -75
- package/server/assets/chat/questions.js +374 -302
- package/server/assets/chat/recall.js +338 -273
- package/server/assets/chat/receipts.js +151 -121
- package/server/assets/chat/reminders.js +345 -214
- package/server/assets/chat/replies.js +181 -130
- package/server/assets/chat/resource-format.js +59 -62
- package/server/assets/chat/room-list.js +58 -55
- package/server/assets/chat/room-watcher.js +76 -50
- package/server/assets/chat/rooms.js +631 -500
- package/server/assets/chat/search.js +138 -114
- package/server/assets/chat/settings.js +225 -201
- package/server/assets/chat/shiki.js +62 -63
- package/server/assets/chat/shortcuts.js +212 -189
- package/server/assets/chat/side-pane.js +161 -150
- package/server/assets/chat/state.js +860 -737
- package/server/assets/chat/system-events.js +46 -44
- package/server/assets/chat/system-messages.js +57 -56
- package/server/assets/chat/task-form.js +179 -183
- package/server/assets/chat/task-row.js +158 -155
- package/server/assets/chat/tasks.js +665 -587
- package/server/assets/chat/viewport.js +32 -29
- package/server/assets/chat/window-controls.js +32 -32
- package/server/assets/conversation.css +10 -4
- package/server/chat-server.ts +9 -1
- package/server/chat.html +3 -1
- package/server/routes/pages.ts +7 -5
- package/server/sw.js +346 -268
- package/src/web/integration-rules-editor.ts +2 -0
|
@@ -1,517 +1,648 @@
|
|
|
1
1
|
import { LS, LOADED_MESSAGE_LIMIT } from "./state.js";
|
|
2
2
|
import { isUnreadMessage } from "./messages.js";
|
|
3
|
-
import {
|
|
3
|
+
import { setRoomPreview } from "./system-messages.js";
|
|
4
4
|
import { watchRoom } from "./room-watcher.js";
|
|
5
5
|
import { cacheRoomSoon, deleteCachedRoom, loadCachedRooms } from "./message-cache.js";
|
|
6
6
|
import { renderRoomOptions } from "./room-list.js";
|
|
7
7
|
import { initPanels } from "./panels.js";
|
|
8
|
-
|
|
8
|
+
const statusOf = (error) => error instanceof Error && "status" in error ? error.status : undefined;
|
|
9
|
+
const messageOf = (error) => error instanceof Error ? error.message : "";
|
|
10
|
+
const isNetworkError = (error) => error instanceof Error && "network" in error && Boolean(error.network);
|
|
9
11
|
export function initRooms(app) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
app.showJoin = (refresh = true, preserveRoomListGeneration = false) => {
|
|
30
|
-
if (!preserveRoomListGeneration) invalidateRoomList();
|
|
31
|
-
document.documentElement.classList.remove("session-restore");
|
|
32
|
-
if (!$("#inName").value) $("#inName").value = LS.name;
|
|
33
|
-
$("#chatView").hidden = true;
|
|
34
|
-
$("#restoreView").hidden = true;
|
|
35
|
-
$("#joinView").hidden = false;
|
|
36
|
-
app.updateTitle();
|
|
37
|
-
if (refresh) void app.refreshRoomList();
|
|
38
|
-
// A late restore must not steal the caret from someone already typing in the join form.
|
|
39
|
-
const active = document.activeElement;
|
|
40
|
-
if (!active || active === document.body || !active.closest?.("#joinView, dialog")) $("#inRoomId").focus();
|
|
41
|
-
};
|
|
42
|
-
app.showChat = () => {
|
|
43
|
-
document.documentElement.classList.remove("session-restore");
|
|
44
|
-
$("#joinView").hidden = true;
|
|
45
|
-
$("#restoreView").hidden = true;
|
|
46
|
-
$("#chatView").hidden = false;
|
|
47
|
-
};
|
|
48
|
-
app.renderHead = () => {
|
|
49
|
-
$("#roomName").textContent = app.state.room.name;
|
|
50
|
-
$("#roomIdShort").textContent = app.state.room.id.slice(0, 8);
|
|
51
|
-
$("#btnCopyId").title = app.t("copyRoomIdTitle", { id: app.state.room.id });
|
|
52
|
-
$("#btnCopyId").setAttribute("aria-label", app.t("copyRoomId"));
|
|
53
|
-
$("#railIdentity").textContent = app.state.myName;
|
|
54
|
-
$("#profileAvatar").textContent = Array.from(app.state.myName.trim())[0]?.toLocaleUpperCase() || "?";
|
|
55
|
-
const description = app.state.room.description || "";
|
|
56
|
-
$("#roomDescription").textContent = description;
|
|
57
|
-
$("#roomDescription").hidden = !description;
|
|
58
|
-
$("#roomPolicy").hidden = app.state.room.joinPolicy !== "approval";
|
|
59
|
-
// Everyone sees the settings entry; people who cannot manage the room get a
|
|
60
|
-
// secondary style and an explanation. Dissolving the room stays the owner's call.
|
|
61
|
-
const owner = app.state.myName === app.state.room.ownerId;
|
|
62
|
-
const admin = owner || (app.state.room.adminIds ?? []).includes(app.state.myName);
|
|
63
|
-
$("#btnEditRoom").classList.toggle("menu-secondary", !admin);
|
|
64
|
-
$("#btnDeleteRoom").hidden = !owner;
|
|
65
|
-
app.updateTitle();
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
app.enterRoom = (room, name, intent = app.state.navigationGeneration) => {
|
|
69
|
-
app.ensureJoinedRoom(room, name);
|
|
70
|
-
if (intent === app.state.navigationGeneration) {
|
|
71
|
-
app.activateRoom(room.id);
|
|
72
|
-
// Membership changed, so the rail needs one refresh. Plain room switches
|
|
73
|
-
// stay cache-only and do not reach the network here.
|
|
74
|
-
void app.refreshRoomList();
|
|
75
|
-
} else {
|
|
76
|
-
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
77
|
-
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
78
|
-
void app.refreshRoomList();
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
app.ensureJoinedRoom = (room, name) => {
|
|
83
|
-
const existing = app.state.rooms.get(room.id);
|
|
84
|
-
const entry = existing || { unreadIds: new Map(), unreadOverflow: false, preview: "", previewAt: 0, position: 0, readTs: 0, receivedTs: room.messages.at(-1)?.createdAt ?? 0, recallVersion: 0 };
|
|
85
|
-
entry.unreadIds ||= new Map();
|
|
86
|
-
entry.unreadOverflow ||= false;
|
|
87
|
-
const latest = room.messages.at(-1), savedReadTs = app.state.lastReadTs.get(room.id) ?? (typeof room.lastReadTs === "number" ? room.lastReadTs : undefined);
|
|
88
|
-
if (savedReadTs !== undefined && !app.state.lastReadTs.has(room.id)) app.saveReadTs(room.id, savedReadTs);
|
|
89
|
-
entry.readTs ||= savedReadTs || 0;
|
|
90
|
-
// A server page only carries the newest window; keep older history already
|
|
91
|
-
// loaded or restored from the local cache instead of dropping it.
|
|
92
|
-
if (existing) {
|
|
93
|
-
const merged = new Map([...existing.room.messages, ...room.messages].map((message) => [message.id, message]));
|
|
94
|
-
room = { ...room, messages: [...merged.values()].sort((left, right) => left.createdAt - right.createdAt).slice(-LOADED_MESSAGE_LIMIT) };
|
|
95
|
-
}
|
|
96
|
-
entry.historyComplete = Boolean(existing?.historyComplete) || room.hasMore === false;
|
|
97
|
-
entry.historyBeforeSeq = entry.historyComplete ? null : existing?.historyBeforeSeq ?? (typeof room.messages[0]?.seq === "number" ? room.messages[0].seq : null);
|
|
98
|
-
entry.room = room;
|
|
99
|
-
entry.name = name;
|
|
100
|
-
// Only rebuild unread history when this browser already has a read watermark;
|
|
101
|
-
// a never-opened room is not a burst of unread messages.
|
|
102
|
-
if (!existing && savedReadTs !== undefined) {
|
|
103
|
-
for (const message of room.messages) {
|
|
104
|
-
const unread = message.createdAt > entry.readTs;
|
|
105
|
-
if (unread && isUnreadMessage(message, app, room, name)) entry.unreadIds.set(message.id, message);
|
|
106
|
-
}
|
|
107
|
-
entry.unreadOverflow ||= Boolean(room.hasMore && entry.readTs < (room.messages[0]?.createdAt ?? Infinity));
|
|
108
|
-
}
|
|
109
|
-
entry.receivedTs ??= latest?.createdAt ?? 0;
|
|
110
|
-
entry.recallStale = Boolean(existing && (Number(room.recallVersion) || 0) > (existing.recallVersion || 0));
|
|
111
|
-
entry.recallVersion = Number(room.recallVersion) || entry.recallVersion || 0;
|
|
112
|
-
if (latest && (!existing || latest.createdAt >= entry.previewAt)) setRoomPreview(entry, latest);
|
|
113
|
-
app.state.rooms.set(room.id, entry);
|
|
114
|
-
app.state.roomNames.set(room.id, name);
|
|
115
|
-
app.state.joinedIds.add(room.id);
|
|
116
|
-
app.saveJoinedIds();
|
|
117
|
-
app.saveRoomNames();
|
|
118
|
-
app.startRoomWatcher();
|
|
119
|
-
cacheRoomSoon(app, room.id);
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
app.activateRoom = (roomId, preserveRoomListGeneration = false) => {
|
|
123
|
-
app.state.navigationGeneration++;
|
|
124
|
-
const entry = app.state.rooms.get(roomId);
|
|
125
|
-
if (!entry) return;
|
|
126
|
-
if (app.state.room?.id === roomId) return;
|
|
127
|
-
app.cancelThreadScrollAnimation?.();
|
|
128
|
-
if (!preserveRoomListGeneration) invalidateRoomList();
|
|
129
|
-
if (app.state.room) {
|
|
130
|
-
app.saveDraft(app.state.room.id, $("#msgInput").draftValue);
|
|
131
|
-
app.captureReadingPosition?.();
|
|
132
|
-
}
|
|
133
|
-
app.stopPolling();
|
|
134
|
-
app.state.previousRoomId = app.state.room?.id;
|
|
135
|
-
app.state.room = entry.room;
|
|
136
|
-
app.state.myName = entry.name;
|
|
137
|
-
app.state.unread = entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0);
|
|
138
|
-
app.rendered = 0;
|
|
139
|
-
app.firstMessageId = null;
|
|
140
|
-
app.state.pendingAction = null;
|
|
141
|
-
app.state.focusMessageId = null;
|
|
142
|
-
LS.roomId = roomId;
|
|
143
|
-
app.showChat();
|
|
144
|
-
app.renderHead();
|
|
145
|
-
$("#msgList").textContent = "";
|
|
146
|
-
app.state.listMetrics = { height: 0, top: 0, client: 0 };
|
|
147
|
-
// Cached history can hold thousands of messages. Render the newest window
|
|
148
|
-
// first so switching stays immediate; older rows are prepended on demand.
|
|
149
|
-
app.renderMessages(false, 300);
|
|
150
|
-
app.restoreReadingPosition?.(roomId);
|
|
151
|
-
app.state.readAnchorGuard = null;
|
|
152
|
-
app.startPolling();
|
|
153
|
-
if (entry.recallStale) {
|
|
154
|
-
entry.recallStale = false;
|
|
155
|
-
void app.syncLoadedHistory?.(roomId, app.state.pollGeneration);
|
|
156
|
-
}
|
|
157
|
-
app.renderMembers();
|
|
158
|
-
$("#msgInput").contentEditable = "true";
|
|
159
|
-
$("#msgInput").setAttribute("aria-disabled", "false"); $("#msgInput").tabIndex = 0;
|
|
160
|
-
$("#msgInput").draftValue = app.state.drafts.get(roomId) || ""; app.renderReplyPreview?.();
|
|
161
|
-
app.renderFailedMessages();
|
|
162
|
-
app.autoGrow($("#msgInput"));
|
|
163
|
-
// A restored draft and late layout can resize the composer after the initial bottom
|
|
164
|
-
// pin. Keep following for a few frames instead of sampling one fragile moment.
|
|
165
|
-
let settleFrames = 0;
|
|
166
|
-
const settleBottom = () => {
|
|
167
|
-
if (app.state.room?.id !== roomId || !app.state.followBottom) return;
|
|
168
|
-
const thread = $("#thread");
|
|
169
|
-
if (thread.scrollHeight - thread.scrollTop - thread.clientHeight > 1) thread.scrollTop = thread.scrollHeight;
|
|
170
|
-
if (++settleFrames < 30) requestAnimationFrame(settleBottom);
|
|
12
|
+
const $ = (selector) => app.$(selector);
|
|
13
|
+
initPanels(app);
|
|
14
|
+
let roomListGeneration = 0;
|
|
15
|
+
let autoRefresh;
|
|
16
|
+
const invalidateRoomList = () => { autoRefresh = undefined; return ++roomListGeneration; };
|
|
17
|
+
app.cacheRoomSoon = (roomId) => cacheRoomSoon(app, roomId);
|
|
18
|
+
// Render joined rooms from the local snapshot before the network round trip;
|
|
19
|
+
// the normal restore then validates membership and merges the server delta.
|
|
20
|
+
app.seedFromCache = async () => {
|
|
21
|
+
for (const record of await loadCachedRooms()) {
|
|
22
|
+
const room = record?.room;
|
|
23
|
+
if (!room?.id || app.state.rooms.has(room.id))
|
|
24
|
+
continue;
|
|
25
|
+
if (!app.state.joinedIds.has(room.id) && room.id !== LS.roomId)
|
|
26
|
+
continue;
|
|
27
|
+
app.ensureJoinedRoom(room, record.name || app.state.roomNames.get(room.id) || LS.name || app.t("guest"));
|
|
28
|
+
}
|
|
29
|
+
if (!app.state.room && LS.roomId && app.state.rooms.has(LS.roomId))
|
|
30
|
+
app.activateRoom(LS.roomId);
|
|
171
31
|
};
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const active = app.state.room?.id === roomId;
|
|
271
|
-
const entry = app.state.rooms.get(roomId);
|
|
272
|
-
app.disposeRoomAttachments?.(roomId);
|
|
273
|
-
if (active) {
|
|
274
|
-
app.state.navigationGeneration++;
|
|
275
|
-
app.saveDraft(roomId, $("#msgInput").draftValue);
|
|
276
|
-
app.captureReadingPosition?.();
|
|
277
|
-
app.stopPolling();
|
|
278
|
-
app.closeMention();
|
|
279
|
-
app.state.room = null; app.renderReplyPreview?.();
|
|
280
|
-
app.state.myName = "";
|
|
281
|
-
app.state.unread = 0;
|
|
282
|
-
app.rendered = 0;
|
|
283
|
-
app.firstMessageId = null;
|
|
284
|
-
app.state.focusMessageId = null;
|
|
285
|
-
$("#msgList").textContent = "";
|
|
286
|
-
$("#msgInput").contentEditable = "false";
|
|
287
|
-
$("#msgInput").setAttribute("aria-disabled", "true"); $("#msgInput").tabIndex = -1;
|
|
288
|
-
$("#btnSend").disabled = true;
|
|
289
|
-
}
|
|
290
|
-
app.state.rooms.delete(roomId);
|
|
291
|
-
app.state.availableRooms = (app.state.availableRooms || []).filter((room) => room.id !== roomId);
|
|
292
|
-
void deleteCachedRoom(roomId);
|
|
293
|
-
app.state.memberSessions.delete(roomId);
|
|
294
|
-
app.state.joinedIds.delete(roomId);
|
|
295
|
-
app.state.roomNames.delete(roomId);
|
|
296
|
-
app.saveJoinedIds();
|
|
297
|
-
app.saveRoomNames();
|
|
298
|
-
if (leaveServer && entry) {
|
|
299
|
-
void app.api(`/rooms/${encodeURIComponent(roomId)}/leave`, {
|
|
300
|
-
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID }),
|
|
301
|
-
}).catch(() => {});
|
|
302
|
-
}
|
|
303
|
-
if (active) {
|
|
304
|
-
const next = app.state.rooms.keys().next().value;
|
|
305
|
-
if (next) app.activateRoom(next, true);
|
|
306
|
-
else {
|
|
307
|
-
LS.roomId = "";
|
|
308
|
-
// Leaving the last room returns to the start screen; reload the list so
|
|
309
|
-
// the room the user just left is available to rejoin without a manual refresh.
|
|
310
|
-
app.showJoin(true, true);
|
|
311
|
-
if (reason) app.showJoinError("roomNotFound");
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
app.startRoomWatcher();
|
|
315
|
-
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
316
|
-
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
app.startRoomWatcher = () => {
|
|
320
|
-
clearTimeout(app.state.watcherTimer);
|
|
321
|
-
if (!app.state.rooms.size && !app.state.restorePending?.size && !app.state.restoreRetry) return;
|
|
322
|
-
app.state.watcherTimer = setTimeout(async () => {
|
|
323
|
-
// A hidden tab has no reader; skip background room polling until it is
|
|
324
|
-
// visible again (the next timer tick after visibility resumes).
|
|
325
|
-
if (typeof document !== "undefined" && document.hidden) { app.startRoomWatcher(); return; }
|
|
326
|
-
const inactive = [...app.state.rooms.keys()].filter((id) => id !== app.state.room?.id);
|
|
327
|
-
await bounded(inactive, 3, (roomId) => watchRoom(roomId, app));
|
|
328
|
-
if (app.state.restorePending?.size || app.state.restoreRetry) void app.refreshRoomList(true);
|
|
329
|
-
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
330
|
-
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
331
|
-
app.startRoomWatcher();
|
|
332
|
-
}, 5000);
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
app.refreshRoomList = (automatic = false) => {
|
|
336
|
-
if (automatic && autoRefresh?.generation === roomListGeneration) return autoRefresh.promise;
|
|
337
|
-
const generation = invalidateRoomList(), promise = loadRoomList(generation);
|
|
338
|
-
if (automatic) { autoRefresh = { generation, promise }; const clear = () => { if (autoRefresh?.promise === promise) autoRefresh = undefined; }; void promise.then(clear, clear); }
|
|
339
|
-
return promise;
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
async function loadRoomList(generation) {
|
|
343
|
-
const empty = $("#roomListEmpty"), chatEmpty = $("#roomListChatEmpty");
|
|
344
|
-
try {
|
|
345
|
-
const rooms = await app.api(`/rooms?sessionID=${encodeURIComponent(LS.sessionID)}`);
|
|
346
|
-
if (generation !== roomListGeneration) return;
|
|
347
|
-
app.state.restoreRetry = false;
|
|
348
|
-
app.state.availableRooms = rooms;
|
|
349
|
-
if (!app.state.restored || app.state.restorePending?.size) await app.restoreJoinedRooms(rooms, generation);
|
|
350
|
-
else {
|
|
351
|
-
syncCachedRooms(rooms, app);
|
|
352
|
-
await app.discoverDirectRooms(rooms, generation);
|
|
353
|
-
}
|
|
354
|
-
if (generation !== roomListGeneration) return;
|
|
355
|
-
app.renderRoomOptions($("#roomList"), rooms);
|
|
356
|
-
app.renderRoomOptions($("#roomListChat"), rooms);
|
|
357
|
-
empty.hidden = rooms.length > 0;
|
|
358
|
-
chatEmpty.hidden = rooms.length > 0;
|
|
359
|
-
} catch (error) {
|
|
360
|
-
if (generation !== roomListGeneration) return;
|
|
361
|
-
if (error.status === 401) {
|
|
362
|
-
if (!app.state.room && !app.state.restored) { app.showJoin(false); app.openTokenBox(); app.showJoinError("authRetry"); }
|
|
363
|
-
else app.state.room ? app.openChatSettings() : app.openTokenBox();
|
|
364
|
-
}
|
|
365
|
-
else if (!app.state.restored && !app.state.room) app.showRestoreFailure(app.t("networkRetry"));
|
|
366
|
-
else {
|
|
367
|
-
app.setText(empty, "roomListError");
|
|
368
|
-
app.setText(chatEmpty, "roomListError");
|
|
369
|
-
empty.hidden = false;
|
|
370
|
-
chatEmpty.hidden = false;
|
|
371
|
-
}
|
|
372
|
-
if (!app.state.restored) {
|
|
373
|
-
app.state.restoreRetry = true;
|
|
374
|
-
app.state.restorePending = new Set([...app.state.joinedIds, LS.roomId].filter(Boolean));
|
|
32
|
+
app.showJoin = (refresh = true, preserveRoomListGeneration = false) => {
|
|
33
|
+
if (!preserveRoomListGeneration)
|
|
34
|
+
invalidateRoomList();
|
|
35
|
+
document.documentElement.classList.remove("session-restore");
|
|
36
|
+
if (!$("#inName").value)
|
|
37
|
+
$("#inName").value = LS.name;
|
|
38
|
+
$("#chatView").hidden = true;
|
|
39
|
+
$("#restoreView").hidden = true;
|
|
40
|
+
$("#joinView").hidden = false;
|
|
41
|
+
app.updateTitle();
|
|
42
|
+
if (refresh)
|
|
43
|
+
void app.refreshRoomList();
|
|
44
|
+
// A late restore must not steal the caret from someone already typing in the join form.
|
|
45
|
+
const active = document.activeElement;
|
|
46
|
+
if (!active || active === document.body || !active.closest?.("#joinView, dialog"))
|
|
47
|
+
$("#inRoomId").focus();
|
|
48
|
+
};
|
|
49
|
+
app.showChat = () => {
|
|
50
|
+
document.documentElement.classList.remove("session-restore");
|
|
51
|
+
$("#joinView").hidden = true;
|
|
52
|
+
$("#restoreView").hidden = true;
|
|
53
|
+
$("#chatView").hidden = false;
|
|
54
|
+
};
|
|
55
|
+
app.renderHead = () => {
|
|
56
|
+
const room = app.state.room;
|
|
57
|
+
if (!room)
|
|
58
|
+
return;
|
|
59
|
+
$("#roomName").textContent = room.name;
|
|
60
|
+
$("#roomIdShort").textContent = room.id.slice(0, 8);
|
|
61
|
+
$("#btnCopyId").title = app.t("copyRoomIdTitle", { id: room.id });
|
|
62
|
+
$("#btnCopyId").setAttribute("aria-label", app.t("copyRoomId"));
|
|
63
|
+
$("#railIdentity").textContent = app.state.myName;
|
|
64
|
+
$("#profileAvatar").textContent = Array.from(app.state.myName.trim())[0]?.toLocaleUpperCase() || "?";
|
|
65
|
+
const description = room.description || "";
|
|
66
|
+
$("#roomDescription").textContent = description;
|
|
67
|
+
$("#roomDescription").hidden = !description;
|
|
68
|
+
$("#roomPolicy").hidden = room.joinPolicy !== "approval";
|
|
69
|
+
// Everyone sees the settings entry; people who cannot manage the room get a
|
|
70
|
+
// secondary style and an explanation. Dissolving the room stays the owner's call.
|
|
71
|
+
const owner = app.state.myName === room.ownerId;
|
|
72
|
+
const admin = owner || (room.adminIds ?? []).includes(app.state.myName);
|
|
73
|
+
$("#btnEditRoom").classList.toggle("menu-secondary", !admin);
|
|
74
|
+
$("#btnDeleteRoom").hidden = !owner;
|
|
75
|
+
app.updateTitle();
|
|
76
|
+
};
|
|
77
|
+
app.enterRoom = (room, name, intent = app.state.navigationGeneration) => {
|
|
78
|
+
app.ensureJoinedRoom(room, name);
|
|
79
|
+
if (intent === app.state.navigationGeneration) {
|
|
80
|
+
app.activateRoom(room.id);
|
|
81
|
+
// Membership changed, so the rail needs one refresh. Plain room switches
|
|
82
|
+
// stay cache-only and do not reach the network here.
|
|
83
|
+
void app.refreshRoomList();
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
87
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
88
|
+
void app.refreshRoomList();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
app.ensureJoinedRoom = (room, name) => {
|
|
92
|
+
const existing = app.state.rooms.get(room.id);
|
|
93
|
+
const entry = existing || { room, name, unreadIds: new Map(), unreadOverflow: false, preview: "", previewAt: 0, position: 0, readTs: 0, receivedTs: room.messages.at(-1)?.createdAt ?? 0, recallVersion: 0 };
|
|
94
|
+
entry.unreadIds ||= new Map();
|
|
95
|
+
entry.unreadOverflow ||= false;
|
|
96
|
+
const latest = room.messages.at(-1), savedReadTs = app.state.lastReadTs.get(room.id) ?? (typeof room.lastReadTs === "number" ? room.lastReadTs : undefined);
|
|
97
|
+
if (savedReadTs !== undefined && !app.state.lastReadTs.has(room.id))
|
|
98
|
+
app.saveReadTs(room.id, savedReadTs);
|
|
99
|
+
entry.readTs ||= savedReadTs || 0;
|
|
100
|
+
// A server page only carries the newest window; keep older history already
|
|
101
|
+
// loaded or restored from the local cache instead of dropping it.
|
|
102
|
+
if (existing) {
|
|
103
|
+
const merged = new Map([...existing.room.messages, ...room.messages].map((message) => [message.id, message]));
|
|
104
|
+
room = { ...room, messages: [...merged.values()].sort((left, right) => left.createdAt - right.createdAt).slice(-LOADED_MESSAGE_LIMIT) };
|
|
105
|
+
}
|
|
106
|
+
entry.historyComplete = Boolean(existing?.historyComplete) || room.hasMore === false;
|
|
107
|
+
entry.historyBeforeSeq = entry.historyComplete ? null : existing?.historyBeforeSeq ?? (typeof room.messages[0]?.seq === "number" ? room.messages[0].seq : null);
|
|
108
|
+
entry.room = room;
|
|
109
|
+
entry.name = name;
|
|
110
|
+
// Only rebuild unread history when this browser already has a read watermark;
|
|
111
|
+
// a never-opened room is not a burst of unread messages.
|
|
112
|
+
if (!existing && savedReadTs !== undefined) {
|
|
113
|
+
for (const message of room.messages) {
|
|
114
|
+
const unread = message.createdAt > entry.readTs;
|
|
115
|
+
if (unread && isUnreadMessage(message, app, room, name))
|
|
116
|
+
entry.unreadIds.set(message.id, message);
|
|
117
|
+
}
|
|
118
|
+
entry.unreadOverflow ||= Boolean(room.hasMore && entry.readTs < (room.messages[0]?.createdAt ?? Infinity));
|
|
119
|
+
}
|
|
120
|
+
entry.receivedTs ??= latest?.createdAt ?? 0;
|
|
121
|
+
entry.recallStale = Boolean(existing && (Number(room.recallVersion) || 0) > (existing.recallVersion || 0));
|
|
122
|
+
entry.recallVersion = Number(room.recallVersion) || entry.recallVersion || 0;
|
|
123
|
+
if (latest && (!existing || latest.createdAt >= entry.previewAt))
|
|
124
|
+
setRoomPreview(entry, latest);
|
|
125
|
+
app.state.rooms.set(room.id, entry);
|
|
126
|
+
app.state.roomNames.set(room.id, name);
|
|
127
|
+
app.state.joinedIds.add(room.id);
|
|
128
|
+
app.saveJoinedIds();
|
|
129
|
+
app.saveRoomNames();
|
|
375
130
|
app.startRoomWatcher();
|
|
376
|
-
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
131
|
+
cacheRoomSoon(app, room.id);
|
|
132
|
+
};
|
|
133
|
+
app.activateRoom = (roomId, preserveRoomListGeneration = false) => {
|
|
134
|
+
app.state.navigationGeneration++;
|
|
135
|
+
const entry = app.state.rooms.get(roomId);
|
|
136
|
+
if (!entry)
|
|
137
|
+
return;
|
|
138
|
+
if (app.state.room?.id === roomId)
|
|
139
|
+
return;
|
|
140
|
+
app.cancelThreadScrollAnimation?.();
|
|
141
|
+
if (!preserveRoomListGeneration)
|
|
142
|
+
invalidateRoomList();
|
|
143
|
+
if (app.state.room) {
|
|
144
|
+
app.saveDraft(app.state.room.id, $("#msgInput").draftValue);
|
|
145
|
+
app.captureReadingPosition?.();
|
|
146
|
+
}
|
|
147
|
+
app.stopPolling();
|
|
148
|
+
app.state.previousRoomId = app.state.room?.id;
|
|
149
|
+
app.state.room = entry.room;
|
|
150
|
+
app.state.myName = entry.name;
|
|
151
|
+
app.state.unread = entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0);
|
|
152
|
+
app.rendered = 0;
|
|
153
|
+
app.firstMessageId = null;
|
|
154
|
+
app.state.pendingAction = null;
|
|
155
|
+
app.state.focusMessageId = null;
|
|
156
|
+
LS.roomId = roomId;
|
|
157
|
+
app.showChat();
|
|
158
|
+
app.renderHead();
|
|
159
|
+
$("#msgList").textContent = "";
|
|
160
|
+
app.state.listMetrics = { height: 0, top: 0, client: 0 };
|
|
161
|
+
// Cached history can hold thousands of messages. Render the newest window
|
|
162
|
+
// first so switching stays immediate; older rows are prepended on demand.
|
|
163
|
+
app.renderMessages(false, 300);
|
|
164
|
+
app.restoreReadingPosition?.();
|
|
165
|
+
app.state.readAnchorGuard = null;
|
|
166
|
+
app.startPolling();
|
|
167
|
+
if (entry.recallStale) {
|
|
168
|
+
entry.recallStale = false;
|
|
169
|
+
void app.syncLoadedHistory?.(roomId, app.state.pollGeneration);
|
|
170
|
+
}
|
|
171
|
+
app.renderMembers();
|
|
172
|
+
$("#msgInput").contentEditable = "true";
|
|
173
|
+
$("#msgInput").setAttribute("aria-disabled", "false");
|
|
174
|
+
$("#msgInput").tabIndex = 0;
|
|
175
|
+
$("#msgInput").draftValue = app.state.drafts.get(roomId) || "";
|
|
176
|
+
app.renderReplyPreview?.();
|
|
177
|
+
app.renderFailedMessages?.();
|
|
178
|
+
app.autoGrow?.($("#msgInput"));
|
|
179
|
+
// A restored draft and late layout can resize the composer after the initial bottom
|
|
180
|
+
// pin. Keep following for a few frames instead of sampling one fragile moment.
|
|
181
|
+
let settleFrames = 0;
|
|
182
|
+
const settleBottom = () => {
|
|
183
|
+
if (app.state.room?.id !== roomId || !app.state.followBottom)
|
|
184
|
+
return;
|
|
185
|
+
const thread = $("#thread");
|
|
186
|
+
if (thread.scrollHeight - thread.scrollTop - thread.clientHeight > 1)
|
|
187
|
+
thread.scrollTop = thread.scrollHeight;
|
|
188
|
+
if (++settleFrames < 30)
|
|
189
|
+
requestAnimationFrame(settleBottom);
|
|
190
|
+
};
|
|
191
|
+
requestAnimationFrame(settleBottom);
|
|
192
|
+
app.updateDraftCount?.();
|
|
193
|
+
app.updateSendState?.();
|
|
194
|
+
app.hideSendError();
|
|
195
|
+
app.updateBadge();
|
|
196
|
+
app.updateJump();
|
|
197
|
+
app.markActiveRoomRead();
|
|
198
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
199
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
200
|
+
// Switching must render from the local cache. The room watcher refreshes
|
|
201
|
+
// membership metadata in the background; do not start a room-list request here.
|
|
202
|
+
// Switching rooms replaces the clicked row; keep the composer ready to type.
|
|
203
|
+
app.focusComposer?.();
|
|
204
|
+
document.dispatchEvent(new CustomEvent("chat-room-activated", { detail: roomId }));
|
|
205
|
+
};
|
|
206
|
+
app.joinRoom = async (roomId, name) => {
|
|
207
|
+
if (app.state.rooms.has(roomId))
|
|
208
|
+
return app.activateRoom(roomId);
|
|
209
|
+
const intent = ++app.state.navigationGeneration;
|
|
210
|
+
invalidateRoomList();
|
|
211
|
+
app.state.pendingAction = null;
|
|
212
|
+
app.setJoinBusy(true);
|
|
213
|
+
app.hideJoinError();
|
|
214
|
+
try {
|
|
215
|
+
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}/join`, {
|
|
216
|
+
method: "POST",
|
|
217
|
+
body: JSON.stringify({ sessionID: LS.sessionID, participantId: name, participantName: name }),
|
|
218
|
+
});
|
|
219
|
+
invalidateRoomList();
|
|
220
|
+
LS.name = name;
|
|
221
|
+
if (room?.status === "pending") {
|
|
222
|
+
app.showJoinPending(roomId, name);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
app.enterRoom(room, name, intent);
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
if (statusOf(error) === 401) {
|
|
229
|
+
app.state.pendingAction = { type: "join", roomId, name };
|
|
230
|
+
if (app.state.room) {
|
|
231
|
+
app.openChatSettings();
|
|
232
|
+
app.showSendError("sendAuth");
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
app.openTokenBox();
|
|
236
|
+
app.showJoinError("authRetry");
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
else if (statusOf(error) === 409) {
|
|
240
|
+
app.showJoinError("joinNameTaken");
|
|
241
|
+
void app.refreshRoomList();
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
app.showJoinError(isNetworkError(error) ? "networkRetry" : "joinFailed", { message: messageOf(error) });
|
|
245
|
+
void app.refreshRoomList();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
finally {
|
|
249
|
+
app.setJoinBusy(false);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
app.createRoom = async (pending) => {
|
|
253
|
+
if (app.state.joinBusy)
|
|
254
|
+
return;
|
|
255
|
+
const action = pending;
|
|
256
|
+
const fromChat = action?.fromChat ?? Boolean(app.state.room);
|
|
257
|
+
const name = action?.name || (fromChat ? app.state.myName : $("#inName").value.trim()) || LS.name || app.t("guest");
|
|
258
|
+
const roomName = action?.roomName || $(fromChat ? "#inRoomNameChat" : "#inRoomName").value.trim();
|
|
259
|
+
const joinPolicy = action?.joinPolicy ?? ((fromChat ? $("#inApprovalRequiredChat").checked : $("#inApprovalRequired").checked) ? "approval" : "open");
|
|
260
|
+
if (!roomName) {
|
|
261
|
+
if (!fromChat)
|
|
262
|
+
return app.showJoinError("roomNameRequired");
|
|
263
|
+
app.showCreateRoomError("roomNameRequired");
|
|
264
|
+
$("#inRoomNameChat").focus();
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const intent = ++app.state.navigationGeneration;
|
|
268
|
+
invalidateRoomList();
|
|
269
|
+
app.setJoinBusy(true);
|
|
270
|
+
app.hideJoinError();
|
|
271
|
+
app.state.pendingAction = null;
|
|
272
|
+
if (fromChat) {
|
|
273
|
+
app.hideCreateRoomError();
|
|
274
|
+
$("#btnCreateChatRoom").disabled = true;
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
const room = await app.api("/rooms", {
|
|
278
|
+
method: "POST",
|
|
279
|
+
body: JSON.stringify({ name: roomName, ownerId: name, sessionID: LS.sessionID, joinPolicy }),
|
|
280
|
+
});
|
|
281
|
+
invalidateRoomList();
|
|
282
|
+
LS.name = name;
|
|
283
|
+
$("#inApprovalRequired").checked = false;
|
|
284
|
+
$("#inApprovalRequiredChat").checked = false;
|
|
285
|
+
if (fromChat) {
|
|
286
|
+
$("#inRoomNameChat").value = "";
|
|
287
|
+
app.closeCreateRoomDialog(false);
|
|
288
|
+
}
|
|
289
|
+
app.enterRoom(room, name, intent);
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
if (statusOf(error) === 401) {
|
|
293
|
+
app.state.pendingAction = { type: "create", name, roomName, fromChat, joinPolicy };
|
|
294
|
+
if (fromChat) {
|
|
295
|
+
app.closeCreateRoomDialog(false);
|
|
296
|
+
app.openChatSettings();
|
|
297
|
+
app.showSendError("sendAuth");
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
app.openTokenBox();
|
|
301
|
+
app.showJoinError("authRetry");
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
else if (fromChat) {
|
|
305
|
+
if ($("#createRoomDialog")?.open)
|
|
306
|
+
app.showCreateRoomError("createFailed", { message: messageOf(error) });
|
|
307
|
+
else
|
|
308
|
+
app.showSendError("createFailed", { message: messageOf(error) });
|
|
309
|
+
void app.refreshRoomList();
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
app.showJoinError("createFailed", { message: messageOf(error) });
|
|
313
|
+
void app.refreshRoomList();
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
finally {
|
|
317
|
+
app.setJoinBusy(false);
|
|
318
|
+
$("#btnCreateChatRoom").disabled = false;
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
app.setJoinBusy = (busy) => {
|
|
322
|
+
app.state.joinBusy = busy;
|
|
323
|
+
$("#btnJoin").disabled = busy;
|
|
324
|
+
$("#btnCreate").disabled = busy;
|
|
325
|
+
};
|
|
326
|
+
app.leaveRoom = async (silent = false) => {
|
|
327
|
+
const room = app.state.room;
|
|
328
|
+
if (!room)
|
|
329
|
+
return;
|
|
330
|
+
if (!silent && !(await app.confirmAction(app.t("leaveRoomConfirm", { name: room.name }))))
|
|
331
|
+
return;
|
|
332
|
+
app.closePanels?.();
|
|
333
|
+
void app.removeJoinedRoom(room.id, { leaveServer: !silent });
|
|
334
|
+
};
|
|
335
|
+
app.removeJoinedRoom = async (roomId, { leaveServer = false, reason = false, reconcile = false } = {}) => {
|
|
336
|
+
if (!reconcile)
|
|
337
|
+
invalidateRoomList();
|
|
338
|
+
const active = app.state.room?.id === roomId;
|
|
339
|
+
const entry = app.state.rooms.get(roomId);
|
|
340
|
+
app.disposeRoomAttachments?.(roomId);
|
|
341
|
+
if (active) {
|
|
342
|
+
app.state.navigationGeneration++;
|
|
343
|
+
app.saveDraft(roomId, $("#msgInput").draftValue);
|
|
344
|
+
app.captureReadingPosition?.();
|
|
345
|
+
app.stopPolling();
|
|
346
|
+
app.closeMention();
|
|
347
|
+
app.state.room = null;
|
|
348
|
+
app.renderReplyPreview?.();
|
|
349
|
+
app.state.myName = "";
|
|
350
|
+
app.state.unread = 0;
|
|
351
|
+
app.rendered = 0;
|
|
352
|
+
app.firstMessageId = null;
|
|
353
|
+
app.state.focusMessageId = null;
|
|
354
|
+
$("#msgList").textContent = "";
|
|
355
|
+
$("#msgInput").contentEditable = "false";
|
|
356
|
+
$("#msgInput").setAttribute("aria-disabled", "true");
|
|
357
|
+
$("#msgInput").tabIndex = -1;
|
|
358
|
+
$("#btnSend").disabled = true;
|
|
359
|
+
}
|
|
360
|
+
app.state.rooms.delete(roomId);
|
|
361
|
+
app.state.availableRooms = (app.state.availableRooms || []).filter((room) => room.id !== roomId);
|
|
362
|
+
void deleteCachedRoom(roomId);
|
|
363
|
+
app.state.memberSessions.delete(roomId);
|
|
364
|
+
app.state.joinedIds.delete(roomId);
|
|
365
|
+
app.state.roomNames.delete(roomId);
|
|
366
|
+
app.saveJoinedIds();
|
|
367
|
+
app.saveRoomNames();
|
|
368
|
+
if (leaveServer && entry) {
|
|
369
|
+
void app.api(`/rooms/${encodeURIComponent(roomId)}/leave`, {
|
|
370
|
+
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID }),
|
|
371
|
+
}).catch(() => { });
|
|
372
|
+
}
|
|
373
|
+
if (active) {
|
|
374
|
+
const next = app.state.rooms.keys().next().value;
|
|
375
|
+
if (next)
|
|
376
|
+
app.activateRoom(next, true);
|
|
377
|
+
else {
|
|
378
|
+
LS.roomId = "";
|
|
379
|
+
// Leaving the last room returns to the start screen; reload the list so
|
|
380
|
+
// the room the user just left is available to rejoin without a manual refresh.
|
|
381
|
+
app.showJoin(true, true);
|
|
382
|
+
if (reason)
|
|
383
|
+
app.showJoinError("roomNotFound");
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
app.startRoomWatcher();
|
|
387
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
388
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
389
|
+
};
|
|
390
|
+
app.startRoomWatcher = () => {
|
|
391
|
+
clearTimeout(app.state.watcherTimer);
|
|
392
|
+
if (!app.state.rooms.size && !app.state.restorePending?.size && !app.state.restoreRetry)
|
|
393
|
+
return;
|
|
394
|
+
app.state.watcherTimer = setTimeout(async () => {
|
|
395
|
+
// A hidden tab has no reader; skip background room polling until it is
|
|
396
|
+
// visible again (the next timer tick after visibility resumes).
|
|
397
|
+
if (typeof document !== "undefined" && document.hidden) {
|
|
398
|
+
app.startRoomWatcher();
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const inactive = [...app.state.rooms.keys()].filter((id) => id !== app.state.room?.id);
|
|
402
|
+
await bounded(inactive, 3, (roomId) => watchRoom(roomId, app));
|
|
403
|
+
if (app.state.restorePending?.size || app.state.restoreRetry)
|
|
404
|
+
void app.refreshRoomList(true);
|
|
405
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
406
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
407
|
+
app.startRoomWatcher();
|
|
408
|
+
}, 5000);
|
|
409
|
+
};
|
|
410
|
+
app.refreshRoomList = (automatic = false) => {
|
|
411
|
+
if (automatic && autoRefresh?.generation === roomListGeneration)
|
|
412
|
+
return autoRefresh.promise;
|
|
413
|
+
const generation = invalidateRoomList(), promise = loadRoomList(generation);
|
|
414
|
+
if (automatic) {
|
|
415
|
+
autoRefresh = { generation, promise };
|
|
416
|
+
const clear = () => { if (autoRefresh?.promise === promise)
|
|
417
|
+
autoRefresh = undefined; };
|
|
418
|
+
void promise.then(clear, clear);
|
|
419
|
+
}
|
|
420
|
+
return promise;
|
|
421
|
+
};
|
|
422
|
+
async function loadRoomList(generation) {
|
|
423
|
+
const empty = $("#roomListEmpty"), chatEmpty = $("#roomListChatEmpty");
|
|
424
|
+
try {
|
|
425
|
+
const rooms = await app.api(`/rooms?sessionID=${encodeURIComponent(LS.sessionID)}`);
|
|
426
|
+
if (generation !== roomListGeneration)
|
|
427
|
+
return;
|
|
428
|
+
app.state.restoreRetry = false;
|
|
429
|
+
app.state.availableRooms = rooms;
|
|
430
|
+
if (!app.state.restored || app.state.restorePending?.size)
|
|
431
|
+
await app.restoreJoinedRooms(rooms, generation);
|
|
432
|
+
else {
|
|
433
|
+
syncCachedRooms(rooms, app);
|
|
434
|
+
await app.discoverDirectRooms(rooms, generation);
|
|
435
|
+
}
|
|
436
|
+
if (generation !== roomListGeneration)
|
|
437
|
+
return;
|
|
438
|
+
app.renderRoomOptions($("#roomList"), rooms);
|
|
439
|
+
app.renderRoomOptions($("#roomListChat"), rooms);
|
|
440
|
+
empty.hidden = rooms.length > 0;
|
|
441
|
+
chatEmpty.hidden = rooms.length > 0;
|
|
442
|
+
}
|
|
443
|
+
catch (error) {
|
|
444
|
+
if (generation !== roomListGeneration)
|
|
445
|
+
return;
|
|
446
|
+
if (statusOf(error) === 401) {
|
|
447
|
+
if (!app.state.room && !app.state.restored) {
|
|
448
|
+
app.showJoin(false);
|
|
449
|
+
app.openTokenBox();
|
|
450
|
+
app.showJoinError("authRetry");
|
|
451
|
+
}
|
|
452
|
+
else
|
|
453
|
+
app.state.room ? app.openChatSettings() : app.openTokenBox();
|
|
454
|
+
}
|
|
455
|
+
else if (!app.state.restored && !app.state.room)
|
|
456
|
+
app.showRestoreFailure(app.t("networkRetry"));
|
|
457
|
+
else {
|
|
458
|
+
app.setText(empty, "roomListError");
|
|
459
|
+
app.setText(chatEmpty, "roomListError");
|
|
460
|
+
empty.hidden = false;
|
|
461
|
+
chatEmpty.hidden = false;
|
|
462
|
+
}
|
|
463
|
+
if (!app.state.restored) {
|
|
464
|
+
app.state.restoreRetry = true;
|
|
465
|
+
app.state.restorePending = new Set([...app.state.joinedIds, LS.roomId].filter((id) => Boolean(id)));
|
|
466
|
+
app.startRoomWatcher();
|
|
467
|
+
}
|
|
408
468
|
}
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
if (generation !== roomListGeneration) return;
|
|
412
|
-
app.state.joinedIds = new Set([...valid, ...unresolved]);
|
|
413
|
-
app.state.restorePending = unresolved;
|
|
414
|
-
app.saveJoinedIds();
|
|
415
|
-
app.state.restored = unresolved.size === 0;
|
|
416
|
-
if (authFailed) { app.showJoin(false, true); app.openTokenBox(); app.showJoinError("authRetry"); return; }
|
|
417
|
-
if (activeId && valid.has(activeId)) app.activateRoom(activeId, true);
|
|
418
|
-
else if (activeId && unresolved.has(activeId)) {
|
|
419
|
-
app.showRestoreFailure(app.t("networkRetry"));
|
|
420
|
-
}
|
|
421
|
-
else {
|
|
422
|
-
LS.roomId = "";
|
|
423
|
-
const fallback = valid.values().next().value;
|
|
424
|
-
if (fallback) app.activateRoom(fallback, true);
|
|
425
|
-
else if (unresolved.size) app.showRestoreFailure(app.t("networkRetry"));
|
|
426
|
-
else app.showJoin(false, true);
|
|
427
469
|
}
|
|
428
|
-
app.
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
470
|
+
app.restoreJoinedRooms = async (rooms, generation = roomListGeneration) => {
|
|
471
|
+
while (app.state.restoring) {
|
|
472
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
473
|
+
if (generation !== roomListGeneration)
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
if (generation !== roomListGeneration)
|
|
477
|
+
return;
|
|
478
|
+
app.state.restoring = true;
|
|
479
|
+
try {
|
|
480
|
+
const activeId = LS.roomId;
|
|
481
|
+
const directIds = rooms.filter((room) => room.kind === "direct" && room.directSessionIDs?.includes(LS.sessionID)).map((room) => room.id);
|
|
482
|
+
const candidates = [...new Set([activeId, ...rooms.filter((room) => room.joined).map((room) => room.id), ...app.state.joinedIds, ...directIds].filter((id) => Boolean(id)))];
|
|
483
|
+
const valid = new Set();
|
|
484
|
+
const unresolved = new Set();
|
|
485
|
+
let authFailed = false;
|
|
486
|
+
await bounded(candidates, 3, async (roomId) => {
|
|
487
|
+
if (generation !== roomListGeneration)
|
|
488
|
+
return;
|
|
489
|
+
try {
|
|
490
|
+
const room = rooms.find((item) => item.id === roomId) || await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
491
|
+
if (generation !== roomListGeneration)
|
|
492
|
+
return;
|
|
493
|
+
const members = await app.api(`/rooms/${encodeURIComponent(roomId)}/members`);
|
|
494
|
+
if (generation !== roomListGeneration)
|
|
495
|
+
return;
|
|
496
|
+
const me = members.find((member) => member.sessionID === LS.sessionID);
|
|
497
|
+
if (!me)
|
|
498
|
+
return;
|
|
499
|
+
valid.add(roomId);
|
|
500
|
+
app.ensureJoinedRoom(room, me.name);
|
|
501
|
+
}
|
|
502
|
+
catch (error) {
|
|
503
|
+
if (generation !== roomListGeneration)
|
|
504
|
+
return;
|
|
505
|
+
if (statusOf(error) !== 404) {
|
|
506
|
+
unresolved.add(roomId);
|
|
507
|
+
if (statusOf(error) === 401)
|
|
508
|
+
authFailed = true;
|
|
509
|
+
if (statusOf(error) === 401)
|
|
510
|
+
app.state.room ? app.openChatSettings() : app.openTokenBox();
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
if (generation !== roomListGeneration)
|
|
515
|
+
return;
|
|
516
|
+
app.state.joinedIds = new Set([...valid, ...unresolved]);
|
|
517
|
+
app.state.restorePending = unresolved;
|
|
518
|
+
app.saveJoinedIds();
|
|
519
|
+
app.state.restored = unresolved.size === 0;
|
|
520
|
+
if (authFailed) {
|
|
521
|
+
app.showJoin(false, true);
|
|
522
|
+
app.openTokenBox();
|
|
523
|
+
app.showJoinError("authRetry");
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
if (activeId && valid.has(activeId))
|
|
527
|
+
app.activateRoom(activeId, true);
|
|
528
|
+
else if (activeId && unresolved.has(activeId)) {
|
|
529
|
+
app.showRestoreFailure(app.t("networkRetry"));
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
LS.roomId = "";
|
|
533
|
+
const fallback = valid.values().next().value;
|
|
534
|
+
if (fallback)
|
|
535
|
+
app.activateRoom(fallback, true);
|
|
536
|
+
else if (unresolved.size)
|
|
537
|
+
app.showRestoreFailure(app.t("networkRetry"));
|
|
538
|
+
else
|
|
539
|
+
app.showJoin(false, true);
|
|
540
|
+
}
|
|
541
|
+
app.startRoomWatcher();
|
|
542
|
+
}
|
|
543
|
+
finally {
|
|
544
|
+
app.state.restoring = false;
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
app.discoverDirectRooms = async (rooms, generation = roomListGeneration) => {
|
|
548
|
+
const candidates = rooms.filter((room) => room.kind === "direct" && !app.state.rooms.has(room.id));
|
|
549
|
+
await bounded(candidates, 3, async (room) => {
|
|
550
|
+
if (generation !== roomListGeneration)
|
|
551
|
+
return;
|
|
552
|
+
try {
|
|
553
|
+
const members = await app.api(`/rooms/${encodeURIComponent(room.id)}/members`);
|
|
554
|
+
if (generation !== roomListGeneration)
|
|
555
|
+
return;
|
|
556
|
+
const me = members.find((member) => member.sessionID === LS.sessionID);
|
|
557
|
+
if (me)
|
|
558
|
+
app.ensureJoinedRoom(room, me.name);
|
|
559
|
+
}
|
|
560
|
+
catch (error) {
|
|
561
|
+
if (generation !== roomListGeneration)
|
|
562
|
+
return;
|
|
563
|
+
if (statusOf(error) !== 404) {
|
|
564
|
+
app.state.restorePending ||= new Set();
|
|
565
|
+
app.state.restorePending.add(room.id);
|
|
566
|
+
app.state.joinedIds.add(room.id);
|
|
567
|
+
app.saveJoinedIds();
|
|
568
|
+
app.state.restored = false;
|
|
569
|
+
if (statusOf(error) === 401)
|
|
570
|
+
app.state.room ? app.openChatSettings() : app.openTokenBox();
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
if (generation !== roomListGeneration)
|
|
575
|
+
return;
|
|
576
|
+
if (app.state.restorePending?.size)
|
|
577
|
+
app.startRoomWatcher();
|
|
578
|
+
};
|
|
579
|
+
app.renderRoomOptions = (list, rooms) => renderRoomOptions(app, list, rooms);
|
|
580
|
+
app.deleteRoom = async () => {
|
|
581
|
+
const room = app.state.room;
|
|
582
|
+
if (!room || !(await app.confirmAction(app.t("deleteConfirm", { name: room.name }))))
|
|
583
|
+
return;
|
|
584
|
+
try {
|
|
585
|
+
await app.api(`/rooms/${encodeURIComponent(room.id)}`, {
|
|
586
|
+
method: "DELETE", body: JSON.stringify({ sessionID: LS.sessionID }),
|
|
587
|
+
});
|
|
588
|
+
await app.removeJoinedRoom(room.id);
|
|
589
|
+
app.clearDraft(room.id);
|
|
590
|
+
app.clearPosition(room.id);
|
|
591
|
+
app.saveFailedMessages(room.id, []);
|
|
450
592
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
} catch { app.showSendError("copyFailed", { id: app.state.room.id }); }
|
|
483
|
-
};
|
|
484
|
-
|
|
485
|
-
app.autoJoin = async () => {
|
|
486
|
-
await app.refreshRoomList();
|
|
487
|
-
};
|
|
488
|
-
|
|
489
|
-
app.retryPendingAction = () => {
|
|
490
|
-
const action = app.state.pendingAction;
|
|
491
|
-
if (action?.type === "join") void app.joinRoom(action.roomId, action.name);
|
|
492
|
-
else if (action?.type === "create") void app.createRoom(action);
|
|
493
|
-
else if (!app.state.room) void app.autoJoin();
|
|
494
|
-
};
|
|
593
|
+
catch (error) {
|
|
594
|
+
app.showSendError("sendFailed", { message: messageOf(error) });
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
app.copyRoomId = async () => {
|
|
598
|
+
const button = $("#btnCopyId");
|
|
599
|
+
const roomId = app.state.room.id;
|
|
600
|
+
try {
|
|
601
|
+
await navigator.clipboard.writeText(roomId);
|
|
602
|
+
// Swap a class so the ID span survives; flash() would overwrite the whole
|
|
603
|
+
// button and then restore it to "undefined" (the button has no data-i18n).
|
|
604
|
+
clearTimeout(button._copiedTimer);
|
|
605
|
+
button.classList.add("copied");
|
|
606
|
+
button._copiedTimer = setTimeout(() => button.classList.remove("copied"), 1200);
|
|
607
|
+
}
|
|
608
|
+
catch {
|
|
609
|
+
app.showSendError("copyFailed", { id: roomId });
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
app.autoJoin = async () => {
|
|
613
|
+
await app.refreshRoomList();
|
|
614
|
+
};
|
|
615
|
+
app.retryPendingAction = () => {
|
|
616
|
+
const action = app.state.pendingAction;
|
|
617
|
+
if (action?.type === "join")
|
|
618
|
+
void app.joinRoom(action.roomId, action.name);
|
|
619
|
+
else if (action?.type === "create")
|
|
620
|
+
void app.createRoom(action);
|
|
621
|
+
else if (!app.state.room)
|
|
622
|
+
void app.autoJoin();
|
|
623
|
+
};
|
|
495
624
|
}
|
|
496
|
-
|
|
497
625
|
async function bounded(items, limit, action) {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
626
|
+
let cursor = 0;
|
|
627
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
628
|
+
while (cursor < items.length)
|
|
629
|
+
await action(items[cursor++]);
|
|
630
|
+
}));
|
|
502
631
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
632
|
function syncCachedRooms(rooms, app) {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
633
|
+
const remote = new Map(rooms.map((room) => [room.id, room]));
|
|
634
|
+
for (const [roomId, entry] of app.state.rooms) {
|
|
635
|
+
const fresh = remote.get(roomId);
|
|
636
|
+
if (!fresh || fresh.joined === false) {
|
|
637
|
+
void app.removeJoinedRoom(roomId, { reason: app.state.room?.id === roomId, reconcile: true });
|
|
638
|
+
continue;
|
|
639
|
+
}
|
|
640
|
+
if (app.state.room?.id === roomId)
|
|
641
|
+
continue;
|
|
642
|
+
const messages = new Map([...entry.room.messages, ...fresh.messages].map((message) => [message.id, message]));
|
|
643
|
+
entry.room = { ...fresh, messages: [...messages.values()].sort((a, b) => a.createdAt - b.createdAt).slice(-LOADED_MESSAGE_LIMIT) };
|
|
644
|
+
const latest = entry.room.messages.at(-1);
|
|
645
|
+
if (latest)
|
|
646
|
+
setRoomPreview(entry, latest);
|
|
647
|
+
}
|
|
517
648
|
}
|