@alexsun-top/opencode-chat-room 0.8.1 → 0.8.3

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