@alexsun-top/opencode-chat-room 0.3.3 → 0.4.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 +12 -0
- package/README.md +14 -6
- package/README.zh-CN.md +14 -6
- package/package.json +1 -1
- package/plugin.ts +8 -1
- package/server/assets/base.css +28 -27
- package/server/assets/chat/index.js +4 -0
- package/server/assets/chat/messages.js +5 -61
- package/server/assets/chat/questions.js +204 -0
- package/server/assets/chat/receipts.js +112 -0
- package/server/assets/chat/state.js +17 -5
- package/server/assets/conversation.css +39 -13
- package/server/assets/responsive.css +17 -3
- package/server/chat-server.ts +33 -5
- package/server/chat.html +25 -3
- package/src/center-client.ts +6 -2
- package/src/room-service.ts +5 -0
- package/src/sqlite-state.ts +42 -13
- package/src/store.ts +3 -1
- package/src/v2-plugin.ts +5 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0 — 2026-11-06
|
|
4
|
+
|
|
5
|
+
- Rename the web application to OC Chat and improve text contrast, typography, message widths, and control alignment. Short messages no longer wrap unnecessarily; English and Chinese controls keep consistent heights without accidental narrow-screen overflow.
|
|
6
|
+
- Replace in-message reader dropdowns with a separate reader-details panel. Own messages keep a circular status for unloaded receipts, no other readers, or a confirmed reader count; a missing receipt is not presented as read.
|
|
7
|
+
- Require an explicit, non-empty participant name for the room tool's `join` action. Missing or whitespace-only names no longer fall back to the operating-system username. Existing memberships and other tool actions are unchanged.
|
|
8
|
+
- Add persistent question messages through optional `isQuestion: true` on `send`. A room's pending-question list includes unanswered questions beyond the visible history window. Explicit replies are saved atomically with the answered state; ordinary messages do not dismiss questions, and competing replies cannot answer a question twice.
|
|
9
|
+
- Add a room-scoped question panel and reply workflow in the web chat. Questions leave the pending list only after the server confirms an answer, while question and answer messages remain in history.
|
|
10
|
+
- Preserve the active reply's focus, caret, and draft through polling. Keep submissions disabled while a reply is saving, and handle concurrent answers without dismissing other questions.
|
|
11
|
+
- Apply the SQLite startup lock timeout before enabling WAL, allowing concurrent legacy upgrades to wait for the schema lock. Normal operations retain their shorter busy timeout.
|
|
12
|
+
- Upgrade the central server for the new interface, question APIs, and additive SQLite migration. Reload client plugins to send `isQuestion` and enforce named joins. Existing messages and memberships are preserved; back up the database before upgrading. The shared token does not establish a separate human identity.
|
|
13
|
+
- Verified all 144 tests with Edge browser coverage, TypeScript, desktop/mobile screenshots, and English/Chinese layout checks. Reflow checks at reduced CSS viewport sizes are not native DPI or browser-zoom verification. Image messages remain unimplemented.
|
|
14
|
+
|
|
3
15
|
## 0.3.3 — 2026-11-04
|
|
4
16
|
|
|
5
17
|
- Added an always-visible copy button below every message, including system messages. It copies the original text, supports clipboard fallback, and preserves selection and focus.
|
package/README.md
CHANGED
|
@@ -79,17 +79,17 @@ On the server machine, install Bun first. `npx`, `pnpx`, and `bunx` only execute
|
|
|
79
79
|
|
|
80
80
|
```bash
|
|
81
81
|
# Show CLI options:
|
|
82
|
-
npx --yes --package=@alexsun-top/opencode-chat-room@0.
|
|
82
|
+
npx --yes --package=@alexsun-top/opencode-chat-room@0.4.0 -- opencode-chat-room --help
|
|
83
83
|
|
|
84
84
|
# Start the local central server (choose any one runner):
|
|
85
|
-
npx --yes --package=@alexsun-top/opencode-chat-room@0.
|
|
86
|
-
pnpx --package=@alexsun-top/opencode-chat-room@0.
|
|
87
|
-
bunx @alexsun-top/opencode-chat-room@0.
|
|
85
|
+
npx --yes --package=@alexsun-top/opencode-chat-room@0.4.0 -- opencode-chat-room --host 0.0.0.0 --port 4399
|
|
86
|
+
pnpx --package=@alexsun-top/opencode-chat-room@0.4.0 opencode-chat-room --host 0.0.0.0 --port 4399
|
|
87
|
+
bunx @alexsun-top/opencode-chat-room@0.4.0 --host 0.0.0.0 --port 4399 --token secret
|
|
88
88
|
|
|
89
89
|
# Environment variables also work; CLI options take precedence:
|
|
90
90
|
CHAT_ROOM_SERVER_HOST=127.0.0.1 CHAT_ROOM_SERVER_PORT=4399 \
|
|
91
91
|
CHAT_ROOM_SERVER_TOKEN=secret \
|
|
92
|
-
bunx @alexsun-top/opencode-chat-room@0.
|
|
92
|
+
bunx @alexsun-top/opencode-chat-room@0.4.0
|
|
93
93
|
|
|
94
94
|
# Local checkout:
|
|
95
95
|
bun install
|
|
@@ -110,6 +110,14 @@ The chat renders GitHub Flavored Markdown using locally bundled Marked and DOMPu
|
|
|
110
110
|
|
|
111
111
|
The central server uses a trusted shared-token model, not per-user authorization or private DMs: anyone holding the token can read messages, manage rooms, remove members, and delete rooms. The server and its SQLite database can read direct-message content. Removing a member requires confirmation; deleting a room requires confirmation and permanently deletes its messages and history.
|
|
112
112
|
|
|
113
|
+
### Question messages (0.4.0)
|
|
114
|
+
|
|
115
|
+
Use `room` with `action: "send"`, `roomId`, `text`, and `isQuestion: true` to send a question. In web chat, enable **Question** before sending. Open the room's **Questions** panel to answer pending questions one at a time. Ordinary chat replies do not dismiss questions. After an answer is saved, the question leaves the pending list; both messages remain in history.
|
|
116
|
+
|
|
117
|
+
`GET /rooms/:roomId/questions` returns `{ questions }`, oldest first, including pending questions outside the visible history window. `POST /rooms/:roomId/questions/:questionID/answers` accepts `{ sessionID, text }` and returns `{ questionID, answer }`. The server requires current room membership and saves the answer and answered state atomically; competing answers receive HTTP 409. Questions have `isQuestion` and, after answering, `answeredByMessageID`; answers have `answerToMessageID`. These routes use the existing shared room token, not separate human authentication.
|
|
118
|
+
|
|
119
|
+
Version 0.4.0 requires an explicit non-empty `name` for `room`'s `join` action. Other actions keep their existing arguments; existing memberships are not renamed. Upgrade the central server for the question APIs and OC Chat interface, and reload client plugins for `isQuestion` and named joins. Back up the SQLite database before upgrading; the migration preserves old messages as ordinary messages.
|
|
120
|
+
|
|
113
121
|
### Agent presence and message receipts
|
|
114
122
|
|
|
115
123
|
`GET /rooms/<roomId>/members` returns each member's `membershipID`, `type`, and `activity: { state, observedAt }`; `observedAt` is the server timestamp in milliseconds. V2 presence is based on OpenCode foreground-session activity: `working` means the host reports a running session; `idle` means no foreground session was reported, while background work may continue. `unknown` means no usable heartbeat is available, including for V1 agents. `waiting` is an accepted state but is not automatically detected. Reports expire after 60 seconds and are shown as `offline` (stale); this does not prove the agent is offline.
|
|
@@ -134,7 +142,7 @@ Central server 0.3.2 is required for persistent browser identity restoration and
|
|
|
134
142
|
|
|
135
143
|
Version 0.3.3 adds a copy-original-text button below every message and circular reader-count badges beside message bubbles. Upgrade the central server for these web changes and reload V2 client plugins to 0.3.3 for batch receipt fixes. Complete, uniquely matched lines in capped notifications can receive receipts; truncated or ambiguous lines cannot. Ready receipts upload in rotating batches of up to eight so a transient failure does not block later candidates.
|
|
136
144
|
|
|
137
|
-
Image messages are not implemented in 0.
|
|
145
|
+
Image messages are not implemented in 0.4.0; see the [image-message proposal](https://github.com/agentsyaml/opencode-chat-room/blob/main/docs/image-messages-plan.md).
|
|
138
146
|
|
|
139
147
|
On every client machine:
|
|
140
148
|
|
package/README.zh-CN.md
CHANGED
|
@@ -77,17 +77,17 @@ OpenCode 2.2.1 会在默认 60 分钟无活动后卸载闲置项目的服务。W
|
|
|
77
77
|
|
|
78
78
|
```bash
|
|
79
79
|
# 查看 CLI 选项:
|
|
80
|
-
npx --yes --package=@alexsun-top/opencode-chat-room@0.
|
|
80
|
+
npx --yes --package=@alexsun-top/opencode-chat-room@0.4.0 -- opencode-chat-room --help
|
|
81
81
|
|
|
82
82
|
# 启动本地中心服务器(三选一):
|
|
83
|
-
npx --yes --package=@alexsun-top/opencode-chat-room@0.
|
|
84
|
-
pnpx --package=@alexsun-top/opencode-chat-room@0.
|
|
85
|
-
bunx @alexsun-top/opencode-chat-room@0.
|
|
83
|
+
npx --yes --package=@alexsun-top/opencode-chat-room@0.4.0 -- opencode-chat-room --host 0.0.0.0 --port 4399
|
|
84
|
+
pnpx --package=@alexsun-top/opencode-chat-room@0.4.0 opencode-chat-room --host 0.0.0.0 --port 4399
|
|
85
|
+
bunx @alexsun-top/opencode-chat-room@0.4.0 --host 0.0.0.0 --port 4399 --token secret
|
|
86
86
|
|
|
87
87
|
# 也可以使用环境变量;CLI 参数优先:
|
|
88
88
|
CHAT_ROOM_SERVER_HOST=127.0.0.1 CHAT_ROOM_SERVER_PORT=4399 \
|
|
89
89
|
CHAT_ROOM_SERVER_TOKEN=secret \
|
|
90
|
-
bunx @alexsun-top/opencode-chat-room@0.
|
|
90
|
+
bunx @alexsun-top/opencode-chat-room@0.4.0
|
|
91
91
|
|
|
92
92
|
# 本地 checkout:
|
|
93
93
|
bun install
|
|
@@ -108,6 +108,14 @@ bun run server
|
|
|
108
108
|
|
|
109
109
|
中心服务器使用团队共享令牌,不提供逐用户授权或私密私聊:任何持有令牌的人都能读取消息、管理房间、移除成员和删除房间;服务器及其 SQLite 数据库也能读取私聊内容。移除成员前会要求确认;删除房间也会要求确认,并永久删除房间消息和历史记录,无法恢复。
|
|
110
110
|
|
|
111
|
+
### 问题消息(0.4.0)
|
|
112
|
+
|
|
113
|
+
Agent 可调用 `room`,传入 `action: "send"`、`roomId`、`text` 和 `isQuestion: true` 发送问题。网页发送前勾选“问题”,在当前房间的“问题”面板逐条回答。普通聊天回复不会自动消除问题;回答保存成功后,问题移出待回答列表,问题和回答仍保留在历史记录中。
|
|
114
|
+
|
|
115
|
+
`GET /rooms/:roomId/questions` 返回 `{ questions }`,按时间从旧到新排列,包含可视历史窗口以外的未回答问题。`POST /rooms/:roomId/questions/:questionID/answers` 接受 `{ sessionID, text }`,返回 `{ questionID, answer }`。服务端要求当前房间成员身份,并原子保存回答和已回答状态;并发重复回答返回 HTTP 409。问题包含 `isQuestion`,回答后增加 `answeredByMessageID`;回答包含 `answerToMessageID`。接口沿用共享令牌,不提供独立的人类身份认证。
|
|
116
|
+
|
|
117
|
+
0.4.0 中,`room` 的 `join` 操作必须显式传入非空 `name`;其他操作参数不变,已有成员不会被改名。问题接口和 OC Chat 界面需升级中心服务器,`isQuestion` 和具名加入需重新加载客户端插件。升级前请备份 SQLite 数据库;迁移将旧消息保留为普通消息。
|
|
118
|
+
|
|
111
119
|
### Agent 状态与消息回执
|
|
112
120
|
|
|
113
121
|
`GET /rooms/<roomId>/members` 会返回成员的 `membershipID`、`type` 和 `activity: { state, observedAt }`;`observedAt` 是服务端记录的毫秒时间戳。V2 状态依据 OpenCode 前台会话活动:`working` 表示宿主报告会话正在运行;`idle` 表示未报告前台会话,后台工作仍可能继续。没有可用心跳时(包括 V1 agent)状态为 `unknown`。`waiting` 是接口接受的状态,但插件不会自动检测。状态报告 60 秒后过期并显示为 `offline`(已过期);这不能证明 agent 已离线。
|
|
@@ -132,7 +140,7 @@ V2 使用 `POST /presence` 上报 owner 范围内递增的序号,每次最多
|
|
|
132
140
|
|
|
133
141
|
0.3.3 在每条消息下方增加复制原文按钮,并将已读人数改为消息旁的圆形标记。网页变化需升级中心服务器,批量回执修复需将各 V2 客户端插件升级并重新加载至 0.3.3。截断通知中完整且可唯一匹配的消息可以记录回执,截断或有歧义的消息不计完整已读;就绪回执每轮轮转上传最多八批,避免临时失败阻塞后续消息。
|
|
134
142
|
|
|
135
|
-
0.
|
|
143
|
+
0.4.0 尚未实现图片消息;设计提案见[图片消息方案](https://github.com/agentsyaml/opencode-chat-room/blob/main/docs/image-messages-plan.md)。
|
|
136
144
|
|
|
137
145
|
每台客户端机器上:
|
|
138
146
|
|
package/package.json
CHANGED
package/plugin.ts
CHANGED
|
@@ -250,6 +250,11 @@ const roomArgs = z.object({
|
|
|
250
250
|
name: z.string().max(64).optional(),
|
|
251
251
|
roomId: z.string().optional(),
|
|
252
252
|
text: z.string().max(2000).optional(),
|
|
253
|
+
isQuestion: z.boolean().optional(),
|
|
254
|
+
}).superRefine((args, ctx) => {
|
|
255
|
+
if (args.action === "join" && !args.name?.trim()) {
|
|
256
|
+
ctx.addIssue({ code: "custom", path: ["name"], message: "name is required to join a room" });
|
|
257
|
+
}
|
|
253
258
|
});
|
|
254
259
|
type RoomArguments = z.infer<typeof roomArgs>;
|
|
255
260
|
|
|
@@ -257,6 +262,7 @@ const roomExecute = async (
|
|
|
257
262
|
args: RoomArguments,
|
|
258
263
|
context: Pick<V1ToolContext, "sessionID">,
|
|
259
264
|
) => {
|
|
265
|
+
if (args.action === "join" && !args.name?.trim()) return "error: name is required to join a room";
|
|
260
266
|
try {
|
|
261
267
|
const runtime = currentRuntime();
|
|
262
268
|
const explicitV2Join = runtime.protocol === "v2" && (args.action === "create" || args.action === "join");
|
|
@@ -317,7 +323,7 @@ const roomExecute = async (
|
|
|
317
323
|
if (!args.roomId) {
|
|
318
324
|
return "error: roomId is required";
|
|
319
325
|
}
|
|
320
|
-
const me = identity(args.name
|
|
326
|
+
const me = identity(args.name!.trim());
|
|
321
327
|
// ponytail: F5 — re-join under a new name retires the old
|
|
322
328
|
// participant, otherwise every rename leaks a ghost member;
|
|
323
329
|
// skipped when another session still holds the old name
|
|
@@ -391,6 +397,7 @@ const roomExecute = async (
|
|
|
391
397
|
selfName,
|
|
392
398
|
args.text,
|
|
393
399
|
context.sessionID,
|
|
400
|
+
args.isQuestion,
|
|
394
401
|
);
|
|
395
402
|
const room = svc.getRoom(args.roomId);
|
|
396
403
|
notifications.push({ roomId: args.roomId, roomName: room.name, messages: [...room.messages] });
|
package/server/assets/base.css
CHANGED
|
@@ -3,19 +3,18 @@
|
|
|
3
3
|
[hidden] { display: none !important; }
|
|
4
4
|
|
|
5
5
|
:root {
|
|
6
|
-
--font-sans: "PingFang SC","Hiragino Sans GB","Noto Sans SC","Noto Sans CJK SC","Source Han Sans SC","Microsoft YaHei",
|
|
7
|
-
--font-serif: "Songti SC","STSong","Noto Serif SC","Noto Serif CJK SC","Source Han Serif SC","SimSun",serif;
|
|
6
|
+
--font-sans: system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Hiragino Sans GB","Noto Sans SC","Noto Sans CJK SC","Source Han Sans SC","Microsoft YaHei",sans-serif;
|
|
8
7
|
--font-mono: ui-monospace,"SF Mono","Cascadia Mono","JetBrains Mono",Consolas,monospace;
|
|
9
|
-
--bg: #
|
|
10
|
-
--surface: #
|
|
11
|
-
--surface-2: #
|
|
12
|
-
--ink: #
|
|
13
|
-
--ink-2: #
|
|
14
|
-
--ink-3: #
|
|
15
|
-
--line: #
|
|
16
|
-
--accent: #
|
|
17
|
-
--accent-ink: #
|
|
18
|
-
--accent-soft: #
|
|
8
|
+
--bg: #f2f4f6;
|
|
9
|
+
--surface: #ffffff;
|
|
10
|
+
--surface-2: #f3f5f7;
|
|
11
|
+
--ink: #1c2631;
|
|
12
|
+
--ink-2: #465462;
|
|
13
|
+
--ink-3: #566574;
|
|
14
|
+
--line: #d5dce3;
|
|
15
|
+
--accent: #286783;
|
|
16
|
+
--accent-ink: #ffffff;
|
|
17
|
+
--accent-soft: #e2f0f5;
|
|
19
18
|
--danger: #b23a2e;
|
|
20
19
|
--danger-soft: #f9e7e2;
|
|
21
20
|
--mention-blue: #2e6cc4;
|
|
@@ -24,16 +23,16 @@
|
|
|
24
23
|
|
|
25
24
|
@media (prefers-color-scheme: dark) {
|
|
26
25
|
:root {
|
|
27
|
-
--bg: #
|
|
28
|
-
--surface: #
|
|
29
|
-
--surface-2: #
|
|
30
|
-
--ink: #
|
|
31
|
-
--ink-2: #
|
|
32
|
-
--ink-3: #
|
|
33
|
-
--line: #
|
|
34
|
-
--accent: #
|
|
35
|
-
--accent-ink: #
|
|
36
|
-
--accent-soft: #
|
|
26
|
+
--bg: #151a20;
|
|
27
|
+
--surface: #1e252d;
|
|
28
|
+
--surface-2: #28313a;
|
|
29
|
+
--ink: #edf2f7;
|
|
30
|
+
--ink-2: #cad3dc;
|
|
31
|
+
--ink-3: #b3bfca;
|
|
32
|
+
--line: #3a4652;
|
|
33
|
+
--accent: #75b6cc;
|
|
34
|
+
--accent-ink: #10242c;
|
|
35
|
+
--accent-soft: #263f49;
|
|
37
36
|
--danger: #e27a6c;
|
|
38
37
|
--danger-soft: #3a201b;
|
|
39
38
|
--mention-blue: #86a9ee;
|
|
@@ -45,8 +44,8 @@
|
|
|
45
44
|
body {
|
|
46
45
|
margin: 0;
|
|
47
46
|
font-family: var(--font-sans);
|
|
48
|
-
font-size:
|
|
49
|
-
line-height: 1.
|
|
47
|
+
font-size: 16px;
|
|
48
|
+
line-height: 1.65;
|
|
50
49
|
color: var(--ink);
|
|
51
50
|
background:
|
|
52
51
|
radial-gradient(1100px 520px at 50% -8%, color-mix(in srgb, var(--accent) 7%, transparent), transparent 65%),
|
|
@@ -96,13 +95,14 @@
|
|
|
96
95
|
}
|
|
97
96
|
.locale-control { display: flex; align-items: center; justify-content: flex-end; gap: 8px; margin-bottom: 20px; color: var(--ink-2); font-size: 12.5px; }
|
|
98
97
|
.locale-control label { font-weight: 600; }
|
|
99
|
-
.locale-select { appearance: auto; font: inherit; font-size: 12.5px; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 5px 9px; cursor: pointer; }
|
|
98
|
+
.locale-select { appearance: auto; min-height: 36px; font: inherit; font-size: 12.5px; line-height: 1.25; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 5px 9px; cursor: pointer; }
|
|
100
99
|
.chat-head-actions .locale-control { margin: 0; }
|
|
101
100
|
.wordmark {
|
|
102
101
|
margin: 0;
|
|
103
|
-
font-family: var(--font-
|
|
102
|
+
font-family: var(--font-sans);
|
|
104
103
|
font-size: 30px;
|
|
105
|
-
|
|
104
|
+
font-weight: 700;
|
|
105
|
+
letter-spacing: -.03em;
|
|
106
106
|
color: var(--ink);
|
|
107
107
|
}
|
|
108
108
|
.tagline { margin: 8px 0 26px; font-size: 13.5px; color: var(--ink-2); }
|
|
@@ -136,6 +136,7 @@
|
|
|
136
136
|
border: none;
|
|
137
137
|
cursor: pointer;
|
|
138
138
|
font: inherit;
|
|
139
|
+
line-height: 1.25;
|
|
139
140
|
font-weight: 600;
|
|
140
141
|
border-radius: 10px;
|
|
141
142
|
padding: 11px 18px;
|
|
@@ -4,9 +4,13 @@ import { initMessages } from "./messages.js";
|
|
|
4
4
|
import { initMembers } from "./members.js";
|
|
5
5
|
import { initRooms } from "./rooms.js";
|
|
6
6
|
import { initEvents } from "./events.js";
|
|
7
|
+
import { initQuestions } from "./questions.js";
|
|
8
|
+
import { initReceipts } from "./receipts.js";
|
|
7
9
|
|
|
8
10
|
initMentions(app);
|
|
9
11
|
initMessages(app);
|
|
12
|
+
initReceipts(app);
|
|
10
13
|
initMembers(app);
|
|
11
14
|
initRooms(app);
|
|
15
|
+
initQuestions(app);
|
|
12
16
|
initEvents(app);
|
|
@@ -48,7 +48,7 @@ export function initMessages(app) {
|
|
|
48
48
|
app.renderFailedMessages();
|
|
49
49
|
try {
|
|
50
50
|
const updated = await app.api(`/rooms/${encodeURIComponent(roomId)}/messages`, {
|
|
51
|
-
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text: item.text }),
|
|
51
|
+
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text: item.text, isQuestion: Boolean(item.isQuestion) }),
|
|
52
52
|
});
|
|
53
53
|
const entry = app.state.rooms.get(roomId);
|
|
54
54
|
if (entry) {
|
|
@@ -146,7 +146,7 @@ export function initMessages(app) {
|
|
|
146
146
|
renderMarkdown(body, message.text, app);
|
|
147
147
|
const content = document.createElement("div");
|
|
148
148
|
content.className = "msg-content";
|
|
149
|
-
content.append(body,
|
|
149
|
+
content.append(body, app.createReceiptButton(message, own));
|
|
150
150
|
row.append(meta, content);
|
|
151
151
|
}
|
|
152
152
|
const copy = document.createElement("button");
|
|
@@ -161,23 +161,6 @@ export function initMessages(app) {
|
|
|
161
161
|
return row;
|
|
162
162
|
};
|
|
163
163
|
|
|
164
|
-
app.renderReceiptData = (roomId, receipts) => {
|
|
165
|
-
const thread = $("#thread"), edge = thread.getBoundingClientRect();
|
|
166
|
-
const atBottom = thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 2;
|
|
167
|
-
const anchor = Array.from($("#msgList").querySelectorAll("[data-msg-id]")).find((node) => node.getBoundingClientRect().bottom > edge.top);
|
|
168
|
-
const anchorId = anchor?.dataset.msgId, anchorTop = anchor ? anchor.getBoundingClientRect().top - edge.top : 0;
|
|
169
|
-
for (const receipt of receipts) app.state.receipts.set(`${roomId}\0${receipt.messageID}`, receipt.readers);
|
|
170
|
-
for (const row of document.querySelectorAll(`#msgList [data-msg-id]`)) {
|
|
171
|
-
const message = app.state.room?.messages.find((item) => item.id === row.dataset.msgId);
|
|
172
|
-
if (message?.roomId === roomId || app.state.room?.id === roomId && message) updateReceiptDetails(row, message, app);
|
|
173
|
-
}
|
|
174
|
-
if (atBottom) thread.scrollTop = thread.scrollHeight;
|
|
175
|
-
else if (anchorId) {
|
|
176
|
-
const next = $("#msgList").querySelector(`[data-msg-id="${CSS.escape(anchorId)}"]`);
|
|
177
|
-
if (next) thread.scrollTop += next.getBoundingClientRect().top - thread.getBoundingClientRect().top - anchorTop;
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
|
|
181
164
|
app.renderMessages = (live = false) => {
|
|
182
165
|
const thread = $("#thread"), list = $("#msgList"), messages = app.state.room.messages;
|
|
183
166
|
const nearBottom = thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 2;
|
|
@@ -269,7 +252,7 @@ export function initMessages(app) {
|
|
|
269
252
|
};
|
|
270
253
|
|
|
271
254
|
app.send = async () => {
|
|
272
|
-
const input = $("#msgInput"), text = input.value.trim(), room = app.state.room;
|
|
255
|
+
const input = $("#msgInput"), text = input.value.trim(), room = app.state.room, isQuestion = Boolean(app.isQuestionSending?.());
|
|
273
256
|
if (!text || !room) return;
|
|
274
257
|
const revision = app.state.draftRevision.get(room.id) || 0;
|
|
275
258
|
app.closeMention();
|
|
@@ -281,7 +264,7 @@ export function initMessages(app) {
|
|
|
281
264
|
app.hideSendError();
|
|
282
265
|
try {
|
|
283
266
|
const updated = await app.api(`/rooms/${encodeURIComponent(room.id)}/messages`, {
|
|
284
|
-
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text }),
|
|
267
|
+
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text, isQuestion }),
|
|
285
268
|
});
|
|
286
269
|
if (app.state.room?.id !== room.id) return;
|
|
287
270
|
app.state.room = updated;
|
|
@@ -302,7 +285,7 @@ export function initMessages(app) {
|
|
|
302
285
|
} catch (error) {
|
|
303
286
|
const unchanged = (app.state.draftRevision.get(room.id) || 0) === revision;
|
|
304
287
|
const failures = app.state.failedMessages.get(room.id) || [];
|
|
305
|
-
app.saveFailedMessages(room.id, [...failures, { id: createRandomId(), text }]);
|
|
288
|
+
app.saveFailedMessages(room.id, [...failures, { id: createRandomId(), text, isQuestion }]);
|
|
306
289
|
app.renderFailedMessages();
|
|
307
290
|
if (app.state.room?.id !== room.id) return;
|
|
308
291
|
if (unchanged) app.clearDraft(room.id);
|
|
@@ -458,43 +441,4 @@ function trimMessages(list, app) {
|
|
|
458
441
|
}
|
|
459
442
|
}
|
|
460
443
|
|
|
461
|
-
function receiptDetails(message, app) {
|
|
462
|
-
const details = document.createElement("details");
|
|
463
|
-
details.className = "message-readers";
|
|
464
|
-
const summary = document.createElement("summary"), count = document.createElement("span"), list = document.createElement("ul");
|
|
465
|
-
count.className = "receipt-count";
|
|
466
|
-
list.className = "receipt-reader-list";
|
|
467
|
-
summary.append(count);
|
|
468
|
-
details.append(summary, list);
|
|
469
|
-
updateReceiptDetails(details.closest("li") || details, message, app);
|
|
470
|
-
return details;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
function updateReceiptDetails(row, message, app) {
|
|
474
|
-
const details = row.matches?.("details.message-readers") ? row : row.querySelector("details.message-readers");
|
|
475
|
-
if (!details) return;
|
|
476
|
-
const detailsKey = `${app.state.room?.id || message.roomId || ""}\0${message.id}`;
|
|
477
|
-
const senderID = message.senderSessionID || (app.state.room?.kind === "direct" ? message.senderId : "");
|
|
478
|
-
const readers = (app.state.receipts.get(detailsKey) || []).filter((reader) => reader.sessionID !== senderID);
|
|
479
|
-
details.hidden = readers.length === 0;
|
|
480
|
-
const summary = details.querySelector("summary"), label = app.t("receiptCount", { count: readers.length });
|
|
481
|
-
summary.setAttribute("aria-label", label);
|
|
482
|
-
summary.title = label;
|
|
483
|
-
details.querySelector(".receipt-count").textContent = String(readers.length);
|
|
484
|
-
const list = details.querySelector(".receipt-reader-list");
|
|
485
|
-
list.textContent = "";
|
|
486
|
-
const members = app.state.memberSessions.get(app.state.room?.id) || [];
|
|
487
|
-
for (const reader of readers) {
|
|
488
|
-
const row = document.createElement("li"), name = document.createElement("span"), evidence = document.createElement("span");
|
|
489
|
-
const duplicates = members.filter((member) => member.name === reader.name).length > 1 || readers.filter((item) => item.name === reader.name).length > 1;
|
|
490
|
-
name.textContent = `${reader.name}${duplicates ? ` · ${reader.sessionID.slice(0, 8)}` : ""}${reader.currentMember === false ? ` · ${app.t("departedMember")}` : ""}`;
|
|
491
|
-
evidence.textContent = app.t(`receiptEvidence_${reader.evidence}`);
|
|
492
|
-
evidence.title = evidence.textContent;
|
|
493
|
-
evidence.setAttribute("aria-label", evidence.textContent);
|
|
494
|
-
row.dataset.readerSessionId = reader.sessionID;
|
|
495
|
-
row.append(name, evidence);
|
|
496
|
-
list.append(row);
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
444
|
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { LS } from "./state.js";
|
|
2
|
+
|
|
3
|
+
export function initQuestions(app) {
|
|
4
|
+
const $ = app.$, pending = new Map(), selected = new Map(), drafts = new Map(), questionMode = new Map(), submitting = new Set();
|
|
5
|
+
let request = 0;
|
|
6
|
+
const panel = $("#questionPanel"), trigger = $("#btnQuestions"), list = $("#questionList"), status = $("#questionStatus");
|
|
7
|
+
app.isQuestionSending = () => Boolean(questionMode.get(app.state.room?.id));
|
|
8
|
+
$("#questionToggle").addEventListener("change", (event) => {
|
|
9
|
+
if (app.state.room) questionMode.set(app.state.room.id, event.target.checked);
|
|
10
|
+
});
|
|
11
|
+
|
|
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
|
+
|
|
26
|
+
function render(roomId) {
|
|
27
|
+
const items = pending.get(roomId) || [];
|
|
28
|
+
const active = app.state.room?.id === roomId;
|
|
29
|
+
if (active) {
|
|
30
|
+
$("#questionBadge").textContent = items.length > 99 ? "99+" : String(items.length);
|
|
31
|
+
$("#questionBadge").hidden = !items.length;
|
|
32
|
+
trigger.setAttribute("aria-label", `${app.t("questions")}: ${items.length}`);
|
|
33
|
+
list.textContent = "";
|
|
34
|
+
for (const question of items) {
|
|
35
|
+
const item = document.createElement("li"), button = document.createElement("button");
|
|
36
|
+
button.type = "button";
|
|
37
|
+
button.className = "question-item";
|
|
38
|
+
button.dataset.questionId = question.id;
|
|
39
|
+
button.setAttribute("aria-pressed", String(selected.get(roomId) === question.id));
|
|
40
|
+
button.textContent = question.text;
|
|
41
|
+
item.append(button);
|
|
42
|
+
list.append(item);
|
|
43
|
+
}
|
|
44
|
+
status.hidden = items.length > 0 || status.dataset.error === "true";
|
|
45
|
+
if (!items.length && status.dataset.error !== "true") app.setText(status, "questionsEmpty");
|
|
46
|
+
renderReply(roomId);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function renderReply(roomId) {
|
|
51
|
+
const box = $("#questionReply"), question = (pending.get(roomId) || []).find((item) => item.id === selected.get(roomId));
|
|
52
|
+
if (question && box.dataset.roomId === roomId && box.dataset.questionId === question.id) {
|
|
53
|
+
const key = `${roomId}\0${question.id}`, input = box.querySelector("textarea"), send = box.querySelector("button");
|
|
54
|
+
box.hidden = false;
|
|
55
|
+
if (input && send) send.disabled = !input.value.trim() || submitting.has(key);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
box.textContent = "";
|
|
59
|
+
box.dataset.roomId = question ? roomId : "";
|
|
60
|
+
box.dataset.questionId = question?.id || "";
|
|
61
|
+
box.hidden = !question;
|
|
62
|
+
if (!question) return;
|
|
63
|
+
const heading = document.createElement("p"), form = document.createElement("form"), input = document.createElement("textarea"), send = document.createElement("button");
|
|
64
|
+
heading.className = "question-reply-quote";
|
|
65
|
+
heading.textContent = question.text;
|
|
66
|
+
form.className = "question-reply-form";
|
|
67
|
+
input.className = "input";
|
|
68
|
+
input.rows = 2;
|
|
69
|
+
input.maxLength = 2000;
|
|
70
|
+
input.placeholder = app.t("answerPlaceholder");
|
|
71
|
+
input.setAttribute("aria-label", app.t("answerPlaceholder"));
|
|
72
|
+
input.value = drafts.get(`${roomId}\0${question.id}`) || "";
|
|
73
|
+
const key = `${roomId}\0${question.id}`;
|
|
74
|
+
send.type = "submit";
|
|
75
|
+
send.className = "btn primary small";
|
|
76
|
+
send.textContent = app.t("sendAnswer");
|
|
77
|
+
send.disabled = !input.value.trim() || submitting.has(key);
|
|
78
|
+
input.addEventListener("input", () => {
|
|
79
|
+
drafts.set(`${roomId}\0${question.id}`, input.value);
|
|
80
|
+
send.disabled = !input.value.trim() || submitting.has(key);
|
|
81
|
+
});
|
|
82
|
+
form.addEventListener("submit", (event) => {
|
|
83
|
+
event.preventDefault();
|
|
84
|
+
void answer(roomId, question.id, input.value.trim(), send);
|
|
85
|
+
});
|
|
86
|
+
form.append(input, send);
|
|
87
|
+
box.append(heading, form);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function load(roomId = app.state.room?.id) {
|
|
91
|
+
if (!roomId) return;
|
|
92
|
+
const id = ++request;
|
|
93
|
+
if (app.state.room?.id === roomId && !pending.has(roomId)) {
|
|
94
|
+
app.setText(status, "questionsLoading");
|
|
95
|
+
status.hidden = false;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/questions`);
|
|
99
|
+
if (id !== request || app.state.room?.id !== roomId || !Array.isArray(result.questions)) return;
|
|
100
|
+
pending.set(roomId, result.questions);
|
|
101
|
+
status.dataset.error = "false";
|
|
102
|
+
status.hidden = true;
|
|
103
|
+
render(roomId);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (id !== request || app.state.room?.id !== roomId) return;
|
|
106
|
+
if (error.status === 401) app.openChatSettings();
|
|
107
|
+
status.dataset.error = "true";
|
|
108
|
+
app.setText(status, "questionsFailed");
|
|
109
|
+
status.hidden = false;
|
|
110
|
+
render(roomId);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function answer(roomId, questionId, text, button) {
|
|
115
|
+
const key = `${roomId}\0${questionId}`;
|
|
116
|
+
if (!text || submitting.has(key)) return;
|
|
117
|
+
submitting.add(key);
|
|
118
|
+
button.disabled = true;
|
|
119
|
+
renderReply(roomId);
|
|
120
|
+
try {
|
|
121
|
+
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/questions/${encodeURIComponent(questionId)}/answers`, {
|
|
122
|
+
method: "POST", body: JSON.stringify({ sessionID: LS.sessionID, text }),
|
|
123
|
+
});
|
|
124
|
+
const entry = app.state.rooms.get(roomId);
|
|
125
|
+
if (entry && result.answer && !entry.room.messages.some((item) => item.id === result.answer.id)) {
|
|
126
|
+
entry.room.messages.push({ ...result.answer, roomId });
|
|
127
|
+
entry.room.messages.sort((left, right) => left.createdAt - right.createdAt);
|
|
128
|
+
if (entry.room.messages.length > 500) entry.room.messages.splice(0, entry.room.messages.length - 500);
|
|
129
|
+
if (app.state.room?.id === roomId) app.renderMessages(true);
|
|
130
|
+
}
|
|
131
|
+
pending.set(roomId, (pending.get(roomId) || []).filter((item) => item.id !== result.questionID));
|
|
132
|
+
if (selected.get(roomId) === questionId) selected.delete(roomId);
|
|
133
|
+
drafts.delete(`${roomId}\0${questionId}`);
|
|
134
|
+
if (app.state.room?.id === roomId) render(roomId);
|
|
135
|
+
void load(roomId);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (error.status === 409 || error.status === 404) {
|
|
138
|
+
await load(roomId);
|
|
139
|
+
if (error.status === 409 && app.state.room?.id === roomId && !(pending.get(roomId) || []).some((item) => item.id === questionId)) {
|
|
140
|
+
if (selected.get(roomId) === questionId) selected.delete(roomId);
|
|
141
|
+
drafts.delete(`${roomId}\0${questionId}`);
|
|
142
|
+
app.setText(status, "questionAlreadyAnswered");
|
|
143
|
+
status.dataset.error = "true";
|
|
144
|
+
status.hidden = false;
|
|
145
|
+
render(roomId);
|
|
146
|
+
}
|
|
147
|
+
} else if (error.status === 401 && app.state.room?.id === roomId) {
|
|
148
|
+
app.openChatSettings();
|
|
149
|
+
app.showSendError("sendAuth");
|
|
150
|
+
} else if (app.state.room?.id === roomId) {
|
|
151
|
+
app.setText(status, "answerFailed");
|
|
152
|
+
status.hidden = false;
|
|
153
|
+
}
|
|
154
|
+
} finally {
|
|
155
|
+
submitting.delete(key);
|
|
156
|
+
if (app.state.room?.id === roomId) renderReply(roomId);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
trigger.addEventListener("click", () => {
|
|
161
|
+
const open = panel.hidden;
|
|
162
|
+
app.closePanels();
|
|
163
|
+
panel.hidden = !open;
|
|
164
|
+
trigger.setAttribute("aria-expanded", String(open));
|
|
165
|
+
if (open) {
|
|
166
|
+
void load();
|
|
167
|
+
$("#questionHeading").focus();
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
$("#btnRefreshQuestions").addEventListener("click", () => void load());
|
|
171
|
+
list.addEventListener("click", (event) => {
|
|
172
|
+
event.stopPropagation();
|
|
173
|
+
const button = event.target.closest("[data-question-id]");
|
|
174
|
+
if (!button || !app.state.room) return;
|
|
175
|
+
selected.set(app.state.room.id, button.dataset.questionId);
|
|
176
|
+
render(app.state.room.id);
|
|
177
|
+
$("#questionReply textarea")?.focus();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const closePanels = app.closePanels;
|
|
181
|
+
app.closePanels = () => {
|
|
182
|
+
closePanels();
|
|
183
|
+
panel.hidden = true;
|
|
184
|
+
trigger.setAttribute("aria-expanded", "false");
|
|
185
|
+
};
|
|
186
|
+
document.addEventListener("keydown", (event) => {
|
|
187
|
+
if (event.key === "Escape" && !panel.hidden) {
|
|
188
|
+
app.closePanels();
|
|
189
|
+
trigger.focus();
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
const activateRoom = app.activateRoom;
|
|
193
|
+
app.activateRoom = (roomId) => {
|
|
194
|
+
activateRoom(roomId);
|
|
195
|
+
$("#questionToggle").checked = Boolean(questionMode.get(roomId));
|
|
196
|
+
void load(roomId);
|
|
197
|
+
};
|
|
198
|
+
const applyLocale = app.applyLocale;
|
|
199
|
+
app.applyLocale = () => {
|
|
200
|
+
applyLocale();
|
|
201
|
+
if (app.state.room) render(app.state.room.id);
|
|
202
|
+
};
|
|
203
|
+
setInterval(() => { if (app.state.room) void load(); }, 5000);
|
|
204
|
+
}
|