@alexsun-top/opencode-chat-room 0.6.7 → 0.6.9
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 +24 -0
- package/README.md +6 -3
- package/README.zh-CN.md +7 -3
- package/docs/administrator-rooms.md +7 -2
- package/package.json +4 -1
- package/plugin.ts +38 -8
- package/server/assets/base.css +222 -28
- package/server/assets/chat/admin-badge.js +28 -6
- package/server/assets/chat/admin.js +7 -1
- package/server/assets/chat/announcements.js +1 -1
- package/server/assets/chat/attachment-store.js +106 -0
- package/server/assets/chat/attachments.js +201 -21
- package/server/assets/chat/confirm-dialog.js +28 -0
- package/server/assets/chat/events.js +37 -9
- package/server/assets/chat/image-zoom.js +86 -2
- 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 +55 -7
- package/server/assets/chat/members.js +5 -3
- package/server/assets/chat/message-polling.js +24 -3
- package/server/assets/chat/messages.js +6 -4
- package/server/assets/chat/questions.js +10 -6
- package/server/assets/chat/recall.js +267 -0
- package/server/assets/chat/reminders.js +53 -9
- package/server/assets/chat/replies.js +1 -1
- package/server/assets/chat/room-list.js +14 -5
- package/server/assets/chat/room-watcher.js +5 -1
- package/server/assets/chat/rooms.js +17 -5
- package/server/assets/chat/shortcuts.js +40 -0
- package/server/assets/chat/state.js +69 -16
- package/server/assets/chat/system-messages.js +4 -0
- package/server/assets/conversation.css +2 -2
- package/server/assets/recall.css +29 -0
- package/server/assets/responsive.css +41 -13
- package/server/chat-server.ts +55 -66
- package/server/chat.html +139 -16
- package/server/inbox.ts +1 -1
- package/server/join-requests.ts +106 -0
- package/server/message-recall-routes.ts +37 -0
- package/server/newrooms.ts +9 -0
- package/server/room-join.ts +59 -0
- package/server/room-owner.ts +3 -20
- package/server/room-payload.ts +2 -0
- package/server/sw.js +1 -0
- package/server/wake.ts +45 -16
- package/skills/chatroom/SKILL.md +32 -0
- package/src/README.md +5 -3
- package/src/agent/admin-client.ts +28 -4
- package/src/center/center-client.ts +81 -4
- package/src/center/center-inbox.ts +35 -10
- package/src/center/center-wake.ts +25 -12
- package/src/center/message-ids.ts +7 -0
- package/src/center/notification-budget.ts +16 -0
- package/src/center/queue.ts +14 -4
- package/src/rooms/room-service.ts +10 -1
- package/src/runtime/plugin-notifications.ts +37 -11
- package/src/runtime/v2-plugin.ts +1 -1
- package/src/state/roles.ts +34 -0
- package/src/state/sqlite-state.ts +197 -27
- package/src/state/store.ts +22 -3
- package/src/tests/announcement-board.ts +12 -2
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { LS } from "./state.js";
|
|
2
|
+
import { isRoomAdmin } from "./admin-badge.js";
|
|
3
|
+
|
|
4
|
+
// Pending join state is browser-local; the server remains authoritative.
|
|
5
|
+
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 };
|
|
9
|
+
let requests = [], requestsRoomId = "", requestsGeneration = 0, requestsLoading = false, 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 };
|
|
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
|
+
$("#btnJoinPendingCheck").hidden = !waiting;
|
|
46
|
+
$("#btnJoinPendingStop").hidden = !waiting;
|
|
47
|
+
$("#btnJoinPendingAgain").hidden = waiting || gone;
|
|
48
|
+
$("#btnJoinNoticeCheck").hidden = !waiting;
|
|
49
|
+
$("#btnJoinNoticeStop").hidden = !waiting;
|
|
50
|
+
$("#btnJoinNoticeAgain").hidden = waiting || gone;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function closePending(keepWaiting = false) {
|
|
54
|
+
clearTimeout(pending.timer);
|
|
55
|
+
pending.timer = 0;
|
|
56
|
+
pending.generation++;
|
|
57
|
+
card.hidden = true;
|
|
58
|
+
notice.hidden = true;
|
|
59
|
+
if (!keepWaiting) {
|
|
60
|
+
pending.roomId = "";
|
|
61
|
+
pending.name = "";
|
|
62
|
+
pending.status = "";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function scheduleCheck(delay = 4000) {
|
|
67
|
+
clearTimeout(pending.timer);
|
|
68
|
+
if (!pending.roomId || pending.status !== "pending") return;
|
|
69
|
+
pending.timer = setTimeout(() => void checkPending(), delay);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function checkPending(manual = false) {
|
|
73
|
+
if (pending.checking || !pending.roomId || pending.status !== "pending") return;
|
|
74
|
+
if (document.hidden) { scheduleCheck(); return; }
|
|
75
|
+
const generation = pending.generation, roomId = pending.roomId;
|
|
76
|
+
pending.checking = true;
|
|
77
|
+
if (manual) { $("#btnJoinPendingCheck").disabled = true; $("#btnJoinNoticeCheck").disabled = 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") {
|
|
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
|
+
} 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");
|
|
112
|
+
notice.hidden = $("#chatView").hidden;
|
|
113
|
+
hint.hidden = true;
|
|
114
|
+
renderPendingActions();
|
|
115
|
+
renderRoomLists();
|
|
116
|
+
} else {
|
|
117
|
+
app.setText(hint, "joinPendingError");
|
|
118
|
+
hint.hidden = false;
|
|
119
|
+
scheduleCheck(8000);
|
|
120
|
+
}
|
|
121
|
+
} finally {
|
|
122
|
+
pending.checking = false;
|
|
123
|
+
$("#btnJoinPendingCheck").disabled = false;
|
|
124
|
+
$("#btnJoinNoticeCheck").disabled = false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function rethrowFocus(element) {
|
|
129
|
+
if (!element || element.hidden) return;
|
|
130
|
+
element.focus({ preventScroll: true });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function renderPendingCopy() {
|
|
134
|
+
if (!pending.roomId) return;
|
|
135
|
+
const roomName = roomNameFor(pending.roomId);
|
|
136
|
+
if (pending.status === "pending") {
|
|
137
|
+
app.setText(title, "joinPendingTitle");
|
|
138
|
+
app.setText(body, "joinPendingBody", { name: roomName });
|
|
139
|
+
app.setText(hint, "joinPendingHint");
|
|
140
|
+
app.setText(noticeText, "joinPendingNotice", { name: roomName });
|
|
141
|
+
notice.hidden = $("#chatView").hidden;
|
|
142
|
+
hint.hidden = false;
|
|
143
|
+
} else if (pending.status === "none") {
|
|
144
|
+
app.setText(title, "joinPendingGoneTitle");
|
|
145
|
+
app.setText(body, "joinPendingGoneBody");
|
|
146
|
+
app.setText(noticeText, "joinPendingGoneBody");
|
|
147
|
+
notice.hidden = $("#chatView").hidden;
|
|
148
|
+
hint.hidden = true;
|
|
149
|
+
} else if (pending.status === "gone") {
|
|
150
|
+
app.setText(title, "joinPendingGoneTitle");
|
|
151
|
+
app.setText(body, "joinPendingRoomGone");
|
|
152
|
+
app.setText(noticeText, "joinPendingRoomGone");
|
|
153
|
+
notice.hidden = $("#chatView").hidden;
|
|
154
|
+
hint.hidden = true;
|
|
155
|
+
}
|
|
156
|
+
renderPendingActions();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
app.refreshJoinRequests = () => void refreshRequests();
|
|
160
|
+
app.clearJoinRequests = () => {
|
|
161
|
+
deniedRoomId = ""; requestsGeneration++; requests = []; requestsRoomId = ""; requestSignature = "";
|
|
162
|
+
renderRequests();
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
async function refreshRequests() {
|
|
166
|
+
const room = app.state.room;
|
|
167
|
+
if (deniedRoomId && deniedRoomId !== room?.id) deniedRoomId = "";
|
|
168
|
+
if (!room || room.kind === "direct" || room.joinPolicy !== "approval" || !isRoomAdmin(app) || deniedRoomId === room.id) {
|
|
169
|
+
requestsGeneration++;
|
|
170
|
+
requests = []; requestsRoomId = ""; requestSignature = "";
|
|
171
|
+
renderRequests();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (requestsLoading) return;
|
|
175
|
+
const generation = ++requestsGeneration, roomId = room.id;
|
|
176
|
+
requestsLoading = true;
|
|
177
|
+
try {
|
|
178
|
+
const data = await app.api(`/rooms/${encodeURIComponent(roomId)}/join-requests?sessionID=${encodeURIComponent(LS.sessionID)}`);
|
|
179
|
+
if (generation !== requestsGeneration || app.state.room?.id !== roomId) return;
|
|
180
|
+
requestsRoomId = roomId;
|
|
181
|
+
requests = Array.isArray(data.requests)
|
|
182
|
+
? data.requests.filter((request) => request && typeof request.sessionID === "string" && request.sessionID && typeof request.name === "string")
|
|
183
|
+
: [];
|
|
184
|
+
renderRequests();
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (generation !== requestsGeneration || app.state.room?.id !== roomId) return;
|
|
187
|
+
if (error.status === 403) { deniedRoomId = roomId; requests = []; renderRequests(); }
|
|
188
|
+
else $("#joinRequestsStatus").textContent = app.t("joinRequestsLoadFailed");
|
|
189
|
+
} finally {
|
|
190
|
+
requestsLoading = false;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function renderRequests() {
|
|
195
|
+
const room = app.state.room;
|
|
196
|
+
const visible = Boolean(room && room.kind !== "direct" && room.joinPolicy === "approval" && isRoomAdmin(app) && deniedRoomId !== room.id);
|
|
197
|
+
section.hidden = !visible;
|
|
198
|
+
if (!visible) {
|
|
199
|
+
list.textContent = ""; requestSignature = "";
|
|
200
|
+
updateBadge(0);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (requestsRoomId && requestsRoomId !== room.id) {
|
|
204
|
+
requests = []; requestsRoomId = room.id; requestSignature = "";
|
|
205
|
+
}
|
|
206
|
+
const count = requests.length;
|
|
207
|
+
$("#joinRequestsHint").hidden = !room.adminSessionID;
|
|
208
|
+
$("#joinRequestsEmpty").hidden = count > 0;
|
|
209
|
+
const countNode = $("#joinRequestsCount");
|
|
210
|
+
countNode.textContent = count > 99 ? "99+" : String(count);
|
|
211
|
+
countNode.hidden = !count;
|
|
212
|
+
const signature = `${room.id}\0${app.locale}\0${requests.map((item) => `${item.sessionID}\0${item.name}\0${item.isAgent}\0${item.createdAt}`).join("\u0001")}`;
|
|
213
|
+
if (signature === requestSignature) { updateBadge(count); return; }
|
|
214
|
+
const active = list.contains(document.activeElement)
|
|
215
|
+
? { id: document.activeElement.closest("[data-request-session]")?.dataset.requestSession, action: document.activeElement.dataset.requestAction }
|
|
216
|
+
: undefined;
|
|
217
|
+
requestSignature = signature;
|
|
218
|
+
list.textContent = "";
|
|
219
|
+
for (const request of requests) list.append(requestRow(request));
|
|
220
|
+
if (active) {
|
|
221
|
+
const row = list.querySelector(`[data-request-session="${CSS.escape(active.id || "")}"]`);
|
|
222
|
+
const target = row?.querySelector(`[data-request-action="${active.action || ""}"]`);
|
|
223
|
+
target?.focus({ preventScroll: true });
|
|
224
|
+
}
|
|
225
|
+
updateBadge(count);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function requestRow(request) {
|
|
229
|
+
const item = document.createElement("li"), info = document.createElement("div"), name = document.createElement("span");
|
|
230
|
+
const meta = document.createElement("span"), kind = document.createElement("span"), actions = document.createElement("div");
|
|
231
|
+
item.className = "join-request-item"; item.dataset.requestSession = request.sessionID;
|
|
232
|
+
info.className = "join-request-info";
|
|
233
|
+
name.className = "join-request-name"; name.textContent = request.name;
|
|
234
|
+
meta.className = "join-request-meta";
|
|
235
|
+
const time = Number(request.createdAt);
|
|
236
|
+
meta.textContent = app.t("joinRequestTime", { time: app.fmtTime(Number.isFinite(time) && time > 0 ? time : Date.now()) });
|
|
237
|
+
kind.className = `join-request-kind${request.isAgent ? " agent" : ""}`;
|
|
238
|
+
kind.textContent = request.isAgent ? app.t("joinRequestAgent") : app.t("joinRequestHuman");
|
|
239
|
+
info.append(name, kind, meta);
|
|
240
|
+
actions.className = "join-request-actions";
|
|
241
|
+
const approve = actionButton("approve", app.t("joinRequestApprove"), approveText(request));
|
|
242
|
+
const deny = actionButton("deny", app.t("joinRequestDeny"), denyText(request));
|
|
243
|
+
approve.className = "btn primary small"; deny.className = "btn ghost small danger-text";
|
|
244
|
+
approve.addEventListener("click", () => void decide(request, "approve", item, approve, deny));
|
|
245
|
+
deny.addEventListener("click", () => void decide(request, "deny", item, approve, deny));
|
|
246
|
+
actions.append(approve, deny); item.append(info, actions);
|
|
247
|
+
return item;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function actionButton(action, label, ariaLabel) {
|
|
251
|
+
const button = document.createElement("button");
|
|
252
|
+
button.type = "button"; button.dataset.requestAction = action;
|
|
253
|
+
button.textContent = label; button.setAttribute("aria-label", ariaLabel);
|
|
254
|
+
return button;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async function decide(request, action, item, approve, deny) {
|
|
258
|
+
const room = app.state.room;
|
|
259
|
+
if (!room) return;
|
|
260
|
+
const index = requests.findIndex((entry) => entry.sessionID === request.sessionID);
|
|
261
|
+
item.setAttribute("aria-busy", "true"); approve.disabled = true; deny.disabled = true;
|
|
262
|
+
try {
|
|
263
|
+
await app.api(`/rooms/${encodeURIComponent(room.id)}/join-requests/${encodeURIComponent(request.sessionID)}/${action}`, {
|
|
264
|
+
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID }),
|
|
265
|
+
});
|
|
266
|
+
requests = requests.filter((entry) => entry.sessionID !== request.sessionID);
|
|
267
|
+
requestSignature = "";
|
|
268
|
+
app.setText($("#joinRequestsStatus"), action === "approve" ? "joinRequestApproved" : "joinRequestDenied", { name: request.name });
|
|
269
|
+
renderRequests();
|
|
270
|
+
focusAfterRequest(index);
|
|
271
|
+
if (action === "approve") void app.refreshRoomMeta?.();
|
|
272
|
+
} catch (error) {
|
|
273
|
+
if (error.status === 404) {
|
|
274
|
+
requests = requests.filter((entry) => entry.sessionID !== request.sessionID);
|
|
275
|
+
requestSignature = "";
|
|
276
|
+
app.setText($("#joinRequestsStatus"), "joinRequestAlreadyHandled");
|
|
277
|
+
renderRequests();
|
|
278
|
+
focusAfterRequest(index);
|
|
279
|
+
} else if (error.status === 403) {
|
|
280
|
+
deniedRoomId = room.id; requests = []; renderRequests();
|
|
281
|
+
} else {
|
|
282
|
+
item.removeAttribute("aria-busy"); approve.disabled = false; deny.disabled = false;
|
|
283
|
+
app.setText($("#joinRequestsStatus"), "joinRequestActionFailed", { message: error.message });
|
|
284
|
+
if (error.status === 409) void refreshRequests();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function focusAfterRequest(index) {
|
|
290
|
+
const rows = [...list.querySelectorAll(".join-request-item")];
|
|
291
|
+
const row = rows[Math.min(index, rows.length - 1)];
|
|
292
|
+
const target = row?.querySelector('[data-request-action="approve"]') || $("#joinRequestsTitle");
|
|
293
|
+
target?.focus({ preventScroll: true });
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function updateBadge(count) {
|
|
297
|
+
const badge = $("#joinRequestBadge"), button = $("#btnMembers");
|
|
298
|
+
$("#joinRequestBadgeCount").textContent = count > 99 ? "99+" : String(count);
|
|
299
|
+
$("#joinRequestBadgeWord").textContent = count === 1 ? app.t("joinRequestBadgeOne") : app.t("joinRequestBadgeMany");
|
|
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
|
+
function stopWaiting() {
|
|
311
|
+
closePending(true);
|
|
312
|
+
pending.status = "dismissed";
|
|
313
|
+
($("#joinView").hidden ? $("#msgInput") : $("#inRoomId"))?.focus();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function requestAgain() {
|
|
317
|
+
if (pending.roomId) void app.joinRoom(pending.roomId, pending.name || LS.name);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
$("#btnJoinPendingCheck").addEventListener("click", () => void checkPending(true));
|
|
321
|
+
$("#btnJoinNoticeCheck").addEventListener("click", () => void checkPending(true));
|
|
322
|
+
$("#btnJoinPendingStop").addEventListener("click", stopWaiting);
|
|
323
|
+
$("#btnJoinNoticeStop").addEventListener("click", stopWaiting);
|
|
324
|
+
$("#btnJoinPendingAgain").addEventListener("click", requestAgain);
|
|
325
|
+
$("#btnJoinNoticeAgain").addEventListener("click", requestAgain);
|
|
326
|
+
$("#btnMembers").addEventListener("click", () => {
|
|
327
|
+
setTimeout(() => { if (!$("#memberPanel").hidden) void refreshRequests(); }, 0);
|
|
328
|
+
});
|
|
329
|
+
document.addEventListener("chat-room-activated", (event) => {
|
|
330
|
+
const roomId = event.detail;
|
|
331
|
+
if (pending.roomId === roomId) {
|
|
332
|
+
pendingIds.delete(roomId);
|
|
333
|
+
closePending();
|
|
334
|
+
}
|
|
335
|
+
deniedRoomId = ""; requests = []; requestsRoomId = roomId || ""; requestSignature = "";
|
|
336
|
+
renderRequests();
|
|
337
|
+
void refreshRequests();
|
|
338
|
+
renderRoomLists();
|
|
339
|
+
});
|
|
340
|
+
document.addEventListener("visibilitychange", () => {
|
|
341
|
+
if (document.hidden) return;
|
|
342
|
+
if (pending.roomId && pending.status === "pending" && !pending.timer) scheduleCheck(0);
|
|
343
|
+
if (!panel.hidden) void refreshRequests();
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
// Locale changes must relabel rows and the open request card without another round trip.
|
|
347
|
+
const applyLocale = app.applyLocale;
|
|
348
|
+
app.applyLocale = () => {
|
|
349
|
+
applyLocale();
|
|
350
|
+
renderPendingCopy();
|
|
351
|
+
renderRequests();
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
renderRequests();
|
|
355
|
+
}
|
|
@@ -129,18 +129,18 @@ async function loadResourceCard(card, roomId, app) {
|
|
|
129
129
|
actions.className = "resource-actions"; status.className = "resource-status"; status.setAttribute("role", "status");
|
|
130
130
|
card.replaceChildren(info, actions, status);
|
|
131
131
|
const download = resourceButton("resourceDownload", resource.name, () => downloadResource(card, roomId, id, resource, app, status, download), app);
|
|
132
|
-
|
|
132
|
+
download.classList.add("resource-download");
|
|
133
|
+
if (isRoomAdmin(app)) {
|
|
134
|
+
let remove;
|
|
135
|
+
remove = resourceMenuItem("resourceDelete", () => deleteResource(card, roomId, id, resource, app, status, remove), app);
|
|
136
|
+
actions.append(resourceSplit(app, resource, download, remove));
|
|
137
|
+
} else actions.append(download);
|
|
133
138
|
if (previewKind(type)) {
|
|
134
139
|
const preview = resourceButton("resourcePreview", resource.name, () => previewResource(card, roomId, id, resource, type, preview, app, status), app);
|
|
135
140
|
actions.append(preview);
|
|
136
141
|
// Media you sent yourself is shown inline; other people's media stays collapsed.
|
|
137
142
|
if (card.closest(".msg.own")) void previewResource(card, roomId, id, resource, type, preview, app, status);
|
|
138
143
|
}
|
|
139
|
-
if (isRoomAdmin(app)) {
|
|
140
|
-
const remove = resourceButton("resourceDelete", resource.name, () => deleteResource(card, roomId, id, resource, app, status, remove), app);
|
|
141
|
-
remove.classList.add("danger-text");
|
|
142
|
-
actions.append(remove);
|
|
143
|
-
}
|
|
144
144
|
} catch (error) { if (card.isConnected) setResourceError(card, app, error?.status === 404 ? "resourceDeleted" : "resourceFailed"); }
|
|
145
145
|
}
|
|
146
146
|
function setResourceButton(button, key, name, app) {
|
|
@@ -153,8 +153,56 @@ function resourceButton(key, name, action, app) {
|
|
|
153
153
|
setResourceButton(button, key, name, app);
|
|
154
154
|
button.addEventListener("click", action); return button;
|
|
155
155
|
}
|
|
156
|
+
function resourceMenuItem(key, action, app) {
|
|
157
|
+
const button = document.createElement("button");
|
|
158
|
+
button.type = "button"; button.className = "menu-action danger-text"; button.setAttribute("role", "menuitem");
|
|
159
|
+
button.dataset.i18n = key; button.textContent = app.t(key);
|
|
160
|
+
button.addEventListener("click", action); return button;
|
|
161
|
+
}
|
|
162
|
+
function resourceSplit(app, resource, download, item) {
|
|
163
|
+
const group = document.createElement("span"), toggle = document.createElement("button"), menu = document.createElement("span");
|
|
164
|
+
group.className = "resource-split";
|
|
165
|
+
toggle.type = "button"; toggle.className = "btn ghost small resource-button resource-split-toggle";
|
|
166
|
+
toggle.setAttribute("aria-haspopup", "menu"); toggle.setAttribute("aria-expanded", "false");
|
|
167
|
+
toggle.setAttribute("data-i18n-aria-label", "resourceMoreActions");
|
|
168
|
+
toggle._i18nValues = { name: resource.name };
|
|
169
|
+
toggle.setAttribute("aria-label", app.t("resourceMoreActions", { name: resource.name }));
|
|
170
|
+
const icon = document.createElementNS("http://www.w3.org/2000/svg", "svg"), path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
171
|
+
icon.setAttribute("aria-hidden", "true"); icon.setAttribute("viewBox", "0 0 24 24"); path.setAttribute("d", "M6 9l6 6 6-6"); icon.append(path); toggle.append(icon);
|
|
172
|
+
menu.className = "resource-split-menu"; menu.hidden = true; menu.setAttribute("role", "menu"); menu.append(item);
|
|
173
|
+
group.append(download, toggle, menu);
|
|
174
|
+
function close(focusToggle = false) {
|
|
175
|
+
if (menu.hidden) return;
|
|
176
|
+
menu.hidden = true; toggle.setAttribute("aria-expanded", "false");
|
|
177
|
+
document.removeEventListener("click", outside);
|
|
178
|
+
if (focusToggle) toggle.focus();
|
|
179
|
+
}
|
|
180
|
+
function open() {
|
|
181
|
+
if (!menu.hidden) return;
|
|
182
|
+
menu.hidden = false; toggle.setAttribute("aria-expanded", "true");
|
|
183
|
+
document.addEventListener("click", outside); item.focus();
|
|
184
|
+
}
|
|
185
|
+
function outside(event) { if (!group.isConnected || !group.contains(event.target)) close(); }
|
|
186
|
+
item.addEventListener("click", () => close(), { capture: true });
|
|
187
|
+
toggle.addEventListener("click", () => { if (menu.hidden) open(); else close(true); });
|
|
188
|
+
toggle.addEventListener("keydown", (event) => {
|
|
189
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") { event.preventDefault(); open(); }
|
|
190
|
+
});
|
|
191
|
+
menu.addEventListener("keydown", (event) => {
|
|
192
|
+
const items = [...menu.querySelectorAll('[role="menuitem"]:not(:disabled)')], index = items.indexOf(document.activeElement);
|
|
193
|
+
if (event.key === "Escape") { event.preventDefault(); close(true); }
|
|
194
|
+
else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
195
|
+
event.preventDefault(); items[(index + (event.key === "ArrowDown" ? 1 : -1) + items.length) % items.length]?.focus();
|
|
196
|
+
} else if (event.key === "Home" || event.key === "End") { event.preventDefault(); (event.key === "Home" ? items[0] : items.at(-1))?.focus(); }
|
|
197
|
+
});
|
|
198
|
+
group.addEventListener("keydown", (event) => {
|
|
199
|
+
if (event.key === "Escape" && !menu.hidden) { event.preventDefault(); close(true); }
|
|
200
|
+
});
|
|
201
|
+
group.addEventListener("focusout", (event) => { if (!menu.hidden && !group.contains(event.relatedTarget)) close(); });
|
|
202
|
+
return group;
|
|
203
|
+
}
|
|
156
204
|
async function deleteResource(card, roomId, id, resource, app, status, button) {
|
|
157
|
-
if (!
|
|
205
|
+
if (!(await app.confirmAction(app.t("resourceDeleteConfirm", { name: resource.name })))) return;
|
|
158
206
|
button.disabled = true;
|
|
159
207
|
status.textContent = app.t("resourceDeleting");
|
|
160
208
|
try {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LS } from "./state.js";
|
|
2
|
-
import {
|
|
2
|
+
import { canRemoveMember, memberRoleBadge } from "./admin-badge.js";
|
|
3
3
|
|
|
4
4
|
export function initMembers(app) {
|
|
5
5
|
const $ = app.$;
|
|
@@ -55,7 +55,8 @@ export function initMembers(app) {
|
|
|
55
55
|
const row = document.createElement("li"), name = document.createElement("span"), identity = document.createElement("span"), actions = document.createElement("span");
|
|
56
56
|
row.dataset.memberSessionId = member.sessionID; row.dataset.memberName = member.name; row.dataset.memberType = member.type;
|
|
57
57
|
name.textContent = member.name; identity.className = "member-identity"; identity.append(name);
|
|
58
|
-
|
|
58
|
+
const roleBadge = memberRoleBadge(app, member);
|
|
59
|
+
if (roleBadge) identity.append(roleBadge);
|
|
59
60
|
row.append(identity);
|
|
60
61
|
if (member.type === "agent") row.append(activityBadge(member, app));
|
|
61
62
|
actions.className = "member-actions";
|
|
@@ -68,6 +69,7 @@ export function initMembers(app) {
|
|
|
68
69
|
}
|
|
69
70
|
} else for (const member of members) updateActivity(list.querySelector(`[data-member-session-id="${CSS.escape(member.sessionID)}"]`), member, app);
|
|
70
71
|
$("#memberEmpty").hidden = members.length > 0;
|
|
72
|
+
void app.refreshJoinRequests?.();
|
|
71
73
|
app.queueReceiptObservation();
|
|
72
74
|
app.state.memberRefreshTimer = setTimeout(() => {
|
|
73
75
|
if (current(app, room.id, generation) && !document.hidden) void app.loadManageableMembers();
|
|
@@ -147,7 +149,7 @@ export function initMembers(app) {
|
|
|
147
149
|
};
|
|
148
150
|
|
|
149
151
|
app.kickMember = async (member) => {
|
|
150
|
-
if (!
|
|
152
|
+
if (!(await app.confirmAction(app.t("kickConfirm", { name: member.name })))) return;
|
|
151
153
|
try {
|
|
152
154
|
await app.api(`/rooms/${encodeURIComponent(app.state.room.id)}/kick`, {
|
|
153
155
|
method: "POST",
|
|
@@ -40,6 +40,9 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
40
40
|
try {
|
|
41
41
|
const entry = app.state.rooms.get(roomId);
|
|
42
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));
|
|
43
46
|
const data = await app.api(`/inbox?${query}`, { signal });
|
|
44
47
|
if (!isCurrentPoll(roomId, generation)) return;
|
|
45
48
|
if (data.member === false) {
|
|
@@ -67,6 +70,14 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
67
70
|
entry.unreadIds.delete(item.answerToMessageID); unreadChanged = true;
|
|
68
71
|
}
|
|
69
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
|
+
}
|
|
70
81
|
if (entry) entry.receivedTs = items.reduce((latest, item) => Number.isSafeInteger(item.createdAt) ? Math.max(latest, item.createdAt) : latest, previousReceivedTs);
|
|
71
82
|
if (entry) entry.unreadOverflow = Boolean(data.hasMore);
|
|
72
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); }
|
|
@@ -117,14 +128,23 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
117
128
|
} catch { /* The next room event will retry. */ }
|
|
118
129
|
};
|
|
119
130
|
|
|
120
|
-
app.reconcileRoom = async (roomId, generation = app.state.pollGeneration) => {
|
|
131
|
+
app.reconcileRoom = async (roomId, generation = app.state.pollGeneration, syncHistory = false) => {
|
|
132
|
+
const entry = app.state.rooms.get(roomId);
|
|
121
133
|
try {
|
|
122
134
|
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
123
135
|
if (!isCurrentPoll(roomId, generation)) return;
|
|
136
|
+
if (entry && Number.isFinite(Number(room.recallVersion))) entry.recallVersion = Number(room.recallVersion);
|
|
124
137
|
const current = app.state.room.messages;
|
|
125
138
|
const ids = new Set(current.map((message) => message.id));
|
|
126
139
|
const missing = room.messages.filter((message) => !ids.has(message.id));
|
|
127
|
-
|
|
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;
|
|
128
148
|
for (const message of missing) {
|
|
129
149
|
const index = current.findIndex((item) => item.createdAt > message.createdAt);
|
|
130
150
|
current.splice(index < 0 ? current.length : index, 0, message);
|
|
@@ -133,5 +153,6 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
133
153
|
const result = app.rerenderMessages();
|
|
134
154
|
if (result.skipped === 0) void app.markActiveRoomRead();
|
|
135
155
|
} catch { /* Try again during the next reconciliation. */ }
|
|
156
|
+
finally { if (entry) entry.recallSync = false; }
|
|
136
157
|
};
|
|
137
|
-
}
|
|
158
|
+
}
|
|
@@ -340,9 +340,9 @@ export function initMessages(app) {
|
|
|
340
340
|
|
|
341
341
|
app.send = async () => {
|
|
342
342
|
const input = $("#msgInput"), room = app.state.room, replyTarget = room && app.replyTargetFor?.(room.id);
|
|
343
|
-
if (!room || app.state.sendingRooms?.has(room.id)) return;
|
|
343
|
+
if (!room || app.state.sendingRooms?.has(room.id)) return false;
|
|
344
344
|
const message = app.prepareMessage?.(room.id, input.value) || { text: input.value.trim(), ids: [], ready: Boolean(input.value.trim()) };
|
|
345
|
-
if (!message.ready) return;
|
|
345
|
+
if (!message.ready) return false;
|
|
346
346
|
// Clicking Send focuses the button; it is disabled when the draft clears, so
|
|
347
347
|
// keep the caret in the composer or the next keystrokes go nowhere.
|
|
348
348
|
input.focus({ preventScroll: true });
|
|
@@ -365,7 +365,7 @@ export function initMessages(app) {
|
|
|
365
365
|
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text, ...(replyTarget ? { replyToMessageID: replyTarget.id } : {}) }),
|
|
366
366
|
});
|
|
367
367
|
app.completeAttachments?.(room.id, message.ids);
|
|
368
|
-
if (app.state.room?.id !== room.id) return;
|
|
368
|
+
if (app.state.room?.id !== room.id) return true;
|
|
369
369
|
const entry = app.state.rooms.get(room.id);
|
|
370
370
|
// The POST snapshot is older than anything polling received while it was
|
|
371
371
|
// in flight; merge instead of dropping those messages.
|
|
@@ -392,6 +392,7 @@ export function initMessages(app) {
|
|
|
392
392
|
void app.markActiveRoomRead();
|
|
393
393
|
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
394
394
|
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
395
|
+
return true;
|
|
395
396
|
} catch (error) {
|
|
396
397
|
const unchanged = (app.state.draftRevision.get(room.id) || 0) === revision;
|
|
397
398
|
// Someone answered the question first; retrying the answer can never succeed,
|
|
@@ -404,7 +405,7 @@ export function initMessages(app) {
|
|
|
404
405
|
app.renderFailedMessages();
|
|
405
406
|
}
|
|
406
407
|
app.completeAttachments?.(room.id, message.ids);
|
|
407
|
-
if (app.state.room?.id !== room.id) return;
|
|
408
|
+
if (app.state.room?.id !== room.id) return false;
|
|
408
409
|
if (unchanged && !staleReply) app.clearDraft(room.id);
|
|
409
410
|
if (staleReply) input.draftValue = text;
|
|
410
411
|
app.autoGrow(input);
|
|
@@ -412,6 +413,7 @@ export function initMessages(app) {
|
|
|
412
413
|
app.updateDraftCount();
|
|
413
414
|
if (error.status === 401) { app.openChatSettings(); app.showSendError("sendAuth"); }
|
|
414
415
|
else app.showSendError(error.network ? "sendNetwork" : "sendFailed", { message: error.message });
|
|
416
|
+
return false;
|
|
415
417
|
} finally {
|
|
416
418
|
app.state.sendingRooms.delete(room.id);
|
|
417
419
|
if (app.state.room?.id === room.id) app.updateSendState();
|
|
@@ -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);
|