@alexsun-top/opencode-chat-room 0.6.6 → 0.6.8
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 +28 -0
- package/README.md +6 -3
- package/README.zh-CN.md +7 -3
- package/cli.ts +1 -1
- package/docs/administrator-rooms.md +11 -5
- package/index.ts +2 -2
- package/package.json +6 -2
- package/plugin.ts +57 -26
- package/server/admin-routes.ts +15 -7
- package/server/admin-state.ts +2 -2
- package/server/announcement-routes.ts +2 -2
- package/server/assets/attachments.css +25 -0
- package/server/assets/base.css +251 -39
- package/server/assets/chat/admin-badge.js +49 -0
- package/server/assets/chat/admin.js +77 -24
- package/server/assets/chat/announcements.js +27 -11
- package/server/assets/chat/attachment-store.js +106 -0
- package/server/assets/chat/attachments.js +245 -25
- package/server/assets/chat/events.js +56 -12
- package/server/assets/chat/image-zoom.js +141 -0
- package/server/assets/chat/index.js +10 -0
- package/server/assets/chat/install-prompt.js +82 -0
- package/server/assets/chat/join-requests.js +355 -0
- package/server/assets/chat/markdown.js +82 -5
- package/server/assets/chat/members.js +16 -6
- package/server/assets/chat/message-polling.js +158 -0
- package/server/assets/chat/message-window.js +14 -0
- package/server/assets/chat/messages.js +12 -132
- package/server/assets/chat/panels.js +37 -0
- package/server/assets/chat/questions.js +10 -6
- package/server/assets/chat/recall.js +279 -0
- package/server/assets/chat/receipts.js +3 -1
- package/server/assets/chat/reminders.js +67 -9
- package/server/assets/chat/replies.js +1 -1
- package/server/assets/chat/room-list.js +60 -0
- package/server/assets/chat/room-watcher.js +5 -1
- package/server/assets/chat/rooms.js +36 -84
- package/server/assets/chat/shortcuts.js +254 -0
- package/server/assets/chat/state.js +97 -24
- package/server/assets/chat/system-messages.js +28 -7
- package/server/assets/conversation.css +18 -26
- package/server/assets/keyboard-hints.css +51 -0
- package/server/assets/recall.css +29 -0
- package/server/assets/responsive.css +58 -16
- package/server/chat-server.ts +81 -62
- package/server/chat.html +238 -72
- package/server/history-route.ts +1 -1
- package/server/inbox.ts +3 -3
- package/server/join-requests.ts +106 -0
- package/server/member-routes.ts +4 -4
- package/server/message-recall-routes.ts +37 -0
- package/server/newrooms.ts +105 -13
- package/server/resource-routes.ts +19 -5
- package/server/room-join.ts +59 -0
- package/server/room-owner.ts +72 -0
- package/server/room-payload.ts +6 -2
- package/server/room-reminders.ts +3 -3
- package/server/sw.js +2 -0
- package/server/wake.ts +48 -19
- package/skills/chatroom/SKILL.md +35 -1
- package/src/README.md +22 -0
- package/src/{admin-client.ts → agent/admin-client.ts} +39 -9
- package/src/{admin-credentials.ts → agent/admin-credentials.ts} +1 -1
- package/src/{agent-presence.ts → agent/agent-presence.ts} +2 -2
- package/src/{agent-receipts.ts → agent/agent-receipts.ts} +2 -2
- package/src/{announcement-tool.ts → agent/announcement-tool.ts} +2 -2
- package/src/{chatroom-skill.ts → agent/chatroom-skill.ts} +1 -1
- package/src/{center-client.ts → center/center-client.ts} +103 -13
- package/src/{center-inbox.ts → center/center-inbox.ts} +44 -19
- package/src/{center-wake.ts → center/center-wake.ts} +31 -18
- package/src/{mentions.ts → center/mentions.ts} +1 -1
- package/src/center/message-ids.ts +7 -0
- package/src/center/notification-budget.ts +16 -0
- package/src/{queue-transport.ts → center/queue-transport.ts} +3 -3
- package/src/{queue.ts → center/queue.ts} +16 -6
- package/src/{reminder-client.ts → center/reminder-client.ts} +1 -1
- package/src/{session-delivery.ts → center/session-delivery.ts} +1 -1
- package/src/{announcements.ts → rooms/announcements.ts} +2 -2
- package/src/{resource-upload.ts → rooms/resource-upload.ts} +1 -1
- package/src/{resources.ts → rooms/resources.ts} +55 -9
- package/src/{room-service.ts → rooms/room-service.ts} +22 -2
- package/src/{background.ts → runtime/background.ts} +3 -3
- package/src/{plugin-notifications.ts → runtime/plugin-notifications.ts} +45 -19
- package/src/{v1-plugin.ts → runtime/v1-plugin.ts} +4 -4
- package/src/{v2-plugin.ts → runtime/v2-plugin.ts} +10 -10
- package/src/{v2-receiver.ts → runtime/v2-receiver.ts} +7 -7
- package/src/{watch.ts → runtime/watch.ts} +12 -12
- package/src/{member-state.ts → state/member-state.ts} +2 -2
- package/src/state/roles.ts +34 -0
- package/src/{sqlite-state.ts → state/sqlite-state.ts} +226 -37
- package/src/{store.ts → state/store.ts} +23 -4
- package/src/tests/announcement-board.ts +185 -0
- /package/src/{resource-read.ts → rooms/resource-read.ts} +0 -0
- /package/src/{receiver-owner.ts → state/receiver-owner.ts} +0 -0
- /package/src/{receiver-state.ts → state/receiver-state.ts} +0 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { LS, LOADED_MESSAGE_LIMIT } from "./state.js";
|
|
2
|
+
import { setRoomPreview } from "./system-messages.js";
|
|
3
|
+
|
|
4
|
+
// Inbox polling, room-meta refresh, and reconciliation live apart from rendering.
|
|
5
|
+
export function initMessagePolling(app, isUnreadMessage) {
|
|
6
|
+
const $ = app.$;
|
|
7
|
+
let pollDelay = 2000;
|
|
8
|
+
|
|
9
|
+
function isCurrentPoll(roomId, generation) {
|
|
10
|
+
return app.state.polling && app.state.pollGeneration === generation && app.state.room?.id === roomId;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
app.startPolling = () => {
|
|
14
|
+
app.stopPolling();
|
|
15
|
+
const generation = ++app.state.pollGeneration;
|
|
16
|
+
const controller = new AbortController();
|
|
17
|
+
app.state.pollController = controller;
|
|
18
|
+
app.state.polling = true;
|
|
19
|
+
pollDelay = 2000;
|
|
20
|
+
void pollLoop(generation, controller.signal);
|
|
21
|
+
};
|
|
22
|
+
app.stopPolling = () => {
|
|
23
|
+
app.state.polling = false;
|
|
24
|
+
app.state.pollGeneration++;
|
|
25
|
+
app.state.pollController?.abort();
|
|
26
|
+
app.state.pollController = null;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
async function pollLoop(generation, signal) {
|
|
30
|
+
while (app.state.polling && app.state.room && generation === app.state.pollGeneration) {
|
|
31
|
+
await poll(generation, signal).catch(() => {});
|
|
32
|
+
if (!app.state.polling || generation !== app.state.pollGeneration) break;
|
|
33
|
+
await new Promise((resolve) => setTimeout(resolve, pollDelay));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function poll(generation, signal) {
|
|
38
|
+
const roomId = app.state.room?.id;
|
|
39
|
+
if (!roomId || !isCurrentPoll(roomId, generation)) return;
|
|
40
|
+
try {
|
|
41
|
+
const entry = app.state.rooms.get(roomId);
|
|
42
|
+
const query = new URLSearchParams({ sessionID: LS.sessionID, roomId, excludeSelf: "0", timeout: "25000", afterTs: String(entry?.receivedTs || 0) });
|
|
43
|
+
// Lets the server answer at once when a recall changed the room since our
|
|
44
|
+
// last rendered snapshot instead of holding the long-poll.
|
|
45
|
+
if (entry && Number.isFinite(entry.recallVersion)) query.set("recallVersion", String(entry.recallVersion));
|
|
46
|
+
const data = await app.api(`/inbox?${query}`, { signal });
|
|
47
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
48
|
+
if (data.member === false) {
|
|
49
|
+
void app.removeJoinedRoom(roomId, { reason: true });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (data.member !== true || !Array.isArray(data.items)) return;
|
|
53
|
+
const items = data.items, previousReceivedTs = entry?.receivedTs || 0, messages = app.state.room.messages;
|
|
54
|
+
const known = new Set(messages.map((message) => message.id));
|
|
55
|
+
const previousUnreadOverflow = Boolean(entry?.unreadOverflow);
|
|
56
|
+
let sawSystem = false, added = 0, unreadChanged = false;
|
|
57
|
+
for (const item of items) {
|
|
58
|
+
if (item.senderId === "system") sawSystem = true;
|
|
59
|
+
const wasKnown = known.has(item.id);
|
|
60
|
+
if (!wasKnown) {
|
|
61
|
+
messages.push({ ...item, roomId }); known.add(item.id);
|
|
62
|
+
if (entry && item.createdAt >= entry.previewAt) setRoomPreview(entry, item);
|
|
63
|
+
added++;
|
|
64
|
+
}
|
|
65
|
+
if (entry && item.createdAt > entry.readTs && isUnreadMessage(item, app) && !entry.unreadIds.has(item.id)) {
|
|
66
|
+
entry.unreadIds.set(item.id, item); unreadChanged = true;
|
|
67
|
+
}
|
|
68
|
+
// An answer to a question clears that question even before the room is read.
|
|
69
|
+
if (entry && item.answerToMessageID && entry.unreadIds.get(item.answerToMessageID)?.isQuestion) {
|
|
70
|
+
entry.unreadIds.delete(item.answerToMessageID); unreadChanged = true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// A changed recall version means rows were hidden since the last look; refetch
|
|
74
|
+
// the newest page and patch the rows already on screen.
|
|
75
|
+
const recallVersion = Number(data.recallVersion);
|
|
76
|
+
if (entry && Number.isFinite(recallVersion) && recallVersion !== (entry.recallVersion ?? 0) && !entry.recallSync) {
|
|
77
|
+
entry.recallSync = true;
|
|
78
|
+
app.lastReconcileAt = Date.now();
|
|
79
|
+
void app.reconcileRoom?.(roomId, generation, true);
|
|
80
|
+
}
|
|
81
|
+
if (entry) entry.receivedTs = items.reduce((latest, item) => Number.isSafeInteger(item.createdAt) ? Math.max(latest, item.createdAt) : latest, previousReceivedTs);
|
|
82
|
+
if (entry) entry.unreadOverflow = Boolean(data.hasMore);
|
|
83
|
+
if (messages.length > LOADED_MESSAGE_LIMIT) { const anchorId = app.state.positions.get(roomId)?.messageId, anchor = anchorId && messages.find((item) => item.id === anchorId); messages.splice(0, messages.length - LOADED_MESSAGE_LIMIT); if (anchor && !messages.includes(anchor)) messages.unshift(anchor); }
|
|
84
|
+
app.cacheRoomSoon?.(roomId);
|
|
85
|
+
const rendered = app.renderMessages(true);
|
|
86
|
+
if (sawSystem) {
|
|
87
|
+
await app.refreshRoomMeta(roomId, generation);
|
|
88
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
89
|
+
}
|
|
90
|
+
if (added || unreadChanged || entry && previousUnreadOverflow !== entry.unreadOverflow) {
|
|
91
|
+
app.state.unread = entry ? entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0) : app.state.unread + added;
|
|
92
|
+
app.updateBadge();
|
|
93
|
+
app.updateJump();
|
|
94
|
+
}
|
|
95
|
+
if (rendered.skipped === 0) void app.markActiveRoomRead();
|
|
96
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
97
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
98
|
+
if (!app.lastReconcileAt || Date.now() - app.lastReconcileAt > 120000) {
|
|
99
|
+
app.lastReconcileAt = Date.now();
|
|
100
|
+
void app.reconcileRoom(roomId, generation);
|
|
101
|
+
}
|
|
102
|
+
app.setOnline(true);
|
|
103
|
+
pollDelay = data.hasMore && (entry?.receivedTs || 0) > previousReceivedTs ? 0 : 2000;
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
106
|
+
if (error.status === 401) {
|
|
107
|
+
app.setOnline(false);
|
|
108
|
+
if ($("#chatSettings").hidden) app.openChatSettings();
|
|
109
|
+
pollDelay = Math.min(pollDelay * 2, 30000);
|
|
110
|
+
} else if (error.status === 404) {
|
|
111
|
+
void app.removeJoinedRoom(roomId, { reason: true });
|
|
112
|
+
} else {
|
|
113
|
+
app.setOnline(false);
|
|
114
|
+
pollDelay = 2000;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
app.refreshRoomMeta = async (roomId = app.state.room?.id, generation = app.state.pollGeneration) => {
|
|
120
|
+
if (!roomId) return;
|
|
121
|
+
try {
|
|
122
|
+
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
123
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
124
|
+
const before = JSON.stringify([app.state.room.ownerId, app.state.room.adminIds ?? [], app.state.room.successorId ?? null, app.state.room.adminSessionID ?? null]);
|
|
125
|
+
Object.assign(app.state.room, { participants: room.participants, ownerId: room.ownerId ?? app.state.room.ownerId, adminIds: room.adminIds ?? [], successorId: room.successorId ?? null, adminSessionID: room.adminSessionID ?? null });
|
|
126
|
+
if (JSON.stringify([app.state.room.ownerId, app.state.room.adminIds ?? [], app.state.room.successorId ?? null, app.state.room.adminSessionID ?? null]) !== before) { app.renderHead(); app.rerenderMessages(); }
|
|
127
|
+
app.renderMembers();
|
|
128
|
+
} catch { /* The next room event will retry. */ }
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
app.reconcileRoom = async (roomId, generation = app.state.pollGeneration, syncHistory = false) => {
|
|
132
|
+
const entry = app.state.rooms.get(roomId);
|
|
133
|
+
try {
|
|
134
|
+
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
135
|
+
if (!isCurrentPoll(roomId, generation)) return;
|
|
136
|
+
if (entry && Number.isFinite(Number(room.recallVersion))) entry.recallVersion = Number(room.recallVersion);
|
|
137
|
+
const current = app.state.room.messages;
|
|
138
|
+
const ids = new Set(current.map((message) => message.id));
|
|
139
|
+
const missing = room.messages.filter((message) => !ids.has(message.id));
|
|
140
|
+
// History carries recall state; patch rows whose live recall event was missed.
|
|
141
|
+
let recalled = false;
|
|
142
|
+
for (const message of room.messages) {
|
|
143
|
+
if (message.recalledAt && app.applyRecall?.(roomId, { messageID: message.id, recalledAt: message.recalledAt, recalledBy: message.recalledBy })) recalled = true;
|
|
144
|
+
}
|
|
145
|
+
// Older loaded pages sit below this payload; hide their recalled text too.
|
|
146
|
+
if (syncHistory) void app.syncLoadedHistory?.(roomId, generation);
|
|
147
|
+
if (!missing.length && !recalled && !app.failedRenderIds.size) return;
|
|
148
|
+
for (const message of missing) {
|
|
149
|
+
const index = current.findIndex((item) => item.createdAt > message.createdAt);
|
|
150
|
+
current.splice(index < 0 ? current.length : index, 0, message);
|
|
151
|
+
}
|
|
152
|
+
if (current.length > LOADED_MESSAGE_LIMIT) current.splice(0, current.length - LOADED_MESSAGE_LIMIT);
|
|
153
|
+
const result = app.rerenderMessages();
|
|
154
|
+
if (result.skipped === 0) void app.markActiveRoomRead();
|
|
155
|
+
} catch { /* Try again during the next reconciliation. */ }
|
|
156
|
+
finally { if (entry) entry.recallSync = false; }
|
|
157
|
+
};
|
|
158
|
+
}
|
|
@@ -12,6 +12,20 @@ export function hasMessageSelection(list) {
|
|
|
12
12
|
&& (list.contains(selection.anchorNode) || list.contains(selection.focusNode)));
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
// A held pointer or a live selection owns the viewport. Layout writes may only
|
|
16
|
+
// follow the bottom when neither gesture is active.
|
|
17
|
+
export function pointerOrSelectionActive(app) {
|
|
18
|
+
if (app.state.threadPointerDown) return true;
|
|
19
|
+
const list = document.querySelector("#msgList");
|
|
20
|
+
return Boolean(list && hasMessageSelection(list));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function followsBottom(app, threshold = -1) {
|
|
24
|
+
if (pointerOrSelectionActive(app)) return false;
|
|
25
|
+
const thread = document.querySelector("#thread");
|
|
26
|
+
return Boolean(app.state.followBottom || (thread && thread.scrollHeight - thread.scrollTop - thread.clientHeight <= threshold));
|
|
27
|
+
}
|
|
28
|
+
|
|
15
29
|
export function trimMessages(list, app) {
|
|
16
30
|
if (list.children.length <= 400 || hasMessageSelection(list)) return;
|
|
17
31
|
const saved = app.state.positions.get(app.state.room?.id), rows = [...list.querySelectorAll("[data-msg-id]")];
|
|
@@ -3,11 +3,12 @@ import { renderMarkdown } from "./markdown.js";
|
|
|
3
3
|
import { hasMessageSelection, pruneMessageRows, trimMessages } from "./message-window.js";
|
|
4
4
|
import { formatSystemMessage, setRoomPreview } from "./system-messages.js";
|
|
5
5
|
import { installEditor } from "./editor.js";
|
|
6
|
+
import { adminAgentBadge, isAdminAgent } from "./admin-badge.js";
|
|
7
|
+
import { initMessagePolling } from "./message-polling.js";
|
|
6
8
|
|
|
7
9
|
export function initMessages(app) {
|
|
8
10
|
const $ = app.$;
|
|
9
11
|
installEditor($("#msgInput"));
|
|
10
|
-
let pollDelay = 2000;
|
|
11
12
|
|
|
12
13
|
app.autoGrow = (textarea) => {
|
|
13
14
|
const thread = $("#thread"), atBottom = Boolean(app.state.room && Math.abs(thread.scrollHeight - thread.clientHeight - thread.scrollTop) < 1);
|
|
@@ -207,7 +208,9 @@ export function initMessages(app) {
|
|
|
207
208
|
name.textContent = own ? app.t("you") : sender;
|
|
208
209
|
time.className = "msg-time";
|
|
209
210
|
time.textContent = app.timeHM(message.createdAt);
|
|
210
|
-
meta.append(name
|
|
211
|
+
meta.append(name);
|
|
212
|
+
if (isAdminAgent(app, message.senderSessionID || message.senderId)) meta.append(adminAgentBadge(app));
|
|
213
|
+
meta.append(time);
|
|
211
214
|
const body = document.createElement("div");
|
|
212
215
|
body.className = "msg-text";
|
|
213
216
|
renderMarkdown(body, message.text, app);
|
|
@@ -337,9 +340,9 @@ export function initMessages(app) {
|
|
|
337
340
|
|
|
338
341
|
app.send = async () => {
|
|
339
342
|
const input = $("#msgInput"), room = app.state.room, replyTarget = room && app.replyTargetFor?.(room.id);
|
|
340
|
-
if (!room || app.state.sendingRooms?.has(room.id)) return;
|
|
343
|
+
if (!room || app.state.sendingRooms?.has(room.id)) return false;
|
|
341
344
|
const message = app.prepareMessage?.(room.id, input.value) || { text: input.value.trim(), ids: [], ready: Boolean(input.value.trim()) };
|
|
342
|
-
if (!message.ready) return;
|
|
345
|
+
if (!message.ready) return false;
|
|
343
346
|
// Clicking Send focuses the button; it is disabled when the draft clears, so
|
|
344
347
|
// keep the caret in the composer or the next keystrokes go nowhere.
|
|
345
348
|
input.focus({ preventScroll: true });
|
|
@@ -362,7 +365,7 @@ export function initMessages(app) {
|
|
|
362
365
|
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text, ...(replyTarget ? { replyToMessageID: replyTarget.id } : {}) }),
|
|
363
366
|
});
|
|
364
367
|
app.completeAttachments?.(room.id, message.ids);
|
|
365
|
-
if (app.state.room?.id !== room.id) return;
|
|
368
|
+
if (app.state.room?.id !== room.id) return true;
|
|
366
369
|
const entry = app.state.rooms.get(room.id);
|
|
367
370
|
// The POST snapshot is older than anything polling received while it was
|
|
368
371
|
// in flight; merge instead of dropping those messages.
|
|
@@ -389,6 +392,7 @@ export function initMessages(app) {
|
|
|
389
392
|
void app.markActiveRoomRead();
|
|
390
393
|
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
391
394
|
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
395
|
+
return true;
|
|
392
396
|
} catch (error) {
|
|
393
397
|
const unchanged = (app.state.draftRevision.get(room.id) || 0) === revision;
|
|
394
398
|
// Someone answered the question first; retrying the answer can never succeed,
|
|
@@ -401,7 +405,7 @@ export function initMessages(app) {
|
|
|
401
405
|
app.renderFailedMessages();
|
|
402
406
|
}
|
|
403
407
|
app.completeAttachments?.(room.id, message.ids);
|
|
404
|
-
if (app.state.room?.id !== room.id) return;
|
|
408
|
+
if (app.state.room?.id !== room.id) return false;
|
|
405
409
|
if (unchanged && !staleReply) app.clearDraft(room.id);
|
|
406
410
|
if (staleReply) input.draftValue = text;
|
|
407
411
|
app.autoGrow(input);
|
|
@@ -409,138 +413,13 @@ export function initMessages(app) {
|
|
|
409
413
|
app.updateDraftCount();
|
|
410
414
|
if (error.status === 401) { app.openChatSettings(); app.showSendError("sendAuth"); }
|
|
411
415
|
else app.showSendError(error.network ? "sendNetwork" : "sendFailed", { message: error.message });
|
|
416
|
+
return false;
|
|
412
417
|
} finally {
|
|
413
418
|
app.state.sendingRooms.delete(room.id);
|
|
414
419
|
if (app.state.room?.id === room.id) app.updateSendState();
|
|
415
420
|
}
|
|
416
421
|
};
|
|
417
422
|
|
|
418
|
-
function isCurrentPoll(roomId, generation) {
|
|
419
|
-
return app.state.polling && app.state.pollGeneration === generation && app.state.room?.id === roomId;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
app.startPolling = () => {
|
|
423
|
-
app.stopPolling();
|
|
424
|
-
const generation = ++app.state.pollGeneration;
|
|
425
|
-
const controller = new AbortController();
|
|
426
|
-
app.state.pollController = controller;
|
|
427
|
-
app.state.polling = true;
|
|
428
|
-
pollDelay = 2000;
|
|
429
|
-
void pollLoop(generation, controller.signal);
|
|
430
|
-
};
|
|
431
|
-
app.stopPolling = () => {
|
|
432
|
-
app.state.polling = false;
|
|
433
|
-
app.state.pollGeneration++;
|
|
434
|
-
app.state.pollController?.abort();
|
|
435
|
-
app.state.pollController = null;
|
|
436
|
-
};
|
|
437
|
-
|
|
438
|
-
async function pollLoop(generation, signal) {
|
|
439
|
-
while (app.state.polling && app.state.room && generation === app.state.pollGeneration) {
|
|
440
|
-
await poll(generation, signal).catch(() => {});
|
|
441
|
-
if (!app.state.polling || generation !== app.state.pollGeneration) break;
|
|
442
|
-
await new Promise((resolve) => setTimeout(resolve, pollDelay));
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
async function poll(generation, signal) {
|
|
447
|
-
const roomId = app.state.room?.id;
|
|
448
|
-
if (!roomId || !isCurrentPoll(roomId, generation)) return;
|
|
449
|
-
try {
|
|
450
|
-
const entry = app.state.rooms.get(roomId);
|
|
451
|
-
const query = new URLSearchParams({ sessionID: LS.sessionID, roomId, excludeSelf: "0", timeout: "25000", afterTs: String(entry?.receivedTs || 0) });
|
|
452
|
-
const data = await app.api(`/inbox?${query}`, { signal });
|
|
453
|
-
if (!isCurrentPoll(roomId, generation)) return;
|
|
454
|
-
if (data.member === false) {
|
|
455
|
-
void app.removeJoinedRoom(roomId, { reason: true });
|
|
456
|
-
return;
|
|
457
|
-
}
|
|
458
|
-
if (data.member !== true || !Array.isArray(data.items)) return;
|
|
459
|
-
const items = data.items, previousReceivedTs = entry?.receivedTs || 0, messages = app.state.room.messages;
|
|
460
|
-
const known = new Set(messages.map((message) => message.id));
|
|
461
|
-
const previousUnreadOverflow = Boolean(entry?.unreadOverflow);
|
|
462
|
-
let sawSystem = false, added = 0, unreadChanged = false;
|
|
463
|
-
for (const item of items) {
|
|
464
|
-
if (item.senderId === "system") sawSystem = true;
|
|
465
|
-
const wasKnown = known.has(item.id);
|
|
466
|
-
if (!wasKnown) {
|
|
467
|
-
messages.push({ ...item, roomId }); known.add(item.id);
|
|
468
|
-
if (entry && item.createdAt >= entry.previewAt) setRoomPreview(entry, item);
|
|
469
|
-
added++;
|
|
470
|
-
}
|
|
471
|
-
if (entry && item.createdAt > entry.readTs && isUnreadMessage(item, app) && !entry.unreadIds.has(item.id)) {
|
|
472
|
-
entry.unreadIds.set(item.id, item); unreadChanged = true;
|
|
473
|
-
}
|
|
474
|
-
// An answer to a question clears that question even before the room is read.
|
|
475
|
-
if (entry && item.answerToMessageID && entry.unreadIds.get(item.answerToMessageID)?.isQuestion) {
|
|
476
|
-
entry.unreadIds.delete(item.answerToMessageID); unreadChanged = true;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
if (entry) entry.receivedTs = items.reduce((latest, item) => Number.isSafeInteger(item.createdAt) ? Math.max(latest, item.createdAt) : latest, previousReceivedTs);
|
|
480
|
-
if (entry) entry.unreadOverflow = Boolean(data.hasMore);
|
|
481
|
-
if (messages.length > LOADED_MESSAGE_LIMIT) { const anchorId = app.state.positions.get(roomId)?.messageId, anchor = anchorId && messages.find((item) => item.id === anchorId); messages.splice(0, messages.length - LOADED_MESSAGE_LIMIT); if (anchor && !messages.includes(anchor)) messages.unshift(anchor); }
|
|
482
|
-
app.cacheRoomSoon?.(roomId);
|
|
483
|
-
const rendered = app.renderMessages(true);
|
|
484
|
-
if (sawSystem) {
|
|
485
|
-
await app.refreshRoomMeta(roomId, generation);
|
|
486
|
-
if (!isCurrentPoll(roomId, generation)) return;
|
|
487
|
-
}
|
|
488
|
-
if (added || unreadChanged || entry && previousUnreadOverflow !== entry.unreadOverflow) {
|
|
489
|
-
app.state.unread = entry ? entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0) : app.state.unread + added;
|
|
490
|
-
app.updateBadge();
|
|
491
|
-
app.updateJump();
|
|
492
|
-
}
|
|
493
|
-
if (rendered.skipped === 0) void app.markActiveRoomRead();
|
|
494
|
-
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
495
|
-
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
496
|
-
if (!app.lastReconcileAt || Date.now() - app.lastReconcileAt > 120000) {
|
|
497
|
-
app.lastReconcileAt = Date.now();
|
|
498
|
-
void app.reconcileRoom(roomId, generation);
|
|
499
|
-
}
|
|
500
|
-
app.setOnline(true);
|
|
501
|
-
pollDelay = data.hasMore && (entry?.receivedTs || 0) > previousReceivedTs ? 0 : 2000;
|
|
502
|
-
} catch (error) {
|
|
503
|
-
if (!isCurrentPoll(roomId, generation)) return;
|
|
504
|
-
if (error.status === 401) {
|
|
505
|
-
app.setOnline(false);
|
|
506
|
-
if ($("#chatSettings").hidden) app.openChatSettings();
|
|
507
|
-
pollDelay = Math.min(pollDelay * 2, 30000);
|
|
508
|
-
} else if (error.status === 404) {
|
|
509
|
-
void app.removeJoinedRoom(roomId, { reason: true });
|
|
510
|
-
} else {
|
|
511
|
-
app.setOnline(false);
|
|
512
|
-
pollDelay = 2000;
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
app.refreshRoomMeta = async (roomId = app.state.room?.id, generation = app.state.pollGeneration) => {
|
|
518
|
-
if (!roomId) return;
|
|
519
|
-
try {
|
|
520
|
-
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
521
|
-
if (!isCurrentPoll(roomId, generation)) return;
|
|
522
|
-
app.state.room.participants = room.participants;
|
|
523
|
-
app.renderMembers();
|
|
524
|
-
} catch { /* The next room event will retry. */ }
|
|
525
|
-
};
|
|
526
|
-
|
|
527
|
-
app.reconcileRoom = async (roomId, generation = app.state.pollGeneration) => {
|
|
528
|
-
try {
|
|
529
|
-
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
530
|
-
if (!isCurrentPoll(roomId, generation)) return;
|
|
531
|
-
const current = app.state.room.messages;
|
|
532
|
-
const ids = new Set(current.map((message) => message.id));
|
|
533
|
-
const missing = room.messages.filter((message) => !ids.has(message.id));
|
|
534
|
-
if (!missing.length && !app.failedRenderIds.size) return;
|
|
535
|
-
for (const message of missing) {
|
|
536
|
-
const index = current.findIndex((item) => item.createdAt > message.createdAt);
|
|
537
|
-
current.splice(index < 0 ? current.length : index, 0, message);
|
|
538
|
-
}
|
|
539
|
-
if (current.length > LOADED_MESSAGE_LIMIT) current.splice(0, current.length - LOADED_MESSAGE_LIMIT);
|
|
540
|
-
const result = app.rerenderMessages();
|
|
541
|
-
if (result.skipped === 0) void app.markActiveRoomRead();
|
|
542
|
-
} catch { /* Try again during the next reconciliation. */ }
|
|
543
|
-
};
|
|
544
423
|
|
|
545
424
|
function failedAction(key, roomId, failedId, app) {
|
|
546
425
|
const button = document.createElement("button");
|
|
@@ -558,6 +437,7 @@ export function initMessages(app) {
|
|
|
558
437
|
return { ...updated, messages: [...messages.values()].sort((a, b) => a.createdAt - b.createdAt).slice(-LOADED_MESSAGE_LIMIT) };
|
|
559
438
|
}
|
|
560
439
|
|
|
440
|
+
initMessagePolling(app, isUnreadMessage);
|
|
561
441
|
}
|
|
562
442
|
|
|
563
443
|
// Unread means a person said something you have not seen; join/leave notices are not a badge.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Panel and key-dialog plumbing: every overlay closes the same way.
|
|
2
|
+
export function initPanels(app) {
|
|
3
|
+
const $ = app.$;
|
|
4
|
+
app.closePanels = () => {
|
|
5
|
+
document.querySelectorAll(".panel").forEach((panel) => { panel.hidden = true; });
|
|
6
|
+
$("#btnMembers").setAttribute("aria-expanded", "false");
|
|
7
|
+
$("#btnSettings").setAttribute("aria-expanded", "false");
|
|
8
|
+
$("#btnRoomOptions").setAttribute("aria-expanded", "false");
|
|
9
|
+
};
|
|
10
|
+
app.togglePanel = (selector) => {
|
|
11
|
+
const panel = $(selector), wasHidden = panel.hidden;
|
|
12
|
+
app.closePanels();
|
|
13
|
+
panel.hidden = !wasHidden;
|
|
14
|
+
const trigger = $(selector === "#memberPanel" ? "#btnMembers" : "#btnSettings");
|
|
15
|
+
trigger.setAttribute("aria-expanded", String(wasHidden));
|
|
16
|
+
if (wasHidden) {
|
|
17
|
+
app.lastFocus = trigger;
|
|
18
|
+
if (selector === "#memberPanel") void app.loadManageableMembers();
|
|
19
|
+
const heading = panel.querySelector("h3");
|
|
20
|
+
heading.tabIndex = -1;
|
|
21
|
+
heading.focus();
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
app.openTokenBox = () => {
|
|
25
|
+
const box = $("#tokenBox");
|
|
26
|
+
if (!box.open) box.showModal();
|
|
27
|
+
$("#inToken").focus();
|
|
28
|
+
};
|
|
29
|
+
app.openChatSettings = () => {
|
|
30
|
+
app.closePanels();
|
|
31
|
+
app.lastFocus = $("#btnSettings");
|
|
32
|
+
$("#chatSettings").hidden = false;
|
|
33
|
+
$("#btnSettings").setAttribute("aria-expanded", "true");
|
|
34
|
+
$("#tokenHint").hidden = false;
|
|
35
|
+
$("#inTokenChat").focus();
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -76,7 +76,10 @@ export function initQuestions(app) {
|
|
|
76
76
|
box.hidden = false;
|
|
77
77
|
copy.textContent = app.t("copyQuestion"); send.textContent = app.t("sendAnswer"); input.setAttribute("aria-placeholder", app.t("answerPlaceholder"));
|
|
78
78
|
if (feedback.dataset.copyState) feedback.textContent = app.t(feedback.dataset.copyState);
|
|
79
|
-
if (input && send)
|
|
79
|
+
if (input && send) {
|
|
80
|
+
send.disabled = !input.value.trim() || input.value.length > 5000 || submitting.has(key);
|
|
81
|
+
if (input.value.length > 5000) { app.setText(status, "answerTooLong"); status.hidden = false; status.dataset.tooLong = "true"; }
|
|
82
|
+
}
|
|
80
83
|
return;
|
|
81
84
|
}
|
|
82
85
|
box.textContent = "";
|
|
@@ -104,12 +107,13 @@ export function initQuestions(app) {
|
|
|
104
107
|
send.type = "submit";
|
|
105
108
|
send.className = "btn primary small";
|
|
106
109
|
send.textContent = app.t("sendAnswer");
|
|
107
|
-
send.disabled = !input.value.trim() || input.value.length >
|
|
110
|
+
send.disabled = !input.value.trim() || input.value.length > 5000 || submitting.has(key);
|
|
108
111
|
input.addEventListener("input", (event) => {
|
|
109
112
|
drafts.set(`${roomId}\0${question.id}`, input.draftValue);
|
|
110
113
|
if (!event.isComposing) app.recomputeMention(input);
|
|
111
|
-
send.disabled = !input.value.trim() || input.value.length >
|
|
112
|
-
if (input.value.length >
|
|
114
|
+
send.disabled = !input.value.trim() || input.value.length > 5000 || submitting.has(key);
|
|
115
|
+
if (input.value.length > 5000) { app.setText(status, "answerTooLong"); status.hidden = false; status.dataset.tooLong = "true"; }
|
|
116
|
+
else if (status.dataset.tooLong) { delete status.dataset.tooLong; status.hidden = true; status.textContent = ""; }
|
|
113
117
|
});
|
|
114
118
|
input.addEventListener("compositionend", () => app.recomputeMention(input));
|
|
115
119
|
input.addEventListener("keydown", (event) => {
|
|
@@ -185,7 +189,7 @@ export function initQuestions(app) {
|
|
|
185
189
|
function showFailed() { status.dataset.error = "true"; app.setText(status, "questionsFailed"); status.hidden = false; }
|
|
186
190
|
|
|
187
191
|
app.answerQuestionFromReply = async (roomId, questionId, text) => {
|
|
188
|
-
if (text.length >
|
|
192
|
+
if (text.length > 5000) throw Object.assign(new Error(app.t("answerTooLong")), { status: 413 });
|
|
189
193
|
try {
|
|
190
194
|
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/questions/${encodeURIComponent(questionId)}/answers`, {
|
|
191
195
|
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text }),
|
|
@@ -201,7 +205,7 @@ export function initQuestions(app) {
|
|
|
201
205
|
async function answer(roomId, questionId, text, button) {
|
|
202
206
|
const key = `${roomId}\0${questionId}`;
|
|
203
207
|
if (!text || submitting.has(key)) return;
|
|
204
|
-
if (text.length >
|
|
208
|
+
if (text.length > 5000) { app.setText(status, "answerTooLong"); status.hidden = false; return; }
|
|
205
209
|
submitting.add(key);
|
|
206
210
|
button.disabled = true;
|
|
207
211
|
renderReply(roomId);
|