From 0b89634af27a83e92087aa74b4b255fed7f9b333 Mon Sep 17 00:00:00 2001 From: xhgit Date: Fri, 13 Nov 2026 19:28:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=90=91=E5=B7=B2=E6=A0=B8?= =?UTF-8?q?=E9=AA=8C=E8=AE=BE=E5=A4=87=E5=8A=A0=E5=AF=86=E8=BD=AC=E5=8F=91?= =?UTF-8?q?=E6=96=87=E5=AD=97=E5=92=8C=E9=99=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- docs/progress.md | 2 +- tests/encrypted-chat.test.mjs | 8 +++++ web/src/chat/attachments.ts | 7 ++++- web/src/chat/chat.ts | 9 +++++- web/src/views/ChatView.vue | 59 +++++++++++++++++++++++++++++++++-- 6 files changed, 81 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7cd6be1..0ef9cab 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ | 联系人 | 按用户名查找、处理好友申请、管理备注与分组,以及拉黑。变更可在双端实时同步。 | | 加密文字聊天 | 在两个已核验设备之间发送 MLS 加密消息,支持离线接收。 | | 加密附件 | 两端可发送和下载最多 8 MiB 的文件,下载时验证完整性。 | -| 消息管理 | 支持引用回复、编辑、撤回、搜索本机历史、收藏和本机删除。 | +| 消息管理 | 支持引用回复、编辑、撤回、转发、搜索本机历史、收藏和本机删除。 | | Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史、下载和隐私窗口,网页运行在独立会话中。 | 当前为开发预览。大文件分片、群聊、音视频和多设备历史同步仍在开发,Windows 安装包尚未发布。 [开发进度与验收记录](docs/progress.md) diff --git a/docs/progress.md b/docs/progress.md index 7652983..a3cfcab 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -59,7 +59,7 @@ Windows 与 Web 支持发送和下载最多 8 MiB 的附件。文件名、长度 Windows 与 Web 可发送带引用摘要的 MLS 文字消息。两端支持按正文、引用和文件名搜索本机历史,以及收藏和本机删除。收藏标记与历史保存在加密本地记录中。集成测试覆盖引用跨端显示、收藏筛选、搜索及删除后另一端仍可读取原消息。 -原发送设备可编辑文字及撤回文字或附件消息。更新作为 MLS 应用消息传递,由客户端核对原发送设备与消息 ID。撤回会更新对方客户端显示,先前保存的附件、截图和其他独立副本仍可能存在。转发与跨设备收藏同步待后续交付。 +原发送设备可编辑文字及撤回文字或附件消息。更新作为 MLS 应用消息传递,由客户端核对原发送设备与消息 ID。撤回会更新对方客户端显示,先前保存的附件、截图和其他独立副本仍可能存在。文字与附件可转发给已核验设备。转发附件会在本机校验并用新的密钥和附件 ID 加密。跨设备收藏同步待后续交付。 ## 浏览器书签和历史 diff --git a/tests/encrypted-chat.test.mjs b/tests/encrypted-chat.test.mjs index 92c37d2..f3e813b 100644 --- a/tests/encrypted-chat.test.mjs +++ b/tests/encrypted-chat.test.mjs @@ -157,6 +157,14 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢 const file = await downloaded assert.equal(file.suggestedFilename(), fileName) assert.deepEqual(await readFile(await file.path()), contents) + const forwardedRequest = web.waitForRequest((req) => req.method() === 'POST' && req.url().includes('/api/v1/attachments/')) + await web.getByRole('listitem').filter({ hasText: fileName }).getByRole('button', { name: '转发', exact: true }).click() + await web.getByLabel('转发给').selectOption({ label: b }) + await web.getByRole('button', { name: '确认转发' }).click() + const forwarded = await forwardedRequest + assert.notEqual(new URL(forwarded.url()).pathname, fileURL.pathname) + assert.notDeepEqual(forwarded.postDataBuffer(), uploaded) + await windows.getByRole('button', { name: '下载 ' + fileName, exact: true }).nth(1).waitFor() await windows.getByRole('button', { name: '退出登录' }).click() await windows.getByRole('heading', { name: '登录 XHChat' }).waitFor() diff --git a/web/src/chat/attachments.ts b/web/src/chat/attachments.ts index 2810755..24c7638 100644 --- a/web/src/chat/attachments.ts +++ b/web/src/chat/attachments.ts @@ -102,7 +102,7 @@ function encodeContent(kind: number, value: object) { return message } -export async function downloadAttachment(attachment: Attachment, call: Call) { +export async function readAttachment(attachment: Attachment, call: Call) { const ciphertext = await call(attachmentPath(attachment.id), 'GET', undefined, true) if (ciphertext.byteLength !== attachment.size + 16) throw new Error('附件完整性校验失败') const raw = decode(attachment.key) @@ -115,6 +115,11 @@ export async function downloadAttachment(attachment: Attachment, call: Call) { } finally { raw.fill(0) } + return bytes +} + +export async function downloadAttachment(attachment: Attachment, call: Call) { + const bytes = await readAttachment(attachment, call) const url = URL.createObjectURL(new Blob([bytes], { type: 'application/octet-stream' })) const link = document.createElement('a') link.download = attachment.name diff --git a/web/src/chat/chat.ts b/web/src/chat/chat.ts index 83e451d..6671957 100644 --- a/web/src/chat/chat.ts +++ b/web/src/chat/chat.ts @@ -3,7 +3,7 @@ import { mlsBinding, signMls } from '../identity' import { base64, decode, fingerprint, loadEngine } from './engine' import type { DeviceMls } from './engine' import { withVault } from './vault' -import { decodeMessage, encodeAttachment, encodeMutation, encodeReply, sealAttachment, uploadAttachment } from './attachments' +import { decodeMessage, encodeAttachment, encodeMutation, encodeReply, readAttachment, sealAttachment, uploadAttachment } from './attachments' import type { Attachment, Mutation } from './attachments' export interface Peer { @@ -259,6 +259,13 @@ export function localMessageAction(identity: ChatIdentity, line: ChatLine, actio }) } +export async function forwardMessage(identity: ChatIdentity, line: ChatLine, recipient: string, device: Peer, call: Call) { + if (line.recalled) throw new Error('撤回的消息无法转发') + const attachment = line.attachment + const content = attachment ? new File([await readAttachment(attachment, call)], attachment.name) : line.text + await send(identity, recipient, device, content, call) +} + export function synchronize(identity: ChatIdentity, user: string, call: Call) { return update(identity, async (state, save, factory) => { const peers = await directory(call, user) diff --git a/web/src/views/ChatView.vue b/web/src/views/ChatView.vue index c4783fe..1c5c102 100644 --- a/web/src/views/ChatView.vue +++ b/web/src/views/ChatView.vue @@ -2,8 +2,9 @@ import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue' import { useRoute } from 'vue-router' import { useSession } from '../session' -import { enableChat, localMessageAction, mutateMessage, send, synchronize, trust } from '../chat/chat' -import type { ChatLine } from '../chat/chat' +import { directory, enableChat, forwardMessage, localMessageAction, mutateMessage, send, synchronize, trust } from '../chat/chat' +import type { ChatLine, Peer } from '../chat/chat' +import type { Contact } from '../contacts' import { downloadAttachment } from '../chat/attachments' import type { Attachment } from '../chat/attachments' @@ -19,6 +20,11 @@ const reply = ref() const deleting = ref() const editing = ref() const recalling = ref() +const forwarding = ref() +const destinations = ref([]) +const destination = ref('') +const destinationDevice = ref('') +const forwardDevices = ref([]) const error = ref('') const busy = ref(false) const view = ref>>() @@ -130,6 +136,37 @@ function localAction(line: ChatLine, action: 'favorite' | 'remove') { }) } +function beginForward(line: ChatLine) { + void execute(async () => { + destinations.value = (await session.contacts()).filter((contact) => contact.state === 'friend') + destination.value = '' + destinationDevice.value = '' + forwardDevices.value = [] + forwarding.value = line + }) +} + +function loadForwardDevices() { + destinationDevice.value = '' + forwardDevices.value = [] + if (!destination.value) return + void execute(async () => { + const devices = await directory(session.authorized, destination.value) + forwardDevices.value = devices.filter((device) => device.mls && view.value?.pinned[device.id] === device.publicKey) + destinationDevice.value = forwardDevices.value[0]?.id ?? '' + }) +} + +function submitForward() { + void execute(async () => { + const device = forwardDevices.value.find((candidate) => candidate.id === destinationDevice.value) + if (!session.identity || !forwarding.value || !device) return + await forwardMessage(session.identity, forwarding.value, destination.value, device, session.authorized) + forwarding.value = undefined + await refresh() + }) +} + function sync() { if (ownCode.value) void execute(refresh) } @@ -148,6 +185,7 @@ watch(selected, () => { deleting.value = undefined editing.value = undefined recalling.value = undefined + forwarding.value = undefined draft.value = '' }) watch(() => [session.inboxChanges, session.contactChanges, session.connection], sync) @@ -204,6 +242,7 @@ onBeforeUnmount(() => clearInterval(timer)) + @@ -221,6 +260,22 @@ onBeforeUnmount(() => clearInterval(timer)) +
+ + +

请先在对方聊天中核验接收设备。

+ + +
正在编辑消息