diff --git a/README.md b/README.md index 4477b59..406c610 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ | 加密群聊 | 创建群组、邀请联系人和管理成员。每个账号选一台已核验设备参与 MLS 加密文字聊天。 | | 加密附件 | 两端可在私聊和群聊发送、下载最多 8 MiB 的文件,并在本机预览图片和视频。 | | 加密语音 | 两端可在私聊和群聊录制、取消或发送最长 60 秒的语音消息,接收方在本机解密播放。 | -| 消息管理 | 私聊和群聊支持引用回复、编辑文字、撤回、本机搜索和收藏。群消息可转发到其他已建立加密会话的群或已核验的联系人设备。私聊支持转发给联系人及可选加密已读回执。 | +| 消息管理 | 私聊和群聊支持引用回复、编辑文字、撤回、本机搜索和收藏。消息可转发到已建立加密会话的群或已核验的联系人设备。私聊支持可选加密已读回执。 | | Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史、下载和隐私窗口,网页运行在独立会话中。 | 当前为开发预览。大文件分片、音视频和多设备历史同步仍在开发,Windows 安装包尚未发布。 [开发进度与验收记录](docs/progress.md) diff --git a/docs/progress.md b/docs/progress.md index 4cf3400..a13a456 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -19,7 +19,7 @@ Web 和 Electron 共用 Vue 客户端,独立接入同一 Go API。真实 Postg ## 尚待实现 - 阶段一还需邮箱和手机号注册、验证码服务、找回密码和双重验证。真实联调需要 SMTP 与短信供应商配置。 -- 阶段二与三还需分片附件、私聊转发至群聊、多设备可信授权、加密备份与跨设备历史同步。群聊和私聊的文字及小附件已进入开发预览。 +- 阶段二与三还需分片附件、多设备可信授权、加密备份与跨设备历史同步。群聊和私聊的文字及小附件已进入开发预览。 - 阶段四还需 WebRTC 音视频、TURN、SFU、屏幕共享及媒体 E2EE。 - 阶段五还需反钓鱼及聊天联动。 - 阶段六到八还需社交、扩展生态、独立管理后台、推送、生产部署与备份演练、压力测试、安全评估、签名安装包和发布流程。 @@ -138,3 +138,9 @@ Windows Electron 与 Edge 集成测试覆盖搜索引用和附件名、离开群 双端可将群消息转发到其他已加入且已核验的群加密会话,或转发到已核验的联系人设备。转发携带文字正文或附件。图片、视频和语音保留原有媒体类型。附件经本机完整性校验和解密,再以新的附件标识、文件密钥与随机数加密上传。待投递的群转发可在目标群继续重试。原消息的引用片段和发送人信息不随转发发送。 Windows Electron 与 Edge 验证向尚无加密会话的群转发会被拒绝、转发群文件后附件标识和密文不同且下载内容逐字节相同,以及群文字转发到联系人私聊。私聊附件转发回归通过。 + +## 私聊转发至群聊 + +Windows 和 Web 的私聊与群聊共用转发目标选择。私聊支持把文字正文和附件转发给已核验的联系人设备,或转发到本机已加入并核验的群加密会话。客户端先核对目标群会话,再读取本机消息或下载附件密文。附件通过认证后使用新的标识和密钥二次加密,目标群世代变更会阻断转发。 + +集成测试验证 Web 转发私聊文字后 Windows 在群内接收,Windows 转发私聊文件后 Web 下载逐字节一致且密文及附件标识变化。未建立加密会话的目标群拒绝转发,测试确认没有群文件上传或群消息提交。原有私聊及群聊转发回归通过。 diff --git a/tests/group-chat.test.mjs b/tests/group-chat.test.mjs index e20b160..6bd1d15 100644 --- a/tests/group-chat.test.mjs +++ b/tests/group-chat.test.mjs @@ -373,6 +373,57 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员 await owner.getByRole('link', { name: names[1] }).click() await owner.getByRole('button', { name: '启用本机加密聊天' }).click() await owner.getByText(edited, { exact: true }).waitFor() + + await owner.getByRole('listitem').filter({ hasText: edited }).getByRole('button', { name: '转发', exact: true }).click() + const privateForward = owner.getByRole('form', { name: '转发私聊消息' }) + await privateForward.getByLabel('转发给').selectOption({ label: targetGroup }) + await privateForward.getByRole('button', { name: '确认转发' }).click() + await owner.getByText('已转发到群聊', { exact: true }).waitFor() + await windows.getByRole('link', { name: new RegExp(targetGroup) }).click() + await chat(windows).getByRole('button', { name: '启用本机群聊' }).click() + await chat(windows).getByText(edited, { exact: true }).waitFor() + + const pendingGroup = '待加密群 ' + id() + await windows.getByLabel('群名称', { exact: true }).fill(pendingGroup) + await windows.getByRole('button', { name: '创建群组' }).click() + await windows.getByRole('heading', { name: pendingGroup }).waitFor() + await work(windows).getByRole('link', { name: '消息', exact: true }).click() + await windows.getByRole('link', { name: names[0] }).click() + await windows.getByRole('button', { name: '启用本机加密聊天' }).click() + const privateFile = '私聊文件-' + id() + '.txt' + const originalUpload = windows.waitForResponse((item) => /\/api\/v1\/attachments\//.test(item.url()) && item.request().method() === 'POST') + await windows.getByLabel('发送附件').setInputFiles({ name: privateFile, mimeType: 'text/plain', buffer: contents }) + const originalFile = await originalUpload + assert.equal(originalFile.status(), 204) + const originalBytes = originalFile.request().postDataBuffer() + assert.ok(originalBytes && originalBytes.length === contents.length + 16) + + await windows.getByRole('listitem').filter({ hasText: privateFile }).getByRole('button', { name: '转发', exact: true }).click() + const desktopForward = windows.getByRole('form', { name: '转发私聊消息' }) + const writes = [] + const recordWrite = (req) => { if (req.method() === 'POST' && req.url().includes('/groups/')) writes.push(req.url()) } + windows.on('request', recordWrite) + await desktopForward.getByLabel('转发给').selectOption({ label: pendingGroup }) + await desktopForward.getByRole('button', { name: '确认转发' }).click() + await windows.getByRole('alert').getByText('请先在目标群加入加密会话').waitFor() + windows.off('request', recordWrite) + assert.deepEqual(writes, []) + + await desktopForward.getByLabel('转发给').selectOption({ label: targetGroup }) + const groupUpload = windows.waitForResponse((item) => item.url().includes('/groups/') && item.url().includes('/attachments/') && item.request().method() === 'POST') + await desktopForward.getByRole('button', { name: '确认转发' }).click() + const targetFile = await groupUpload + assert.equal(targetFile.status(), 204) + assert.notEqual(new URL(targetFile.url()).pathname.split('/').at(-1), new URL(originalFile.url()).pathname.split('/').at(-1)) + await windows.getByText('已转发到群聊', { exact: true }).waitFor() + await work(owner).getByRole('link', { name: '群组' }).click() + await owner.getByRole('link', { name: new RegExp(targetGroup) }).click() + await chat(owner).getByRole('button', { name: '启用本机群聊' }).click() + const groupDownload = owner.waitForEvent('download') + const groupFileResponse = owner.waitForResponse((item) => item.url().endsWith(new URL(targetFile.url()).pathname) && item.request().method() === 'GET') + await chat(owner).getByRole('button', { name: '下载 ' + privateFile, exact: true }).click() + assert.notDeepEqual(await (await groupFileResponse).body(), originalBytes) + assert.deepEqual(await readFile(await (await groupDownload).path()), contents) } catch (error) { const status = await Promise.all(pages.map((page) => page.locator('[role=alert], .group-chat [role=status]').allTextContents())) throw new Error(JSON.stringify(status), { cause: error }) diff --git a/web/src/chat/GroupForward.vue b/web/src/chat/ForwardDialog.vue similarity index 90% rename from web/src/chat/GroupForward.vue rename to web/src/chat/ForwardDialog.vue index 52ec79e..7b2865d 100644 --- a/web/src/chat/GroupForward.vue +++ b/web/src/chat/ForwardDialog.vue @@ -6,7 +6,7 @@ import type { Peer } from './chat' import type { Contact } from '../contacts' import type { ForwardDestination, GroupSummary } from './group-chat' -const props = defineProps<{ groupId: string; pinned: Record; busy: boolean }>() +const props = defineProps<{ sourceGroup?: string; pinned: Record; busy: boolean }>() const emit = defineEmits<{ cancel: []; confirm: [destination: ForwardDestination] }>() const session = useSession() const groups = ref([]) @@ -28,7 +28,7 @@ const target = computed(() => { onMounted(async () => { try { const [allGroups, allContacts] = await Promise.all([session.authorized('/groups'), session.contacts()]) - groups.value = allGroups.filter((group) => group.state === 'active' && group.id !== props.groupId) + groups.value = allGroups.filter((group) => group.state === 'active' && group.id !== props.sourceGroup) contacts.value = allContacts.filter((contact) => contact.state === 'friend') } catch (cause) { error.value = cause instanceof Error ? cause.message : '无法读取转发目标' @@ -57,7 +57,7 @@ watch(destination, async (value, _previous, onCleanup) => { diff --git a/web/src/chat/GroupChat.vue b/web/src/chat/GroupChat.vue index 9f681ac..06fb774 100644 --- a/web/src/chat/GroupChat.vue +++ b/web/src/chat/GroupChat.vue @@ -4,7 +4,7 @@ import { useSession } from '../session' import { downloadAttachment } from './attachments' import type { Attachment } from './attachments' import MediaPreview from './MediaPreview.vue' -import GroupForward from './GroupForward.vue' +import ForwardDialog from './ForwardDialog.vue' import VoiceMessage from './VoiceMessage.vue' import VoiceRecorder from './VoiceRecorder.vue' import { enableChat, trust } from './chat' @@ -277,8 +277,8 @@ onBeforeUnmount(() => clearInterval(timer)) - +

