@alexsun-top/opencode-chat-room 0.4.0 → 0.5.0
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 +15 -0
- package/README.md +30 -5
- package/README.zh-CN.md +26 -4
- package/package.json +4 -1
- package/plugin.ts +18 -9
- package/server/announcement-routes.ts +34 -0
- package/server/assets/MERMAID-LICENSE +21 -0
- package/server/assets/NOTICE.txt +2 -1
- package/server/assets/base.css +15 -0
- package/server/assets/chat/announcements.js +119 -0
- package/server/assets/chat/events.js +180 -6
- package/server/assets/chat/index.js +2 -0
- package/server/assets/chat/markdown.js +214 -11
- package/server/assets/chat/members.js +4 -1
- package/server/assets/chat/messages.js +54 -18
- package/server/assets/chat/receipts.js +22 -17
- package/server/assets/chat/rooms.js +11 -6
- package/server/assets/chat/state.js +47 -13
- package/server/assets/conversation.css +21 -6
- package/server/assets/mermaid-11.12.2.min.js +2811 -0
- package/server/assets/responsive.css +39 -29
- package/server/chat-server.ts +8 -7
- package/server/chat.html +28 -9
- package/server/member-routes.ts +3 -0
- package/server/newrooms.ts +2 -0
- package/server/resource-routes.ts +65 -0
- package/server/wake.ts +2 -2
- package/skills/chatroom/SKILL.md +92 -0
- package/src/agent-receipts.ts +4 -4
- package/src/announcement-tool.ts +25 -0
- package/src/announcements.ts +16 -0
- package/src/center-client.ts +26 -0
- package/src/center-inbox.ts +9 -9
- package/src/chatroom-skill.ts +15 -0
- package/src/plugin-notifications.ts +7 -9
- package/src/resource-upload.ts +50 -0
- package/src/resources.ts +104 -0
- package/src/sqlite-state.ts +41 -0
- package/src/store.ts +1 -10
- package/src/v1-plugin.ts +6 -1
- package/src/v2-plugin.ts +34 -8
|
@@ -3,6 +3,137 @@ import { copyCodeFromButton, copyTextToClipboard } from "./markdown.js";
|
|
|
3
3
|
|
|
4
4
|
export function initEvents(app) {
|
|
5
5
|
const $ = app.$;
|
|
6
|
+
const roomRail = $("#roomRail"), drawerTrigger = $("#btnRoomsDrawer"), backdrop = $("#roomDrawerBackdrop");
|
|
7
|
+
const smallScreen = () => matchMedia("(max-width: 760px)").matches;
|
|
8
|
+
let readingPosition = null, resizeFrame = 0, resizeGeneration = 0, scrollGeneration = 0, resizeResetTimer = 0;
|
|
9
|
+
function captureReadingPosition() {
|
|
10
|
+
const thread = $("#thread"), edge = thread.getBoundingClientRect(), top = edge.top + thread.clientTop;
|
|
11
|
+
const anchor = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((row) => row.getBoundingClientRect().bottom > top);
|
|
12
|
+
readingPosition = { roomId: app.state.room?.id, messageId: anchor?.dataset.msgId, offset: anchor ? anchor.getBoundingClientRect().top - top : 0,
|
|
13
|
+
atBottom: thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 4 };
|
|
14
|
+
const missing = app.state.readAnchorGuard?.roomId === readingPosition.roomId && app.state.readAnchorGuard.missing;
|
|
15
|
+
if (readingPosition.roomId && anchor && (!missing || performance.now() <= app.state.userScrollIntentUntil)) {
|
|
16
|
+
app.savePosition(readingPosition.roomId, { messageId: readingPosition.messageId, offset: readingPosition.offset });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
app.captureReadingPosition = captureReadingPosition;
|
|
20
|
+
let layoutQueue = Promise.resolve();
|
|
21
|
+
app.preserveReadingLayout = (change) => {
|
|
22
|
+
const task = layoutQueue.then(async () => {
|
|
23
|
+
captureReadingPosition();
|
|
24
|
+
const saved = readingPosition, roomId = app.state.room?.id, generation = scrollGeneration;
|
|
25
|
+
app.state.layoutScrollAdjusting = true;
|
|
26
|
+
try {
|
|
27
|
+
await change();
|
|
28
|
+
await new Promise((resolve) => requestAnimationFrame(resolve));
|
|
29
|
+
if (roomId && app.state.room?.id === roomId && generation === scrollGeneration) {
|
|
30
|
+
const thread = $("#thread");
|
|
31
|
+
if (saved.atBottom) thread.scrollTop = thread.scrollHeight;
|
|
32
|
+
else if (saved.messageId) {
|
|
33
|
+
const row = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((item) => item.dataset.msgId === saved.messageId);
|
|
34
|
+
if (row) thread.scrollTop += row.getBoundingClientRect().top - (thread.getBoundingClientRect().top + thread.clientTop + saved.offset);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
captureReadingPosition();
|
|
38
|
+
} finally { requestAnimationFrame(() => { app.state.layoutScrollAdjusting = false; }); }
|
|
39
|
+
});
|
|
40
|
+
layoutQueue = task.catch(() => {});
|
|
41
|
+
return task;
|
|
42
|
+
};
|
|
43
|
+
app.restoreReadingPosition = (roomId) => {
|
|
44
|
+
const thread = $("#thread"), saved = app.state.positions.get(roomId), entry = app.state.rooms.get(roomId);
|
|
45
|
+
const align = (messageId, offset) => {
|
|
46
|
+
const row = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((item) => item.dataset.msgId === messageId);
|
|
47
|
+
if (!row) return false;
|
|
48
|
+
thread.scrollTop += row.getBoundingClientRect().top - (thread.getBoundingClientRect().top + thread.clientTop + offset);
|
|
49
|
+
captureReadingPosition(); return true;
|
|
50
|
+
};
|
|
51
|
+
if (typeof saved === "number") { thread.scrollTop = saved; captureReadingPosition(); return { messageId: readingPosition?.messageId }; }
|
|
52
|
+
if (saved?.messageId && align(saved.messageId, Number(saved.offset) || 0)) return { messageId: saved.messageId };
|
|
53
|
+
const readTs = entry?.readTs || app.state.lastReadTs.get(roomId) || 0;
|
|
54
|
+
const boundary = readTs && entry?.room.messages.find((message) => message.createdAt > readTs);
|
|
55
|
+
if (boundary && align(boundary.id, 0)) return { messageId: boundary.id };
|
|
56
|
+
return saved?.messageId || readTs ? { messageId: saved?.messageId || null, missing: true } : false;
|
|
57
|
+
};
|
|
58
|
+
function closeRoomDrawer(restore = true) {
|
|
59
|
+
roomRail.classList.remove("drawer-open"); roomRail.inert = true; roomRail.setAttribute("aria-hidden", "true");
|
|
60
|
+
backdrop.hidden = true; drawerTrigger.setAttribute("aria-expanded", "false");
|
|
61
|
+
if (restore && smallScreen()) drawerTrigger.focus({ preventScroll: true });
|
|
62
|
+
}
|
|
63
|
+
function syncRoomDrawer() {
|
|
64
|
+
if (!smallScreen()) { roomRail.classList.remove("drawer-open"); roomRail.inert = false; roomRail.removeAttribute("aria-hidden"); backdrop.hidden = true; drawerTrigger.setAttribute("aria-expanded", "false"); }
|
|
65
|
+
else if (!roomRail.classList.contains("drawer-open")) {
|
|
66
|
+
if (roomRail.contains(document.activeElement)) drawerTrigger.focus({ preventScroll: true });
|
|
67
|
+
roomRail.inert = true; roomRail.setAttribute("aria-hidden", "true");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function preserveReadingPosition() {
|
|
71
|
+
syncRoomDrawer();
|
|
72
|
+
if (!app.state.resizeScrollAdjusting) app.state.userScrollIntentUntil = 0;
|
|
73
|
+
app.state.resizeScrollAdjusting = true;
|
|
74
|
+
const generation = ++resizeGeneration;
|
|
75
|
+
if (app.state.receiptObservationFrame) cancelAnimationFrame(app.state.receiptObservationFrame);
|
|
76
|
+
app.state.receiptObservationFrame = 0; app.state.receiptObservation = null;
|
|
77
|
+
clearTimeout(resizeResetTimer);
|
|
78
|
+
resizeResetTimer = setTimeout(() => { if (generation === resizeGeneration) app.state.resizeScrollAdjusting = false; }, 500);
|
|
79
|
+
if (resizeFrame) cancelAnimationFrame(resizeFrame);
|
|
80
|
+
resizeFrame = requestAnimationFrame(() => {
|
|
81
|
+
resizeFrame = 0;
|
|
82
|
+
const position = readingPosition, thread = $("#thread");
|
|
83
|
+
if (!position?.roomId || position.roomId !== app.state.room?.id) return captureReadingPosition();
|
|
84
|
+
if (position.atBottom) thread.scrollTop = thread.scrollHeight;
|
|
85
|
+
else if (position.messageId) {
|
|
86
|
+
const row = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((item) => item.dataset.msgId === position.messageId);
|
|
87
|
+
if (row) thread.scrollTop += row.getBoundingClientRect().top - (thread.getBoundingClientRect().top + thread.clientTop + position.offset);
|
|
88
|
+
}
|
|
89
|
+
captureReadingPosition();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
syncRoomDrawer();
|
|
93
|
+
window.addEventListener("resize", preserveReadingPosition);
|
|
94
|
+
window.addEventListener("pagehide", captureReadingPosition);
|
|
95
|
+
drawerTrigger.addEventListener("click", () => {
|
|
96
|
+
if (roomRail.classList.contains("drawer-open")) return closeRoomDrawer();
|
|
97
|
+
app.closePanels(); roomRail.inert = false; roomRail.removeAttribute("aria-hidden"); roomRail.classList.add("drawer-open");
|
|
98
|
+
backdrop.hidden = false; drawerTrigger.setAttribute("aria-expanded", "true");
|
|
99
|
+
(roomRail.querySelector('.room-row[aria-current="page"]') || $("#btnRefreshRooms")).focus();
|
|
100
|
+
});
|
|
101
|
+
backdrop.addEventListener("click", () => closeRoomDrawer());
|
|
102
|
+
$("#roomListChat").addEventListener("click", (event) => { if (roomRail.classList.contains("drawer-open") && event.target.closest("[data-room-id]")) closeRoomDrawer(); });
|
|
103
|
+
const activateRoom = app.activateRoom;
|
|
104
|
+
const pendingAnchorLoads = new Map();
|
|
105
|
+
function cancelAnchorLoads() {
|
|
106
|
+
for (const { controller } of pendingAnchorLoads.values()) controller.abort();
|
|
107
|
+
pendingAnchorLoads.clear();
|
|
108
|
+
}
|
|
109
|
+
const showJoin = app.showJoin;
|
|
110
|
+
app.showJoin = (...args) => { cancelAnchorLoads(); return showJoin(...args); };
|
|
111
|
+
const removeJoinedRoom = app.removeJoinedRoom;
|
|
112
|
+
app.removeJoinedRoom = (roomId, options) => {
|
|
113
|
+
if (app.state.room?.id === roomId) cancelAnchorLoads();
|
|
114
|
+
return removeJoinedRoom(roomId, options);
|
|
115
|
+
};
|
|
116
|
+
function activateNow(id) {
|
|
117
|
+
activateRoom(id); captureReadingPosition();
|
|
118
|
+
if (roomRail.classList.contains("drawer-open")) closeRoomDrawer();
|
|
119
|
+
}
|
|
120
|
+
app.activateRoom = (id) => {
|
|
121
|
+
cancelAnchorLoads();
|
|
122
|
+
const entry = app.state.rooms.get(id), saved = app.state.positions.get(id);
|
|
123
|
+
const intent = ++app.state.navigationGeneration;
|
|
124
|
+
if (!entry) return activateNow(id);
|
|
125
|
+
const readTs = entry.readTs || app.state.lastReadTs.get(id) || 0;
|
|
126
|
+
const needAnchor = saved?.messageId && !entry.room.messages.some((message) => message.id === saved.messageId);
|
|
127
|
+
const latest = entry.room.messages.at(-1);
|
|
128
|
+
const needBoundary = !saved?.messageId && readTs && (!latest || latest.createdAt > readTs && !entry.room.messages.some((message) => message.createdAt <= readTs));
|
|
129
|
+
if (!needAnchor && !needBoundary) return activateNow(id);
|
|
130
|
+
const controller = new AbortController();
|
|
131
|
+
const task = loadAnchorHistory(id, entry, saved?.messageId, app, needBoundary ? readTs : 0, controller.signal).catch(() => {}).finally(() => {
|
|
132
|
+
if (pendingAnchorLoads.get(id)?.controller === controller) pendingAnchorLoads.delete(id);
|
|
133
|
+
if (intent === app.state.navigationGeneration) activateNow(id);
|
|
134
|
+
});
|
|
135
|
+
pendingAnchorLoads.set(id, { controller, task });
|
|
136
|
+
};
|
|
6
137
|
document.querySelectorAll(".locale-select").forEach((select) => {
|
|
7
138
|
select.addEventListener("change", () => {
|
|
8
139
|
app.setLocale(select.value);
|
|
@@ -36,6 +167,7 @@ export function initEvents(app) {
|
|
|
36
167
|
$("#inRoomNameChat").addEventListener("input", app.hideSendError);
|
|
37
168
|
|
|
38
169
|
const input = $("#msgInput");
|
|
170
|
+
window.addEventListener("resize", () => app.autoGrow(input));
|
|
39
171
|
$(".composer").addEventListener("click", (event) => {
|
|
40
172
|
const button = event.target.closest?.("[data-failed-action]");
|
|
41
173
|
if (!button) return;
|
|
@@ -66,17 +198,32 @@ export function initEvents(app) {
|
|
|
66
198
|
event.preventDefault();
|
|
67
199
|
app.insertMention(option.dataset.name);
|
|
68
200
|
});
|
|
69
|
-
$("#thread")
|
|
70
|
-
|
|
71
|
-
|
|
201
|
+
const thread = $("#thread");
|
|
202
|
+
const markScrollIntent = () => { scrollGeneration++; app.state.userScrollIntentUntil = performance.now() + 700; };
|
|
203
|
+
thread.addEventListener("wheel", markScrollIntent, { passive: true });
|
|
204
|
+
thread.addEventListener("touchmove", markScrollIntent, { passive: true });
|
|
205
|
+
thread.addEventListener("pointerdown", (event) => {
|
|
206
|
+
if (event.clientX >= thread.getBoundingClientRect().right - 18) markScrollIntent();
|
|
207
|
+
});
|
|
208
|
+
thread.addEventListener("scroll", () => {
|
|
209
|
+
const adjusting = (app.state.resizeScrollAdjusting || app.state.layoutScrollAdjusting)
|
|
210
|
+
&& performance.now() > app.state.userScrollIntentUntil;
|
|
211
|
+
if (!adjusting || performance.now() <= app.state.userScrollIntentUntil) captureReadingPosition();
|
|
212
|
+
if (!adjusting && thread.scrollHeight - thread.scrollTop - thread.clientHeight < 80) void app.markActiveRoomRead();
|
|
72
213
|
app.queueReceiptRefresh();
|
|
73
|
-
app.queueReceiptObservation();
|
|
214
|
+
if (!adjusting) app.queueReceiptObservation();
|
|
74
215
|
app.updateJump();
|
|
75
216
|
});
|
|
76
217
|
document.addEventListener("visibilitychange", () => {
|
|
77
|
-
if (!document.hidden) {
|
|
218
|
+
if (!document.hidden) {
|
|
219
|
+
if (!app.state.resizeScrollAdjusting && !app.state.layoutScrollAdjusting) void app.markActiveRoomRead();
|
|
220
|
+
void app.loadManageableMembers();
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
window.addEventListener("focus", () => {
|
|
224
|
+
if (!app.state.resizeScrollAdjusting && !app.state.layoutScrollAdjusting) { void app.markActiveRoomRead(); app.queueReceiptObservation(); }
|
|
225
|
+
app.queueReceiptRefresh();
|
|
78
226
|
});
|
|
79
|
-
window.addEventListener("focus", () => { void app.markActiveRoomRead(); app.queueReceiptRefresh(); app.queueReceiptObservation(); });
|
|
80
227
|
$("#msgList").addEventListener("click", (event) => {
|
|
81
228
|
const button = event.target.closest("[data-copy-code]");
|
|
82
229
|
if (button) void copyCodeFromButton(button, app);
|
|
@@ -106,6 +253,14 @@ export function initEvents(app) {
|
|
|
106
253
|
if (!event.target.closest("#railRoomForm, #btnNewRoom")) $("#railRoomForm").hidden = true;
|
|
107
254
|
});
|
|
108
255
|
document.addEventListener("keydown", (event) => {
|
|
256
|
+
if (event.key === "Tab" && roomRail.classList.contains("drawer-open")) {
|
|
257
|
+
const stops = [...roomRail.querySelectorAll("button:not(:disabled), input:not(:disabled)")].filter((node) => node.getClientRects().length);
|
|
258
|
+
const first = stops[0], last = stops.at(-1);
|
|
259
|
+
if (!roomRail.contains(document.activeElement)) { event.preventDefault(); first?.focus(); }
|
|
260
|
+
else if (event.shiftKey && document.activeElement === first) { event.preventDefault(); last?.focus(); }
|
|
261
|
+
else if (!event.shiftKey && document.activeElement === last) { event.preventDefault(); first?.focus(); }
|
|
262
|
+
}
|
|
263
|
+
if (event.key === "Escape" && roomRail.classList.contains("drawer-open")) { event.preventDefault(); closeRoomDrawer(); return; }
|
|
109
264
|
if (event.key === "Escape" && app.mentionActive) app.closeMention();
|
|
110
265
|
if (event.key === "Escape" && !$("#railRoomForm").hidden) $("#railRoomForm").hidden = true;
|
|
111
266
|
if (event.key === "Escape" && (!$("#memberPanel").hidden || !$("#chatSettings").hidden)) {
|
|
@@ -121,6 +276,25 @@ export function initEvents(app) {
|
|
|
121
276
|
$("#inRoomId").focus();
|
|
122
277
|
}
|
|
123
278
|
|
|
279
|
+
async function loadAnchorHistory(roomId, entry, messageId, app, readTs = 0, signal) {
|
|
280
|
+
let beforeSeq;
|
|
281
|
+
const messages = [];
|
|
282
|
+
while (!signal?.aborted) {
|
|
283
|
+
const query = new URLSearchParams({ limit: "100" });
|
|
284
|
+
if (beforeSeq) query.set("beforeSeq", String(beforeSeq));
|
|
285
|
+
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/history?${query}`, { signal });
|
|
286
|
+
if (signal?.aborted) return;
|
|
287
|
+
messages.push(...(result.messages || []));
|
|
288
|
+
const found = messageId ? messages.some((message) => message.id === messageId)
|
|
289
|
+
: messages.some((message) => message.createdAt > readTs) && messages.some((message) => message.createdAt <= readTs);
|
|
290
|
+
if (found || !result.hasMore || !result.nextBeforeSeq || result.nextBeforeSeq === beforeSeq) break;
|
|
291
|
+
beforeSeq = result.nextBeforeSeq;
|
|
292
|
+
}
|
|
293
|
+
if (messageId ? !messages.some((message) => message.id === messageId) : !messages.some((message) => message.createdAt > readTs)) return;
|
|
294
|
+
const merged = new Map([...entry.room.messages, ...messages].map((message) => [message.id, { ...message, roomId }]));
|
|
295
|
+
entry.room.messages = [...merged.values()].sort((left, right) => left.createdAt - right.createdAt);
|
|
296
|
+
}
|
|
297
|
+
|
|
124
298
|
function saveAccessToken(button, app) {
|
|
125
299
|
const input = document.getElementById(button.dataset.tokenSave);
|
|
126
300
|
input.value = app.setToken(input.value);
|
|
@@ -6,6 +6,7 @@ import { initRooms } from "./rooms.js";
|
|
|
6
6
|
import { initEvents } from "./events.js";
|
|
7
7
|
import { initQuestions } from "./questions.js";
|
|
8
8
|
import { initReceipts } from "./receipts.js";
|
|
9
|
+
import { initAnnouncements } from "./announcements.js";
|
|
9
10
|
|
|
10
11
|
initMentions(app);
|
|
11
12
|
initMessages(app);
|
|
@@ -14,3 +15,4 @@ initMembers(app);
|
|
|
14
15
|
initRooms(app);
|
|
15
16
|
initQuestions(app);
|
|
16
17
|
initEvents(app);
|
|
18
|
+
initAnnouncements(app);
|
|
@@ -1,33 +1,236 @@
|
|
|
1
1
|
import { createRandomId } from "./state.js";
|
|
2
2
|
|
|
3
3
|
const mentionPattern = /<at>@([^<]+)<\/at>/g;
|
|
4
|
+
const resourcePattern = /\[\[resource:([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})\]\]/gi;
|
|
5
|
+
let mermaidReady = false;
|
|
6
|
+
const previewUrls = new Map();
|
|
7
|
+
let previewObserver;
|
|
4
8
|
|
|
5
9
|
export function renderMarkdown(root, text, app) {
|
|
6
|
-
const mentions = [];
|
|
10
|
+
const mentions = [], diagrams = new Map();
|
|
7
11
|
const nonce = createRandomId();
|
|
8
12
|
const source = String(text).replace(mentionPattern, (_, name) => {
|
|
9
13
|
const token = `CHATMENTION${nonce}${mentions.length}END`;
|
|
10
14
|
mentions.push({ token, name, literal: `<at>@${name}</at>` });
|
|
11
15
|
return token;
|
|
12
16
|
});
|
|
13
|
-
const
|
|
17
|
+
const renderer = new window.marked.Renderer(), renderCode = renderer.code.bind(renderer);
|
|
18
|
+
renderer.code = (token) => {
|
|
19
|
+
if ((token.lang || "").trim().toLowerCase().split(/\s+/)[0] !== "mermaid") return renderCode(token);
|
|
20
|
+
const key = `CHATDIAGRAM${nonce}${diagrams.size}END`;
|
|
21
|
+
const literalToken = new RegExp(`CHATMENTION${nonce}\\d+END`, "g");
|
|
22
|
+
diagrams.set(key, token.text.replace(literalToken, (value) => mentions.find((mention) => mention.token === value)?.literal || value));
|
|
23
|
+
return `<div data-chat-mermaid="${key}"></div>`;
|
|
24
|
+
};
|
|
25
|
+
const parsed = window.marked.parse(source, { gfm: true, breaks: true, async: false, renderer });
|
|
14
26
|
const fragment = window.DOMPurify.sanitize(parsed, { RETURN_DOM_FRAGMENT: true });
|
|
15
27
|
replaceMentionTokens(fragment, mentions, app);
|
|
28
|
+
const resources = replaceResourceMarkers(fragment, app);
|
|
29
|
+
const diagramNodes = [...fragment.querySelectorAll("[data-chat-mermaid]")];
|
|
30
|
+
for (const node of diagramNodes) {
|
|
31
|
+
const source = diagrams.get(node.dataset.chatMermaid);
|
|
32
|
+
node.removeAttribute("data-chat-mermaid"); node.className = "mermaid-diagram";
|
|
33
|
+
if (source !== undefined) showDiagramFallback(node, source, app, !isUnsafeDiagram(source));
|
|
34
|
+
}
|
|
16
35
|
fragment.querySelectorAll("a").forEach((link) => {
|
|
17
36
|
link.target = "_blank";
|
|
18
37
|
link.rel = "noopener noreferrer";
|
|
19
38
|
});
|
|
20
39
|
fragment.querySelectorAll('input[type="checkbox"]').forEach((input) => { input.disabled = true; });
|
|
21
|
-
fragment.querySelectorAll("pre").forEach((pre) =>
|
|
22
|
-
const button = document.createElement("button");
|
|
23
|
-
button.type = "button";
|
|
24
|
-
button.className = "code-copy";
|
|
25
|
-
button.dataset.copyCode = "";
|
|
26
|
-
button.dataset.i18n = "copyCode";
|
|
27
|
-
button.textContent = app.t("copyCode");
|
|
28
|
-
pre.appendChild(button);
|
|
29
|
-
});
|
|
40
|
+
fragment.querySelectorAll("pre").forEach((pre) => addCodeCopy(pre, app));
|
|
30
41
|
root.appendChild(fragment);
|
|
42
|
+
for (let i = 0; i < diagramNodes.length; i++) {
|
|
43
|
+
const source = diagrams.get(`CHATDIAGRAM${nonce}${i}END`);
|
|
44
|
+
if (!isUnsafeDiagram(source)) void renderDiagram(diagramNodes[i], source, app);
|
|
45
|
+
}
|
|
46
|
+
for (const resource of resources) void loadResourceCard(resource.card, resource.roomId, app);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function initializeMermaid() {
|
|
50
|
+
if (mermaidReady) return;
|
|
51
|
+
if (!window.mermaid) await new Promise((resolve, reject) => {
|
|
52
|
+
const script = document.querySelector('script[src$="mermaid-11.12.2.min.js"]');
|
|
53
|
+
if (!script) return reject(new Error("Mermaid bundle is missing"));
|
|
54
|
+
script.addEventListener("load", resolve, { once: true });
|
|
55
|
+
script.addEventListener("error", reject, { once: true });
|
|
56
|
+
window.addEventListener("load", resolve, { once: true });
|
|
57
|
+
});
|
|
58
|
+
if (!window.mermaid) throw new Error("Mermaid bundle failed to load");
|
|
59
|
+
window.mermaid.initialize({
|
|
60
|
+
startOnLoad: false, securityLevel: "strict", htmlLabels: false,
|
|
61
|
+
theme: matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "default",
|
|
62
|
+
flowchart: { htmlLabels: false }, maxTextSize: 20000, maxEdges: 200, suppressErrorRendering: true,
|
|
63
|
+
secure: ["secure", "securityLevel", "startOnLoad", "htmlLabels", "flowchart", "theme", "themeCSS", "themeVariables", "dompurifyConfig", "maxTextSize", "maxEdges", "suppressErrorRendering"],
|
|
64
|
+
});
|
|
65
|
+
mermaidReady = true;
|
|
66
|
+
}
|
|
67
|
+
async function renderDiagram(node, source, app) {
|
|
68
|
+
if (source === undefined || isUnsafeDiagram(source)) return;
|
|
69
|
+
try {
|
|
70
|
+
await initializeMermaid();
|
|
71
|
+
const { svg } = await window.mermaid.render(`oc-chat-${createRandomId()}`, source);
|
|
72
|
+
if (!node.isConnected) return;
|
|
73
|
+
const fragment = window.DOMPurify.sanitize(svg, {
|
|
74
|
+
RETURN_DOM_FRAGMENT: true, USE_PROFILES: { svg: true, svgFilters: true },
|
|
75
|
+
FORBID_TAGS: ["foreignObject", "a", "script"], FORBID_ATTR: ["href", "xlink:href", "onclick", "onload"],
|
|
76
|
+
ALLOW_DATA_ATTR: false,
|
|
77
|
+
});
|
|
78
|
+
if (!fragment.querySelector("svg")) throw new Error("Diagram SVG was rejected");
|
|
79
|
+
const update = () => node.replaceChildren(fragment);
|
|
80
|
+
if (app.preserveReadingLayout) await app.preserveReadingLayout(update); else update();
|
|
81
|
+
} catch {
|
|
82
|
+
if (node.isConnected) showDiagramFallback(node, source, app, false);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function isUnsafeDiagram(source = "") {
|
|
86
|
+
return /%%\s*\{|^\s*---\s*(?:\r?\n)|\burl\s*\(|@import|@\s*\{[^}]*\b(?:img|image)\s*:/i.test(source);
|
|
87
|
+
}
|
|
88
|
+
function showDiagramFallback(node, source, app, loading) {
|
|
89
|
+
node.textContent = "";
|
|
90
|
+
const status = document.createElement("p"); status.className = "diagram-status";
|
|
91
|
+
status.setAttribute("role", "status"); status.textContent = app.t(loading ? "diagramLoading" : "diagramRenderFailed");
|
|
92
|
+
node.append(status);
|
|
93
|
+
if (!loading) {
|
|
94
|
+
const pre = document.createElement("pre"), code = document.createElement("code");
|
|
95
|
+
code.textContent = source; pre.append(code); addCodeCopy(pre, app); node.append(pre);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function addCodeCopy(pre, app) {
|
|
99
|
+
const button = document.createElement("button");
|
|
100
|
+
button.type = "button"; button.className = "code-copy"; button.dataset.copyCode = "";
|
|
101
|
+
button.dataset.i18n = "copyCode"; button.textContent = app.t("copyCode"); pre.append(button);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function replaceResourceMarkers(fragment, app) {
|
|
105
|
+
const walker = document.createTreeWalker(fragment, NodeFilter.SHOW_TEXT), nodes = [], cards = [], roomId = app.state.room?.id;
|
|
106
|
+
while (walker.nextNode()) nodes.push(walker.currentNode);
|
|
107
|
+
for (const node of nodes) {
|
|
108
|
+
if (node.parentElement?.closest("code, pre, a")) continue;
|
|
109
|
+
const value = node.nodeValue || "", replacement = document.createDocumentFragment();
|
|
110
|
+
let last = 0, match;
|
|
111
|
+
resourcePattern.lastIndex = 0;
|
|
112
|
+
while ((match = resourcePattern.exec(value))) {
|
|
113
|
+
replacement.append(document.createTextNode(value.slice(last, match.index)));
|
|
114
|
+
const card = document.createElement("span"), status = document.createElement("span");
|
|
115
|
+
card.className = "chat-resource"; card.dataset.resourceId = match[1]; card.setAttribute("role", "group");
|
|
116
|
+
status.className = "resource-status"; status.setAttribute("role", "status");
|
|
117
|
+
status.textContent = app.t("resourceLoading"); card.append(status); replacement.append(card);
|
|
118
|
+
cards.push({ card, roomId }); last = resourcePattern.lastIndex;
|
|
119
|
+
}
|
|
120
|
+
if (last) { replacement.append(document.createTextNode(value.slice(last))); node.replaceWith(replacement); }
|
|
121
|
+
}
|
|
122
|
+
return cards;
|
|
123
|
+
}
|
|
124
|
+
async function loadResourceCard(card, roomId, app) {
|
|
125
|
+
try {
|
|
126
|
+
const id = card.dataset.resourceId;
|
|
127
|
+
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}`);
|
|
128
|
+
if (!card.isConnected) return;
|
|
129
|
+
const resource = result.resource, type = String(resource.mimeType).split(";")[0].toLowerCase();
|
|
130
|
+
card.setAttribute("aria-label", resource.name);
|
|
131
|
+
const info = document.createElement("span"), name = document.createElement("span"), size = document.createElement("span"), actions = document.createElement("span"), status = document.createElement("span");
|
|
132
|
+
info.className = "resource-info"; name.className = "resource-name"; name.textContent = resource.name;
|
|
133
|
+
size.className = "resource-size"; size.textContent = formatBytes(resource.size); info.append(name, size);
|
|
134
|
+
actions.className = "resource-actions"; status.className = "resource-status"; status.setAttribute("role", "status");
|
|
135
|
+
card.replaceChildren(info, actions, status);
|
|
136
|
+
const download = resourceButton("resourceDownload", resource.name, () => downloadResource(card, roomId, id, resource, app, status, download), app);
|
|
137
|
+
actions.append(download);
|
|
138
|
+
if (previewKind(type)) {
|
|
139
|
+
const preview = resourceButton("resourcePreview", resource.name, () => previewResource(card, roomId, id, resource, type, preview, app, status), app);
|
|
140
|
+
actions.append(preview);
|
|
141
|
+
}
|
|
142
|
+
} catch { if (card.isConnected) setResourceError(card, app); }
|
|
143
|
+
}
|
|
144
|
+
function resourceButton(key, name, action, app) {
|
|
145
|
+
const button = document.createElement("button");
|
|
146
|
+
button.type = "button"; button.className = "btn ghost small resource-button";
|
|
147
|
+
button.dataset.i18n = key; button.textContent = app.t(key);
|
|
148
|
+
button.setAttribute("aria-label", `${app.t(key)}: ${name}`);
|
|
149
|
+
button.addEventListener("click", action); return button;
|
|
150
|
+
}
|
|
151
|
+
async function downloadResource(card, roomId, id, resource, app, status, button) {
|
|
152
|
+
if (!card.isConnected || button.disabled) return;
|
|
153
|
+
button.disabled = true;
|
|
154
|
+
status.textContent = app.t("resourceLoading");
|
|
155
|
+
try {
|
|
156
|
+
const blob = await app.apiBlob(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}/content`);
|
|
157
|
+
if (!card.isConnected) return;
|
|
158
|
+
const url = URL.createObjectURL(blob), link = document.createElement("a");
|
|
159
|
+
link.href = url; link.download = String(resource.name).replace(/[\\/\u0000-\u001f\u007f]/g, "_") || "download";
|
|
160
|
+
document.body.append(link); link.click(); link.remove(); setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
161
|
+
status.textContent = "";
|
|
162
|
+
} catch { if (card.isConnected) status.textContent = app.t("resourceFailed"); }
|
|
163
|
+
finally { if (card.isConnected) button.disabled = false; }
|
|
164
|
+
}
|
|
165
|
+
async function previewResource(card, roomId, id, resource, type, button, app, status) {
|
|
166
|
+
if (!card.isConnected || button.disabled) return;
|
|
167
|
+
button.disabled = true;
|
|
168
|
+
if (card._previewElement) {
|
|
169
|
+
try {
|
|
170
|
+
await app.preserveReadingLayout(() => card._previewElement.remove());
|
|
171
|
+
URL.revokeObjectURL(card._resourceUrl); previewUrls.delete(card); delete card._resourceUrl; delete card._previewElement;
|
|
172
|
+
if (card.isConnected) { button.dataset.i18n = "resourcePreview"; button.textContent = app.t("resourcePreview"); }
|
|
173
|
+
} catch {
|
|
174
|
+
if (!card.isConnected) return;
|
|
175
|
+
} finally { if (card.isConnected) button.disabled = false; }
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
status.textContent = app.t("resourceLoading");
|
|
179
|
+
let url;
|
|
180
|
+
try {
|
|
181
|
+
const blob = await app.apiBlob(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}/content`);
|
|
182
|
+
if (!card.isConnected) return;
|
|
183
|
+
url = URL.createObjectURL(blob);
|
|
184
|
+
const kind = previewKind(type), media = document.createElement(kind), ready = waitForMediaSize(media, kind);
|
|
185
|
+
if (kind === "img") media.alt = resource.name;
|
|
186
|
+
else { media.controls = true; media.preload = "metadata"; }
|
|
187
|
+
media.addEventListener("error", () => { if (card.isConnected) status.textContent = app.t("resourceFailed"); });
|
|
188
|
+
media.src = url;
|
|
189
|
+
await app.preserveReadingLayout(async () => { status.textContent = ""; card.append(media); await ready; });
|
|
190
|
+
if (!card.isConnected) { URL.revokeObjectURL(url); return; }
|
|
191
|
+
card._resourceUrl = url; card._previewElement = media; previewUrls.set(card, url); watchPreviewRemoval();
|
|
192
|
+
button.dataset.i18n = "resourceHide"; button.textContent = app.t("resourceHide");
|
|
193
|
+
} catch { if (url) URL.revokeObjectURL(url); if (card.isConnected) status.textContent = app.t("resourceFailed"); }
|
|
194
|
+
finally { if (card.isConnected) button.disabled = false; }
|
|
195
|
+
}
|
|
196
|
+
function waitForMediaSize(media, kind) {
|
|
197
|
+
const event = kind === "img" ? "load" : "loadedmetadata";
|
|
198
|
+
if (kind === "img" && media.complete && media.naturalWidth) return Promise.resolve();
|
|
199
|
+
if (kind !== "img" && media.readyState >= 1) return Promise.resolve();
|
|
200
|
+
return new Promise((resolve, reject) => {
|
|
201
|
+
media.addEventListener(event, resolve, { once: true });
|
|
202
|
+
media.addEventListener("error", reject, { once: true });
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
function previewKind(type) {
|
|
206
|
+
if (["image/png", "image/jpeg", "image/gif", "image/webp", "image/avif"].includes(type)) return "img";
|
|
207
|
+
if (["audio/mpeg", "audio/ogg", "audio/wav", "audio/webm", "audio/mp4"].includes(type)) return "audio";
|
|
208
|
+
if (["video/mp4", "video/webm", "video/ogg"].includes(type)) return "video";
|
|
209
|
+
return "";
|
|
210
|
+
}
|
|
211
|
+
function formatBytes(value) {
|
|
212
|
+
const bytes = Math.max(0, Number(value) || 0);
|
|
213
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
214
|
+
return bytes < 1048576 ? `${(bytes / 1024).toFixed(1)} KB` : `${(bytes / 1048576).toFixed(1)} MB`;
|
|
215
|
+
}
|
|
216
|
+
function setResourceError(card, app) {
|
|
217
|
+
card.textContent = "";
|
|
218
|
+
const status = document.createElement("span"); status.className = "resource-status";
|
|
219
|
+
status.setAttribute("role", "status"); status.textContent = app.t("resourceFailed"); card.append(status);
|
|
220
|
+
}
|
|
221
|
+
function watchPreviewRemoval() {
|
|
222
|
+
const list = document.querySelector("#msgList");
|
|
223
|
+
if (previewObserver || !list) return;
|
|
224
|
+
previewObserver = new MutationObserver((records) => {
|
|
225
|
+
for (const node of records.flatMap((record) => [...record.removedNodes])) {
|
|
226
|
+
if (!(node instanceof Element)) continue;
|
|
227
|
+
for (const card of [node, ...node.querySelectorAll(".chat-resource")]) {
|
|
228
|
+
const url = previewUrls.get(card);
|
|
229
|
+
if (url && !card.isConnected) { URL.revokeObjectURL(url); previewUrls.delete(card); }
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
previewObserver.observe(list, { childList: true, subtree: true });
|
|
31
234
|
}
|
|
32
235
|
|
|
33
236
|
function replaceMentionTokens(fragment, mentions, app) {
|
|
@@ -90,13 +90,16 @@ export function initMembers(app) {
|
|
|
90
90
|
app.state.receiptRefreshTimer = setTimeout(() => void app.loadVisibleReceipts(roomId, generation), 180);
|
|
91
91
|
};
|
|
92
92
|
app.queueReceiptObservation = () => {
|
|
93
|
+
if ((app.state.resizeScrollAdjusting || app.state.layoutScrollAdjusting)
|
|
94
|
+
&& performance.now() > app.state.userScrollIntentUntil) return;
|
|
93
95
|
app.state.receiptObservation = { roomId: app.state.room?.id, generation: app.state.pollGeneration };
|
|
94
96
|
if (app.state.receiptObservationFrame) return;
|
|
95
97
|
app.state.receiptObservationFrame = requestAnimationFrame(() => {
|
|
96
98
|
app.state.receiptObservationFrame = 0;
|
|
97
99
|
const target = app.state.receiptObservation;
|
|
98
100
|
app.state.receiptObservation = null;
|
|
99
|
-
if (!target ||
|
|
101
|
+
if (!target || (app.state.resizeScrollAdjusting || app.state.layoutScrollAdjusting)
|
|
102
|
+
&& performance.now() > app.state.userScrollIntentUntil || document.hidden || !document.hasFocus() || !current(app, target.roomId, target.generation)) return;
|
|
100
103
|
void app.loadVisibleReceipts(target.roomId, target.generation);
|
|
101
104
|
void app.ackVisibleMessages(target.roomId, target.generation);
|
|
102
105
|
});
|
|
@@ -8,8 +8,19 @@ export function initMessages(app) {
|
|
|
8
8
|
let pollDelay = 2000;
|
|
9
9
|
|
|
10
10
|
app.autoGrow = (textarea) => {
|
|
11
|
+
textarea.style.overflowY = "hidden";
|
|
12
|
+
const style = getComputedStyle(textarea), line = parseFloat(style.lineHeight);
|
|
13
|
+
const padding = parseFloat(style.paddingTop) + parseFloat(style.paddingBottom);
|
|
14
|
+
const border = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
|
|
15
|
+
if (!textarea.value) {
|
|
16
|
+
textarea.style.height = `${Math.max(line + padding + border, parseFloat(style.minHeight) || 0)}px`;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
11
19
|
textarea.style.height = "auto";
|
|
12
|
-
|
|
20
|
+
const limit = line * 5 + padding;
|
|
21
|
+
const overflow = textarea.scrollHeight > limit + 1;
|
|
22
|
+
textarea.style.height = `${Math.min(textarea.scrollHeight, limit) + border}px`;
|
|
23
|
+
textarea.style.overflowY = overflow ? "auto" : "hidden";
|
|
13
24
|
};
|
|
14
25
|
app.updateSendState = () => { $("#btnSend").disabled = !$("#msgInput").value.trim(); };
|
|
15
26
|
app.updateDraftCount = () => app.setText($("#draftCount"), "messageCount", { count: $("#msgInput").value.length });
|
|
@@ -73,6 +84,7 @@ export function initMessages(app) {
|
|
|
73
84
|
}
|
|
74
85
|
};
|
|
75
86
|
app.jumpToBottom = () => {
|
|
87
|
+
app.state.readAnchorGuard = null;
|
|
76
88
|
$("#thread").scrollTop = $("#thread").scrollHeight;
|
|
77
89
|
const entry = app.state.room && app.state.rooms.get(app.state.room.id);
|
|
78
90
|
app.state.unread = entry ? entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0) : 0;
|
|
@@ -97,6 +109,10 @@ export function initMessages(app) {
|
|
|
97
109
|
const thread = $("#thread");
|
|
98
110
|
if (!room || !entry || document.hidden || !document.hasFocus()
|
|
99
111
|
|| thread.scrollHeight - thread.scrollTop - thread.clientHeight >= 80) return;
|
|
112
|
+
if (app.state.readAnchorGuard?.roomId === room.id) {
|
|
113
|
+
if (performance.now() > app.state.userScrollIntentUntil) return;
|
|
114
|
+
app.state.readAnchorGuard = null;
|
|
115
|
+
}
|
|
100
116
|
const timestamp = room.messages.reduce((latest, message) => Math.max(latest, message.createdAt), entry.readTs);
|
|
101
117
|
if (!timestamp || timestamp <= entry.readTs) return;
|
|
102
118
|
try {
|
|
@@ -105,6 +121,7 @@ export function initMessages(app) {
|
|
|
105
121
|
});
|
|
106
122
|
if (app.state.room?.id !== room.id || app.state.pollGeneration !== generation) return;
|
|
107
123
|
entry.readTs = timestamp;
|
|
124
|
+
app.saveReadTs(room.id, timestamp);
|
|
108
125
|
entry.unreadOverflow = false;
|
|
109
126
|
for (const [id, message] of entry.unreadIds) if (message.createdAt <= timestamp) entry.unreadIds.delete(id);
|
|
110
127
|
app.state.unread = entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0);
|
|
@@ -126,10 +143,7 @@ export function initMessages(app) {
|
|
|
126
143
|
row.append(text);
|
|
127
144
|
} else {
|
|
128
145
|
const direct = app.state.room.kind === "direct";
|
|
129
|
-
const
|
|
130
|
-
const own = direct ? message.senderId === LS.sessionID
|
|
131
|
-
: message.senderSessionID ? message.senderSessionID === LS.sessionID
|
|
132
|
-
: message.senderId === app.state.myName && sameName?.length === 1 && sameName[0].sessionID === LS.sessionID;
|
|
146
|
+
const own = isOwnMessage(message, app);
|
|
133
147
|
const sender = direct
|
|
134
148
|
? app.state.room.participants.find((person) => person.id === message.senderId)?.name || app.t("guest")
|
|
135
149
|
: message.senderId;
|
|
@@ -338,18 +352,22 @@ export function initMessages(app) {
|
|
|
338
352
|
return;
|
|
339
353
|
}
|
|
340
354
|
if (data.member !== true || !Array.isArray(data.items)) return;
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
355
|
+
const items = data.items, previousReceivedTs = entry?.receivedTs || 0, messages = app.state.room.messages;
|
|
356
|
+
const known = new Set(messages.map((message) => message.id));
|
|
357
|
+
const previousUnreadOverflow = Boolean(entry?.unreadOverflow);
|
|
358
|
+
let sawSystem = false, added = 0, unreadChanged = false;
|
|
359
|
+
for (const item of items) {
|
|
360
|
+
if (item.senderId === "system") sawSystem = true;
|
|
361
|
+
const wasKnown = known.has(item.id);
|
|
362
|
+
if (!wasKnown) {
|
|
363
|
+
messages.push({ ...item, roomId }); known.add(item.id);
|
|
364
|
+
if (entry && item.createdAt >= entry.previewAt) setRoomPreview(entry, item);
|
|
365
|
+
added++;
|
|
366
|
+
}
|
|
367
|
+
if (entry && item.createdAt > entry.readTs && !(wasKnown && isOwnMessage(item, app)) && !entry.unreadIds.has(item.id)) {
|
|
368
|
+
entry.unreadIds.set(item.id, item); unreadChanged = true;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
353
371
|
if (entry) entry.receivedTs = items.reduce((latest, item) => Number.isSafeInteger(item.createdAt) ? Math.max(latest, item.createdAt) : latest, previousReceivedTs);
|
|
354
372
|
if (entry) entry.unreadOverflow = Boolean(data.hasMore);
|
|
355
373
|
if (messages.length > 500) messages.splice(0, messages.length - 500);
|
|
@@ -358,7 +376,7 @@ export function initMessages(app) {
|
|
|
358
376
|
await app.refreshRoomMeta(roomId, generation);
|
|
359
377
|
if (!isCurrentPoll(roomId, generation)) return;
|
|
360
378
|
}
|
|
361
|
-
|
|
379
|
+
if (added || unreadChanged || entry && previousUnreadOverflow !== entry.unreadOverflow) {
|
|
362
380
|
app.state.unread = entry ? entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0) : app.state.unread + added;
|
|
363
381
|
app.updateBadge();
|
|
364
382
|
}
|
|
@@ -432,6 +450,16 @@ export function initMessages(app) {
|
|
|
432
450
|
|
|
433
451
|
function trimMessages(list, app) {
|
|
434
452
|
if (list.children.length <= 400 || hasMessageSelection(list)) return;
|
|
453
|
+
const saved = app.state.positions.get(app.state.room?.id), rows = [...list.querySelectorAll("[data-msg-id]")];
|
|
454
|
+
const anchor = saved?.messageId && rows.find((row) => row.dataset.msgId === saved.messageId);
|
|
455
|
+
if (anchor && rows.indexOf(anchor) < rows.length - 280) {
|
|
456
|
+
const recent = rows.at(-300);
|
|
457
|
+
while (list.firstElementChild && list.firstElementChild !== anchor) list.firstElementChild.remove();
|
|
458
|
+
let next = anchor.nextElementSibling;
|
|
459
|
+
while (next && next !== recent) { const after = next.nextElementSibling; next.remove(); next = after; }
|
|
460
|
+
const gap = document.createElement("li"); gap.className = "gap"; app.setText(gap, "messagesBetweenOmitted"); anchor.after(gap);
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
435
463
|
while (list.children.length > 300) list.removeChild(list.firstChild);
|
|
436
464
|
if (list.firstElementChild && !list.firstElementChild.classList.contains("gap")) {
|
|
437
465
|
const gap = document.createElement("li");
|
|
@@ -441,4 +469,12 @@ function trimMessages(list, app) {
|
|
|
441
469
|
}
|
|
442
470
|
}
|
|
443
471
|
|
|
472
|
+
function isOwnMessage(message, app) {
|
|
473
|
+
const room = app.state.room, direct = room.kind === "direct";
|
|
474
|
+
const sameName = app.state.memberSessions.get(room.id)?.filter((member) => member.name === message.senderId);
|
|
475
|
+
return direct ? message.senderId === LS.sessionID
|
|
476
|
+
: message.senderSessionID ? message.senderSessionID === LS.sessionID
|
|
477
|
+
: message.senderId === app.state.myName && sameName?.length === 1 && sameName[0].sessionID === LS.sessionID;
|
|
478
|
+
}
|
|
479
|
+
|
|
444
480
|
}
|