@alexsun-top/opencode-chat-room 0.8.2 → 0.8.3
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 +10 -0
- package/README.md +18 -62
- package/README.zh-CN.md +25 -69
- package/dist/cli.js +466 -188
- package/package.json +8 -5
- package/packages/chat-room-channeld/README.md +25 -3
- package/packages/chat-room-channeld/dist/mock-adapter.js +5 -5
- package/packages/chat-room-channeld/src/adapter.ts +5 -0
- package/packages/chat-room-channeld/src/cli.ts +72 -5
- package/packages/chat-room-channeld/src/host.ts +17 -2
- package/packages/chat-room-channeld/src/pg/database.ts +187 -0
- package/packages/chat-room-channeld/src/pg/migrate.ts +150 -0
- package/packages/chat-room-channeld/src/pg/schema.ts +60 -0
- package/packages/chat-room-channeld/src/pg/sql.mjs +175 -0
- package/packages/chat-room-channeld/src/pg/sqlite.ts +100 -0
- package/packages/chat-room-channeld/src/pg/worker.mjs +133 -0
- package/packages/chat-room-channeld/src/protocol.ts +1 -0
- package/packages/chat-room-channeld/src/state.ts +73 -97
- package/packages/chat-room-channeld/src/stats.ts +140 -109
- package/server/assets/base.css +3 -1
- package/server/assets/chat/admin-badge.js +32 -30
- package/server/assets/chat/admin.js +198 -161
- package/server/assets/chat/announcements.js +295 -208
- package/server/assets/chat/attachment-store.js +106 -75
- package/server/assets/chat/attachments.js +686 -441
- package/server/assets/chat/chat-background.js +185 -156
- package/server/assets/chat/clipboard.js +44 -25
- package/server/assets/chat/confirm-dialog.js +36 -33
- package/server/assets/chat/copy-action.js +40 -40
- package/server/assets/chat/create-room-dialog.js +50 -47
- package/server/assets/chat/diagram-viewer.js +293 -234
- package/server/assets/chat/doc-preview.js +507 -441
- package/server/assets/chat/editor.js +423 -335
- package/server/assets/chat/events.js +637 -510
- package/server/assets/chat/files.js +476 -430
- package/server/assets/chat/history-loader.js +79 -73
- package/server/assets/chat/icons.js +9 -11
- package/server/assets/chat/image-zoom.js +239 -176
- package/server/assets/chat/index.js +2 -3
- package/server/assets/chat/install-prompt.js +86 -66
- package/server/assets/chat/integration-account-schema.js +88 -86
- package/server/assets/chat/integration-editor.js +162 -153
- package/server/assets/chat/integration-values.js +109 -100
- package/server/assets/chat/integrations.js +689 -622
- package/server/assets/chat/join-requests.js +408 -358
- package/server/assets/chat/locales/de.js +193 -194
- package/server/assets/chat/locales/es.js +193 -194
- package/server/assets/chat/locales/fr.js +193 -194
- package/server/assets/chat/locales/index.js +1 -2
- package/server/assets/chat/locales/it.js +193 -194
- package/server/assets/chat/locales/ja.js +193 -193
- package/server/assets/chat/locales/ko.js +193 -193
- package/server/assets/chat/markdown-footnotes.js +134 -102
- package/server/assets/chat/markdown.js +547 -403
- package/server/assets/chat/members.js +236 -197
- package/server/assets/chat/mentions.js +157 -133
- package/server/assets/chat/message-cache.js +40 -33
- package/server/assets/chat/message-polling.js +280 -227
- package/server/assets/chat/message-window.js +74 -55
- package/server/assets/chat/messages.js +552 -480
- package/server/assets/chat/office-preview.js +46 -45
- package/server/assets/chat/palette.js +450 -417
- package/server/assets/chat/panels.js +94 -78
- package/server/assets/chat/pwa.js +144 -73
- package/server/assets/chat/questions.js +372 -302
- package/server/assets/chat/recall.js +320 -273
- package/server/assets/chat/receipts.js +149 -121
- package/server/assets/chat/reminders.js +331 -214
- package/server/assets/chat/replies.js +165 -130
- package/server/assets/chat/resource-format.js +59 -62
- package/server/assets/chat/room-list.js +58 -55
- package/server/assets/chat/room-watcher.js +72 -50
- package/server/assets/chat/rooms.js +623 -500
- package/server/assets/chat/search.js +136 -114
- package/server/assets/chat/settings.js +223 -200
- package/server/assets/chat/shiki.js +61 -62
- package/server/assets/chat/shortcuts.js +212 -187
- package/server/assets/chat/side-pane.js +160 -150
- package/server/assets/chat/state.js +849 -735
- package/server/assets/chat/system-events.js +45 -43
- package/server/assets/chat/system-messages.js +57 -56
- package/server/assets/chat/task-form.js +178 -182
- package/server/assets/chat/task-row.js +158 -155
- package/server/assets/chat/tasks.js +653 -587
- package/server/assets/chat/viewport.js +32 -29
- package/server/assets/chat/window-controls.js +32 -32
- package/server/assets/conversation.css +10 -4
- package/server/chat-server.ts +9 -1
- package/server/chat.html +2 -1
- package/server/routes/pages.ts +7 -5
- package/server/sw.js +346 -268
- package/src/web/integration-rules-editor.ts +2 -0
|
@@ -1,317 +1,387 @@
|
|
|
1
1
|
import { LS, LOADED_MESSAGE_LIMIT } from "./state.js";
|
|
2
2
|
import { autoMentionDraft, installEditor } from "./editor.js";
|
|
3
3
|
import { copyTextToClipboard, renderMarkdown } from "./markdown.js";
|
|
4
|
-
|
|
5
4
|
export function initQuestions(app) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
$("#questionBadge").hidden = !value;
|
|
32
|
-
trigger.setAttribute("aria-label", `${app.t("questions")}: ${value}`);
|
|
33
|
-
}
|
|
34
|
-
app.updateQuestionBadge = renderBadge;
|
|
35
|
-
|
|
36
|
-
function render(roomId) {
|
|
37
|
-
const items = pending.get(roomId) || [];
|
|
38
|
-
const entry = app.state.rooms.get(roomId);
|
|
39
|
-
// Keep the room's question-mark badge in sync with the loaded question list.
|
|
40
|
-
if (entry && pending.has(roomId) && Number(entry.room.openQuestions) !== items.length) {
|
|
41
|
-
entry.room.openQuestions = items.length;
|
|
42
|
-
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
43
|
-
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
44
|
-
if (app.state.room?.id === roomId) app.renderHead();
|
|
5
|
+
const $ = app.$, pending = new Map(), selected = new Map(), drafts = new Map(), submitting = new Set(), unsupported = new Set();
|
|
6
|
+
let request = 0;
|
|
7
|
+
const panel = $("#questionPanel"), trigger = $("#btnQuestions"), list = $("#questionList"), status = $("#questionStatus");
|
|
8
|
+
// Undefined until the server list for this room has loaded; callers must not
|
|
9
|
+
// treat that as "no open questions".
|
|
10
|
+
app.unansweredQuestionIds = (roomId = app.state.room?.id) => pending.has(roomId)
|
|
11
|
+
? new Set((pending.get(roomId) || []).map((item) => item.id)) : undefined;
|
|
12
|
+
const renderMessage = app.renderMessage;
|
|
13
|
+
app.renderMessage = (message, live) => {
|
|
14
|
+
const row = renderMessage(message, live);
|
|
15
|
+
if (message.isQuestion) {
|
|
16
|
+
row.classList.add("question-message");
|
|
17
|
+
const mark = document.createElement("span");
|
|
18
|
+
mark.className = "question-mark";
|
|
19
|
+
mark.textContent = app.t("questionLabel");
|
|
20
|
+
mark.setAttribute("aria-label", app.t("questionLabel"));
|
|
21
|
+
row.querySelector(".msg-meta")?.append(mark);
|
|
22
|
+
}
|
|
23
|
+
return row;
|
|
24
|
+
};
|
|
25
|
+
function renderBadge(count) {
|
|
26
|
+
const value = Math.max(0, Number(count) || 0);
|
|
27
|
+
$("#questionBadge").textContent = value > 99 ? "99+" : String(value);
|
|
28
|
+
$("#questionBadge").hidden = !value;
|
|
29
|
+
trigger.setAttribute("aria-label", `${app.t("questions")}: ${value}`);
|
|
45
30
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
31
|
+
app.updateQuestionBadge = renderBadge;
|
|
32
|
+
function render(roomId) {
|
|
33
|
+
const items = pending.get(roomId) || [];
|
|
34
|
+
const entry = app.state.rooms.get(roomId);
|
|
35
|
+
// Keep the room's question-mark badge in sync with the loaded question list.
|
|
36
|
+
if (entry && pending.has(roomId) && Number(entry.room.openQuestions) !== items.length) {
|
|
37
|
+
entry.room.openQuestions = items.length;
|
|
38
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
39
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
40
|
+
if (app.state.room?.id === roomId)
|
|
41
|
+
app.renderHead();
|
|
42
|
+
}
|
|
43
|
+
const active = app.state.room?.id === roomId;
|
|
44
|
+
if (active) {
|
|
45
|
+
const signature = `${roomId}\0${app.locale}\0${selected.get(roomId) || ""}\0${items.map((question) => `${question.id}\0${question.text}`).join("\u0001")}`;
|
|
46
|
+
if (list.dataset.signature === signature) {
|
|
47
|
+
renderReply(roomId);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const hadItems = Boolean(list.querySelector(".question-item"));
|
|
51
|
+
list.dataset.signature = signature;
|
|
52
|
+
renderBadge(items.length);
|
|
53
|
+
list.textContent = "";
|
|
54
|
+
for (const question of items) {
|
|
55
|
+
const item = document.createElement("li"), card = document.createElement("article"), select = document.createElement("button"), body = document.createElement("div");
|
|
56
|
+
card.className = `question-item${selected.get(roomId) === question.id ? " selected" : ""}`;
|
|
57
|
+
card.dataset.questionId = question.id;
|
|
58
|
+
select.type = "button";
|
|
59
|
+
select.className = "question-select";
|
|
60
|
+
select.textContent = app.t("selectQuestion");
|
|
61
|
+
select.setAttribute("aria-pressed", String(selected.get(roomId) === question.id));
|
|
62
|
+
body.className = "question-item-body";
|
|
63
|
+
renderMarkdown(body, question.text, app);
|
|
64
|
+
card.append(body, select);
|
|
65
|
+
item.append(card);
|
|
66
|
+
list.append(item);
|
|
67
|
+
}
|
|
68
|
+
status.hidden = items.length > 0 || status.dataset.error === "true";
|
|
69
|
+
if (!items.length && status.dataset.error !== "true")
|
|
70
|
+
app.setText(status, "questionsEmpty");
|
|
71
|
+
// The panel exists to work through open questions; close it once the last one is answered,
|
|
72
|
+
// and keep the caret in the composer where the answer button used to be.
|
|
73
|
+
if (!items.length && hadItems && !panel.hidden && app.state.room?.id === roomId) {
|
|
74
|
+
app.closePanels();
|
|
75
|
+
if (!matchMedia("(max-width: 760px)").matches && !document.querySelector("dialog[open]"))
|
|
76
|
+
$("#msgInput").focus({ preventScroll: true });
|
|
77
|
+
}
|
|
78
|
+
renderReply(roomId);
|
|
79
|
+
}
|
|
75
80
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
function renderReply(roomId) {
|
|
82
|
+
const box = $("#questionReply"), question = (pending.get(roomId) || []).find((item) => item.id === selected.get(roomId));
|
|
83
|
+
if (question && box.dataset.roomId === roomId && box.dataset.questionId === question.id) {
|
|
84
|
+
const key = `${roomId}\0${question.id}`, input = box.querySelector("[contenteditable]"), send = box.querySelector("button[type=submit]"), copy = box.querySelector(".question-copy"), feedback = box.querySelector(".question-copy-feedback");
|
|
85
|
+
box.hidden = false;
|
|
86
|
+
copy.textContent = app.t("copyQuestion");
|
|
87
|
+
send.textContent = app.t("sendAnswer");
|
|
88
|
+
input.setAttribute("aria-placeholder", app.t("answerPlaceholder"));
|
|
89
|
+
if (feedback.dataset.copyState)
|
|
90
|
+
feedback.textContent = app.t(feedback.dataset.copyState);
|
|
91
|
+
if (input && send) {
|
|
92
|
+
send.disabled = !input.value.trim() || input.value.length > 5000 || submitting.has(key);
|
|
93
|
+
if (input.value.length > 5000) {
|
|
94
|
+
app.setText(status, "answerTooLong");
|
|
95
|
+
status.hidden = false;
|
|
96
|
+
status.dataset.tooLong = "true";
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
box.textContent = "";
|
|
102
|
+
box.dataset.roomId = question ? roomId : "";
|
|
103
|
+
box.dataset.questionId = question?.id || "";
|
|
104
|
+
box.hidden = !question;
|
|
105
|
+
if (!question)
|
|
106
|
+
return;
|
|
107
|
+
const copy = document.createElement("button"), feedback = document.createElement("p"), form = document.createElement("form"), input = document.createElement("div"), send = document.createElement("button");
|
|
108
|
+
copy.type = "button";
|
|
109
|
+
copy.className = "btn ghost small question-copy";
|
|
110
|
+
copy.textContent = app.t("copyQuestion");
|
|
111
|
+
feedback.className = "question-copy-feedback";
|
|
112
|
+
feedback.setAttribute("role", "status");
|
|
113
|
+
feedback.setAttribute("aria-live", "polite");
|
|
114
|
+
copy.addEventListener("click", async () => {
|
|
115
|
+
feedback.dataset.copyState = await copyTextToClipboard(question.text, true) ? "questionCopied" : "questionCopyFailed";
|
|
116
|
+
feedback.textContent = app.t(feedback.dataset.copyState);
|
|
117
|
+
});
|
|
118
|
+
form.className = "question-reply-form";
|
|
119
|
+
input.className = "input";
|
|
120
|
+
input.setAttribute("role", "textbox");
|
|
121
|
+
input.setAttribute("aria-multiline", "true");
|
|
122
|
+
input.setAttribute("contenteditable", "true");
|
|
123
|
+
input.classList.add("message-editor");
|
|
124
|
+
input.setAttribute("aria-autocomplete", "list");
|
|
125
|
+
input.setAttribute("aria-controls", "mentionPicker");
|
|
126
|
+
input.setAttribute("aria-expanded", "false");
|
|
127
|
+
input.setAttribute("aria-placeholder", app.t("answerPlaceholder"));
|
|
128
|
+
input.setAttribute("aria-label", app.t("answerPlaceholder"));
|
|
129
|
+
installEditor(input);
|
|
130
|
+
input.isKnownMention = (name) => app.mentionExists(name);
|
|
131
|
+
input.mentionLabel = (name) => name === "all" ? app.t("allMembers") : name;
|
|
132
|
+
const key = `${roomId}\0${question.id}`;
|
|
133
|
+
input.draftValue = drafts.has(key) ? drafts.get(key) : questionAuthorMention(question, roomId, app);
|
|
134
|
+
input.dataset.empty = input.value ? "false" : "true";
|
|
135
|
+
send.type = "submit";
|
|
136
|
+
send.className = "btn primary small";
|
|
137
|
+
send.textContent = app.t("sendAnswer");
|
|
86
138
|
send.disabled = !input.value.trim() || input.value.length > 5000 || submitting.has(key);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
139
|
+
input.addEventListener("input", (event) => {
|
|
140
|
+
input.dataset.empty = input.value ? "false" : "true";
|
|
141
|
+
drafts.set(`${roomId}\0${question.id}`, input.draftValue);
|
|
142
|
+
if (!event.isComposing)
|
|
143
|
+
app.recomputeMention(input);
|
|
144
|
+
send.disabled = !input.value.trim() || input.value.length > 5000 || submitting.has(key);
|
|
145
|
+
if (input.value.length > 5000) {
|
|
146
|
+
app.setText(status, "answerTooLong");
|
|
147
|
+
status.hidden = false;
|
|
148
|
+
status.dataset.tooLong = "true";
|
|
149
|
+
}
|
|
150
|
+
else if (status.dataset.tooLong) {
|
|
151
|
+
delete status.dataset.tooLong;
|
|
152
|
+
status.hidden = true;
|
|
153
|
+
status.textContent = "";
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
input.addEventListener("compositionend", () => app.recomputeMention(input));
|
|
157
|
+
input.addEventListener("keydown", (event) => {
|
|
158
|
+
if (app.handleMentionKey(event, input))
|
|
159
|
+
return;
|
|
160
|
+
if (event.key === "Enter" && !event.shiftKey && !event.isComposing && event.keyCode !== 229) {
|
|
161
|
+
event.preventDefault();
|
|
162
|
+
form.requestSubmit();
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
input.addEventListener("keyup", (event) => { if (!event.isComposing && /^(ArrowLeft|ArrowRight|Home|End)$/.test(event.key))
|
|
166
|
+
app.recomputeMention(input); });
|
|
167
|
+
input.addEventListener("click", () => app.recomputeMention(input));
|
|
168
|
+
input.addEventListener("blur", () => { if (app.mention?.editor === input)
|
|
169
|
+
app.closeMention(); });
|
|
170
|
+
form.addEventListener("submit", (event) => {
|
|
171
|
+
event.preventDefault();
|
|
172
|
+
void answer(roomId, question.id, input.value.trim(), send);
|
|
173
|
+
});
|
|
174
|
+
form.append(input, send);
|
|
175
|
+
box.append(copy, feedback, form);
|
|
176
|
+
input.setCaret(input.value.length);
|
|
90
177
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
copy.addEventListener("click", async () => {
|
|
100
|
-
feedback.dataset.copyState = await copyTextToClipboard(question.text, true) ? "questionCopied" : "questionCopyFailed";
|
|
101
|
-
feedback.textContent = app.t(feedback.dataset.copyState);
|
|
102
|
-
});
|
|
103
|
-
form.className = "question-reply-form";
|
|
104
|
-
input.className = "input";
|
|
105
|
-
input.setAttribute("role", "textbox"); input.setAttribute("aria-multiline", "true");
|
|
106
|
-
input.setAttribute("contenteditable", "true"); input.classList.add("message-editor");
|
|
107
|
-
input.setAttribute("aria-autocomplete", "list"); input.setAttribute("aria-controls", "mentionPicker"); input.setAttribute("aria-expanded", "false");
|
|
108
|
-
input.setAttribute("aria-placeholder", app.t("answerPlaceholder"));
|
|
109
|
-
input.setAttribute("aria-label", app.t("answerPlaceholder"));
|
|
110
|
-
installEditor(input);
|
|
111
|
-
input.isKnownMention = (name) => app.mentionExists(name);
|
|
112
|
-
input.mentionLabel = (name) => name === "all" ? app.t("allMembers") : name;
|
|
113
|
-
const key = `${roomId}\0${question.id}`;
|
|
114
|
-
input.draftValue = drafts.has(key) ? drafts.get(key) : questionAuthorMention(question, roomId, app);
|
|
115
|
-
input.dataset.empty = input.value ? "false" : "true";
|
|
116
|
-
send.type = "submit";
|
|
117
|
-
send.className = "btn primary small";
|
|
118
|
-
send.textContent = app.t("sendAnswer");
|
|
119
|
-
send.disabled = !input.value.trim() || input.value.length > 5000 || submitting.has(key);
|
|
120
|
-
input.addEventListener("input", (event) => {
|
|
121
|
-
input.dataset.empty = input.value ? "false" : "true";
|
|
122
|
-
drafts.set(`${roomId}\0${question.id}`, input.draftValue);
|
|
123
|
-
if (!event.isComposing) app.recomputeMention(input);
|
|
124
|
-
send.disabled = !input.value.trim() || input.value.length > 5000 || submitting.has(key);
|
|
125
|
-
if (input.value.length > 5000) { app.setText(status, "answerTooLong"); status.hidden = false; status.dataset.tooLong = "true"; }
|
|
126
|
-
else if (status.dataset.tooLong) { delete status.dataset.tooLong; status.hidden = true; status.textContent = ""; }
|
|
127
|
-
});
|
|
128
|
-
input.addEventListener("compositionend", () => app.recomputeMention(input));
|
|
129
|
-
input.addEventListener("keydown", (event) => {
|
|
130
|
-
if (app.handleMentionKey(event, input)) return;
|
|
131
|
-
if (event.key === "Enter" && !event.shiftKey && !event.isComposing && event.keyCode !== 229) { event.preventDefault(); form.requestSubmit(); }
|
|
132
|
-
});
|
|
133
|
-
input.addEventListener("keyup", (event) => { if (!event.isComposing && /^(ArrowLeft|ArrowRight|Home|End)$/.test(event.key)) app.recomputeMention(input); });
|
|
134
|
-
input.addEventListener("click", () => app.recomputeMention(input));
|
|
135
|
-
input.addEventListener("blur", () => { if (app.mention?.editor === input) app.closeMention(); });
|
|
136
|
-
form.addEventListener("submit", (event) => {
|
|
137
|
-
event.preventDefault();
|
|
138
|
-
void answer(roomId, question.id, input.value.trim(), send);
|
|
139
|
-
});
|
|
140
|
-
form.append(input, send);
|
|
141
|
-
box.append(copy, feedback, form);
|
|
142
|
-
input.setCaret(input.value.length);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function questionAuthorMention(question, roomId, app) {
|
|
146
|
-
const members = app.state.memberSessions.get(roomId) || [];
|
|
147
|
-
const author = question.senderSessionID
|
|
148
|
-
? members.filter((member) => member.sessionID === question.senderSessionID)
|
|
149
|
-
: members.filter((member) => member.name === question.senderId);
|
|
150
|
-
if (author.length !== 1 || members.filter((member) => member.name === author[0].name).length !== 1 || author[0].sessionID === LS.sessionID || author[0].name === "all") return "";
|
|
151
|
-
return autoMentionDraft(author[0].name);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
async function load(roomId = app.state.room?.id) {
|
|
155
|
-
if (!roomId) return;
|
|
156
|
-
const id = ++request;
|
|
157
|
-
if (unsupported.has(roomId)) {
|
|
158
|
-
const member = await roomMembership(roomId);
|
|
159
|
-
if (id !== request || app.state.room?.id !== roomId) return;
|
|
160
|
-
if (member === false) await app.removeJoinedRoom(roomId, { reason: true });
|
|
161
|
-
else if (member === true) showUnavailable();
|
|
162
|
-
else showFailed();
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
if (app.state.room?.id === roomId && !pending.has(roomId)) {
|
|
166
|
-
app.setText(status, "questionsLoading");
|
|
167
|
-
status.hidden = false;
|
|
168
|
-
}
|
|
169
|
-
try {
|
|
170
|
-
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/questions`);
|
|
171
|
-
if (id !== request || app.state.room?.id !== roomId || !Array.isArray(result.questions)) return;
|
|
172
|
-
pending.set(roomId, result.questions);
|
|
173
|
-
status.dataset.error = "false";
|
|
174
|
-
// render() skips identical lists, so the empty state must survive that early return.
|
|
175
|
-
status.hidden = result.questions.length > 0;
|
|
176
|
-
render(roomId);
|
|
177
|
-
} catch (error) {
|
|
178
|
-
if (id !== request || app.state.room?.id !== roomId) return;
|
|
179
|
-
if (error.status === 404) {
|
|
180
|
-
const member = await roomMembership(roomId);
|
|
181
|
-
if (id !== request || app.state.room?.id !== roomId) return;
|
|
182
|
-
if (member === false) await app.removeJoinedRoom(roomId, { reason: true });
|
|
183
|
-
else if (member === true) { unsupported.add(roomId); showUnavailable(); }
|
|
184
|
-
else showFailed();
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
if (error.status === 401) app.openChatSettings();
|
|
188
|
-
showFailed();
|
|
189
|
-
render(roomId);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async function roomMembership(roomId) {
|
|
194
|
-
const query = new URLSearchParams({ sessionID: LS.sessionID, roomId, timeout: "0" });
|
|
195
|
-
try { const result = await app.api(`/inbox?${query}`); return typeof result.member === "boolean" ? result.member : null; }
|
|
196
|
-
catch { return null; }
|
|
197
|
-
}
|
|
198
|
-
function showUnavailable() { status.dataset.error = "true"; app.setText(status, "questionsUnsupported"); status.hidden = false; }
|
|
199
|
-
function showFailed() { status.dataset.error = "true"; app.setText(status, "questionsFailed"); status.hidden = false; }
|
|
200
|
-
|
|
201
|
-
app.answerQuestionFromReply = async (roomId, questionId, text) => {
|
|
202
|
-
if (text.length > 5000) throw Object.assign(new Error(app.t("answerTooLong")), { status: 413 });
|
|
203
|
-
try {
|
|
204
|
-
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/questions/${encodeURIComponent(questionId)}/answers`, {
|
|
205
|
-
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text }),
|
|
206
|
-
});
|
|
207
|
-
applyAnswer(roomId, questionId, result);
|
|
208
|
-
return result;
|
|
209
|
-
} catch (error) {
|
|
210
|
-
if (error.status === 409 || error.status === 404) await load(roomId);
|
|
211
|
-
throw error;
|
|
178
|
+
function questionAuthorMention(question, roomId, app) {
|
|
179
|
+
const members = app.state.memberSessions.get(roomId) || [];
|
|
180
|
+
const author = question.senderSessionID
|
|
181
|
+
? members.filter((member) => member.sessionID === question.senderSessionID)
|
|
182
|
+
: members.filter((member) => member.name === question.senderId);
|
|
183
|
+
if (author.length !== 1 || members.filter((member) => member.name === author[0].name).length !== 1 || author[0].sessionID === LS.sessionID || author[0].name === "all")
|
|
184
|
+
return "";
|
|
185
|
+
return autoMentionDraft(author[0].name);
|
|
212
186
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
187
|
+
async function load(roomId = app.state.room?.id) {
|
|
188
|
+
if (!roomId)
|
|
189
|
+
return;
|
|
190
|
+
const id = ++request;
|
|
191
|
+
if (unsupported.has(roomId)) {
|
|
192
|
+
const member = await roomMembership(roomId);
|
|
193
|
+
if (id !== request || app.state.room?.id !== roomId)
|
|
194
|
+
return;
|
|
195
|
+
if (member === false)
|
|
196
|
+
await app.removeJoinedRoom(roomId, { reason: true });
|
|
197
|
+
else if (member === true)
|
|
198
|
+
showUnavailable();
|
|
199
|
+
else
|
|
200
|
+
showFailed();
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (app.state.room?.id === roomId && !pending.has(roomId)) {
|
|
204
|
+
app.setText(status, "questionsLoading");
|
|
205
|
+
status.hidden = false;
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/questions`);
|
|
209
|
+
if (id !== request || app.state.room?.id !== roomId || !Array.isArray(result.questions))
|
|
210
|
+
return;
|
|
211
|
+
pending.set(roomId, result.questions);
|
|
212
|
+
status.dataset.error = "false";
|
|
213
|
+
// render() skips identical lists, so the empty state must survive that early return.
|
|
214
|
+
status.hidden = result.questions.length > 0;
|
|
215
|
+
render(roomId);
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
if (id !== request || app.state.room?.id !== roomId)
|
|
219
|
+
return;
|
|
220
|
+
if (error.status === 404) {
|
|
221
|
+
const member = await roomMembership(roomId);
|
|
222
|
+
if (id !== request || app.state.room?.id !== roomId)
|
|
223
|
+
return;
|
|
224
|
+
if (member === false)
|
|
225
|
+
await app.removeJoinedRoom(roomId, { reason: true });
|
|
226
|
+
else if (member === true) {
|
|
227
|
+
unsupported.add(roomId);
|
|
228
|
+
showUnavailable();
|
|
229
|
+
}
|
|
230
|
+
else
|
|
231
|
+
showFailed();
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (error.status === 401)
|
|
235
|
+
app.openChatSettings();
|
|
236
|
+
showFailed();
|
|
237
|
+
render(roomId);
|
|
238
238
|
}
|
|
239
|
-
} else if (error.status === 401 && app.state.room?.id === roomId) {
|
|
240
|
-
app.openChatSettings();
|
|
241
|
-
app.showSendError("sendAuth");
|
|
242
|
-
} else if (app.state.room?.id === roomId) {
|
|
243
|
-
app.setText(status, "answerFailed");
|
|
244
|
-
status.hidden = false;
|
|
245
|
-
}
|
|
246
|
-
} finally {
|
|
247
|
-
submitting.delete(key);
|
|
248
|
-
if (app.state.room?.id === roomId) renderReply(roomId);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function applyAnswer(roomId, questionId, result) {
|
|
253
|
-
const entry = app.state.rooms.get(roomId);
|
|
254
|
-
if (entry && result.answer) {
|
|
255
|
-
const question = entry.room.messages.find((item) => item.id === questionId);
|
|
256
|
-
if (question) question.answeredByMessageID = result.answer.id;
|
|
257
239
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
240
|
+
async function roomMembership(roomId) {
|
|
241
|
+
const query = new URLSearchParams({ sessionID: LS.sessionID, roomId, timeout: "0" });
|
|
242
|
+
try {
|
|
243
|
+
const result = await app.api(`/inbox?${query}`);
|
|
244
|
+
return typeof result.member === "boolean" ? result.member : null;
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
262
249
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
250
|
+
function showUnavailable() { status.dataset.error = "true"; app.setText(status, "questionsUnsupported"); status.hidden = false; }
|
|
251
|
+
function showFailed() { status.dataset.error = "true"; app.setText(status, "questionsFailed"); status.hidden = false; }
|
|
252
|
+
app.answerQuestionFromReply = async (roomId, questionId, text) => {
|
|
253
|
+
if (text.length > 5000)
|
|
254
|
+
throw Object.assign(new Error(app.t("answerTooLong")), { status: 413 });
|
|
255
|
+
try {
|
|
256
|
+
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/questions/${encodeURIComponent(questionId)}/answers`, {
|
|
257
|
+
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text }),
|
|
258
|
+
});
|
|
259
|
+
applyAnswer(roomId, questionId, result);
|
|
260
|
+
return result;
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
if (error.status === 409 || error.status === 404)
|
|
264
|
+
await load(roomId);
|
|
265
|
+
throw error;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
async function answer(roomId, questionId, text, button) {
|
|
269
|
+
const key = `${roomId}\0${questionId}`;
|
|
270
|
+
if (!text || submitting.has(key))
|
|
271
|
+
return;
|
|
272
|
+
if (text.length > 5000) {
|
|
273
|
+
app.setText(status, "answerTooLong");
|
|
274
|
+
status.hidden = false;
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
submitting.add(key);
|
|
278
|
+
button.disabled = true;
|
|
279
|
+
renderReply(roomId);
|
|
280
|
+
try {
|
|
281
|
+
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/questions/${encodeURIComponent(questionId)}/answers`, {
|
|
282
|
+
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text }),
|
|
283
|
+
});
|
|
284
|
+
applyAnswer(roomId, questionId, result);
|
|
285
|
+
void load(roomId);
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
if (error.status === 409 || error.status === 404) {
|
|
289
|
+
await load(roomId);
|
|
290
|
+
if (error.status === 409 && app.state.room?.id === roomId && !(pending.get(roomId) || []).some((item) => item.id === questionId)) {
|
|
291
|
+
if (selected.get(roomId) === questionId)
|
|
292
|
+
selected.delete(roomId);
|
|
293
|
+
drafts.delete(`${roomId}\0${questionId}`);
|
|
294
|
+
app.setText(status, "questionAlreadyAnswered");
|
|
295
|
+
status.dataset.error = "true";
|
|
296
|
+
status.hidden = false;
|
|
297
|
+
render(roomId);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
else if (error.status === 401 && app.state.room?.id === roomId) {
|
|
301
|
+
app.openChatSettings();
|
|
302
|
+
app.showSendError("sendAuth");
|
|
303
|
+
}
|
|
304
|
+
else if (app.state.room?.id === roomId) {
|
|
305
|
+
app.setText(status, "answerFailed");
|
|
306
|
+
status.hidden = false;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
finally {
|
|
310
|
+
submitting.delete(key);
|
|
311
|
+
if (app.state.room?.id === roomId)
|
|
312
|
+
renderReply(roomId);
|
|
313
|
+
}
|
|
277
314
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
315
|
+
function applyAnswer(roomId, questionId, result) {
|
|
316
|
+
const entry = app.state.rooms.get(roomId);
|
|
317
|
+
if (entry && result.answer) {
|
|
318
|
+
const question = entry.room.messages.find((item) => item.id === questionId);
|
|
319
|
+
if (question)
|
|
320
|
+
question.answeredByMessageID = result.answer.id;
|
|
321
|
+
}
|
|
322
|
+
if (entry && result.answer && !entry.room.messages.some((item) => item.id === result.answer.id)) {
|
|
323
|
+
entry.room.messages.push({ ...result.answer, roomId });
|
|
324
|
+
entry.room.messages.sort((a, b) => a.createdAt - b.createdAt);
|
|
325
|
+
if (entry.room.messages.length > LOADED_MESSAGE_LIMIT)
|
|
326
|
+
entry.room.messages.splice(0, entry.room.messages.length - LOADED_MESSAGE_LIMIT);
|
|
327
|
+
if (app.state.room?.id === roomId)
|
|
328
|
+
app.renderMessages(true);
|
|
329
|
+
}
|
|
330
|
+
pending.set(roomId, (pending.get(roomId) || []).filter((item) => item.id !== (result.questionID || questionId)));
|
|
331
|
+
if (selected.get(roomId) === questionId)
|
|
332
|
+
selected.delete(roomId);
|
|
333
|
+
drafts.delete(`${roomId}\0${questionId}`);
|
|
334
|
+
if (app.state.room?.id === roomId)
|
|
335
|
+
render(roomId);
|
|
299
336
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
337
|
+
trigger.addEventListener("click", () => {
|
|
338
|
+
const open = panel.hidden;
|
|
339
|
+
app.closePanels();
|
|
340
|
+
panel.hidden = !open;
|
|
341
|
+
trigger.setAttribute("aria-expanded", String(open));
|
|
342
|
+
if (open) {
|
|
343
|
+
void load();
|
|
344
|
+
$("#questionHeading").focus();
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
$("#btnRefreshQuestions").addEventListener("click", () => void load());
|
|
348
|
+
list.addEventListener("click", (event) => {
|
|
349
|
+
event.stopPropagation();
|
|
350
|
+
const card = event.target.closest(".question-item[data-question-id]"), select = event.target.closest(".question-select");
|
|
351
|
+
if (!card || (!select && event.target.closest("a, button, input, textarea, select, [contenteditable=true]")) || !app.state.room)
|
|
352
|
+
return;
|
|
353
|
+
selected.set(app.state.room.id, card.dataset.questionId);
|
|
354
|
+
render(app.state.room.id);
|
|
355
|
+
$("#questionReply [contenteditable]")?.focus();
|
|
356
|
+
});
|
|
357
|
+
const closePanels = app.closePanels;
|
|
358
|
+
app.closePanels = () => {
|
|
359
|
+
closePanels();
|
|
360
|
+
panel.hidden = true;
|
|
361
|
+
trigger.setAttribute("aria-expanded", "false");
|
|
362
|
+
};
|
|
363
|
+
document.addEventListener("keydown", (event) => {
|
|
364
|
+
if (event.key === "Escape" && !panel.hidden) {
|
|
365
|
+
app.closePanels();
|
|
366
|
+
trigger.focus();
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
const activateRoom = app.activateRoom;
|
|
370
|
+
app.activateRoom = (roomId, preserveRoomListGeneration = false) => {
|
|
371
|
+
activateRoom(roomId, preserveRoomListGeneration);
|
|
372
|
+
// Error and too-long banners belong to the room that produced them.
|
|
373
|
+
delete status.dataset.error;
|
|
374
|
+
delete status.dataset.tooLong;
|
|
375
|
+
status.hidden = true;
|
|
376
|
+
status.textContent = "";
|
|
377
|
+
void load(roomId);
|
|
378
|
+
};
|
|
379
|
+
const applyLocale = app.applyLocale;
|
|
380
|
+
app.applyLocale = () => {
|
|
381
|
+
applyLocale();
|
|
382
|
+
if (app.state.room)
|
|
383
|
+
render(app.state.room.id);
|
|
384
|
+
};
|
|
385
|
+
setInterval(() => { if (app.state.room && !panel.hidden)
|
|
386
|
+
void load(); }, 5000);
|
|
317
387
|
}
|