@alexsun-top/opencode-chat-room 0.6.6 → 0.6.7
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 +13 -0
- package/cli.ts +1 -1
- package/docs/administrator-rooms.md +5 -4
- package/index.ts +2 -2
- package/package.json +3 -2
- package/plugin.ts +21 -20
- package/server/admin-routes.ts +15 -7
- package/server/admin-state.ts +2 -2
- package/server/announcement-routes.ts +2 -2
- package/server/assets/attachments.css +25 -0
- package/server/assets/base.css +42 -30
- package/server/assets/chat/admin-badge.js +27 -0
- package/server/assets/chat/admin.js +71 -24
- package/server/assets/chat/announcements.js +27 -11
- package/server/assets/chat/attachments.js +50 -10
- package/server/assets/chat/events.js +31 -7
- package/server/assets/chat/image-zoom.js +57 -0
- package/server/assets/chat/index.js +2 -0
- package/server/assets/chat/markdown.js +33 -4
- package/server/assets/chat/members.js +14 -6
- package/server/assets/chat/message-polling.js +137 -0
- package/server/assets/chat/message-window.js +14 -0
- package/server/assets/chat/messages.js +6 -128
- package/server/assets/chat/panels.js +37 -0
- package/server/assets/chat/receipts.js +3 -1
- package/server/assets/chat/reminders.js +19 -5
- package/server/assets/chat/room-list.js +51 -0
- package/server/assets/chat/rooms.js +20 -80
- package/server/assets/chat/state.js +28 -14
- package/server/assets/chat/system-messages.js +25 -7
- package/server/assets/conversation.css +18 -26
- package/server/assets/responsive.css +25 -8
- package/server/chat-server.ts +38 -8
- package/server/chat.html +103 -67
- package/server/history-route.ts +1 -1
- package/server/inbox.ts +2 -2
- package/server/member-routes.ts +4 -4
- package/server/newrooms.ts +96 -13
- package/server/resource-routes.ts +19 -5
- package/server/room-owner.ts +89 -0
- package/server/room-payload.ts +4 -2
- package/server/room-reminders.ts +3 -3
- package/server/sw.js +1 -0
- package/server/wake.ts +5 -5
- package/skills/chatroom/SKILL.md +3 -1
- package/src/README.md +20 -0
- package/src/{admin-client.ts → agent/admin-client.ts} +15 -9
- package/src/{admin-credentials.ts → agent/admin-credentials.ts} +1 -1
- package/src/{agent-presence.ts → agent/agent-presence.ts} +2 -2
- package/src/{agent-receipts.ts → agent/agent-receipts.ts} +2 -2
- package/src/{announcement-tool.ts → agent/announcement-tool.ts} +2 -2
- package/src/{chatroom-skill.ts → agent/chatroom-skill.ts} +1 -1
- package/src/{center-client.ts → center/center-client.ts} +22 -9
- package/src/{center-inbox.ts → center/center-inbox.ts} +9 -9
- package/src/{center-wake.ts → center/center-wake.ts} +6 -6
- package/src/{mentions.ts → center/mentions.ts} +1 -1
- package/src/{queue-transport.ts → center/queue-transport.ts} +3 -3
- package/src/{queue.ts → center/queue.ts} +2 -2
- package/src/{reminder-client.ts → center/reminder-client.ts} +1 -1
- package/src/{session-delivery.ts → center/session-delivery.ts} +1 -1
- package/src/{announcements.ts → rooms/announcements.ts} +2 -2
- package/src/{resource-upload.ts → rooms/resource-upload.ts} +1 -1
- package/src/{resources.ts → rooms/resources.ts} +55 -9
- package/src/{room-service.ts → rooms/room-service.ts} +12 -1
- package/src/{background.ts → runtime/background.ts} +3 -3
- package/src/{plugin-notifications.ts → runtime/plugin-notifications.ts} +8 -8
- package/src/{v1-plugin.ts → runtime/v1-plugin.ts} +4 -4
- package/src/{v2-plugin.ts → runtime/v2-plugin.ts} +9 -9
- package/src/{v2-receiver.ts → runtime/v2-receiver.ts} +7 -7
- package/src/{watch.ts → runtime/watch.ts} +12 -12
- package/src/{member-state.ts → state/member-state.ts} +2 -2
- package/src/{sqlite-state.ts → state/sqlite-state.ts} +35 -15
- package/src/{store.ts → state/store.ts} +2 -2
- package/src/tests/announcement-board.ts +176 -0
- /package/src/{resource-read.ts → rooms/resource-read.ts} +0 -0
- /package/src/{receiver-owner.ts → state/receiver-owner.ts} +0 -0
- /package/src/{receiver-state.ts → state/receiver-state.ts} +0 -0
|
@@ -3,6 +3,7 @@ import { LS } from "./state.js";
|
|
|
3
3
|
export function initAnnouncements(app) {
|
|
4
4
|
const $ = app.$, drafts = new Map();
|
|
5
5
|
const panel = $("#announcementPanel"), trigger = $("#btnAnnouncements"), list = $("#announcementList");
|
|
6
|
+
const dialog = $("#announcementDialog"), heading = $("#announcementDialogHeading");
|
|
6
7
|
let items = [], selected = "", generation = 0, roomGeneration = 0, editorRevision = 0, saveToken = 0;
|
|
7
8
|
const key = (id, name = selected) => `${id}\0${name ? `item\0${name}` : "new"}`;
|
|
8
9
|
const roomId = () => app.state.room?.id;
|
|
@@ -20,14 +21,15 @@ export function initAnnouncements(app) {
|
|
|
20
21
|
if (saved?.name === snapshot.name && saved.content === snapshot.content) drafts.delete(key(id, name));
|
|
21
22
|
}
|
|
22
23
|
function status(text) { $("#announcementStatus").textContent = text; }
|
|
24
|
+
function formStatus(text) { $("#announcementDialogStatus").textContent = text; }
|
|
23
25
|
function count() { $("#announcementCount").textContent = app.t("announcementCount", { count: Array.from($("#announcementContent").value).length }); }
|
|
24
26
|
function render() {
|
|
25
27
|
list.textContent = "";
|
|
26
28
|
for (const entry of items) {
|
|
27
29
|
const li = document.createElement("li"), button = document.createElement("button");
|
|
28
|
-
button.type = "button"; button.textContent = entry.name;
|
|
30
|
+
button.type = "button"; button.textContent = entry.name; button.setAttribute("aria-haspopup", "dialog");
|
|
29
31
|
if (entry.name === selected) button.setAttribute("aria-current", "true");
|
|
30
|
-
button.addEventListener("click", (event) => { event.stopPropagation();
|
|
32
|
+
button.addEventListener("click", (event) => { event.stopPropagation(); openEditor(entry); });
|
|
31
33
|
li.append(button); list.append(li);
|
|
32
34
|
}
|
|
33
35
|
if (!items.length) status(app.t("announcementEmpty"));
|
|
@@ -45,6 +47,14 @@ export function initAnnouncements(app) {
|
|
|
45
47
|
$("#btnDeleteAnnouncement").hidden = !selected;
|
|
46
48
|
render(); count();
|
|
47
49
|
}
|
|
50
|
+
function openEditor(entry) {
|
|
51
|
+
select(entry);
|
|
52
|
+
if (entry) { delete heading.dataset.i18n; heading.textContent = entry.name; }
|
|
53
|
+
else { heading.dataset.i18n = "announcementNew"; heading.textContent = app.t("announcementNew"); }
|
|
54
|
+
formStatus("");
|
|
55
|
+
if (!dialog.open) dialog.showModal();
|
|
56
|
+
$(entry ? "#announcementContent" : "#announcementName").focus();
|
|
57
|
+
}
|
|
48
58
|
async function load(id = roomId()) {
|
|
49
59
|
if (!id) return;
|
|
50
60
|
const request = ++generation, context = roomGeneration; status(app.t("announcementLoading"));
|
|
@@ -78,8 +88,8 @@ export function initAnnouncements(app) {
|
|
|
78
88
|
$("#announcementForm").addEventListener("submit", async (event) => {
|
|
79
89
|
event.preventDefault();
|
|
80
90
|
const id = roomId(), name = $("#announcementName").value.trim(), content = $("#announcementContent").value;
|
|
81
|
-
if (!id || !name || Array.from(name).length > 64 || Array.from(content).length > 5000) return
|
|
82
|
-
if (!selected && items.some((item) => item.name === name)) return
|
|
91
|
+
if (!id || !name || Array.from(name).length > 64 || Array.from(content).length > 5000) return formStatus(app.t("announcementInvalid"));
|
|
92
|
+
if (!selected && items.some((item) => item.name === name)) return formStatus(app.t("announcementDuplicate"));
|
|
83
93
|
const context = roomGeneration, captured = editorSnapshot(), draftName = selected, button = $("#btnSaveAnnouncement"), token = ++saveToken;
|
|
84
94
|
storeDraft(id, draftName); button.disabled = true;
|
|
85
95
|
try {
|
|
@@ -89,11 +99,12 @@ export function initAnnouncements(app) {
|
|
|
89
99
|
if (currentRoom(id, context)) {
|
|
90
100
|
generation++; items = [...items.filter((item) => item.name !== name), result.announcement]; render();
|
|
91
101
|
clearDraftIfUnchanged(id, draftName, captured);
|
|
92
|
-
if (editorMatches(captured)) { select(result.announcement); status(app.t("announcementSaved")); }
|
|
102
|
+
if (editorMatches(captured)) { select(result.announcement); status(app.t("announcementSaved")); formStatus(""); dialog.close(); }
|
|
93
103
|
else if (!selected) {
|
|
94
104
|
// Keep editing the newer draft under the name the create just reserved.
|
|
95
105
|
drafts.delete(key(id, draftName));
|
|
96
106
|
selected = result.announcement.name;
|
|
107
|
+
heading.textContent = selected; delete heading.dataset.i18n;
|
|
97
108
|
$("#announcementName").value = selected; $("#announcementName").readOnly = true;
|
|
98
109
|
$("#btnDeleteAnnouncement").hidden = false;
|
|
99
110
|
drafts.set(key(id, selected), { name: selected, content: $("#announcementContent").value });
|
|
@@ -101,11 +112,11 @@ export function initAnnouncements(app) {
|
|
|
101
112
|
}
|
|
102
113
|
}
|
|
103
114
|
} catch (error) {
|
|
104
|
-
if (currentRoom(id, context) && editorMatches(captured))
|
|
115
|
+
if (currentRoom(id, context) && editorMatches(captured)) formStatus(error.status === 403 ? error.message : app.t("announcementLoadFailed"));
|
|
105
116
|
}
|
|
106
117
|
finally { if (token === saveToken) button.disabled = false; }
|
|
107
118
|
});
|
|
108
|
-
$("#btnNewAnnouncement").addEventListener("click", () =>
|
|
119
|
+
$("#btnNewAnnouncement").addEventListener("click", () => openEditor(null));
|
|
109
120
|
$("#btnDeleteAnnouncement").addEventListener("click", async () => {
|
|
110
121
|
const id = roomId(), name = selected;
|
|
111
122
|
if (!id || !name || !confirm(app.t("announcementDeleteConfirm", { name }))) return;
|
|
@@ -115,20 +126,25 @@ export function initAnnouncements(app) {
|
|
|
115
126
|
if (currentRoom(id, context)) {
|
|
116
127
|
generation++; items = items.filter((item) => item.name !== name); render();
|
|
117
128
|
clearDraftIfUnchanged(id, name, captured);
|
|
118
|
-
if (editorMatches(captured)) { select(null); status(app.t("announcementDeleted")); }
|
|
129
|
+
if (editorMatches(captured)) { select(null); status(app.t("announcementDeleted")); formStatus(""); dialog.close(); }
|
|
119
130
|
}
|
|
120
|
-
} catch (error) { if (currentRoom(id, context) && editorMatches(captured))
|
|
131
|
+
} catch (error) { if (currentRoom(id, context) && editorMatches(captured)) formStatus(error.message); }
|
|
121
132
|
});
|
|
122
133
|
$("#btnRefreshAnnouncements").addEventListener("click", () => void load());
|
|
134
|
+
$("#btnCloseAnnouncement").addEventListener("click", () => dialog.close());
|
|
135
|
+
$("#btnCancelAnnouncement").addEventListener("click", () => dialog.close());
|
|
136
|
+
dialog.addEventListener("click", (event) => { if (event.target === dialog) dialog.close(); });
|
|
123
137
|
trigger.addEventListener("click", () => {
|
|
124
138
|
const open = panel.hidden; app.closePanels(); panel.hidden = !open;
|
|
125
139
|
trigger.setAttribute("aria-expanded", String(open)); if (open) { void load(); $("#announcementHeading").focus(); }
|
|
126
140
|
});
|
|
127
|
-
document.addEventListener("keydown", (event) => { if (event.key === "Escape" && !panel.hidden) { app.closePanels(); trigger.focus(); } });
|
|
141
|
+
document.addEventListener("keydown", (event) => { if (event.key === "Escape" && !dialog.open && !panel.hidden) { app.closePanels(); trigger.focus(); } });
|
|
128
142
|
const close = app.closePanels;
|
|
129
|
-
app.closePanels = () => { close(); panel.hidden = true; trigger.setAttribute("aria-expanded", "false"); };
|
|
143
|
+
app.closePanels = () => { close(); panel.hidden = true; trigger.setAttribute("aria-expanded", "false"); if (dialog.open) dialog.close(); };
|
|
130
144
|
document.addEventListener("chat-room-activated", (event) => {
|
|
131
145
|
const id = event.detail;
|
|
146
|
+
if (dialog.open) dialog.close();
|
|
147
|
+
formStatus("");
|
|
132
148
|
roomGeneration++; generation++; saveToken++; $("#btnSaveAnnouncement").disabled = false;
|
|
133
149
|
selected = ""; items = []; select(null); void load(id);
|
|
134
150
|
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { LS } from "./state.js";
|
|
2
|
+
import { followsBottom } from "./message-window.js";
|
|
2
3
|
|
|
3
4
|
const MAX_PARALLEL_UPLOADS = 2;
|
|
5
|
+
const MAX_BUFFERED_BYTES = 32 * 1024 * 1024; // Pasted files are small; larger ones stream to keep the heap bounded.
|
|
4
6
|
const TEXT_LIMIT = 2000;
|
|
5
7
|
const previewTypes = new Set(["image/png", "image/jpeg", "image/gif", "image/webp", "image/avif", "image/bmp"]);
|
|
6
8
|
|
|
@@ -13,6 +15,14 @@ export function initAttachments(app) {
|
|
|
13
15
|
const roomId = () => app.state.room?.id || "";
|
|
14
16
|
const filesFor = (id) => drafts.get(id) || [];
|
|
15
17
|
const markers = (items) => items.filter((item) => item.resource).map((item) => `[[resource:${item.resource.id}]]`);
|
|
18
|
+
const validResource = (resource, id) => /^[\da-f]{8}-[\da-f]{4}-[1-8][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i.test(resource?.id) && resource.roomID === id;
|
|
19
|
+
// crypto.randomUUID needs a secure context; the chat also runs on plain-HTTP LANs.
|
|
20
|
+
function newUploadID() {
|
|
21
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
22
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40; bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
23
|
+
const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
24
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
25
|
+
}
|
|
16
26
|
const textFor = (id, caption) => {
|
|
17
27
|
const parts = [String(caption || "").trim(), ...markers(filesFor(id))].filter(Boolean);
|
|
18
28
|
return { text: parts.join("\n\n"), ids: filesFor(id).filter((item) => item.resource).map((item) => item.resource.id) };
|
|
@@ -25,7 +35,9 @@ export function initAttachments(app) {
|
|
|
25
35
|
return `${value < 10 ? value.toFixed(1) : Math.round(value)} ${units[index]}`;
|
|
26
36
|
}
|
|
27
37
|
function preserveLayout(change) {
|
|
28
|
-
const thread = $("#thread")
|
|
38
|
+
const thread = $("#thread");
|
|
39
|
+
// A follower can sit a few lines above the edge while a restored draft grows the composer.
|
|
40
|
+
const atBottom = Boolean(roomId() && followsBottom(app, 0));
|
|
29
41
|
if (atBottom) { change(); thread.scrollTop = thread.scrollHeight; }
|
|
30
42
|
else if (roomId() && app.preserveReadingLayout) void app.preserveReadingLayout(change).catch(() => {});
|
|
31
43
|
else change();
|
|
@@ -152,14 +164,16 @@ export function initAttachments(app) {
|
|
|
152
164
|
item.refreshLimit = false;
|
|
153
165
|
if (item.cancelled) return;
|
|
154
166
|
if (max !== null && item.file.size > max) { item.errorKey = "fileTooLarge"; item.status = "failed"; if (roomId() === id) setStatus(item.errorKey, true, id); return; }
|
|
155
|
-
item.
|
|
167
|
+
if (item.pasted && item.buffer === undefined && item.file.size <= MAX_BUFFERED_BYTES) item.buffer = await item.file.arrayBuffer().catch(() => null);
|
|
168
|
+
if (item.cancelled) return;
|
|
169
|
+
item.resource = await sendUpload(id, item);
|
|
156
170
|
if (item.cancelled) return;
|
|
157
|
-
item.status = "uploaded"; item.loaded = item.file.size; item.percent = 100;
|
|
171
|
+
item.status = "uploaded"; item.loaded = item.file.size; item.percent = 100; item.buffer = null;
|
|
158
172
|
if (roomId() === id && filesFor(id).every((entry) => entry.status === "uploaded")) setStatus("uploadReady");
|
|
159
173
|
} catch (error) {
|
|
160
174
|
if (item.cancelled) return;
|
|
161
175
|
if (error.status === 413) { limit = null; item.errorKey = "fileTooLarge"; }
|
|
162
|
-
else item.errorKey = "uploadFailed";
|
|
176
|
+
else { item.errorKey = "uploadFailed"; item.errorValues = { reason: error.status ? `HTTP ${error.status}` : error.timeout ? "timeout" : error.network ? "network" : error.message }; }
|
|
163
177
|
item.status = "failed";
|
|
164
178
|
if (roomId() === id) setStatus(item.errorKey, true, id, item.errorValues || {});
|
|
165
179
|
} finally {
|
|
@@ -168,6 +182,29 @@ export function initAttachments(app) {
|
|
|
168
182
|
pump();
|
|
169
183
|
}
|
|
170
184
|
}
|
|
185
|
+
// One fallback after a network failure: a lost response is recovered by ID, and a
|
|
186
|
+
// file the XHR could not send is retried once from bytes read into memory.
|
|
187
|
+
async function sendUpload(id, item) {
|
|
188
|
+
try { return await upload(id, item); }
|
|
189
|
+
catch (error) {
|
|
190
|
+
if (!error.network || item.cancelled || item.retried) throw error;
|
|
191
|
+
item.retried = true; item.loaded = 0; item.percent = 0;
|
|
192
|
+
const existing = await findUploaded(id, item);
|
|
193
|
+
if (item.cancelled) throw error;
|
|
194
|
+
if (existing) return existing;
|
|
195
|
+
if (item.buffer === undefined && item.file.size <= MAX_BUFFERED_BYTES) item.buffer = await item.file.arrayBuffer().catch(() => null);
|
|
196
|
+
if (item.cancelled || !item.buffer) throw error;
|
|
197
|
+
return upload(id, item);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
async function findUploaded(id, item) {
|
|
201
|
+
try {
|
|
202
|
+
const response = await fetch(`/rooms/${encodeURIComponent(id)}/resources/${item.uploadID}`, { headers: LS.token ? { Authorization: `Bearer ${LS.token}` } : {} });
|
|
203
|
+
if (!response.ok) return undefined;
|
|
204
|
+
const resource = (await response.json()).resource;
|
|
205
|
+
return validResource(resource, id) && resource.name === item.file.name && resource.uploaderSessionID === LS.sessionID ? resource : undefined;
|
|
206
|
+
} catch { return undefined; }
|
|
207
|
+
}
|
|
171
208
|
function upload(id, item) {
|
|
172
209
|
return new Promise((resolve, reject) => {
|
|
173
210
|
const xhr = new XMLHttpRequest(); item.xhr = xhr;
|
|
@@ -175,6 +212,7 @@ export function initAttachments(app) {
|
|
|
175
212
|
xhr.setRequestHeader("Content-Type", "application/octet-stream");
|
|
176
213
|
xhr.setRequestHeader("X-Session-ID", LS.sessionID);
|
|
177
214
|
xhr.setRequestHeader("X-File-Name", encodeURIComponent(item.file.name));
|
|
215
|
+
xhr.setRequestHeader("X-Resource-ID", item.uploadID);
|
|
178
216
|
if (LS.token) xhr.setRequestHeader("Authorization", `Bearer ${LS.token}`);
|
|
179
217
|
xhr.timeout = 1800000;
|
|
180
218
|
xhr.upload.onprogress = (event) => {
|
|
@@ -189,27 +227,29 @@ export function initAttachments(app) {
|
|
|
189
227
|
if (xhr.status !== 201) return reject(Object.assign(new Error("Upload failed"), { status: xhr.status }));
|
|
190
228
|
try {
|
|
191
229
|
const resource = JSON.parse(xhr.responseText).resource;
|
|
192
|
-
if (
|
|
230
|
+
if (!validResource(resource, id)) throw new Error("Invalid resource response");
|
|
193
231
|
resolve(resource);
|
|
194
232
|
} catch { reject(new Error("Invalid resource response")); }
|
|
195
233
|
};
|
|
234
|
+
// A browser can surface a file handle it cannot read (cloud placeholder, network
|
|
235
|
+
// drive, cleaned temp file) as a bare network error; XHR exposes no finer cause.
|
|
196
236
|
xhr.onerror = () => { item.xhr = null; reject(Object.assign(new Error("Upload failed"), { network: true })); };
|
|
197
237
|
xhr.ontimeout = () => { item.xhr = null; reject(Object.assign(new Error("Upload timed out"), { timeout: true })); };
|
|
198
238
|
xhr.onabort = () => { item.xhr = null; reject(Object.assign(new Error("Upload cancelled"), { aborted: true })); };
|
|
199
|
-
xhr.send(item.file);
|
|
239
|
+
xhr.send(item.buffer ?? item.file);
|
|
200
240
|
});
|
|
201
241
|
}
|
|
202
|
-
function addFiles(files) {
|
|
242
|
+
function addFiles(files, pasted = false) {
|
|
203
243
|
const id = roomId(); if (!id) return;
|
|
204
244
|
const accepted = [...files].filter((file) => file instanceof File);
|
|
205
245
|
if (!accepted.length) return;
|
|
206
246
|
const items = filesFor(id);
|
|
207
|
-
for (const file of accepted) items.push({ file, status: "queued", loaded: 0, percent: 0, cancelled: false });
|
|
247
|
+
for (const file of accepted) items.push({ file, uploadID: newUploadID(), pasted, buffer: undefined, retried: false, status: "queued", loaded: 0, percent: 0, cancelled: false });
|
|
208
248
|
drafts.set(id, items); setStatus(""); render(); pump();
|
|
209
249
|
}
|
|
210
250
|
function retryItem(item) {
|
|
211
251
|
if (item.status !== "failed") return;
|
|
212
|
-
item.refreshLimit = item.errorKey === "fileTooLarge"; item.status = "queued"; item.errorKey = ""; item.errorValues = null; item.loaded = 0; item.percent = 0;
|
|
252
|
+
item.refreshLimit = item.errorKey === "fileTooLarge"; item.status = "queued"; item.errorKey = ""; item.errorValues = null; item.loaded = 0; item.percent = 0; item.retried = false;
|
|
213
253
|
if (roomId()) setStatus(""); render(); pump();
|
|
214
254
|
}
|
|
215
255
|
$("#btnAddAttachments").addEventListener("click", () => picker.click());
|
|
@@ -233,7 +273,7 @@ export function initAttachments(app) {
|
|
|
233
273
|
const images = Array.from(event.clipboardData?.items || []).filter((item) => item.kind === "file" && item.type.startsWith("image/")).map((item) => item.getAsFile()).filter(Boolean);
|
|
234
274
|
if (!images.length) return;
|
|
235
275
|
if (!event.clipboardData.getData("text/plain")) event.preventDefault();
|
|
236
|
-
addFiles(images);
|
|
276
|
+
addFiles(images, true);
|
|
237
277
|
});
|
|
238
278
|
document.addEventListener("chat-room-activated", (event) => {
|
|
239
279
|
render(); const id = event.detail; $("#btnAddAttachments").disabled = !id;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { LS } from "./state.js";
|
|
2
2
|
import { copyTextToClipboard } from "./markdown.js";
|
|
3
|
+
import { followsBottom, pointerOrSelectionActive } from "./message-window.js";
|
|
3
4
|
|
|
4
5
|
export function initEvents(app) {
|
|
5
6
|
const $ = app.$;
|
|
@@ -19,6 +20,14 @@ export function initEvents(app) {
|
|
|
19
20
|
new ResizeObserver(syncHeadBottom).observe(chatHead);
|
|
20
21
|
syncHeadBottom();
|
|
21
22
|
}
|
|
23
|
+
// The rail footer divider sits above the identity row, so it has to land on the
|
|
24
|
+
// composer's divider at any composer height, including five wrapped lines.
|
|
25
|
+
const composer = document.querySelector("#chatView > .composer");
|
|
26
|
+
if (composer) {
|
|
27
|
+
const syncComposerBlock = () => document.documentElement.style.setProperty("--composer-block", `${Math.round(composer.getBoundingClientRect().height)}px`);
|
|
28
|
+
new ResizeObserver(syncComposerBlock).observe(composer);
|
|
29
|
+
syncComposerBlock();
|
|
30
|
+
}
|
|
22
31
|
// Images, diagrams and attachments grow the list after the first paint and the browser
|
|
23
32
|
// answers with a scroll that looks like the reader moving. Subtract the growth: only a
|
|
24
33
|
// reader who was already at the bottom gets pinned back.
|
|
@@ -33,10 +42,9 @@ export function initEvents(app) {
|
|
|
33
42
|
// Rows arrive in bursts, so judging each burst on its own loses the reader after the
|
|
34
43
|
// first one. Follow while the reader has not scrolled away: then every growth pins.
|
|
35
44
|
// A changed viewport height (composer, window) is not list growth and keeps its anchor.
|
|
36
|
-
|
|
37
|
-
//
|
|
38
|
-
|
|
39
|
-
if (grew && sameBox && app.state.followBottom && !selecting && !app.state.threadPointerDown
|
|
45
|
+
// A pointer or selection is mid-gesture: moving the content under it loses a drag,
|
|
46
|
+
// a click, or the reader's own position.
|
|
47
|
+
if (grew && sameBox && followsBottom(app)
|
|
40
48
|
&& thread.scrollHeight - thread.scrollTop - thread.clientHeight > 1) {
|
|
41
49
|
thread.scrollTop = thread.scrollHeight;
|
|
42
50
|
}
|
|
@@ -82,7 +90,7 @@ export function initEvents(app) {
|
|
|
82
90
|
await new Promise((resolve) => requestAnimationFrame(resolve));
|
|
83
91
|
if (roomId && app.state.room?.id === roomId && generation === scrollGeneration) {
|
|
84
92
|
const thread = $("#thread");
|
|
85
|
-
if (saved.atBottom) thread.scrollTop = thread.scrollHeight;
|
|
93
|
+
if (saved.atBottom && followsBottom(app)) thread.scrollTop = thread.scrollHeight;
|
|
86
94
|
else if (saved.messageId) {
|
|
87
95
|
const row = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((item) => item.dataset.msgId === saved.messageId);
|
|
88
96
|
if (row) thread.scrollTop += row.getBoundingClientRect().top - (thread.getBoundingClientRect().top + thread.clientTop + saved.offset);
|
|
@@ -129,7 +137,7 @@ export function initEvents(app) {
|
|
|
129
137
|
resizeFrame = 0;
|
|
130
138
|
const position = readingPosition, thread = $("#thread");
|
|
131
139
|
if (!position?.roomId || position.roomId !== app.state.room?.id) return captureReadingPosition();
|
|
132
|
-
if (position.atBottom) thread.scrollTop = thread.scrollHeight;
|
|
140
|
+
if (position.atBottom && !pointerOrSelectionActive(app)) thread.scrollTop = thread.scrollHeight;
|
|
133
141
|
else if (position.messageId) {
|
|
134
142
|
const row = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((item) => item.dataset.msgId === position.messageId);
|
|
135
143
|
if (row) thread.scrollTop += row.getBoundingClientRect().top - (thread.getBoundingClientRect().top + thread.clientTop + position.offset);
|
|
@@ -188,6 +196,11 @@ export function initEvents(app) {
|
|
|
188
196
|
$("#restoreError").hidden = true;
|
|
189
197
|
void app.refreshRoomList();
|
|
190
198
|
});
|
|
199
|
+
$("#btnCancelToken").addEventListener("click", () => $("#tokenBox").close());
|
|
200
|
+
$("#tokenBox").addEventListener("close", () => {
|
|
201
|
+
if (LS.token) return;
|
|
202
|
+
try { sessionStorage.setItem("chat.keyPromptDismissed", "1"); } catch { /* Storage may be blocked. */ }
|
|
203
|
+
});
|
|
191
204
|
$("#btnRefreshRooms").addEventListener("click", () => void app.refreshRoomList());
|
|
192
205
|
$("#btnNewRoom").addEventListener("click", () => {
|
|
193
206
|
const form = $("#railRoomForm");
|
|
@@ -310,7 +323,7 @@ export function initEvents(app) {
|
|
|
310
323
|
return;
|
|
311
324
|
}
|
|
312
325
|
if (app.mentionActive && !event.target.closest("#mentionPicker")) app.closeMention();
|
|
313
|
-
if (!event.target.closest(".anchor")) app.closePanels();
|
|
326
|
+
if (!event.target.closest(".anchor, dialog")) app.closePanels();
|
|
314
327
|
if (!event.target.closest("#railRoomForm, #btnNewRoom")) $("#railRoomForm").hidden = true;
|
|
315
328
|
});
|
|
316
329
|
document.addEventListener("keydown", (event) => {
|
|
@@ -328,6 +341,15 @@ export function initEvents(app) {
|
|
|
328
341
|
app.closePanels();
|
|
329
342
|
app.lastFocus?.focus();
|
|
330
343
|
}
|
|
344
|
+
if (event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
|
|
345
|
+
// Same switch as a rail row click on a joined room, in the order the rail renders.
|
|
346
|
+
const joined = (app.state.availableRooms || []).filter((room) => app.state.rooms.has(room.id));
|
|
347
|
+
const index = joined.findIndex((room) => room.id === app.state.room?.id);
|
|
348
|
+
if (event.defaultPrevented || joined.length < 2 || index < 0 || document.querySelector("dialog[open]")) return;
|
|
349
|
+
event.preventDefault();
|
|
350
|
+
const step = event.key === "ArrowDown" ? 1 : -1;
|
|
351
|
+
app.activateRoom(joined[(index + step + joined.length) % joined.length].id);
|
|
352
|
+
}
|
|
331
353
|
});
|
|
332
354
|
|
|
333
355
|
app.applyLocale();
|
|
@@ -345,6 +367,8 @@ function saveAccessToken(button, app) {
|
|
|
345
367
|
let persistent = false;
|
|
346
368
|
try { persistent = localStorage.getItem("chat.token") === LS.token; } catch { /* Memory fallback is tab-scoped. */ }
|
|
347
369
|
app.flash(button, persistent ? "saved" : "savedMemory");
|
|
370
|
+
// The key prompt is a one-time visit: a stored key gets out of the way again.
|
|
371
|
+
if (LS.token) document.getElementById("tokenBox")?.close();
|
|
348
372
|
void app.refreshRoomList();
|
|
349
373
|
app.retryPendingAction();
|
|
350
374
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// An image in a message opens full-screen in a native dialog. Nothing in the
|
|
2
|
+
// message list moves: the overlay is a top-layer dialog, not a row change.
|
|
3
|
+
export function initImageZoom(app) {
|
|
4
|
+
const dialog = app.$("#imageDialog"), preview = app.$("#imageDialogImage");
|
|
5
|
+
let trigger = null;
|
|
6
|
+
const imageOf = (node) => node?.closest?.("#msgList img, #attachmentList img");
|
|
7
|
+
function open(image) {
|
|
8
|
+
if (!image || !dialog) return;
|
|
9
|
+
trigger = image;
|
|
10
|
+
// Reuse the URL the list already loaded, so the overlay costs no second download.
|
|
11
|
+
preview.src = image.currentSrc || image.src;
|
|
12
|
+
preview.alt = image.alt || "";
|
|
13
|
+
dialog.showModal();
|
|
14
|
+
}
|
|
15
|
+
const close = () => { if (dialog.open) dialog.close(); };
|
|
16
|
+
// A click that focuses the image makes the browser scroll it into view; the
|
|
17
|
+
// overlay must leave the thread exactly where the reader left it.
|
|
18
|
+
for (const selector of ["#msgList", "#attachmentList"]) app.$(selector)?.addEventListener("mousedown", (event) => {
|
|
19
|
+
if (imageOf(event.target)) event.preventDefault();
|
|
20
|
+
});
|
|
21
|
+
for (const selector of ["#msgList", "#attachmentList"]) app.$(selector)?.addEventListener("click", (event) => {
|
|
22
|
+
const image = imageOf(event.target);
|
|
23
|
+
if (!image) return;
|
|
24
|
+
// An image wrapped in a link is zoomed, not followed.
|
|
25
|
+
event.preventDefault();
|
|
26
|
+
open(image);
|
|
27
|
+
});
|
|
28
|
+
document.addEventListener("keydown", (event) => {
|
|
29
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
30
|
+
const image = imageOf(event.target);
|
|
31
|
+
if (!image) return;
|
|
32
|
+
event.preventDefault();
|
|
33
|
+
open(image);
|
|
34
|
+
});
|
|
35
|
+
app.$("#btnCloseImage")?.addEventListener("click", close);
|
|
36
|
+
dialog.addEventListener("click", (event) => { if (event.target === dialog) close(); });
|
|
37
|
+
dialog.addEventListener("close", () => {
|
|
38
|
+
preview.removeAttribute("src");
|
|
39
|
+
if (trigger?.isConnected) trigger.focus({ preventScroll: true });
|
|
40
|
+
trigger = null;
|
|
41
|
+
});
|
|
42
|
+
// Images are in the tab order so the overlay can hand focus back to them.
|
|
43
|
+
const enhance = () => {
|
|
44
|
+
for (const image of document.querySelectorAll("#msgList img, #attachmentList img")) {
|
|
45
|
+
if (image.hasAttribute("tabindex")) continue;
|
|
46
|
+
image.tabIndex = 0;
|
|
47
|
+
image.setAttribute("role", "button");
|
|
48
|
+
image.dataset.i18nAriaLabel = "zoomImage";
|
|
49
|
+
image.setAttribute("aria-label", app.t("zoomImage"));
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
for (const selector of ["#msgList", "#attachmentList"]) {
|
|
53
|
+
const list = app.$(selector);
|
|
54
|
+
if (list) new MutationObserver(enhance).observe(list, { childList: true, subtree: true });
|
|
55
|
+
}
|
|
56
|
+
enhance();
|
|
57
|
+
}
|
|
@@ -11,6 +11,7 @@ import { initAdmin } from "./admin.js";
|
|
|
11
11
|
import { initAttachments } from "./attachments.js";
|
|
12
12
|
import { initReplies } from "./replies.js";
|
|
13
13
|
import { initHistoryLoader } from "./history-loader.js";
|
|
14
|
+
import { initImageZoom } from "./image-zoom.js";
|
|
14
15
|
import { initReminders } from "./reminders.js";
|
|
15
16
|
import { initPwa } from "./pwa.js";
|
|
16
17
|
|
|
@@ -27,4 +28,5 @@ initEvents(app);
|
|
|
27
28
|
initAnnouncements(app);
|
|
28
29
|
initReminders(app);
|
|
29
30
|
initAdmin(app);
|
|
31
|
+
initImageZoom(app);
|
|
30
32
|
initPwa();
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { createRandomId } from "./state.js";
|
|
1
|
+
import { createRandomId, LS } from "./state.js";
|
|
2
|
+
import { isRoomAdmin } from "./admin-badge.js";
|
|
2
3
|
|
|
3
4
|
const mentionPattern = /<at>@([^<]+)<\/at>/g;
|
|
4
5
|
const resourcePattern = /\[\[resource:([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})\]\]/gi;
|
|
@@ -135,7 +136,12 @@ async function loadResourceCard(card, roomId, app) {
|
|
|
135
136
|
// Media you sent yourself is shown inline; other people's media stays collapsed.
|
|
136
137
|
if (card.closest(".msg.own")) void previewResource(card, roomId, id, resource, type, preview, app, status);
|
|
137
138
|
}
|
|
138
|
-
|
|
139
|
+
if (isRoomAdmin(app)) {
|
|
140
|
+
const remove = resourceButton("resourceDelete", resource.name, () => deleteResource(card, roomId, id, resource, app, status, remove), app);
|
|
141
|
+
remove.classList.add("danger-text");
|
|
142
|
+
actions.append(remove);
|
|
143
|
+
}
|
|
144
|
+
} catch (error) { if (card.isConnected) setResourceError(card, app, error?.status === 404 ? "resourceDeleted" : "resourceFailed"); }
|
|
139
145
|
}
|
|
140
146
|
function setResourceButton(button, key, name, app) {
|
|
141
147
|
button.dataset.i18n = key; button.textContent = app.t(key);
|
|
@@ -147,6 +153,29 @@ function resourceButton(key, name, action, app) {
|
|
|
147
153
|
setResourceButton(button, key, name, app);
|
|
148
154
|
button.addEventListener("click", action); return button;
|
|
149
155
|
}
|
|
156
|
+
async function deleteResource(card, roomId, id, resource, app, status, button) {
|
|
157
|
+
if (!confirm(app.t("resourceDeleteConfirm", { name: resource.name }))) return;
|
|
158
|
+
button.disabled = true;
|
|
159
|
+
status.textContent = app.t("resourceDeleting");
|
|
160
|
+
try {
|
|
161
|
+
await app.api(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}`, { method: "DELETE", headers: { "x-session-id": LS.sessionID }, body: "{}" });
|
|
162
|
+
if (!card.isConnected) return;
|
|
163
|
+
card._previewElement?.remove();
|
|
164
|
+
if (card._resourceUrl) URL.revokeObjectURL(card._resourceUrl);
|
|
165
|
+
previewUrls.delete(card); delete card._resourceUrl; delete card._previewElement;
|
|
166
|
+
card.querySelector(".resource-actions")?.remove();
|
|
167
|
+
status.textContent = app.t("resourceDeleted");
|
|
168
|
+
// The same file can appear in quoted or repeated messages; mark those copies too.
|
|
169
|
+
for (const other of document.querySelectorAll(".chat-resource")) {
|
|
170
|
+
if (other === card || other.dataset.resourceId !== id) continue;
|
|
171
|
+
other.querySelector(".resource-actions")?.remove();
|
|
172
|
+
const otherStatus = other.querySelector(".resource-status");
|
|
173
|
+
if (otherStatus) otherStatus.textContent = app.t("resourceDeleted");
|
|
174
|
+
}
|
|
175
|
+
} catch {
|
|
176
|
+
if (card.isConnected) { status.textContent = app.t("resourceDeleteFailed"); button.disabled = false; }
|
|
177
|
+
}
|
|
178
|
+
}
|
|
150
179
|
async function downloadResource(card, roomId, id, resource, app, status, button) {
|
|
151
180
|
if (!card.isConnected || button.disabled) return;
|
|
152
181
|
button.disabled = true;
|
|
@@ -227,10 +256,10 @@ function formatBytes(value) {
|
|
|
227
256
|
if (bytes < 1024) return `${bytes} B`;
|
|
228
257
|
return bytes < 1048576 ? `${(bytes / 1024).toFixed(1)} KB` : `${(bytes / 1048576).toFixed(1)} MB`;
|
|
229
258
|
}
|
|
230
|
-
function setResourceError(card, app) {
|
|
259
|
+
function setResourceError(card, app, key = "resourceFailed") {
|
|
231
260
|
card.textContent = "";
|
|
232
261
|
const status = document.createElement("span"); status.className = "resource-status";
|
|
233
|
-
status.setAttribute("role", "status"); status.textContent = app.t(
|
|
262
|
+
status.setAttribute("role", "status"); status.textContent = app.t(key); card.append(status);
|
|
234
263
|
}
|
|
235
264
|
function watchPreviewRemoval() {
|
|
236
265
|
const list = document.querySelector("#msgList");
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LS } from "./state.js";
|
|
2
|
+
import { adminAgentBadge, canRemoveMember, isAdminAgent } from "./admin-badge.js";
|
|
2
3
|
|
|
3
4
|
export function initMembers(app) {
|
|
4
5
|
const $ = app.$;
|
|
@@ -44,17 +45,24 @@ export function initMembers(app) {
|
|
|
44
45
|
if (changed && room.kind !== "direct" && room.messages.some((message) => !message.senderSessionID && message.senderId === app.state.myName)) app.rerenderMessages();
|
|
45
46
|
const list = $("#memberList");
|
|
46
47
|
const rows = Array.from(list.querySelectorAll("li[data-member-session-id]"));
|
|
47
|
-
|
|
48
|
+
const actionKey = `${room.ownerId}\u0000${(room.adminIds ?? []).join("\u0000")}`;
|
|
49
|
+
const actionsChanged = list.dataset.actionKey !== actionKey;
|
|
50
|
+
list.dataset.actionKey = actionKey;
|
|
51
|
+
if (actionsChanged || signature(rows.map((row) => ({ sessionID: row.dataset.memberSessionId, name: row.dataset.memberName, type: row.dataset.memberType }))) !== signature(members)) {
|
|
48
52
|
list.textContent = "";
|
|
53
|
+
const selfIsAgent = members.find((member) => member.sessionID === LS.sessionID)?.type === "agent";
|
|
49
54
|
for (const member of members) {
|
|
50
|
-
const row = document.createElement("li"), name = document.createElement("span"), actions = document.createElement("span");
|
|
55
|
+
const row = document.createElement("li"), name = document.createElement("span"), identity = document.createElement("span"), actions = document.createElement("span");
|
|
51
56
|
row.dataset.memberSessionId = member.sessionID; row.dataset.memberName = member.name; row.dataset.memberType = member.type;
|
|
52
|
-
name.textContent = member.name;
|
|
57
|
+
name.textContent = member.name; identity.className = "member-identity"; identity.append(name);
|
|
58
|
+
if (isAdminAgent(app, member.sessionID)) identity.append(adminAgentBadge(app));
|
|
59
|
+
row.append(identity);
|
|
53
60
|
if (member.type === "agent") row.append(activityBadge(member, app));
|
|
54
61
|
actions.className = "member-actions";
|
|
55
62
|
if (member.sessionID !== LS.sessionID) {
|
|
56
63
|
actions.append(actionButton(member, "messagePrivately", app.startDirect));
|
|
57
|
-
|
|
64
|
+
// The backend binds the real membership; the UI only hides impossible actions.
|
|
65
|
+
if (canRemoveMember(app, member, selfIsAgent, LS.sessionID)) actions.append(actionButton(member, "removeMember", app.kickMember));
|
|
58
66
|
}
|
|
59
67
|
row.append(actions); list.append(row);
|
|
60
68
|
}
|
|
@@ -139,8 +147,7 @@ export function initMembers(app) {
|
|
|
139
147
|
};
|
|
140
148
|
|
|
141
149
|
app.kickMember = async (member) => {
|
|
142
|
-
|
|
143
|
-
if (!confirm(app.t("kickConfirm", { name: member.name, id }))) return;
|
|
150
|
+
if (!confirm(app.t("kickConfirm", { name: member.name }))) return;
|
|
144
151
|
try {
|
|
145
152
|
await app.api(`/rooms/${encodeURIComponent(app.state.room.id)}/kick`, {
|
|
146
153
|
method: "POST",
|
|
@@ -155,6 +162,7 @@ export function initMembers(app) {
|
|
|
155
162
|
button.type = "button";
|
|
156
163
|
button.className = "member-action";
|
|
157
164
|
button.textContent = app.t(label);
|
|
165
|
+
button.title = `${button.textContent}: ${member.name}`;
|
|
158
166
|
button.addEventListener("click", () => action(member));
|
|
159
167
|
return button;
|
|
160
168
|
}
|