@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.
Files changed (92) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +18 -62
  3. package/README.zh-CN.md +25 -69
  4. package/dist/cli.js +466 -188
  5. package/package.json +11 -5
  6. package/packages/chat-room-channeld/README.md +25 -3
  7. package/packages/chat-room-channeld/dist/mock-adapter.js +5 -5
  8. package/packages/chat-room-channeld/src/adapter.ts +5 -0
  9. package/packages/chat-room-channeld/src/cli.ts +72 -5
  10. package/packages/chat-room-channeld/src/host.ts +17 -2
  11. package/packages/chat-room-channeld/src/pg/database.ts +187 -0
  12. package/packages/chat-room-channeld/src/pg/migrate.ts +150 -0
  13. package/packages/chat-room-channeld/src/pg/schema.ts +60 -0
  14. package/packages/chat-room-channeld/src/pg/sql.mjs +175 -0
  15. package/packages/chat-room-channeld/src/pg/sqlite.ts +100 -0
  16. package/packages/chat-room-channeld/src/pg/worker.mjs +133 -0
  17. package/packages/chat-room-channeld/src/protocol.ts +1 -0
  18. package/packages/chat-room-channeld/src/state.ts +73 -97
  19. package/packages/chat-room-channeld/src/stats.ts +140 -109
  20. package/server/assets/base.css +3 -1
  21. package/server/assets/chat/admin-badge.js +32 -30
  22. package/server/assets/chat/admin.js +201 -161
  23. package/server/assets/chat/announcements.js +302 -208
  24. package/server/assets/chat/attachment-store.js +116 -76
  25. package/server/assets/chat/attachments.js +701 -441
  26. package/server/assets/chat/chat-background.js +179 -156
  27. package/server/assets/chat/clipboard.js +45 -25
  28. package/server/assets/chat/confirm-dialog.js +38 -35
  29. package/server/assets/chat/copy-action.js +40 -41
  30. package/server/assets/chat/create-room-dialog.js +50 -47
  31. package/server/assets/chat/diagram-viewer.js +293 -236
  32. package/server/assets/chat/doc-preview.js +516 -443
  33. package/server/assets/chat/editor.js +446 -336
  34. package/server/assets/chat/events.js +650 -510
  35. package/server/assets/chat/files.js +505 -431
  36. package/server/assets/chat/history-loader.js +81 -73
  37. package/server/assets/chat/icons.js +9 -11
  38. package/server/assets/chat/image-zoom.js +246 -179
  39. package/server/assets/chat/index.js +2 -3
  40. package/server/assets/chat/install-prompt.js +90 -69
  41. package/server/assets/chat/integration-account-schema.js +91 -87
  42. package/server/assets/chat/integration-editor.js +162 -155
  43. package/server/assets/chat/integration-values.js +116 -101
  44. package/server/assets/chat/integrations.js +750 -626
  45. package/server/assets/chat/join-requests.js +415 -358
  46. package/server/assets/chat/locales/de.js +193 -194
  47. package/server/assets/chat/locales/es.js +193 -194
  48. package/server/assets/chat/locales/fr.js +193 -194
  49. package/server/assets/chat/locales/index.js +1 -2
  50. package/server/assets/chat/locales/it.js +193 -194
  51. package/server/assets/chat/locales/ja.js +193 -193
  52. package/server/assets/chat/locales/ko.js +193 -193
  53. package/server/assets/chat/markdown-footnotes.js +134 -103
  54. package/server/assets/chat/markdown.js +557 -404
  55. package/server/assets/chat/members.js +259 -197
  56. package/server/assets/chat/mentions.js +181 -133
  57. package/server/assets/chat/message-cache.js +40 -33
  58. package/server/assets/chat/message-polling.js +299 -227
  59. package/server/assets/chat/message-window.js +78 -55
  60. package/server/assets/chat/messages.js +562 -480
  61. package/server/assets/chat/office-preview.js +46 -45
  62. package/server/assets/chat/palette.js +464 -417
  63. package/server/assets/chat/panels.js +94 -79
  64. package/server/assets/chat/pwa.js +147 -75
  65. package/server/assets/chat/questions.js +374 -302
  66. package/server/assets/chat/recall.js +338 -273
  67. package/server/assets/chat/receipts.js +151 -121
  68. package/server/assets/chat/reminders.js +345 -214
  69. package/server/assets/chat/replies.js +181 -130
  70. package/server/assets/chat/resource-format.js +59 -62
  71. package/server/assets/chat/room-list.js +58 -55
  72. package/server/assets/chat/room-watcher.js +76 -50
  73. package/server/assets/chat/rooms.js +631 -500
  74. package/server/assets/chat/search.js +138 -114
  75. package/server/assets/chat/settings.js +225 -201
  76. package/server/assets/chat/shiki.js +62 -63
  77. package/server/assets/chat/shortcuts.js +212 -189
  78. package/server/assets/chat/side-pane.js +161 -150
  79. package/server/assets/chat/state.js +860 -737
  80. package/server/assets/chat/system-events.js +46 -44
  81. package/server/assets/chat/system-messages.js +57 -56
  82. package/server/assets/chat/task-form.js +179 -183
  83. package/server/assets/chat/task-row.js +158 -155
  84. package/server/assets/chat/tasks.js +665 -587
  85. package/server/assets/chat/viewport.js +32 -29
  86. package/server/assets/chat/window-controls.js +32 -32
  87. package/server/assets/conversation.css +10 -4
  88. package/server/chat-server.ts +9 -1
  89. package/server/chat.html +3 -1
  90. package/server/routes/pages.ts +7 -5
  91. package/server/sw.js +346 -268
  92. package/src/web/integration-rules-editor.ts +2 -0
