@alexsun-top/opencode-chat-room 0.8.3 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -1
- package/README.md +4 -4
- package/README.zh-CN.md +4 -4
- package/package.json +6 -3
- package/server/assets/chat/admin-badge.js +1 -1
- package/server/assets/chat/admin.js +12 -9
- package/server/assets/chat/announcements.js +9 -2
- package/server/assets/chat/attachment-store.js +15 -6
- package/server/assets/chat/attachments.js +46 -31
- package/server/assets/chat/chat-background.js +8 -14
- package/server/assets/chat/clipboard.js +2 -1
- package/server/assets/chat/confirm-dialog.js +3 -3
- package/server/assets/chat/copy-action.js +0 -1
- package/server/assets/chat/diagram-viewer.js +7 -9
- package/server/assets/chat/doc-preview.js +18 -11
- package/server/assets/chat/editor.js +80 -58
- package/server/assets/chat/events.js +43 -30
- package/server/assets/chat/files.js +41 -13
- package/server/assets/chat/history-loader.js +5 -3
- package/server/assets/chat/image-zoom.js +21 -17
- package/server/assets/chat/install-prompt.js +10 -9
- package/server/assets/chat/integration-account-schema.js +5 -3
- package/server/assets/chat/integration-editor.js +0 -2
- package/server/assets/chat/integration-values.js +17 -11
- package/server/assets/chat/integrations.js +137 -80
- package/server/assets/chat/join-requests.js +19 -12
- package/server/assets/chat/markdown-footnotes.js +0 -1
- package/server/assets/chat/markdown.js +43 -34
- package/server/assets/chat/members.js +45 -22
- package/server/assets/chat/mentions.js +31 -7
- package/server/assets/chat/message-cache.js +1 -1
- package/server/assets/chat/message-polling.js +38 -19
- package/server/assets/chat/message-window.js +9 -5
- package/server/assets/chat/messages.js +53 -43
- package/server/assets/chat/palette.js +40 -26
- package/server/assets/chat/panels.js +0 -1
- package/server/assets/chat/pwa.js +5 -4
- package/server/assets/chat/questions.js +22 -20
- package/server/assets/chat/recall.js +39 -21
- package/server/assets/chat/receipts.js +9 -7
- package/server/assets/chat/reminders.js +22 -8
- package/server/assets/chat/replies.js +28 -12
- package/server/assets/chat/room-list.js +2 -2
- package/server/assets/chat/room-watcher.js +8 -4
- package/server/assets/chat/rooms.js +44 -36
- package/server/assets/chat/search.js +8 -6
- package/server/assets/chat/settings.js +12 -11
- package/server/assets/chat/shiki.js +1 -1
- package/server/assets/chat/shortcuts.js +0 -2
- package/server/assets/chat/side-pane.js +3 -2
- package/server/assets/chat/state.js +35 -26
- package/server/assets/chat/system-events.js +4 -4
- package/server/assets/chat/task-form.js +5 -5
- package/server/assets/chat/task-row.js +1 -1
- package/server/assets/chat/tasks.js +37 -25
- package/server/chat.html +1 -0
|
@@ -11,7 +11,7 @@ const mentionPattern = /<at>@([^<]+)<\/at>/g;
|
|
|
11
11
|
const resourcePattern = /\[\[resource:([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})\]\]/gi;
|
|
12
12
|
const previewUrls = new Map();
|
|
13
13
|
const sanitizerTags = new Map();
|
|
14
|
-
let previewObserver;
|
|
14
|
+
let previewObserver = null;
|
|
15
15
|
// Raw HTML is not a formatting surface here: unknown tags must stay visible as
|
|
16
16
|
// text instead of disappearing inside the sanitizer. Known safe tags still pass
|
|
17
17
|
// through, and DOMPurify remains the final gate.
|
|
@@ -24,8 +24,9 @@ function preserveRawHtml(value) {
|
|
|
24
24
|
const SANITIZER_TAG_CACHE_MAX = 256;
|
|
25
25
|
function sanitizerKeepsTag(name) {
|
|
26
26
|
const key = name.toLowerCase();
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
const kept = sanitizerTags.get(key);
|
|
28
|
+
if (kept !== undefined)
|
|
29
|
+
return kept;
|
|
29
30
|
// A message with endless distinct tag names must not probe DOMPurify forever;
|
|
30
31
|
// past the cap, unknown tags simply stay literal text.
|
|
31
32
|
if (sanitizerTags.size >= SANITIZER_TAG_CACHE_MAX)
|
|
@@ -36,7 +37,7 @@ function sanitizerKeepsTag(name) {
|
|
|
36
37
|
return keeps;
|
|
37
38
|
}
|
|
38
39
|
function escapeHtmlText(value) {
|
|
39
|
-
return String(value).replace(/[&<>"']/g, (char) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }
|
|
40
|
+
return String(value).replace(/[&<>"']/g, (char) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[char]));
|
|
40
41
|
}
|
|
41
42
|
/**
|
|
42
43
|
* Render plain text that may contain resource markers into attachment cards.
|
|
@@ -45,7 +46,7 @@ function escapeHtmlText(value) {
|
|
|
45
46
|
export function renderResourceText(root, text, app) {
|
|
46
47
|
root.textContent = text;
|
|
47
48
|
for (const resource of replaceResourceMarkers(root, app)) {
|
|
48
|
-
void loadResourceCard(resource.card, resource.roomId, app);
|
|
49
|
+
void loadResourceCard(resource.card, resource.roomId ?? "", app);
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
export function renderMarkdown(root, text, app) {
|
|
@@ -75,7 +76,7 @@ export function renderMarkdown(root, text, app) {
|
|
|
75
76
|
const resources = replaceResourceMarkers(fragment, app);
|
|
76
77
|
const diagramNodes = [...fragment.querySelectorAll("[data-chat-mermaid]")];
|
|
77
78
|
for (const node of diagramNodes) {
|
|
78
|
-
const source = diagrams.get(node.dataset.chatMermaid);
|
|
79
|
+
const source = diagrams.get(node.dataset.chatMermaid ?? "");
|
|
79
80
|
node.removeAttribute("data-chat-mermaid");
|
|
80
81
|
node.className = "mermaid-diagram";
|
|
81
82
|
if (source !== undefined) {
|
|
@@ -101,7 +102,7 @@ export function renderMarkdown(root, text, app) {
|
|
|
101
102
|
void renderDiagram(diagramNodes[i], source, app);
|
|
102
103
|
}
|
|
103
104
|
for (const resource of resources)
|
|
104
|
-
void loadResourceCard(resource.card, resource.roomId, app);
|
|
105
|
+
void loadResourceCard(resource.card, resource.roomId ?? "", app);
|
|
105
106
|
}
|
|
106
107
|
// A narrow screen scrolls the table inside a frame instead of widening the message.
|
|
107
108
|
function wrapTables(fragment) {
|
|
@@ -184,7 +185,7 @@ const wrapIcon = () => iconNode("text_wrap", "code-tool-icon code-wrap-icon");
|
|
|
184
185
|
const copyIcon = () => iconNode("copy", "code-tool-icon code-copy-icon");
|
|
185
186
|
const checkIcon = () => iconNode("checkmark", "code-tool-icon code-check-icon");
|
|
186
187
|
async function copyCode(button, code, app) {
|
|
187
|
-
const copied = await copyTextToClipboard(code.textContent);
|
|
188
|
+
const copied = await copyTextToClipboard(code.textContent ?? "");
|
|
188
189
|
clearTimeout(button._feedbackTimer);
|
|
189
190
|
button.classList.toggle("is-copied", copied);
|
|
190
191
|
button.classList.toggle("is-copy-failed", !copied);
|
|
@@ -247,14 +248,14 @@ function replaceResourceMarkers(fragment, app) {
|
|
|
247
248
|
}
|
|
248
249
|
if (last) {
|
|
249
250
|
replacement.append(document.createTextNode(value.slice(last)));
|
|
250
|
-
node.
|
|
251
|
+
node.parentNode?.replaceChild(replacement, node);
|
|
251
252
|
}
|
|
252
253
|
}
|
|
253
254
|
return cards;
|
|
254
255
|
}
|
|
255
256
|
async function loadResourceCard(card, roomId, app) {
|
|
256
257
|
try {
|
|
257
|
-
const id = card.dataset.resourceId;
|
|
258
|
+
const id = card.dataset.resourceId ?? "";
|
|
258
259
|
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}`);
|
|
259
260
|
if (!card.isConnected)
|
|
260
261
|
return;
|
|
@@ -294,7 +295,7 @@ async function loadResourceCard(card, roomId, app) {
|
|
|
294
295
|
}
|
|
295
296
|
catch (error) {
|
|
296
297
|
if (card.isConnected)
|
|
297
|
-
setResourceError(card, app, error
|
|
298
|
+
setResourceError(card, app, error instanceof Error && error.status === 404 ? "resourceDeleted" : "resourceFailed");
|
|
298
299
|
}
|
|
299
300
|
}
|
|
300
301
|
function setResourceButton(button, key, name, app) {
|
|
@@ -467,14 +468,16 @@ async function previewResource(card, roomId, id, resource, type, button, app, st
|
|
|
467
468
|
if (!card.isConnected || button.disabled)
|
|
468
469
|
return;
|
|
469
470
|
button.disabled = true;
|
|
470
|
-
|
|
471
|
+
const current = card._previewElement;
|
|
472
|
+
if (current) {
|
|
471
473
|
try {
|
|
472
474
|
await app.preserveReadingLayout(() => {
|
|
473
|
-
|
|
475
|
+
current.remove();
|
|
474
476
|
setResourceButton(button, "resourcePreview", resource.name, app);
|
|
475
477
|
button.disabled = false;
|
|
476
478
|
});
|
|
477
|
-
|
|
479
|
+
if (card._resourceUrl)
|
|
480
|
+
URL.revokeObjectURL(card._resourceUrl);
|
|
478
481
|
previewUrls.delete(card);
|
|
479
482
|
delete card._resourceUrl;
|
|
480
483
|
delete card._previewElement;
|
|
@@ -490,7 +493,8 @@ async function previewResource(card, roomId, id, resource, type, button, app, st
|
|
|
490
493
|
return;
|
|
491
494
|
}
|
|
492
495
|
status.textContent = app.t("resourceLoading");
|
|
493
|
-
let url
|
|
496
|
+
let url;
|
|
497
|
+
let media;
|
|
494
498
|
const fail = () => {
|
|
495
499
|
media?.remove();
|
|
496
500
|
media = undefined;
|
|
@@ -507,23 +511,17 @@ async function previewResource(card, roomId, id, resource, type, button, app, st
|
|
|
507
511
|
const blob = await app.apiBlob(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}/content`);
|
|
508
512
|
if (!card.isConnected)
|
|
509
513
|
return;
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
media =
|
|
513
|
-
const ready = waitForMediaSize(
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
else {
|
|
517
|
-
media.controls = true;
|
|
518
|
-
media.preload = "metadata";
|
|
519
|
-
}
|
|
520
|
-
media.addEventListener("error", fail, { once: true });
|
|
521
|
-
media.src = url;
|
|
514
|
+
const objectUrl = URL.createObjectURL(blob), kind = previewKind(type), element = createPreviewMedia(kind, resource.name);
|
|
515
|
+
url = objectUrl;
|
|
516
|
+
media = element;
|
|
517
|
+
const ready = waitForMediaSize(element, kind);
|
|
518
|
+
element.addEventListener("error", fail, { once: true });
|
|
519
|
+
element.src = objectUrl;
|
|
522
520
|
// Update the control inside the layout step: a stalled animation frame must not
|
|
523
521
|
// leave the preview open with a disabled button.
|
|
524
522
|
await app.preserveReadingLayout(async () => {
|
|
525
523
|
status.textContent = "";
|
|
526
|
-
card.append(
|
|
524
|
+
card.append(element);
|
|
527
525
|
setResourceButton(button, "resourceHide", resource.name, app);
|
|
528
526
|
button.disabled = false;
|
|
529
527
|
await ready;
|
|
@@ -532,9 +530,9 @@ async function previewResource(card, roomId, id, resource, type, button, app, st
|
|
|
532
530
|
fail();
|
|
533
531
|
return;
|
|
534
532
|
}
|
|
535
|
-
card._resourceUrl =
|
|
536
|
-
card._previewElement =
|
|
537
|
-
previewUrls.set(card,
|
|
533
|
+
card._resourceUrl = objectUrl;
|
|
534
|
+
card._previewElement = element;
|
|
535
|
+
previewUrls.set(card, objectUrl);
|
|
538
536
|
watchPreviewRemoval();
|
|
539
537
|
}
|
|
540
538
|
catch {
|
|
@@ -545,11 +543,22 @@ async function previewResource(card, roomId, id, resource, type, button, app, st
|
|
|
545
543
|
button.disabled = false;
|
|
546
544
|
}
|
|
547
545
|
}
|
|
546
|
+
function createPreviewMedia(kind, name) {
|
|
547
|
+
if (kind === "img") {
|
|
548
|
+
const image = document.createElement("img");
|
|
549
|
+
image.alt = name;
|
|
550
|
+
return image;
|
|
551
|
+
}
|
|
552
|
+
const element = document.createElement(kind === "audio" ? "audio" : "video");
|
|
553
|
+
element.controls = true;
|
|
554
|
+
element.preload = "metadata";
|
|
555
|
+
return element;
|
|
556
|
+
}
|
|
548
557
|
function waitForMediaSize(media, kind) {
|
|
549
558
|
const event = kind === "img" ? "load" : "loadedmetadata";
|
|
550
|
-
if (
|
|
559
|
+
if (media instanceof HTMLImageElement && media.complete && media.naturalWidth)
|
|
551
560
|
return Promise.resolve();
|
|
552
|
-
if (
|
|
561
|
+
if (media instanceof HTMLMediaElement && media.readyState >= 1)
|
|
553
562
|
return Promise.resolve();
|
|
554
563
|
return new Promise((resolve, reject) => {
|
|
555
564
|
media.addEventListener(event, resolve, { once: true });
|
|
@@ -627,6 +636,6 @@ function replaceMentionTokens(fragment, mentions, app) {
|
|
|
627
636
|
chip.textContent = `@${mention.name === "all" ? app.t("allMembers") : mention.name}`;
|
|
628
637
|
replacement.appendChild(chip);
|
|
629
638
|
}
|
|
630
|
-
node.
|
|
639
|
+
node.parentNode?.replaceChild(replacement, node);
|
|
631
640
|
}
|
|
632
641
|
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { LS } from "./state.js";
|
|
2
2
|
import { canRemoveMember, memberRoleBadge } from "./admin-badge.js";
|
|
3
|
+
const errorStatus = (error) => error instanceof Error && "status" in error && typeof error.status === "number" ? error.status : undefined;
|
|
4
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
5
|
+
const activityState = (member) => {
|
|
6
|
+
const activity = member.activity;
|
|
7
|
+
if (typeof activity === "object" && activity !== null && "state" in activity && typeof activity.state === "string")
|
|
8
|
+
return activity.state || "unknown";
|
|
9
|
+
return "unknown";
|
|
10
|
+
};
|
|
3
11
|
export function initMembers(app) {
|
|
4
12
|
const $ = app.$;
|
|
5
13
|
app.mentionNames = () => {
|
|
@@ -11,26 +19,35 @@ export function initMembers(app) {
|
|
|
11
19
|
app.mentionExists = (name) => name === "all" || name === app.state.myName || app.mentionNames().has(name);
|
|
12
20
|
app.renderMembers = () => {
|
|
13
21
|
const people = app.state.room?.participants || [];
|
|
14
|
-
|
|
22
|
+
const membersLabel = $("#btnMembers .action-label");
|
|
23
|
+
if (membersLabel)
|
|
24
|
+
app.setText(membersLabel, "membersCount", { count: people.length });
|
|
15
25
|
const list = $("#memberList");
|
|
26
|
+
if (!list)
|
|
27
|
+
return;
|
|
16
28
|
list.textContent = "";
|
|
17
29
|
for (const person of people) {
|
|
18
30
|
const row = document.createElement("li"), name = document.createElement("span"), time = document.createElement("span");
|
|
19
31
|
name.textContent = person.name;
|
|
20
32
|
time.className = "m-time";
|
|
21
|
-
time.textContent = app.fmtTime(person.joinedAt);
|
|
33
|
+
time.textContent = app.fmtTime(person.joinedAt ?? 0);
|
|
22
34
|
row.append(name, time);
|
|
23
35
|
list.append(row);
|
|
24
36
|
}
|
|
25
|
-
$("#memberEmpty")
|
|
37
|
+
const memberEmpty = $("#memberEmpty");
|
|
38
|
+
if (memberEmpty)
|
|
39
|
+
memberEmpty.hidden = people.length > 0;
|
|
26
40
|
if (app.mentionActive)
|
|
27
|
-
app.updateMentionList();
|
|
41
|
+
app.updateMentionList?.();
|
|
28
42
|
void app.loadManageableMembers();
|
|
29
43
|
};
|
|
30
44
|
app.loadManageableMembers = async () => {
|
|
31
45
|
const room = app.state.room;
|
|
32
46
|
if (!room || app.state.memberApiUnavailable.has(room.id))
|
|
33
47
|
return;
|
|
48
|
+
const list = $("#memberList");
|
|
49
|
+
if (!list)
|
|
50
|
+
return;
|
|
34
51
|
const generation = app.state.pollGeneration;
|
|
35
52
|
clearTimeout(app.state.memberRefreshTimer);
|
|
36
53
|
try {
|
|
@@ -42,10 +59,9 @@ export function initMembers(app) {
|
|
|
42
59
|
const changed = signature(previous) !== signature(members);
|
|
43
60
|
app.state.memberSessions.set(room.id, members);
|
|
44
61
|
if (changed && app.mentionActive)
|
|
45
|
-
app.updateMentionList();
|
|
62
|
+
app.updateMentionList?.();
|
|
46
63
|
if (changed && room.kind !== "direct" && room.messages.some((message) => !message.senderSessionID && message.senderId === app.state.myName))
|
|
47
64
|
app.rerenderMessages();
|
|
48
|
-
const list = $("#memberList");
|
|
49
65
|
const rows = Array.from(list.querySelectorAll("li[data-member-session-id]"));
|
|
50
66
|
const actionKey = `${room.ownerId}\u0000${(room.adminIds ?? []).join("\u0000")}`;
|
|
51
67
|
const actionsChanged = list.dataset.actionKey !== actionKey;
|
|
@@ -81,25 +97,28 @@ export function initMembers(app) {
|
|
|
81
97
|
else
|
|
82
98
|
for (const member of members)
|
|
83
99
|
updateActivity(list.querySelector(`[data-member-session-id="${CSS.escape(member.sessionID)}"]`), member, app);
|
|
84
|
-
$("#memberEmpty")
|
|
100
|
+
const memberEmpty = $("#memberEmpty");
|
|
101
|
+
if (memberEmpty)
|
|
102
|
+
memberEmpty.hidden = members.length > 0;
|
|
85
103
|
void app.refreshJoinRequests?.();
|
|
86
|
-
app.queueReceiptObservation();
|
|
104
|
+
app.queueReceiptObservation?.();
|
|
87
105
|
app.state.memberRefreshTimer = setTimeout(() => {
|
|
88
106
|
if (current(app, room.id, generation) && !document.hidden)
|
|
89
107
|
void app.loadManageableMembers();
|
|
90
108
|
}, 5000);
|
|
91
109
|
}
|
|
92
110
|
catch (error) {
|
|
93
|
-
|
|
111
|
+
const status = errorStatus(error);
|
|
112
|
+
if (status === 401)
|
|
94
113
|
app.openChatSettings();
|
|
95
|
-
else if (
|
|
114
|
+
else if (status === 404)
|
|
96
115
|
app.state.memberApiUnavailable.add(room.id);
|
|
97
116
|
else if (current(app, room.id, generation) && !document.hidden)
|
|
98
117
|
app.state.memberRefreshTimer = setTimeout(() => void app.loadManageableMembers(), 5000);
|
|
99
118
|
}
|
|
100
119
|
};
|
|
101
120
|
app.loadVisibleReceipts = async (roomId, generation = app.state.pollGeneration) => {
|
|
102
|
-
if (app.state.receiptApiUnavailable.has(roomId) || !current(app, roomId, generation))
|
|
121
|
+
if (!roomId || app.state.receiptApiUnavailable.has(roomId) || !current(app, roomId, generation))
|
|
103
122
|
return;
|
|
104
123
|
const ids = visibleMessageIDs(app);
|
|
105
124
|
if (!ids.length)
|
|
@@ -107,12 +126,12 @@ export function initMembers(app) {
|
|
|
107
126
|
const query = new URLSearchParams({ messageIDs: ids.join(",") });
|
|
108
127
|
try {
|
|
109
128
|
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/receipts?${query}`);
|
|
110
|
-
if (!current(app, roomId, generation))
|
|
129
|
+
if (!current(app, roomId, generation) || !isRecord(result))
|
|
111
130
|
return;
|
|
112
131
|
app.renderReceiptData(roomId, result.receipts || []);
|
|
113
132
|
}
|
|
114
133
|
catch (error) {
|
|
115
|
-
if (error
|
|
134
|
+
if (errorStatus(error) === 404)
|
|
116
135
|
app.state.receiptApiUnavailable.add(roomId);
|
|
117
136
|
}
|
|
118
137
|
};
|
|
@@ -157,11 +176,12 @@ export function initMembers(app) {
|
|
|
157
176
|
try {
|
|
158
177
|
const result = await app.api(`/rooms/${encodeURIComponent(room.id)}/receipts`, { method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, membershipID: self.membershipID, messageIDs: ids, evidence: "displayed" }) });
|
|
159
178
|
if (current(app, room.id, generation) && app.state.memberSessions.get(room.id)?.some((member) => member.sessionID === LS.sessionID && member.membershipID === self.membershipID)) {
|
|
160
|
-
|
|
179
|
+
const accepted = isRecord(result) && Array.isArray(result.acceptedIDs) ? result.acceptedIDs : [];
|
|
180
|
+
acknowledgedMessages(app, key, acknowledged, accepted);
|
|
161
181
|
}
|
|
162
182
|
}
|
|
163
183
|
catch (error) {
|
|
164
|
-
if (error
|
|
184
|
+
if (errorStatus(error) === 404)
|
|
165
185
|
app.state.receiptApiUnavailable.add(room.id);
|
|
166
186
|
}
|
|
167
187
|
finally {
|
|
@@ -181,21 +201,24 @@ export function initMembers(app) {
|
|
|
181
201
|
app.enterRoom(room, name, intent);
|
|
182
202
|
}
|
|
183
203
|
catch (error) {
|
|
184
|
-
app.showSendError("sendFailed", { message: error.message });
|
|
204
|
+
app.showSendError("sendFailed", { message: error instanceof Error ? error.message : "" });
|
|
185
205
|
}
|
|
186
206
|
};
|
|
187
207
|
app.kickMember = async (member) => {
|
|
188
208
|
if (!(await app.confirmAction(app.t("kickConfirm", { name: member.name }))))
|
|
189
209
|
return;
|
|
210
|
+
const room = app.state.room;
|
|
211
|
+
if (!room)
|
|
212
|
+
return;
|
|
190
213
|
try {
|
|
191
|
-
await app.api(`/rooms/${encodeURIComponent(
|
|
214
|
+
await app.api(`/rooms/${encodeURIComponent(room.id)}/kick`, {
|
|
192
215
|
method: "POST",
|
|
193
216
|
body: JSON.stringify({ sessionID: LS.sessionID, targetSessionID: member.sessionID }),
|
|
194
217
|
});
|
|
195
218
|
await app.refreshRoomMeta();
|
|
196
219
|
}
|
|
197
220
|
catch (error) {
|
|
198
|
-
app.showSendError("sendFailed", { message: error.message });
|
|
221
|
+
app.showSendError("sendFailed", { message: error instanceof Error ? error.message : "" });
|
|
199
222
|
}
|
|
200
223
|
};
|
|
201
224
|
function actionButton(member, label, action) {
|
|
@@ -209,11 +232,11 @@ export function initMembers(app) {
|
|
|
209
232
|
}
|
|
210
233
|
}
|
|
211
234
|
function current(app, roomId, generation) {
|
|
212
|
-
return app.state.room?.id === roomId && app.state.pollGeneration === generation &&
|
|
235
|
+
return app.state.room?.id === roomId && app.state.pollGeneration === generation && app.$("#chatView")?.hidden === false;
|
|
213
236
|
}
|
|
214
237
|
function visibleMessageIDs(app) {
|
|
215
238
|
const thread = app.$("#thread");
|
|
216
|
-
if (document.hidden || !document.hasFocus() || app.$("#chatView")
|
|
239
|
+
if (!thread || document.hidden || !document.hasFocus() || app.$("#chatView")?.hidden !== false)
|
|
217
240
|
return [];
|
|
218
241
|
const top = thread.getBoundingClientRect().top + thread.clientTop, bottom = top + thread.clientHeight;
|
|
219
242
|
const overlays = Array.from(document.querySelectorAll(".panel:not([hidden]), #mentionPicker:not([hidden]), #railRoomForm:not([hidden])"))
|
|
@@ -221,7 +244,7 @@ function visibleMessageIDs(app) {
|
|
|
221
244
|
return Array.from(thread.querySelectorAll("[data-msg-id]")).filter((row) => {
|
|
222
245
|
const rect = row.getBoundingClientRect();
|
|
223
246
|
return rect.bottom > top && rect.top < bottom && !overlays.some((overlay) => rect.left < overlay.right && rect.right > overlay.left && rect.top < overlay.bottom && rect.bottom > overlay.top);
|
|
224
|
-
}).map((row) => row.dataset.msgId).filter((id) =>
|
|
247
|
+
}).map((row) => row.dataset.msgId).filter((id) => typeof id === "string" && !id.startsWith("pending-"));
|
|
225
248
|
}
|
|
226
249
|
function acknowledgedMessages(app, key, set, ids) {
|
|
227
250
|
ids.forEach((id) => set.add(id));
|
|
@@ -247,7 +270,7 @@ function updateActivity(row, member, app) {
|
|
|
247
270
|
updateActivityBadge(badge, member, app);
|
|
248
271
|
}
|
|
249
272
|
function updateActivityBadge(badge, member, app) {
|
|
250
|
-
const state = member
|
|
273
|
+
const state = activityState(member);
|
|
251
274
|
badge.className = `member-activity ${state}`;
|
|
252
275
|
badge.textContent = app.t(`activity_${state}`);
|
|
253
276
|
badge.title = app.t(`activityHint_${state}`);
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { editorSelectionOffsets, replaceEditorRange } from "./editor.js";
|
|
2
|
+
// The composer keeps its text in a non-standard "value" property patched onto
|
|
3
|
+
// each contenteditable editor, so read it through a guard instead of HTMLElement.
|
|
4
|
+
function editorValue(editor) {
|
|
5
|
+
const value = Reflect.get(editor, "value");
|
|
6
|
+
return typeof value === "string" ? value : "";
|
|
7
|
+
}
|
|
2
8
|
export function initMentions(app) {
|
|
3
9
|
const $ = app.$;
|
|
4
10
|
const mention = { active: false, start: 0, query: "", items: [], index: 0, editor: null };
|
|
@@ -9,6 +15,8 @@ export function initMentions(app) {
|
|
|
9
15
|
mention.items = filterNames(query, app.mentionNames());
|
|
10
16
|
mention.index = mention.items.length ? 0 : -1;
|
|
11
17
|
const picker = $("#mentionPicker");
|
|
18
|
+
if (!picker)
|
|
19
|
+
return;
|
|
12
20
|
picker.textContent = "";
|
|
13
21
|
const list = document.createElement("ul");
|
|
14
22
|
mention.items.forEach((name, index) => list.append(createOption(name, index, query, app)));
|
|
@@ -26,11 +34,15 @@ export function initMentions(app) {
|
|
|
26
34
|
highlightMention(app, mention);
|
|
27
35
|
};
|
|
28
36
|
app.openMention = (start, query, input = $("#msgInput")) => {
|
|
37
|
+
if (!input)
|
|
38
|
+
return;
|
|
29
39
|
mention.active = app.mentionActive = true;
|
|
30
40
|
mention.editor = input;
|
|
31
41
|
mention.start = start;
|
|
32
|
-
app.updateMentionList(query);
|
|
42
|
+
app.updateMentionList?.(query);
|
|
33
43
|
const picker = $("#mentionPicker");
|
|
44
|
+
if (!picker)
|
|
45
|
+
return;
|
|
34
46
|
picker.hidden = false;
|
|
35
47
|
if (input.id !== "msgInput") {
|
|
36
48
|
const rect = input.getBoundingClientRect();
|
|
@@ -45,17 +57,21 @@ export function initMentions(app) {
|
|
|
45
57
|
mention.editor?.removeAttribute("aria-activedescendant");
|
|
46
58
|
mention.editor = null;
|
|
47
59
|
const picker = $("#mentionPicker");
|
|
60
|
+
if (!picker)
|
|
61
|
+
return;
|
|
48
62
|
picker.hidden = true;
|
|
49
63
|
picker.removeAttribute("style");
|
|
50
64
|
};
|
|
51
65
|
app.recomputeMention = (input = $("#msgInput")) => {
|
|
52
66
|
if (!(input instanceof HTMLElement))
|
|
53
67
|
input = $("#msgInput");
|
|
68
|
+
if (!input)
|
|
69
|
+
return;
|
|
54
70
|
if (mention.active && mention.editor !== input)
|
|
55
71
|
app.closeMention();
|
|
56
72
|
const match = currentMention(input);
|
|
57
73
|
if (match) {
|
|
58
|
-
mention.active ? app.updateMentionList(match.query) : app.openMention(match.start, match.query, input);
|
|
74
|
+
mention.active ? app.updateMentionList?.(match.query) : app.openMention(match.start, match.query, input);
|
|
59
75
|
}
|
|
60
76
|
else if (mention.active)
|
|
61
77
|
app.closeMention();
|
|
@@ -91,14 +107,19 @@ export function initMentions(app) {
|
|
|
91
107
|
return false;
|
|
92
108
|
};
|
|
93
109
|
app.insertMention = (name) => {
|
|
94
|
-
const input = mention.editor || $("#msgInput")
|
|
95
|
-
if (input
|
|
110
|
+
const input = mention.editor || $("#msgInput");
|
|
111
|
+
if (!input)
|
|
112
|
+
return;
|
|
113
|
+
const end = mention.start + 1 + mention.query.length;
|
|
114
|
+
if (editorValue(input).slice(mention.start, end) !== `@${mention.query}`)
|
|
96
115
|
return app.closeMention();
|
|
97
116
|
replaceEditorRange(input, mention.start, end, `<at>@${name}</at>`);
|
|
98
117
|
app.closeMention();
|
|
99
118
|
// Chrome can reset the selection while focus() lands; restore the caret last.
|
|
100
119
|
input.focus({ preventScroll: true });
|
|
101
|
-
|
|
120
|
+
const setCaret = Reflect.get(input, "setCaret");
|
|
121
|
+
if (typeof setCaret === "function")
|
|
122
|
+
setCaret.call(input, mention.start + `<at>@${name}</at>`.length);
|
|
102
123
|
input.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertText" }));
|
|
103
124
|
};
|
|
104
125
|
}
|
|
@@ -140,7 +161,7 @@ function createOption(name, index, query, app) {
|
|
|
140
161
|
return option;
|
|
141
162
|
}
|
|
142
163
|
function currentMention(input) {
|
|
143
|
-
const [position, end] = editorSelectionOffsets(input), value = input
|
|
164
|
+
const [position, end] = editorSelectionOffsets(input), value = editorValue(input);
|
|
144
165
|
if (position !== end)
|
|
145
166
|
return null;
|
|
146
167
|
let start = position;
|
|
@@ -153,7 +174,10 @@ function currentMention(input) {
|
|
|
153
174
|
return null;
|
|
154
175
|
}
|
|
155
176
|
function highlightMention(app, mention) {
|
|
156
|
-
const input = mention.editor || app.$("#msgInput"),
|
|
177
|
+
const input = mention.editor || app.$("#msgInput"), picker = app.$("#mentionPicker");
|
|
178
|
+
if (!input || !picker)
|
|
179
|
+
return;
|
|
180
|
+
const options = picker.querySelectorAll(".mention-opt");
|
|
157
181
|
options.forEach((option, index) => {
|
|
158
182
|
option.classList.toggle("active", index === mention.index);
|
|
159
183
|
option.setAttribute("aria-selected", String(index === mention.index));
|
|
@@ -24,7 +24,7 @@ async function withStore(mode, run) {
|
|
|
24
24
|
return new Promise((resolve, reject) => {
|
|
25
25
|
const transaction = db.transaction(STORE, mode);
|
|
26
26
|
const request = run(transaction.objectStore(STORE));
|
|
27
|
-
transaction.oncomplete = () => resolve(request
|
|
27
|
+
transaction.oncomplete = () => resolve(request === undefined ? undefined : request.result);
|
|
28
28
|
transaction.onerror = () => reject(transaction.error);
|
|
29
29
|
transaction.onabort = () => reject(transaction.error);
|
|
30
30
|
});
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { LS, LOADED_MESSAGE_LIMIT } from "./state.js";
|
|
2
2
|
import { setRoomPreview } from "./system-messages.js";
|
|
3
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
4
|
+
const errorStatus = (error) => error instanceof Error && "status" in error && typeof error.status === "number" ? error.status : undefined;
|
|
5
|
+
const errorNetwork = (error) => error instanceof Error && "network" in error && error.network === true;
|
|
3
6
|
// Inbox polling, room-meta refresh, and reconciliation live apart from rendering.
|
|
4
7
|
export function initMessagePolling(app, isUnreadMessage) {
|
|
5
8
|
const $ = app.$;
|
|
@@ -55,15 +58,16 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
55
58
|
stopRecovery();
|
|
56
59
|
return;
|
|
57
60
|
}
|
|
58
|
-
|
|
61
|
+
const status = errorStatus(error);
|
|
62
|
+
if (status === 404) {
|
|
59
63
|
stopRecovery();
|
|
60
64
|
void app.removeJoinedRoom(roomId, { reason: true });
|
|
61
65
|
return;
|
|
62
66
|
}
|
|
63
|
-
if (
|
|
67
|
+
if (status === 401) {
|
|
64
68
|
stopRecovery();
|
|
65
69
|
app.setOnline(false);
|
|
66
|
-
if (!app.settingsOpen())
|
|
70
|
+
if (!app.settingsOpen?.())
|
|
67
71
|
app.openChatSettings();
|
|
68
72
|
return;
|
|
69
73
|
}
|
|
@@ -128,7 +132,7 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
128
132
|
clearTimeout(timer);
|
|
129
133
|
signal.removeEventListener("abort", abort);
|
|
130
134
|
}
|
|
131
|
-
if (!isCurrentPoll(roomId, generation))
|
|
135
|
+
if (!isCurrentPoll(roomId, generation) || !isRecord(data))
|
|
132
136
|
return;
|
|
133
137
|
if (data.member === false) {
|
|
134
138
|
void app.removeJoinedRoom(roomId, { reason: true });
|
|
@@ -136,7 +140,10 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
136
140
|
}
|
|
137
141
|
if (data.member !== true || !Array.isArray(data.items))
|
|
138
142
|
return;
|
|
139
|
-
const
|
|
143
|
+
const active = app.state.room;
|
|
144
|
+
if (!active)
|
|
145
|
+
return;
|
|
146
|
+
const items = data.items, previousReceivedTs = entry?.receivedTs || 0, messages = active.messages;
|
|
140
147
|
const known = new Set(messages.map((message) => message.id));
|
|
141
148
|
const previousUnreadOverflow = Boolean(entry?.unreadOverflow);
|
|
142
149
|
let sawSystem = false, sawQuestion = false, added = 0, unreadChanged = false;
|
|
@@ -176,7 +183,7 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
176
183
|
if (entry)
|
|
177
184
|
entry.unreadOverflow = Boolean(data.hasMore);
|
|
178
185
|
if (messages.length > LOADED_MESSAGE_LIMIT) {
|
|
179
|
-
const
|
|
186
|
+
const position = app.state.positions.get(roomId), anchorId = typeof position === "object" ? position?.messageId : undefined, anchor = anchorId && messages.find((item) => item.id === anchorId);
|
|
180
187
|
messages.splice(0, messages.length - LOADED_MESSAGE_LIMIT);
|
|
181
188
|
if (anchor && !messages.includes(anchor))
|
|
182
189
|
messages.unshift(anchor);
|
|
@@ -195,11 +202,14 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
195
202
|
}
|
|
196
203
|
if (rendered.skipped === 0)
|
|
197
204
|
void app.markActiveRoomRead();
|
|
198
|
-
|
|
199
|
-
|
|
205
|
+
const roomList = $("#roomList"), roomListChat = $("#roomListChat");
|
|
206
|
+
if (roomList)
|
|
207
|
+
app.renderRoomOptions(roomList, app.state.availableRooms || []);
|
|
208
|
+
if (roomListChat)
|
|
209
|
+
app.renderRoomOptions(roomListChat, app.state.availableRooms || []);
|
|
200
210
|
if (!app.lastReconcileAt || Date.now() - app.lastReconcileAt > 120000) {
|
|
201
211
|
app.lastReconcileAt = Date.now();
|
|
202
|
-
void app.reconcileRoom(roomId, generation);
|
|
212
|
+
void app.reconcileRoom?.(roomId, generation);
|
|
203
213
|
}
|
|
204
214
|
app.setOnline(true);
|
|
205
215
|
stopRecovery();
|
|
@@ -208,20 +218,22 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
208
218
|
catch (error) {
|
|
209
219
|
if (!isCurrentPoll(roomId, generation))
|
|
210
220
|
return;
|
|
211
|
-
|
|
221
|
+
const status = errorStatus(error);
|
|
222
|
+
if (status === 401) {
|
|
212
223
|
stopRecovery();
|
|
213
224
|
app.setOnline(false);
|
|
214
|
-
if (!app.settingsOpen())
|
|
225
|
+
if (!app.settingsOpen?.())
|
|
215
226
|
app.openChatSettings();
|
|
216
227
|
pollDelay = Math.min(pollDelay * 2, 30000);
|
|
217
228
|
}
|
|
218
|
-
else if (
|
|
229
|
+
else if (status === 404) {
|
|
219
230
|
void app.removeJoinedRoom(roomId, { reason: true });
|
|
220
231
|
}
|
|
221
232
|
else {
|
|
222
233
|
app.setOnline(false);
|
|
223
234
|
pollDelay = 2000;
|
|
224
|
-
|
|
235
|
+
const name = error instanceof Error ? error.name : "";
|
|
236
|
+
if (errorNetwork(error) || name === "AbortError" || name === "TimeoutError")
|
|
225
237
|
scheduleRecovery();
|
|
226
238
|
}
|
|
227
239
|
}
|
|
@@ -233,13 +245,16 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
233
245
|
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
234
246
|
if (!isCurrentPoll(roomId, generation))
|
|
235
247
|
return;
|
|
248
|
+
const active = app.state.room;
|
|
249
|
+
if (!active)
|
|
250
|
+
return;
|
|
236
251
|
app.setOnline(true);
|
|
237
|
-
const before = JSON.stringify([
|
|
252
|
+
const before = JSON.stringify([active.ownerId, active.adminIds ?? [], active.successorId ?? null, active.adminSessionID ?? null]);
|
|
238
253
|
const openQuestions = Number(room.openQuestions);
|
|
239
|
-
Object.assign(
|
|
254
|
+
Object.assign(active, { participants: room.participants, ownerId: room.ownerId ?? active.ownerId, adminIds: room.adminIds ?? [], successorId: room.successorId ?? null, adminSessionID: room.adminSessionID ?? null, openQuestions: Number.isFinite(openQuestions) ? Math.max(0, openQuestions) : active.openQuestions });
|
|
240
255
|
if (Number.isFinite(openQuestions))
|
|
241
256
|
app.updateQuestionBadge?.(openQuestions);
|
|
242
|
-
if (JSON.stringify([
|
|
257
|
+
if (JSON.stringify([active.ownerId, active.adminIds ?? [], active.successorId ?? null, active.adminSessionID ?? null]) !== before) {
|
|
243
258
|
app.renderHead();
|
|
244
259
|
app.rerenderMessages();
|
|
245
260
|
}
|
|
@@ -253,15 +268,19 @@ export function initMessagePolling(app, isUnreadMessage) {
|
|
|
253
268
|
const room = await app.api(`/rooms/${encodeURIComponent(roomId)}`);
|
|
254
269
|
if (!isCurrentPoll(roomId, generation))
|
|
255
270
|
return;
|
|
271
|
+
const active = app.state.room;
|
|
272
|
+
if (!active)
|
|
273
|
+
return;
|
|
256
274
|
app.setOnline(true);
|
|
257
275
|
if (entry && Number.isFinite(Number(room.recallVersion)))
|
|
258
276
|
entry.recallVersion = Number(room.recallVersion);
|
|
259
|
-
const current =
|
|
277
|
+
const current = active.messages;
|
|
278
|
+
const fetched = room.messages ?? [];
|
|
260
279
|
const ids = new Set(current.map((message) => message.id));
|
|
261
|
-
const missing =
|
|
280
|
+
const missing = fetched.filter((message) => !ids.has(message.id));
|
|
262
281
|
// History carries recall state; patch rows whose live recall event was missed.
|
|
263
282
|
let recalled = false;
|
|
264
|
-
for (const message of
|
|
283
|
+
for (const message of fetched) {
|
|
265
284
|
if (message.recalledAt && app.applyRecall?.(roomId, { messageID: message.id, recalledAt: message.recalledAt, recalledBy: message.recalledBy }))
|
|
266
285
|
recalled = true;
|
|
267
286
|
}
|