撤回后,群成员的聊天界面会更新。成员可能已保存副本。

diff --git a/web/src/chat/chat.ts b/web/src/chat/chat.ts index f360312..d3ef022 100644 --- a/web/src/chat/chat.ts +++ b/web/src/chat/chat.ts @@ -276,10 +276,12 @@ 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 content = line.attachment ? await readForwardedFile(line.attachment, call) : line.text - await send(identity, recipient, device, content, call) +export function readPrivateForward(identity: ChatIdentity, message: Pick, call: Call) { + return update(identity, async (state) => { + const line = state.lines.find((item) => item.id === message.id && item.peer === message.peer && item.own === message.own) + if (!line || line.recalled) throw new Error('此私聊消息无法转发') + return line.attachment ? readForwardedFile(line.attachment, call) : line.text + }) } export function recentConversations(identity: ChatIdentity) { diff --git a/web/src/chat/group-chat.ts b/web/src/chat/group-chat.ts index e9de381..36b9a7d 100644 --- a/web/src/chat/group-chat.ts +++ b/web/src/chat/group-chat.ts @@ -284,14 +284,17 @@ export function discardPendingGroupMessage(identity: ChatIdentity, group: GroupD } export async function forwardGroupMessage(identity: ChatIdentity, source: GroupDetail, message: LineRef, destination: ForwardDestination, call: Call) { - const content = await update(identity, async (state) => { + return forwardContent(identity, destination, call, () => update(identity, async (state) => { const item = state.groupChats?.[source.id]?.lines.find((entry) => entry.id === message.id && entry.epoch === message.epoch && entry.sender === message.sender) if (!item || item.recalled) throw new Error('此群消息无法转发') return item.attachment ? readForwardedFile(item.attachment, call, groupAttachmentPath(source, item.attachment.id)) : item.text - }) + })) +} + +export async function forwardContent(identity: ChatIdentity, destination: ForwardDestination, call: Call, readContent: () => Promise) { if ('user' in destination) { - await send(identity, destination.user, destination.device, content, call) + await send(identity, destination.user, destination.device, await readContent(), call) return '已提交转发' } @@ -300,7 +303,7 @@ export async function forwardGroupMessage(identity: ChatIdentity, source: GroupD const view = await synchronizeGroup(identity, target, call) if (!view.ready || !view.current) throw new Error('请先在目标群加入加密会话') if (view.hasPending) throw new Error('目标群还有待处理的消息,请先打开目标群') - await sendGroupMessage(identity, target, content, call, view.current.id) + await sendGroupMessage(identity, target, await readContent(), call, view.current.id) // At this point the outbox is durable. The target can retry after a network failure. try { const result = await synchronizeGroup(identity, target, call) diff --git a/web/src/views/ChatView.vue b/web/src/views/ChatView.vue index d4b0a5a..64bf458 100644 --- a/web/src/views/ChatView.vue +++ b/web/src/views/ChatView.vue @@ -2,8 +2,11 @@ import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue' import { useRoute } from 'vue-router' import { useSession } from '../session' -import { directory, enableChat, forwardMessage, localMessageAction, markRead, mutateMessage, recentConversations, send, setReadReceipts, synchronize, trust } from '../chat/chat' -import type { ChatLine, Peer } from '../chat/chat' +import { enableChat, localMessageAction, markRead, mutateMessage, readPrivateForward, recentConversations, send, setReadReceipts, synchronize, trust } from '../chat/chat' +import type { ChatLine } from '../chat/chat' +import { forwardContent } from '../chat/group-chat' +import type { ForwardDestination } from '../chat/group-chat' +import ForwardDialog from '../chat/ForwardDialog.vue' import type { Contact } from '../contacts' import { downloadAttachment } from '../chat/attachments' import type { Attachment } from '../chat/attachments' @@ -24,10 +27,7 @@ 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 forwardStatus = ref('') const error = ref('') const busy = ref(false) const contacts = ref([]) @@ -249,31 +249,18 @@ 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 - }) + forwardStatus.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) +function submitForward(destination: ForwardDestination) { + const identity = session.identity + const message = forwarding.value + if (identity && message) void execute(async () => { + const status = await forwardContent(identity, destination, session.authorized, + () => readPrivateForward(identity, message, session.authorized)) + if (forwarding.value !== message) return + forwardStatus.value = status forwarding.value = undefined await refresh() }) @@ -298,6 +285,7 @@ watch(selected, () => { editing.value = undefined recalling.value = undefined forwarding.value = undefined + forwardStatus.value = '' pendingVoice.value = undefined draft.value = '' scheduleReceipt() @@ -336,6 +324,7 @@ onBeforeUnmount(() => {
+

{{ forwardStatus }}