@@ -1,378 +1,435 @@
1
1
  import { LS } from "./state.js";
2
2
  import { isRoomAdmin } from "./admin-badge.js";
3
-
3
+ const isJoinRequest = (value) => typeof value === "object" && value !== null
4
+ && "sessionID" in value && typeof value.sessionID === "string" && value.sessionID.length > 0
5
+ && "name" in value && typeof value.name === "string";
6
+ const statusOf = (error) => error instanceof Error && "status" in error && typeof error.status === "number" ? error.status : 0;
4
7
  // Pending join state is browser-local; the server remains authoritative.
5
8
  export function initJoinRequests(app) {
6
- const $ = app.$;
7
- const pendingIds = (app.state.pendingJoinIds ??= new Set());
8
- let pending = { roomId: "", name: "", status: "", timer: 0, generation: 0, checking: false, cancelling: false };
9
- let requests = [], requestsRoomId = "", requestsGeneration = 0, requestsLoading = false, loadingRoomId = "", deniedRoomId = "";
10
- let requestSignature = "";
11
-
12
- const panel = $("#memberPanel"), section = $("#joinRequests"), list = $("#joinRequestList");
13
- const card = $("#joinPending"), title = $("#joinPendingTitle"), body = $("#joinPendingBody"), hint = $("#joinPendingHint");
14
- const notice = $("#joinNotice"), noticeText = $("#joinNoticeText");
15
- const roomNameFor = (roomId) => app.state.availableRooms?.find((item) => item.id === roomId)?.name
16
- || app.state.rooms.get(roomId)?.room.name || roomId;
17
- const approveText = (request) => app.t("joinRequestApproveLabel", { name: request.name });
18
- const denyText = (request) => app.t("joinRequestDenyLabel", { name: request.name });
19
-
20
- app.showJoinPending = (roomId, name) => {
21
- clearTimeout(pending.timer);
22
- pending = { roomId, name, status: "pending", timer: 0, generation: pending.generation + 1, checking: false, cancelling: false };
23
- pendingIds.add(roomId);
24
- const roomName = roomNameFor(roomId);
25
- app.setText(title, "joinPendingTitle");
26
- app.setText(body, "joinPendingBody", { name: roomName });
27
- app.setText(hint, "joinPendingHint");
28
- app.setText(noticeText, "joinPendingNotice", { name: roomName });
29
- notice.hidden = $("#chatView").hidden;
30
- hint.hidden = false;
31
- card.hidden = false;
32
- $("#joinError").hidden = true;
33
- renderPendingActions();
34
- app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
35
- app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
36
- scheduleCheck();
37
- if ($("#joinView").hidden) return;
38
- title.focus({ preventScroll: true });
39
- card.scrollIntoView({ block: "nearest" });
40
- };
41
-
42
- function renderPendingActions() {
43
- const waiting = pending.status === "pending";
44
- const gone = pending.status === "gone";
45
- $("#btnJoinPendingStop").hidden = !waiting;
46
- $("#btnJoinPendingAgain").hidden = waiting || gone;
47
- $("#btnJoinNoticeStop").hidden = !waiting;
48
- $("#btnJoinNoticeAgain").hidden = waiting || gone;
49
- $("#btnJoinPendingStop").disabled = Boolean(pending.cancelling);
50
- $("#btnJoinNoticeStop").disabled = Boolean(pending.cancelling);
51
- }
52
-
53
- function closePending(keepWaiting = false) {
54
- clearTimeout(pending.timer);
55
- pending.timer = 0;
56
- pending.cancelling = false;
57
- pending.generation++;
58
- card.hidden = true;
59
- notice.hidden = true;
60
- if (!keepWaiting) {
61
- pending.roomId = "";
62
- pending.name = "";
63
- pending.status = "";
64
- }
65
- }
66
-
67
- function scheduleCheck(delay = 4000) {
68
- clearTimeout(pending.timer);
69
- if (!pending.roomId || pending.status !== "pending") return;
70
- pending.timer = setTimeout(() => void checkPending(), delay);
71
- }
72
-
73
- async function checkPending() {
74
- if (pending.checking || !pending.roomId || pending.status !== "pending") return;
75
- if (document.hidden) { scheduleCheck(); return; }
76
- const generation = pending.generation, roomId = pending.roomId;
77
- pending.checking = true;
78
- try {
79
- const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/join-requests/status?sessionID=${encodeURIComponent(LS.sessionID)}`);
80
- if (generation !== pending.generation) return;
81
- if (result.status === "joined") {
82
- const name = pending.name || LS.name;
83
- pendingIds.delete(roomId);
84
- closePending();
85
- void app.joinRoom(roomId, name);
86
- return;
87
- }
88
- if (result.status === "pending") {
9
+ const $ = (selector) => {
10
+ const element = app.$(selector);
11
+ if (!element)
12
+ throw new Error(`Missing element: ${selector}`);
13
+ return element;
14
+ };
15
+ const pendingIds = (app.state.pendingJoinIds ??= new Set());
16
+ let pending = { roomId: "", name: "", status: "", timer: 0, generation: 0, checking: false, cancelling: false };
17
+ let requests = [], requestsRoomId = "", requestsGeneration = 0, requestsLoading = false, loadingRoomId = "", deniedRoomId = "";
18
+ let requestSignature = "";
19
+ const panel = $("#memberPanel"), section = $("#joinRequests"), list = $("#joinRequestList");
20
+ const card = $("#joinPending"), title = $("#joinPendingTitle"), body = $("#joinPendingBody"), hint = $("#joinPendingHint");
21
+ const notice = $("#joinNotice"), noticeText = $("#joinNoticeText");
22
+ const roomNameFor = (roomId) => app.state.availableRooms?.find((item) => item.id === roomId)?.name
23
+ || app.state.rooms.get(roomId)?.room.name || roomId;
24
+ const approveText = (request) => app.t("joinRequestApproveLabel", { name: request.name });
25
+ const denyText = (request) => app.t("joinRequestDenyLabel", { name: request.name });
26
+ app.showJoinPending = (roomId, name) => {
27
+ clearTimeout(pending.timer);
28
+ pending = { roomId, name, status: "pending", timer: 0, generation: pending.generation + 1, checking: false, cancelling: false };
29
+ pendingIds.add(roomId);
30
+ const roomName = roomNameFor(roomId);
31
+ app.setText(title, "joinPendingTitle");
32
+ app.setText(body, "joinPendingBody", { name: roomName });
89
33
  app.setText(hint, "joinPendingHint");
90
- hint.hidden = false;
91
- scheduleCheck();
92
- return;
93
- }
94
- pendingIds.delete(roomId);
95
- pending.status = "none";
96
- app.setText(title, "joinPendingGoneTitle");
97
- app.setText(body, "joinPendingGoneBody");
98
- app.setText(noticeText, "joinPendingGoneBody");
99
- notice.hidden = $("#chatView").hidden;
100
- hint.hidden = true;
101
- renderPendingActions();
102
- renderRoomLists();
103
- rethrowFocus($("#joinView").hidden ? $("#btnJoinNoticeAgain") : $("#btnJoinPendingAgain"));
104
- } catch (error) {
105
- if (generation !== pending.generation) return;
106
- if (error.status === 404) {
107
- pendingIds.delete(roomId);
108
- pending.status = "gone";
109
- app.setText(title, "joinPendingGoneTitle");
110
- app.setText(body, "joinPendingRoomGone");
111
- app.setText(noticeText, "joinPendingRoomGone");
34
+ app.setText(noticeText, "joinPendingNotice", { name: roomName });
112
35
  notice.hidden = $("#chatView").hidden;
113
- hint.hidden = true;
114
- renderPendingActions();
115
- renderRoomLists();
116
- } else {
117
- app.setText(hint, "joinPendingError");
118
36
  hint.hidden = false;
119
- scheduleCheck(8000);
120
- }
121
- } finally {
122
- pending.checking = false;
123
- }
124
- }
125
-
126
- function rethrowFocus(element) {
127
- if (!element || element.hidden) return;
128
- element.focus({ preventScroll: true });
129
- }
130
-
131
- function renderPendingCopy() {
132
- if (!pending.roomId) return;
133
- const roomName = roomNameFor(pending.roomId);
134
- if (pending.status === "pending") {
135
- app.setText(title, "joinPendingTitle");
136
- app.setText(body, "joinPendingBody", { name: roomName });
137
- app.setText(hint, "joinPendingHint");
138
- app.setText(noticeText, "joinPendingNotice", { name: roomName });
139
- notice.hidden = $("#chatView").hidden;
140
- hint.hidden = false;
141
- } else if (pending.status === "none") {
142
- app.setText(title, "joinPendingGoneTitle");
143
- app.setText(body, "joinPendingGoneBody");
144
- app.setText(noticeText, "joinPendingGoneBody");
145
- notice.hidden = $("#chatView").hidden;
146
- hint.hidden = true;
147
- } else if (pending.status === "gone") {
148
- app.setText(title, "joinPendingGoneTitle");
149
- app.setText(body, "joinPendingRoomGone");
150
- app.setText(noticeText, "joinPendingRoomGone");
151
- notice.hidden = $("#chatView").hidden;
152
- hint.hidden = true;
37
+ card.hidden = false;
38
+ $("#joinError").hidden = true;
39
+ renderPendingActions();
40
+ app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
41
+ app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
42
+ scheduleCheck();
43
+ if ($("#joinView").hidden)
44
+ return;
45
+ title.focus({ preventScroll: true });
46
+ card.scrollIntoView({ block: "nearest" });
47
+ };
48
+ function renderPendingActions() {
49
+ const waiting = pending.status === "pending";
50
+ const gone = pending.status === "gone";
51
+ $("#btnJoinPendingStop").hidden = !waiting;
52
+ $("#btnJoinPendingAgain").hidden = waiting || gone;
53
+ $("#btnJoinNoticeStop").hidden = !waiting;
54
+ $("#btnJoinNoticeAgain").hidden = waiting || gone;
55
+ $("#btnJoinPendingStop").disabled = Boolean(pending.cancelling);
56
+ $("#btnJoinNoticeStop").disabled = Boolean(pending.cancelling);
153
57
  }
154
- renderPendingActions();
155
- }
156
-
157
- app.refreshJoinRequests = () => void refreshRequests();
158
- app.clearJoinRequests = () => {
159
- deniedRoomId = ""; requestsGeneration++; requests = []; requestsRoomId = ""; requestSignature = "";
160
- renderRequests();
161
- };
162
-
163
- async function refreshRequests() {
164
- const room = app.state.room;
165
- if (deniedRoomId && deniedRoomId !== room?.id) deniedRoomId = "";
166
- if (!room || room.kind === "direct" || room.joinPolicy !== "approval" || !isRoomAdmin(app) || deniedRoomId === room.id) {
167
- requestsGeneration++;
168
- requests = []; requestsRoomId = ""; requestSignature = "";
169
- renderRequests();
170
- return;
58
+ function closePending(keepWaiting = false) {
59
+ clearTimeout(pending.timer);
60
+ pending.timer = 0;
61
+ pending.cancelling = false;
62
+ pending.generation++;
63
+ card.hidden = true;
64
+ notice.hidden = true;
65
+ if (!keepWaiting) {
66
+ pending.roomId = "";
67
+ pending.name = "";
68
+ pending.status = "";
69
+ }
171
70
  }
172
- // A refresh for another room must not be skipped by the previous room's
173
- // in-flight request, or the list stays empty until the next poll.
174
- if (requestsLoading && loadingRoomId === room.id) return;
175
- const generation = ++requestsGeneration, roomId = room.id;
176
- requestsLoading = true;
177
- loadingRoomId = roomId;
178
- try {
179
- const data = await app.api(`/rooms/${encodeURIComponent(roomId)}/join-requests?sessionID=${encodeURIComponent(LS.sessionID)}`);
180
- if (generation !== requestsGeneration || app.state.room?.id !== roomId) return;
181
- requestsRoomId = roomId;
182
- requests = Array.isArray(data.requests)
183
- ? data.requests.filter((request) => request && typeof request.sessionID === "string" && request.sessionID && typeof request.name === "string")
184
- : [];
185
- renderRequests();
186
- } catch (error) {
187
- if (generation !== requestsGeneration || app.state.room?.id !== roomId) return;
188
- if (error.status === 403) { deniedRoomId = roomId; requests = []; renderRequests(); }
189
- else $("#joinRequestsStatus").textContent = app.t("joinRequestsLoadFailed");
190
- } finally {
191
- if (loadingRoomId === roomId) { requestsLoading = false; loadingRoomId = ""; }
71
+ function scheduleCheck(delay = 4000) {
72
+ clearTimeout(pending.timer);
73
+ if (!pending.roomId || pending.status !== "pending")
74
+ return;
75
+ pending.timer = setTimeout(() => void checkPending(), delay);
192
76
  }
193
- }
194
-
195
- function renderRequests() {
196
- const room = app.state.room;
197
- const visible = Boolean(room && room.kind !== "direct" && room.joinPolicy === "approval" && isRoomAdmin(app) && deniedRoomId !== room.id);
198
- section.hidden = !visible;
199
- if (!visible) {
200
- list.textContent = ""; requestSignature = "";
201
- updateBadge(0);
202
- return;
77
+ async function checkPending() {
78
+ if (pending.checking || !pending.roomId || pending.status !== "pending")
79
+ return;
80
+ if (document.hidden) {
81
+ scheduleCheck();
82
+ return;
83
+ }
84
+ const generation = pending.generation, roomId = pending.roomId;
85
+ pending.checking = true;
86
+ try {
87
+ const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/join-requests/status?sessionID=${encodeURIComponent(LS.sessionID)}`);
88
+ if (generation !== pending.generation)
89
+ return;
90
+ if (result.status === "joined") {
91
+ const name = pending.name || LS.name;
92
+ pendingIds.delete(roomId);
93
+ closePending();
94
+ void app.joinRoom(roomId, name);
95
+ return;
96
+ }
97
+ if (result.status === "pending") {
98
+ app.setText(hint, "joinPendingHint");
99
+ hint.hidden = false;
100
+ scheduleCheck();
101
+ return;
102
+ }
103
+ pendingIds.delete(roomId);
104
+ pending.status = "none";
105
+ app.setText(title, "joinPendingGoneTitle");
106
+ app.setText(body, "joinPendingGoneBody");
107
+ app.setText(noticeText, "joinPendingGoneBody");
108
+ notice.hidden = $("#chatView").hidden;
109
+ hint.hidden = true;
110
+ renderPendingActions();
111
+ renderRoomLists();
112
+ rethrowFocus($("#joinView").hidden ? $("#btnJoinNoticeAgain") : $("#btnJoinPendingAgain"));
113
+ }
114
+ catch (error) {
115
+ if (generation !== pending.generation)
116
+ return;
117
+ if (statusOf(error) === 404) {
118
+ pendingIds.delete(roomId);
119
+ pending.status = "gone";
120
+ app.setText(title, "joinPendingGoneTitle");
121
+ app.setText(body, "joinPendingRoomGone");
122
+ app.setText(noticeText, "joinPendingRoomGone");
123
+ notice.hidden = $("#chatView").hidden;
124
+ hint.hidden = true;
125
+ renderPendingActions();
126
+ renderRoomLists();
127
+ }
128
+ else {
129
+ app.setText(hint, "joinPendingError");
130
+ hint.hidden = false;
131
+ scheduleCheck(8000);
132
+ }
133
+ }
134
+ finally {
135
+ pending.checking = false;
136
+ }
203
137
  }
