@alexsun-top/opencode-chat-room 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/README.md +28 -215
- package/README.zh-CN.md +27 -209
- package/docs/administrator-rooms.md +6 -22
- package/package.json +1 -4
- package/plugin.ts +1 -1
- package/server/admin-auth.ts +4 -2
- package/server/admin-routes.ts +2 -11
- package/server/admin-state.ts +15 -4
- package/server/assets/base.css +1 -1
- package/server/assets/chat/admin.js +30 -123
- package/server/assets/chat/announcements.js +17 -1
- package/server/assets/chat/attachments.js +4 -1
- package/server/assets/chat/editor.js +65 -15
- package/server/assets/chat/events.js +12 -7
- package/server/assets/chat/markdown.js +31 -11
- package/server/assets/chat/mentions.js +3 -1
- package/server/assets/chat/messages.js +55 -20
- package/server/assets/chat/questions.js +15 -4
- package/server/assets/chat/reminders.js +4 -3
- package/server/assets/chat/replies.js +2 -1
- package/server/assets/chat/room-watcher.js +45 -0
- package/server/assets/chat/rooms.js +62 -69
- package/server/assets/chat/state.js +8 -6
- package/server/assets/conversation.css +8 -5
- package/server/assets/responsive.css +5 -12
- package/server/chat-server.ts +26 -37
- package/server/chat.html +3 -10
- package/server/member-routes.ts +9 -11
- package/server/room-reminders.ts +15 -5
- package/server/wake.ts +11 -2
- package/skills/chatroom/SKILL.md +3 -7
- package/src/admin-client.ts +8 -45
- package/src/admin-credentials.ts +0 -19
- package/src/center-inbox.ts +0 -2
- package/src/mentions.ts +2 -1
- package/src/plugin-notifications.ts +4 -3
- package/src/resource-upload.ts +1 -1
- package/src/resources.ts +4 -1
- package/src/sqlite-state.ts +40 -0
- package/src/store.ts +4 -1
- package/src/v2-plugin.ts +1 -1
- package/server/admin-plan-routes.ts +0 -79
- package/server/admin-scheduler.ts +0 -42
- package/server/admin-task-routes.ts +0 -182
- package/src/supervision-inbox.ts +0 -62
|
@@ -61,6 +61,7 @@ export function initEvents(app) {
|
|
|
61
61
|
const readTs = entry?.readTs || app.state.lastReadTs.get(roomId) || 0;
|
|
62
62
|
const boundary = readTs && entry?.room.messages.find((message) => message.createdAt > readTs);
|
|
63
63
|
if (boundary && align(boundary.id, 0)) return { messageId: boundary.id };
|
|
64
|
+
if (!saved && !boundary) return false;
|
|
64
65
|
return saved?.messageId || readTs ? { messageId: saved?.messageId || null, missing: true } : false;
|
|
65
66
|
};
|
|
66
67
|
function closeRoomDrawer(restore = true) {
|
|
@@ -121,24 +122,24 @@ export function initEvents(app) {
|
|
|
121
122
|
if (app.state.room?.id === roomId) cancelAnchorLoads();
|
|
122
123
|
return removeJoinedRoom(roomId, options);
|
|
123
124
|
};
|
|
124
|
-
function activateNow(id) {
|
|
125
|
-
activateRoom(id); captureReadingPosition();
|
|
125
|
+
function activateNow(id, preserveRoomListGeneration = false) {
|
|
126
|
+
activateRoom(id, preserveRoomListGeneration); captureReadingPosition();
|
|
126
127
|
if (roomRail.classList.contains("drawer-open")) closeRoomDrawer();
|
|
127
128
|
}
|
|
128
|
-
app.activateRoom = (id) => {
|
|
129
|
+
app.activateRoom = (id, preserveRoomListGeneration = false) => {
|
|
129
130
|
cancelAnchorLoads();
|
|
130
131
|
const entry = app.state.rooms.get(id), saved = app.state.positions.get(id);
|
|
131
132
|
const intent = ++app.state.navigationGeneration;
|
|
132
|
-
if (!entry) return activateNow(id);
|
|
133
|
+
if (!entry) return activateNow(id, preserveRoomListGeneration);
|
|
133
134
|
const readTs = entry.readTs || app.state.lastReadTs.get(id) || 0;
|
|
134
135
|
const needAnchor = saved?.messageId && !entry.room.messages.some((message) => message.id === saved.messageId);
|
|
135
136
|
const latest = entry.room.messages.at(-1);
|
|
136
137
|
const needBoundary = !saved?.messageId && readTs && (!latest || latest.createdAt > readTs && !entry.room.messages.some((message) => message.createdAt <= readTs));
|
|
137
|
-
if (!needAnchor && !needBoundary) return activateNow(id);
|
|
138
|
+
if (!needAnchor && !needBoundary) return activateNow(id, preserveRoomListGeneration);
|
|
138
139
|
const controller = new AbortController();
|
|
139
140
|
const task = loadAnchorHistory(id, entry, saved?.messageId, app, needBoundary ? readTs : 0, controller.signal).catch(() => {}).finally(() => {
|
|
140
141
|
if (pendingAnchorLoads.get(id)?.controller === controller) pendingAnchorLoads.delete(id);
|
|
141
|
-
if (intent === app.state.navigationGeneration) activateNow(id);
|
|
142
|
+
if (intent === app.state.navigationGeneration) activateNow(id, preserveRoomListGeneration);
|
|
142
143
|
});
|
|
143
144
|
pendingAnchorLoads.set(id, { controller, task });
|
|
144
145
|
};
|
|
@@ -227,6 +228,9 @@ export function initEvents(app) {
|
|
|
227
228
|
if (event.deltaY < 0 && thread.scrollTop <= 0 || event.deltaY > 0 && gap <= 2) event.preventDefault();
|
|
228
229
|
}, { passive: false });
|
|
229
230
|
thread.addEventListener("touchmove", markScrollIntent, { passive: true });
|
|
231
|
+
thread.addEventListener("keydown", (event) => {
|
|
232
|
+
if (/^(PageDown|PageUp|End|Home|ArrowDown|ArrowUp| )$/.test(event.key)) markScrollIntent();
|
|
233
|
+
});
|
|
230
234
|
thread.addEventListener("pointerdown", (event) => {
|
|
231
235
|
if (event.clientX >= thread.getBoundingClientRect().right - 18) markScrollIntent();
|
|
232
236
|
});
|
|
@@ -234,7 +238,7 @@ export function initEvents(app) {
|
|
|
234
238
|
const adjusting = (app.state.resizeScrollAdjusting || app.state.layoutScrollAdjusting)
|
|
235
239
|
&& performance.now() > app.state.userScrollIntentUntil;
|
|
236
240
|
if (!adjusting || performance.now() <= app.state.userScrollIntentUntil) captureReadingPosition();
|
|
237
|
-
if (!adjusting && thread.scrollHeight - thread.scrollTop - thread.clientHeight < 80) void app.markActiveRoomRead();
|
|
241
|
+
if (!adjusting && thread.scrollHeight - thread.scrollTop - thread.clientHeight < 80) void app.markActiveRoomRead(performance.now() <= app.state.userScrollIntentUntil);
|
|
238
242
|
app.queueReceiptRefresh();
|
|
239
243
|
if (!adjusting) app.queueReceiptObservation();
|
|
240
244
|
app.updateJump();
|
|
@@ -327,6 +331,7 @@ function saveAccessToken(button, app) {
|
|
|
327
331
|
try { persistent = localStorage.getItem("chat.token") === LS.token; } catch { /* Memory fallback is tab-scoped. */ }
|
|
328
332
|
app.flash(button, persistent ? "saved" : "savedMemory");
|
|
329
333
|
void app.refreshRoomList();
|
|
334
|
+
void app.verifyAdminToken?.();
|
|
330
335
|
app.retryPendingAction();
|
|
331
336
|
}
|
|
332
337
|
|
|
@@ -132,14 +132,19 @@ async function loadResourceCard(card, roomId, app) {
|
|
|
132
132
|
if (previewKind(type)) {
|
|
133
133
|
const preview = resourceButton("resourcePreview", resource.name, () => previewResource(card, roomId, id, resource, type, preview, app, status), app);
|
|
134
134
|
actions.append(preview);
|
|
135
|
+
// Media you sent yourself is shown inline; other people's media stays collapsed.
|
|
136
|
+
if (card.closest(".msg.own")) void previewResource(card, roomId, id, resource, type, preview, app, status);
|
|
135
137
|
}
|
|
136
138
|
} catch { if (card.isConnected) setResourceError(card, app); }
|
|
137
139
|
}
|
|
140
|
+
function setResourceButton(button, key, name, app) {
|
|
141
|
+
button.dataset.i18n = key; button.textContent = app.t(key);
|
|
142
|
+
button.setAttribute("aria-label", `${app.t(key)}: ${name}`);
|
|
143
|
+
}
|
|
138
144
|
function resourceButton(key, name, action, app) {
|
|
139
145
|
const button = document.createElement("button");
|
|
140
146
|
button.type = "button"; button.className = "btn ghost small resource-button";
|
|
141
|
-
button
|
|
142
|
-
button.setAttribute("aria-label", `${app.t(key)}: ${name}`);
|
|
147
|
+
setResourceButton(button, key, name, app);
|
|
143
148
|
button.addEventListener("click", action); return button;
|
|
144
149
|
}
|
|
145
150
|
async function downloadResource(card, roomId, id, resource, app, status, button) {
|
|
@@ -161,30 +166,45 @@ async function previewResource(card, roomId, id, resource, type, button, app, st
|
|
|
161
166
|
button.disabled = true;
|
|
162
167
|
if (card._previewElement) {
|
|
163
168
|
try {
|
|
164
|
-
await app.preserveReadingLayout(() =>
|
|
169
|
+
await app.preserveReadingLayout(() => {
|
|
170
|
+
card._previewElement.remove();
|
|
171
|
+
setResourceButton(button, "resourcePreview", resource.name, app); button.disabled = false;
|
|
172
|
+
});
|
|
165
173
|
URL.revokeObjectURL(card._resourceUrl); previewUrls.delete(card); delete card._resourceUrl; delete card._previewElement;
|
|
166
|
-
if (card.isConnected) { button.dataset.i18n = "resourcePreview"; button.textContent = app.t("resourcePreview"); }
|
|
167
174
|
} catch {
|
|
168
175
|
if (!card.isConnected) return;
|
|
169
176
|
} finally { if (card.isConnected) button.disabled = false; }
|
|
170
177
|
return;
|
|
171
178
|
}
|
|
172
179
|
status.textContent = app.t("resourceLoading");
|
|
173
|
-
let url;
|
|
180
|
+
let url, media;
|
|
181
|
+
const fail = () => {
|
|
182
|
+
media?.remove(); media = undefined;
|
|
183
|
+
if (url) { URL.revokeObjectURL(url); url = undefined; }
|
|
184
|
+
setResourceButton(button, "resourcePreview", resource.name, app); button.disabled = false;
|
|
185
|
+
if (card.isConnected) status.textContent = app.t("resourceFailed");
|
|
186
|
+
};
|
|
174
187
|
try {
|
|
175
188
|
const blob = await app.apiBlob(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}/content`);
|
|
176
189
|
if (!card.isConnected) return;
|
|
177
190
|
url = URL.createObjectURL(blob);
|
|
178
|
-
const kind = previewKind(type)
|
|
191
|
+
const kind = previewKind(type);
|
|
192
|
+
media = document.createElement(kind);
|
|
193
|
+
const ready = waitForMediaSize(media, kind);
|
|
179
194
|
if (kind === "img") media.alt = resource.name;
|
|
180
195
|
else { media.controls = true; media.preload = "metadata"; }
|
|
181
|
-
media.addEventListener("error",
|
|
196
|
+
media.addEventListener("error", fail, { once: true });
|
|
182
197
|
media.src = url;
|
|
183
|
-
|
|
184
|
-
|
|
198
|
+
// Update the control inside the layout step: a stalled animation frame must not
|
|
199
|
+
// leave the preview open with a disabled button.
|
|
200
|
+
await app.preserveReadingLayout(async () => {
|
|
201
|
+
status.textContent = ""; card.append(media);
|
|
202
|
+
setResourceButton(button, "resourceHide", resource.name, app); button.disabled = false;
|
|
203
|
+
await ready;
|
|
204
|
+
});
|
|
205
|
+
if (!card.isConnected) { fail(); return; }
|
|
185
206
|
card._resourceUrl = url; card._previewElement = media; previewUrls.set(card, url); watchPreviewRemoval();
|
|
186
|
-
|
|
187
|
-
} catch { if (url) URL.revokeObjectURL(url); if (card.isConnected) status.textContent = app.t("resourceFailed"); }
|
|
207
|
+
} catch { fail(); }
|
|
188
208
|
finally { if (card.isConnected) button.disabled = false; }
|
|
189
209
|
}
|
|
190
210
|
function waitForMediaSize(media, kind) {
|
|
@@ -77,8 +77,10 @@ export function initMentions(app) {
|
|
|
77
77
|
if (input.value.slice(mention.start, end) !== `@${mention.query}`) return app.closeMention();
|
|
78
78
|
replaceEditorRange(input, mention.start, end, `<at>@${name}</at>`);
|
|
79
79
|
app.closeMention();
|
|
80
|
-
|
|
80
|
+
// Chrome can reset the selection while focus() lands; restore the caret last.
|
|
81
81
|
input.focus({ preventScroll: true });
|
|
82
|
+
input.setCaret?.(mention.start + `<at>@${name}</at>`.length);
|
|
83
|
+
input.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertText" }));
|
|
82
84
|
};
|
|
83
85
|
}
|
|
84
86
|
|
|
@@ -11,6 +11,7 @@ export function initMessages(app) {
|
|
|
11
11
|
|
|
12
12
|
app.autoGrow = (textarea) => {
|
|
13
13
|
const thread = $("#thread"), atBottom = Boolean(app.state.room && Math.abs(thread.scrollHeight - thread.clientHeight - thread.scrollTop) < 1);
|
|
14
|
+
const scrollTop = textarea.scrollTop;
|
|
14
15
|
textarea.style.overflowY = "hidden";
|
|
15
16
|
const style = getComputedStyle(textarea), line = parseFloat(style.lineHeight);
|
|
16
17
|
const padding = parseFloat(style.paddingTop) + parseFloat(style.paddingBottom);
|
|
@@ -25,6 +26,8 @@ export function initMessages(app) {
|
|
|
25
26
|
const overflow = textarea.scrollHeight > limit + 1;
|
|
26
27
|
textarea.style.height = `${Math.min(textarea.scrollHeight, limit) + border}px`;
|
|
27
28
|
textarea.style.overflowY = overflow ? "auto" : "hidden";
|
|
29
|
+
// Collapsing the height to measure content resets scrollTop; keep the caret view where it was.
|
|
30
|
+
if (overflow) textarea.scrollTop = scrollTop;
|
|
28
31
|
if (atBottom) thread.scrollTop = thread.scrollHeight;
|
|
29
32
|
};
|
|
30
33
|
app.updateSendState = () => { $("#btnSend").disabled = !$("#msgInput").value.trim(); };
|
|
@@ -100,12 +103,12 @@ export function initMessages(app) {
|
|
|
100
103
|
app.state.unread = entry ? entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0) : 0;
|
|
101
104
|
app.updateBadge();
|
|
102
105
|
app.updateJump();
|
|
103
|
-
void app.markActiveRoomRead();
|
|
106
|
+
void app.markActiveRoomRead(true);
|
|
104
107
|
};
|
|
105
108
|
app.updateJump = () => {
|
|
106
109
|
const thread = $("#thread");
|
|
107
110
|
const away = thread.scrollHeight - thread.scrollTop - thread.clientHeight > 160;
|
|
108
|
-
$("#btnJump").hidden = !
|
|
111
|
+
$("#btnJump").hidden = !away;
|
|
109
112
|
};
|
|
110
113
|
app.updateBadge = () => {
|
|
111
114
|
const badge = $("#jumpBadge");
|
|
@@ -113,33 +116,55 @@ export function initMessages(app) {
|
|
|
113
116
|
badge.hidden = app.state.unread === 0;
|
|
114
117
|
app.updateTitle();
|
|
115
118
|
};
|
|
116
|
-
app.markActiveRoomRead = async () => {
|
|
119
|
+
app.markActiveRoomRead = async (interactive = false) => {
|
|
117
120
|
const room = app.state.room, entry = room && app.state.rooms.get(room.id);
|
|
118
121
|
const generation = app.state.pollGeneration;
|
|
119
122
|
const thread = $("#thread");
|
|
120
|
-
if (!room || !entry || document.hidden || !document.hasFocus()
|
|
123
|
+
if (!room || !entry || document.hidden || (!interactive && !document.hasFocus())
|
|
121
124
|
|| thread.scrollHeight - thread.scrollTop - thread.clientHeight >= 80) return;
|
|
122
125
|
if (app.state.readAnchorGuard?.roomId === room.id) {
|
|
123
|
-
if (performance.now() > app.state.userScrollIntentUntil) return;
|
|
126
|
+
if (!interactive && performance.now() > app.state.userScrollIntentUntil) return;
|
|
124
127
|
app.state.readAnchorGuard = null;
|
|
125
128
|
}
|
|
126
129
|
const timestamp = room.messages.reduce((latest, message) => Math.max(latest, message.createdAt), entry.readTs);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
130
|
+
const unreadBefore = entry.unreadIds.size, overflowBefore = entry.unreadOverflow;
|
|
131
|
+
let advanced = false;
|
|
132
|
+
if (timestamp > entry.readTs) {
|
|
133
|
+
try {
|
|
134
|
+
await app.api("/read", {
|
|
135
|
+
method: "POST", body: JSON.stringify({ roomId: room.id, sessionID: LS.sessionID, ts: timestamp }),
|
|
136
|
+
});
|
|
137
|
+
if (app.state.room?.id !== room.id || app.state.pollGeneration !== generation) return;
|
|
138
|
+
entry.readTs = timestamp;
|
|
139
|
+
app.saveReadTs(room.id, timestamp);
|
|
140
|
+
entry.unreadOverflow = false;
|
|
141
|
+
advanced = true;
|
|
142
|
+
} catch { return; /* The next active poll will retry the read watermark. */ }
|
|
143
|
+
}
|
|
144
|
+
// Unanswered questions stay unread until answered.
|
|
145
|
+
const known = new Map(room.messages.map((message) => [message.id, message]));
|
|
146
|
+
const answered = new Set(room.messages.filter((message) => message.answerToMessageID).map((message) => message.answerToMessageID));
|
|
147
|
+
const listed = app.unansweredQuestionIds?.();
|
|
148
|
+
const open = (message) => {
|
|
149
|
+
if (!message.isQuestion || message.answeredByMessageID) return false;
|
|
150
|
+
// A question outside the message window is judged by the server list.
|
|
151
|
+
return known.has(message.id) ? !answered.has(message.id) : !listed || listed.has(message.id);
|
|
152
|
+
};
|
|
153
|
+
for (const [id, message] of entry.unreadIds) {
|
|
154
|
+
const current = known.get(id) || message;
|
|
155
|
+
if (current.createdAt <= entry.readTs && !open(current)) entry.unreadIds.delete(id);
|
|
156
|
+
else if (known.has(id)) entry.unreadIds.set(id, current);
|
|
157
|
+
}
|
|
158
|
+
for (const message of room.messages) {
|
|
159
|
+
if (open(message) && !isOwnMessage(message, app, room)) entry.unreadIds.set(message.id, message);
|
|
160
|
+
}
|
|
161
|
+
app.state.unread = entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0);
|
|
162
|
+
if (advanced || entry.unreadIds.size !== unreadBefore || entry.unreadOverflow !== overflowBefore) {
|
|
138
163
|
app.updateBadge();
|
|
139
164
|
app.updateJump();
|
|
140
165
|
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
141
166
|
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
142
|
-
}
|
|
167
|
+
}
|
|
143
168
|
};
|
|
144
169
|
|
|
145
170
|
app.renderMessage = (message, live) => {
|
|
@@ -316,11 +341,20 @@ export function initMessages(app) {
|
|
|
316
341
|
});
|
|
317
342
|
app.completeAttachments?.(room.id, message.ids);
|
|
318
343
|
if (app.state.room?.id !== room.id) return;
|
|
319
|
-
app.state.room = updated;
|
|
320
344
|
const entry = app.state.rooms.get(room.id);
|
|
345
|
+
// The POST snapshot is older than anything polling received while it was
|
|
346
|
+
// in flight; merge instead of dropping those messages.
|
|
347
|
+
let current = updated;
|
|
348
|
+
if (entry) {
|
|
349
|
+
const merged = new Map([...entry.room.messages, ...updated.messages].map((item) => [item.id, { ...item, roomId: updated.id }]));
|
|
350
|
+
const messages = [...merged.values()].sort((a, b) => a.createdAt - b.createdAt);
|
|
351
|
+
if (messages.length > 500) messages.splice(0, messages.length - 500);
|
|
352
|
+
current = { ...updated, messages };
|
|
353
|
+
}
|
|
354
|
+
app.state.room = current;
|
|
321
355
|
if (entry) {
|
|
322
|
-
entry.room =
|
|
323
|
-
const latest =
|
|
356
|
+
entry.room = current;
|
|
357
|
+
const latest = current.messages.at(-1);
|
|
324
358
|
if (latest) setRoomPreview(entry, latest);
|
|
325
359
|
}
|
|
326
360
|
app.renderMessages(true);
|
|
@@ -418,6 +452,7 @@ export function initMessages(app) {
|
|
|
418
452
|
if (added || unreadChanged || entry && previousUnreadOverflow !== entry.unreadOverflow) {
|
|
419
453
|
app.state.unread = entry ? entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0) : app.state.unread + added;
|
|
420
454
|
app.updateBadge();
|
|
455
|
+
app.updateJump();
|
|
421
456
|
}
|
|
422
457
|
if (rendered.skipped === 0) void app.markActiveRoomRead();
|
|
423
458
|
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LS } from "./state.js";
|
|
2
|
-
import { installEditor } from "./editor.js";
|
|
2
|
+
import { autoMentionDraft, installEditor } from "./editor.js";
|
|
3
3
|
import { copyTextToClipboard, renderMarkdown } from "./markdown.js";
|
|
4
4
|
|
|
5
5
|
export function initQuestions(app) {
|
|
@@ -7,6 +7,10 @@ export function initQuestions(app) {
|
|
|
7
7
|
let request = 0;
|
|
8
8
|
const panel = $("#questionPanel"), trigger = $("#btnQuestions"), list = $("#questionList"), status = $("#questionStatus");
|
|
9
9
|
app.isQuestionSending = () => Boolean(questionMode.get(app.state.room?.id));
|
|
10
|
+
// Undefined until the server list for this room has loaded; callers must not
|
|
11
|
+
// treat that as "no open questions".
|
|
12
|
+
app.unansweredQuestionIds = (roomId = app.state.room?.id) => pending.has(roomId)
|
|
13
|
+
? new Set((pending.get(roomId) || []).map((item) => item.id)) : undefined;
|
|
10
14
|
$("#questionToggle").addEventListener("change", (event) => {
|
|
11
15
|
if (app.state.room) questionMode.set(app.state.room.id, event.target.checked);
|
|
12
16
|
});
|
|
@@ -29,6 +33,9 @@ export function initQuestions(app) {
|
|
|
29
33
|
const items = pending.get(roomId) || [];
|
|
30
34
|
const active = app.state.room?.id === roomId;
|
|
31
35
|
if (active) {
|
|
36
|
+
const signature = `${roomId}\0${app.locale}\0${selected.get(roomId) || ""}\0${items.map((question) => `${question.id}\0${question.text}`).join("\u0001")}`;
|
|
37
|
+
if (list.dataset.signature === signature) { renderReply(roomId); return; }
|
|
38
|
+
list.dataset.signature = signature;
|
|
32
39
|
$("#questionBadge").textContent = items.length > 99 ? "99+" : String(items.length);
|
|
33
40
|
$("#questionBadge").hidden = !items.length;
|
|
34
41
|
trigger.setAttribute("aria-label", `${app.t("questions")}: ${items.length}`);
|
|
@@ -116,7 +123,7 @@ export function initQuestions(app) {
|
|
|
116
123
|
? members.filter((member) => member.sessionID === question.senderSessionID)
|
|
117
124
|
: members.filter((member) => member.name === question.senderId);
|
|
118
125
|
if (author.length !== 1 || members.filter((member) => member.name === author[0].name).length !== 1 || author[0].sessionID === LS.sessionID || author[0].name === "all") return "";
|
|
119
|
-
return
|
|
126
|
+
return autoMentionDraft(author[0].name);
|
|
120
127
|
}
|
|
121
128
|
|
|
122
129
|
async function load(roomId = app.state.room?.id) {
|
|
@@ -218,6 +225,10 @@ export function initQuestions(app) {
|
|
|
218
225
|
|
|
219
226
|
function applyAnswer(roomId, questionId, result) {
|
|
220
227
|
const entry = app.state.rooms.get(roomId);
|
|
228
|
+
if (entry && result.answer) {
|
|
229
|
+
const question = entry.room.messages.find((item) => item.id === questionId);
|
|
230
|
+
if (question) question.answeredByMessageID = result.answer.id;
|
|
231
|
+
}
|
|
221
232
|
if (entry && result.answer && !entry.room.messages.some((item) => item.id === result.answer.id)) {
|
|
222
233
|
entry.room.messages.push({ ...result.answer, roomId }); entry.room.messages.sort((a, b) => a.createdAt - b.createdAt);
|
|
223
234
|
if (entry.room.messages.length > 500) entry.room.messages.splice(0, entry.room.messages.length - 500);
|
|
@@ -262,8 +273,8 @@ export function initQuestions(app) {
|
|
|
262
273
|
}
|
|
263
274
|
});
|
|
264
275
|
const activateRoom = app.activateRoom;
|
|
265
|
-
app.activateRoom = (roomId) => {
|
|
266
|
-
activateRoom(roomId);
|
|
276
|
+
app.activateRoom = (roomId, preserveRoomListGeneration = false) => {
|
|
277
|
+
activateRoom(roomId, preserveRoomListGeneration);
|
|
267
278
|
$("#questionToggle").checked = Boolean(questionMode.get(roomId));
|
|
268
279
|
void load(roomId);
|
|
269
280
|
};
|
|
@@ -2,6 +2,7 @@ import { LS } from "./state.js";
|
|
|
2
2
|
|
|
3
3
|
export function initReminders(app) {
|
|
4
4
|
const $ = app.$, panel = $("#reminderPanel"), trigger = $("#btnReminders"), list = $("#reminderList");
|
|
5
|
+
app.setText($("#reminderTemplateHint"), "reminderTemplateHint", { time: "{time}", initiator: "{initiator}", room: "{room}" });
|
|
5
6
|
let reminders = [], generation = 0, roomGeneration = 0, activeRoomId;
|
|
6
7
|
const drafts = new Map();
|
|
7
8
|
const roomId = () => app.state.room?.id;
|
|
@@ -31,7 +32,8 @@ export function initReminders(app) {
|
|
|
31
32
|
text.textContent = reminder.text; detail.className = "hint";
|
|
32
33
|
const timezone = reminder.schedule.timezone;
|
|
33
34
|
const schedule = reminder.schedule.type === "once" ? formatTime(reminder.schedule.at, timezone) : `${reminder.schedule.expression} · ${timezone}`;
|
|
34
|
-
|
|
35
|
+
const next = reminder.schedule.type === "cron" && reminder.nextRunAt ? ` · ${app.t("reminderNext", { time: formatTime(reminder.nextRunAt, timezone) })}` : "";
|
|
36
|
+
detail.textContent = `${schedule}${reminder.paused ? ` · ${app.t("reminderPaused")}` : next}`;
|
|
35
37
|
const toggle = document.createElement("button"); toggle.type = "button"; toggle.className = "btn ghost small"; toggle.textContent = app.t(reminder.paused ? "reminderResume" : "reminderPause");
|
|
36
38
|
toggle.addEventListener("click", () => void update(reminder, !reminder.paused));
|
|
37
39
|
const remove = document.createElement("button"); remove.type = "button"; remove.className = "btn ghost small danger-text"; remove.textContent = app.t("reminderDelete");
|
|
@@ -50,8 +52,7 @@ export function initReminders(app) {
|
|
|
50
52
|
} catch (error) { if (current(id, context) && generation === request) status(errorText(error)); }
|
|
51
53
|
}
|
|
52
54
|
function validCron(expression) {
|
|
53
|
-
|
|
54
|
-
return fields.length === 5 && fields.every((field) => /^[0-9*/?,\-]+$/.test(field));
|
|
55
|
+
return expression.trim().split(/\s+/).length === 5;
|
|
55
56
|
}
|
|
56
57
|
async function update(reminder, paused) {
|
|
57
58
|
const id = roomId(), context = roomGeneration;
|
|
@@ -3,6 +3,7 @@ import { replaceEditorRange, setEditorSelection, setCaret } from "./editor.js";
|
|
|
3
3
|
|
|
4
4
|
export function initReplies(app) {
|
|
5
5
|
const $ = app.$, targets = new Map(Object.entries(LS.replyDrafts || {}).filter(([, item]) => validTarget(item)));
|
|
6
|
+
$("#replyCancel").textContent = app.t("cancelReply");
|
|
6
7
|
app.replyTargetFor = (roomId) => targets.get(roomId);
|
|
7
8
|
app.setReplyTarget = (roomId, target) => {
|
|
8
9
|
if (validTarget(target)) targets.set(roomId, target); else targets.delete(roomId);
|
|
@@ -40,7 +41,7 @@ function bindMessageActions(app) {
|
|
|
40
41
|
if (copy) {
|
|
41
42
|
const actions = document.createElement("div"), reply = document.createElement("button");
|
|
42
43
|
actions.className = "message-actions";
|
|
43
|
-
reply.type = "button"; reply.className = "message-reply"; reply.
|
|
44
|
+
reply.type = "button"; reply.className = "message-reply"; reply.innerHTML = '<svg class="message-reply-icon" aria-hidden="true" viewBox="0 0 24 24"><path d="M9 10 4 15l5 5M4 15h9a7 7 0 0 0 7-7V5"/></svg>';
|
|
44
45
|
reply.setAttribute("aria-label", app.t("replyMessage")); reply.dataset.i18nAriaLabel = "replyMessage"; reply.dataset.replyMessage = message.id;
|
|
45
46
|
copy.before(actions); actions.append(copy, reply);
|
|
46
47
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { isOwnMessage } from "./messages.js";
|
|
2
|
+
import { setRoomPreview } from "./system-messages.js";
|
|
3
|
+
|
|
4
|
+
export async function watchRoom(roomId, app) {
|
|
5
|
+
const entry = app.state.rooms.get(roomId);
|
|
6
|
+
if (!entry || app.state.room?.id === roomId) return;
|
|
7
|
+
try {
|
|
8
|
+
const query = new URLSearchParams({ sessionID: app.LS.sessionID, roomId, excludeSelf: "0", timeout: "0" });
|
|
9
|
+
query.set("afterTs", String(entry.receivedTs || 0));
|
|
10
|
+
const result = await app.api(`/inbox?${query}`);
|
|
11
|
+
if (app.state.rooms.get(roomId) !== entry) return;
|
|
12
|
+
if (result.member === false) { await app.removeJoinedRoom(roomId, { reason: false }); return; }
|
|
13
|
+
if (result.member !== true || !Array.isArray(result.items) || app.state.room?.id === roomId) return;
|
|
14
|
+
const previousReceivedTs = entry.receivedTs || 0;
|
|
15
|
+
const sawSystem = result.items.some((message) => message.senderId === "system");
|
|
16
|
+
const before = new Set(entry.room.messages.map((message) => message.id));
|
|
17
|
+
for (const message of result.items) {
|
|
18
|
+
if (message.createdAt > entry.readTs && !isOwnMessage(message, app, entry.room, entry.name) && !entry.unreadIds.has(message.id)) entry.unreadIds.set(message.id, message);
|
|
19
|
+
if (message.answerToMessageID && entry.unreadIds.get(message.answerToMessageID)?.isQuestion) entry.unreadIds.delete(message.answerToMessageID);
|
|
20
|
+
if (!before.has(message.id)) entry.room.messages.push({ ...message, roomId });
|
|
21
|
+
if (message.createdAt >= entry.previewAt) setRoomPreview(entry, message);
|
|
22
|
+
}
|
|
23
|
+
entry.receivedTs = result.items.reduce((latest, message) => Number.isSafeInteger(message.createdAt) ? Math.max(latest, message.createdAt) : latest, previousReceivedTs);
|
|
24
|
+
entry.unreadOverflow = Boolean(result.hasMore);
|
|
25
|
+
if (sawSystem || result.hasMore) {
|
|
26
|
+
const latest = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
27
|
+
if (app.state.rooms.get(roomId) !== entry || app.state.room?.id === roomId) return;
|
|
28
|
+
entry.room.participants = latest.participants;
|
|
29
|
+
entry.room.messages = [...new Map([...entry.room.messages, ...latest.messages].map((message) => [message.id, message])).values()].sort((a, b) => a.createdAt - b.createdAt).slice(-500);
|
|
30
|
+
const newest = latest.messages.at(-1);
|
|
31
|
+
if (newest && newest.createdAt >= entry.previewAt) setRoomPreview(entry, newest);
|
|
32
|
+
// A full refresh pages past the inbox window; count what arrived since the last look.
|
|
33
|
+
for (const message of latest.messages) {
|
|
34
|
+
if (message.answeredByMessageID) entry.unreadIds.delete(message.id);
|
|
35
|
+
if (message.createdAt > previousReceivedTs && message.createdAt > entry.readTs && !isOwnMessage(message, app, entry.room, entry.name) && !entry.unreadIds.has(message.id)) entry.unreadIds.set(message.id, message);
|
|
36
|
+
}
|
|
37
|
+
if (newest && Number.isSafeInteger(newest.createdAt)) entry.receivedTs = Math.max(entry.receivedTs, newest.createdAt);
|
|
38
|
+
}
|
|
39
|
+
if (entry.room.messages.length > 500) entry.room.messages.splice(0, entry.room.messages.length - 500);
|
|
40
|
+
if (result.hasMore && entry.receivedTs > previousReceivedTs) await watchRoom(roomId, app);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error.status === 401) app.openChatSettings();
|
|
43
|
+
else if (error.status === 404) await app.removeJoinedRoom(roomId, { reason: false });
|
|
44
|
+
}
|
|
45
|
+
}
|