204
- if (requestsRoomId && requestsRoomId !== room.id) {
205
- requests = []; requestsRoomId = room.id; requestSignature = "";
138
+ function rethrowFocus(element) {
139
+ if (!element || element.hidden)
140
+ return;
141
+ element.focus({ preventScroll: true });
206
142
  }
207
- const count = requests.length;
208
- $("#joinRequestsHint").hidden = !room.adminSessionID;
209
- $("#joinRequestsEmpty").hidden = count > 0;
210
- const countNode = $("#joinRequestsCount");
211
- countNode.textContent = count > 99 ? "99+" : String(count);
212
- countNode.hidden = !count;
213
- const signature = `${room.id}\0${app.locale}\0${requests.map((item) => `${item.sessionID}\0${item.name}\0${item.isAgent}\0${item.createdAt}`).join("\u0001")}`;
214
- if (signature === requestSignature) { updateBadge(count); return; }
215
- const active = list.contains(document.activeElement)
216
- ? { id: document.activeElement.closest("[data-request-session]")?.dataset.requestSession, action: document.activeElement.dataset.requestAction }
217
- : undefined;
218
- requestSignature = signature;
219
- list.textContent = "";
220
- for (const request of requests) list.append(requestRow(request));
221
- if (active) {
222
- const row = list.querySelector(`[data-request-session="${CSS.escape(active.id || "")}"]`);
223
- const target = row?.querySelector(`[data-request-action="${active.action || ""}"]`);
224
- target?.focus({ preventScroll: true });
143
+ function renderPendingCopy() {
144
+ if (!pending.roomId)
145
+ return;
146
+ const roomName = roomNameFor(pending.roomId);
147
+ if (pending.status === "pending") {
148
+ app.setText(title, "joinPendingTitle");
149
+ app.setText(body, "joinPendingBody", { name: roomName });
150
+ app.setText(hint, "joinPendingHint");
151
+ app.setText(noticeText, "joinPendingNotice", { name: roomName });
152
+ notice.hidden = $("#chatView").hidden;
153
+ hint.hidden = false;
154
+ }
155
+ else if (pending.status === "none") {
156
+ app.setText(title, "joinPendingGoneTitle");
157
+ app.setText(body, "joinPendingGoneBody");
158
+ app.setText(noticeText, "joinPendingGoneBody");
159
+ notice.hidden = $("#chatView").hidden;
160
+ hint.hidden = true;
161
+ }
162
+ else if (pending.status === "gone") {
163
+ app.setText(title, "joinPendingGoneTitle");
164
+ app.setText(body, "joinPendingRoomGone");
165
+ app.setText(noticeText, "joinPendingRoomGone");
166
+ notice.hidden = $("#chatView").hidden;
167
+ hint.hidden = true;
168
+ }
169
+ renderPendingActions();
225
170
  }
226
- updateBadge(count);
227
- }
228
-
229
- function requestRow(request) {
230
- const item = document.createElement("li"), info = document.createElement("div"), name = document.createElement("span");
231
- const meta = document.createElement("span"), kind = document.createElement("span"), actions = document.createElement("div");
232
- item.className = "join-request-item"; item.dataset.requestSession = request.sessionID;
233
- info.className = "join-request-info";
234
- name.className = "join-request-name"; name.textContent = request.name;
235
- meta.className = "join-request-meta";
236
- const time = Number(request.createdAt);
237
- meta.textContent = app.t("joinRequestTime", { time: app.fmtTime(Number.isFinite(time) && time > 0 ? time : Date.now()) });
238
- kind.className = `join-request-kind${request.isAgent ? " agent" : ""}`;
239
- kind.textContent = request.isAgent ? app.t("joinRequestAgent") : app.t("joinRequestHuman");
240
- info.append(name, kind, meta);
241
- actions.className = "join-request-actions";
242
- const approve = actionButton("approve", app.t("joinRequestApprove"), approveText(request));
243
- const deny = actionButton("deny", app.t("joinRequestDeny"), denyText(request));
244
- approve.className = "btn primary small"; deny.className = "btn ghost small danger-text";
245
- approve.addEventListener("click", () => void decide(request, "approve", item, approve, deny));
246
- deny.addEventListener("click", () => void decide(request, "deny", item, approve, deny));
247
- actions.append(approve, deny); item.append(info, actions);
248
- return item;
249
- }
250
-
251
- function actionButton(action, label, ariaLabel) {
252
- const button = document.createElement("button");
253
- button.type = "button"; button.dataset.requestAction = action;
254
- button.textContent = label; button.setAttribute("aria-label", ariaLabel);
255
- return button;
256
- }
257
-
258
- async function decide(request, action, item, approve, deny) {
259
- const room = app.state.room;
260
- if (!room) return;
261
- const index = requests.findIndex((entry) => entry.sessionID === request.sessionID);
262
- item.setAttribute("aria-busy", "true"); approve.disabled = true; deny.disabled = true;
263
- try {
264
- await app.api(`/rooms/${encodeURIComponent(room.id)}/join-requests/${encodeURIComponent(request.sessionID)}/${action}`, {
265
- method: "POST", body: JSON.stringify({ sessionID: LS.sessionID }),
266
- });
267
- requests = requests.filter((entry) => entry.sessionID !== request.sessionID);
268
- requestSignature = "";
269
- app.setText($("#joinRequestsStatus"), action === "approve" ? "joinRequestApproved" : "joinRequestDenied", { name: request.name });
270
- renderRequests();
271
- focusAfterRequest(index);
272
- if (action === "approve") void app.refreshRoomMeta?.();
273
- } catch (error) {
274
- if (error.status === 404) {
275
- requests = requests.filter((entry) => entry.sessionID !== request.sessionID);
171
+ app.refreshJoinRequests = () => void refreshRequests();
172
+ app.clearJoinRequests = () => {
173
+ deniedRoomId = "";
174
+ requestsGeneration++;
175
+ requests = [];
176
+ requestsRoomId = "";
276
177
  requestSignature = "";
277
- app.setText($("#joinRequestsStatus"), "joinRequestAlreadyHandled");
278
178
  renderRequests();
279
- focusAfterRequest(index);
280
- } else if (error.status === 403) {
281
- deniedRoomId = room.id; requests = []; renderRequests();
282
- } else {
283
- item.removeAttribute("aria-busy"); approve.disabled = false; deny.disabled = false;
284
- app.setText($("#joinRequestsStatus"), "joinRequestActionFailed", { message: error.message });
285
- if (error.status === 409) void refreshRequests();
286
- }
179
+ };
180
+ async function refreshRequests() {
181
+ const room = app.state.room;
182
+ if (deniedRoomId && deniedRoomId !== room?.id)
183
+ deniedRoomId = "";
184
+ if (!room || room.kind === "direct" || room.joinPolicy !== "approval" || !isRoomAdmin(app) || deniedRoomId === room.id) {
185
+ requestsGeneration++;
186
+ requests = [];
187
+ requestsRoomId = "";
188
+ requestSignature = "";
189
+ renderRequests();
190
+ return;
191
+ }
192
+ // A refresh for another room must not be skipped by the previous room's
193
+ // in-flight request, or the list stays empty until the next poll.
194
+ if (requestsLoading && loadingRoomId === room.id)
195
+ return;
196
+ const generation = ++requestsGeneration, roomId = room.id;
197
+ requestsLoading = true;
198
+ loadingRoomId = roomId;
199
+ try {
200
+ const data = await app.api(`/rooms/${encodeURIComponent(roomId)}/join-requests?sessionID=${encodeURIComponent(LS.sessionID)}`);
201
+ if (generation !== requestsGeneration || app.state.room?.id !== roomId)
202
+ return;
203
+ requestsRoomId = roomId;
204
+ requests = Array.isArray(data.requests) ? data.requests.filter(isJoinRequest) : [];
205
+ renderRequests();
206
+ }
207
+ catch (error) {
208
+ if (generation !== requestsGeneration || app.state.room?.id !== roomId)
209
+ return;
210
+ if (statusOf(error) === 403) {
211
+ deniedRoomId = roomId;
212
+ requests = [];
213
+ renderRequests();
214
+ }
215
+ else
216
+ $("#joinRequestsStatus").textContent = app.t("joinRequestsLoadFailed");
217
+ }
218
+ finally {
219
+ if (loadingRoomId === roomId) {
220
+ requestsLoading = false;
221
+ loadingRoomId = "";
222
+ }
223
+ }
287
224
  }
288
- }
289
-
290
- function focusAfterRequest(index) {
291
- const rows = [...list.querySelectorAll(".join-request-item")];
292
- const row = rows[Math.min(index, rows.length - 1)];
293
- const target = row?.querySelector('[data-request-action="approve"]') || $("#joinRequestsTitle");
294
- target?.focus({ preventScroll: true });
295
- }
296
-
297
- function updateBadge(count) {
298
- const badge = $("#joinRequestBadge"), button = $("#btnMembers");
299
- $("#joinRequestBadgeCount").textContent = count > 99 ? "99+" : String(count);
300
- badge.title = count ? app.t("membersWithRequests", { count }) : "";
301
- badge.hidden = !count;
302
- button.setAttribute("aria-label", count ? app.t("membersWithRequests", { count }) : app.t("members"));
303
- }
304
-
305
- function renderRoomLists() {
306
- app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
307
- app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
308
- }
309
-
310
- async function cancelPending() {
311
- if (!pending.roomId || pending.status !== "pending" || pending.cancelling) return;
312
- const generation = pending.generation, roomId = pending.roomId;
313
- pending.cancelling = true;
314
- renderPendingActions();
315
- try {
316
- await app.api(`/rooms/${encodeURIComponent(roomId)}/join-requests?sessionID=${encodeURIComponent(LS.sessionID)}`, { method: "DELETE", body: "{}" });
317
- if (generation !== pending.generation) return;
318
- pendingIds.delete(roomId);
319
- closePending();
320
- ($("#joinView").hidden ? $("#msgInput") : $("#inRoomId"))?.focus({ preventScroll: true });
321
- } catch (error) {
322
- if (generation !== pending.generation) return;
323
- if (error.status === 404) {
324
- pendingIds.delete(roomId);
325
- closePending();
326
- ($("#joinView").hidden ? $("#msgInput") : $("#inRoomId"))?.focus({ preventScroll: true });
327
- return;
328
- }
329
- app.setText(hint, "joinPendingCancelError");
330
- hint.hidden = false;
331
- app.setText(noticeText, "joinPendingCancelError");
332
- notice.hidden = $("#chatView").hidden;
333
- } finally {
334
- if (generation === pending.generation) {
335
- pending.cancelling = false;
225
+ function renderRequests() {
226
+ const room = app.state.room;
227
+ const visible = Boolean(room && room.kind !== "direct" && room.joinPolicy === "approval" && isRoomAdmin(app) && deniedRoomId !== room.id);
228
+ section.hidden = !visible;
229
+ if (!room || !visible) {
230
+ list.textContent = "";
231
+ requestSignature = "";
232
+ updateBadge(0);
233
+ return;
234
+ }
235
+ if (requestsRoomId && requestsRoomId !== room.id) {
236
+ requests = [];
237
+ requestsRoomId = room.id;
238
+ requestSignature = "";
239
+ }
240
+ const count = requests.length;
241
+ $("#joinRequestsHint").hidden = !room.adminSessionID;
242
+ $("#joinRequestsEmpty").hidden = count > 0;
243
+ const countNode = $("#joinRequestsCount");
244
+ countNode.textContent = count > 99 ? "99+" : String(count);
245
+ countNode.hidden = !count;
246
+ const signature = `${room.id}\0${app.locale}\0${requests.map((item) => `${item.sessionID}\0${item.name}\0${item.isAgent}\0${item.createdAt}`).join("\u0001")}`;
247
+ if (signature === requestSignature) {
248
+ updateBadge(count);
249
+ return;
250
+ }
251
+ const active = list.contains(document.activeElement)
252
+ ? { id: document.activeElement.closest("[data-request-session]")?.dataset.requestSession, action: document.activeElement.dataset.requestAction }
253
+ : undefined;
254
+ requestSignature = signature;
255
+ list.textContent = "";
256
+ for (const request of requests)
257
+ list.append(requestRow(request));
258
+ if (active) {
259
+ const row = list.querySelector(`[data-request-session="${CSS.escape(active.id || "")}"]`);
260
+ const target = row?.querySelector(`[data-request-action="${active.action || ""}"]`);
261
+ target?.focus({ preventScroll: true });
262
+ }
263
+ updateBadge(count);
264
+ }
265
+ function requestRow(request) {
266
+ const item = document.createElement("li"), info = document.createElement("div"), name = document.createElement("span");
267
+ const meta = document.createElement("span"), kind = document.createElement("span"), actions = document.createElement("div");
268
+ item.className = "join-request-item";
269
+ item.dataset.requestSession = request.sessionID;
270
+ info.className = "join-request-info";
271
+ name.className = "join-request-name";
272
+ name.textContent = request.name;
273
+ meta.className = "join-request-meta";
274
+ const time = Number(request.createdAt);
275
+ meta.textContent = app.t("joinRequestTime", { time: app.fmtTime(Number.isFinite(time) && time > 0 ? time : Date.now()) });
276
+ kind.className = `join-request-kind${request.isAgent ? " agent" : ""}`;
277
+ kind.textContent = request.isAgent ? app.t("joinRequestAgent") : app.t("joinRequestHuman");
278
+ info.append(name, kind, meta);
279
+ actions.className = "join-request-actions";
280
+ const approve = actionButton("approve", app.t("joinRequestApprove"), approveText(request));
281
+ const deny = actionButton("deny", app.t("joinRequestDeny"), denyText(request));
282
+ approve.className = "btn primary small";
283
+ deny.className = "btn ghost small danger-text";
284
+ approve.addEventListener("click", () => void decide(request, "approve", item, approve, deny));
285
+ deny.addEventListener("click", () => void decide(request, "deny", item, approve, deny));
286
+ actions.append(approve, deny);
287
+ item.append(info, actions);
288
+ return item;
289
+ }
290
+ function actionButton(action, label, ariaLabel) {
291
+ const button = document.createElement("button");
292
+ button.type = "button";
293
+ button.dataset.requestAction = action;
294
+ button.textContent = label;
295
+ button.setAttribute("aria-label", ariaLabel);
296
+ return button;
297
+ }
298
+ async function decide(request, action, item, approve, deny) {
299
+ const room = app.state.room;
300
+ if (!room)
301
+ return;
302
+ const index = requests.findIndex((entry) => entry.sessionID === request.sessionID);
303
+ item.setAttribute("aria-busy", "true");
304
+ approve.disabled = true;
305
+ deny.disabled = true;
306
+ try {
307
+ await app.api(`/rooms/${encodeURIComponent(room.id)}/join-requests/${encodeURIComponent(request.sessionID)}/${action}`, {
308
+ method: "POST", body: JSON.stringify({ sessionID: LS.sessionID }),
309
+ });
310
+ requests = requests.filter((entry) => entry.sessionID !== request.sessionID);
311
+ requestSignature = "";
312
+ app.setText($("#joinRequestsStatus"), action === "approve" ? "joinRequestApproved" : "joinRequestDenied", { name: request.name });
313
+ renderRequests();
314
+ focusAfterRequest(index);
315
+ if (action === "approve")
316
+ void app.refreshRoomMeta?.();
317
+ }
318
+ catch (error) {
319
+ if (statusOf(error) === 404) {
320
+ requests = requests.filter((entry) => entry.sessionID !== request.sessionID);
321
+ requestSignature = "";
322
+ app.setText($("#joinRequestsStatus"), "joinRequestAlreadyHandled");
323
+ renderRequests();
324
+ focusAfterRequest(index);
325
+ }
326
+ else if (statusOf(error) === 403) {
327
+ deniedRoomId = room.id;
328
+ requests = [];
329
+ renderRequests();
330
+ }
331
+ else {
332
+ item.removeAttribute("aria-busy");
333
+ approve.disabled = false;
334
+ deny.disabled = false;
335
+ app.setText($("#joinRequestsStatus"), "joinRequestActionFailed", { message: error instanceof Error ? error.message : String(error) });
336
+ if (statusOf(error) === 409)
337
+ void refreshRequests();
338
+ }
339
+ }
340
+ }
341
+ function focusAfterRequest(index) {
342
+ const rows = [...list.querySelectorAll(".join-request-item")];
343
+ const row = rows[Math.min(index, rows.length - 1)];
344
+ const target = row?.querySelector('[data-request-action="approve"]') || $("#joinRequestsTitle");
345
+ target?.focus({ preventScroll: true });
346
+ }
347
+ function updateBadge(count) {
348
+ const badge = $("#joinRequestBadge"), button = $("#btnMembers");
349
+ $("#joinRequestBadgeCount").textContent = count > 99 ? "99+" : String(count);
350
+ badge.title = count ? app.t("membersWithRequests", { count }) : "";
351
+ badge.hidden = !count;
352
+ button.setAttribute("aria-label", count ? app.t("membersWithRequests", { count }) : app.t("members"));
353
+ }
354
+ function renderRoomLists() {
355
+ app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
356
+ app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
357
+ }
358
+ async function cancelPending() {
359
+ if (!pending.roomId || pending.status !== "pending" || pending.cancelling)
360
+ return;
361
+ const generation = pending.generation, roomId = pending.roomId;
362
+ pending.cancelling = true;
336
363
  renderPendingActions();
337
- }
364
+ try {
365
+ await app.api(`/rooms/${encodeURIComponent(roomId)}/join-requests?sessionID=${encodeURIComponent(LS.sessionID)}`, { method: "DELETE", body: "{}" });
366
+ if (generation !== pending.generation)
367
+ return;
368
+ pendingIds.delete(roomId);
369
+ closePending();
370
+ ($("#joinView").hidden ? $("#msgInput") : $("#inRoomId"))?.focus({ preventScroll: true });
371
+ }
372
+ catch (error) {
373
+ if (generation !== pending.generation)
374
+ return;
375
+ if (statusOf(error) === 404) {
376
+ pendingIds.delete(roomId);
377
+ closePending();
378
+ ($("#joinView").hidden ? $("#msgInput") : $("#inRoomId"))?.focus({ preventScroll: true });
379
+ return;
380
+ }
381
+ app.setText(hint, "joinPendingCancelError");
382
+ hint.hidden = false;
383
+ app.setText(noticeText, "joinPendingCancelError");
384
+ notice.hidden = $("#chatView").hidden;
385
+ }
386
+ finally {
387
+ if (generation === pending.generation) {
388
+ pending.cancelling = false;
389
+ renderPendingActions();
390
+ }
391
+ }
338
392
  }
339
- }
340
-
341
- function requestAgain() {
342
- if (pending.roomId) void app.joinRoom(pending.roomId, pending.name || LS.name);
343
- }
344
-
345
- $("#btnJoinPendingStop").addEventListener("click", () => void cancelPending());
346
- $("#btnJoinNoticeStop").addEventListener("click", () => void cancelPending());
347
- $("#btnJoinPendingAgain").addEventListener("click", requestAgain);
348
- $("#btnJoinNoticeAgain").addEventListener("click", requestAgain);
349
- $("#btnMembers").addEventListener("click", () => {
350
- setTimeout(() => { if (!$("#memberPanel").hidden) void refreshRequests(); }, 0);
351
- });
352
- document.addEventListener("chat-room-activated", (event) => {
353
- const roomId = event.detail;
354
- if (pending.roomId === roomId) {
355
- pendingIds.delete(roomId);
356
- closePending();
393
+ function requestAgain() {
394
+ if (pending.roomId)
395
+ void app.joinRoom(pending.roomId, pending.name || LS.name);
357
396
  }
358
- deniedRoomId = ""; requests = []; requestsRoomId = roomId || ""; requestSignature = "";
359
- renderRequests();
360
- void refreshRequests();
361
- renderRoomLists();
362
- });
363
- document.addEventListener("visibilitychange", () => {
364
- if (document.hidden) return;
365
- if (pending.roomId && pending.status === "pending" && !pending.timer) scheduleCheck(0);
366
- if (!panel.hidden) void refreshRequests();
367
- });
368
-
369
- // Locale changes must relabel rows and the open request card without another round trip.
370
- const applyLocale = app.applyLocale;
371
- app.applyLocale = () => {
372
- applyLocale();
373
- renderPendingCopy();
397
+ $("#btnJoinPendingStop").addEventListener("click", () => void cancelPending());
398
+ $("#btnJoinNoticeStop").addEventListener("click", () => void cancelPending());
399
+ $("#btnJoinPendingAgain").addEventListener("click", requestAgain);
400
+ $("#btnJoinNoticeAgain").addEventListener("click", requestAgain);
401
+ $("#btnMembers").addEventListener("click", () => {
402
+ setTimeout(() => { if (!$("#memberPanel").hidden)
403
+ void refreshRequests(); }, 0);
404
+ });
405
+ document.addEventListener("chat-room-activated", (event) => {
406
+ const roomId = event.detail;
407
+ if (pending.roomId === roomId) {
408
+ pendingIds.delete(roomId);
409
+ closePending();
410
+ }
411
+ deniedRoomId = "";
412
+ requests = [];
413
+ requestsRoomId = roomId || "";
414
+ requestSignature = "";
415
+ renderRequests();
416
+ void refreshRequests();
417
+ renderRoomLists();
418
+ });
419
+ document.addEventListener("visibilitychange", () => {
420
+ if (document.hidden)
421
+ return;
422
+ if (pending.roomId && pending.status === "pending" && !pending.timer)
423
+ scheduleCheck(0);
424
+ if (!panel.hidden)
425
+ void refreshRequests();
426
+ });
427
+ // Locale changes must relabel rows and the open request card without another round trip.
428
+ const applyLocale = app.applyLocale;
429
+ app.applyLocale = () => {
430
+ applyLocale();
431
+ renderPendingCopy();
432
+ renderRequests();
433
+ };
374
434
  renderRequests();
375
- };
376
-
377
- renderRequests();
378
- }
435
+ }