Merge pull request '增加群文件、图片和视频视图' (#35) from xhgit/group-media into main

Reviewed-on: #35
This commit was merged in pull request #35.
This commit is contained in:
xhgit committed 2026-11-15 14:34:42 +00:00
commit 4b0a1947a2
4 files changed
+102 -13

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@
| 联系人 | 按用户名查找、处理好友申请、管理备注与分组,以及拉黑。变更可在双端实时同步。 |
| 加密文字聊天 | 在两个已核验设备之间发送 MLS 加密消息,支持离线接收。 |
| 加密群聊 | 创建群组、邀请联系人和管理成员。群列表显示本机未读,并按账号同步置顶和免打扰。每个账号选一台已核验设备参与 MLS 加密文字聊天。 |
| 加密附件 | 两端可在私聊和群聊发送、下载最多 8 MiB 的文件,并在本机预览图片和视频。 |
| 加密附件 | 两端可在私聊和群聊发送、下载最多 8 MiB 的文件,并在本机预览图片和视频。群聊提供文件、图片和视频的分类视图。 |
| 加密语音 | 两端可在私聊和群聊录制、取消或发送最长 60 秒的语音消息,接收方在本机解密播放。 |
| 消息管理 | 私聊和群聊支持引用回复、编辑文字、撤回、本机搜索和收藏。消息可转发到已建立加密会话的群或已核验的联系人设备。私聊支持可选加密已读回执。 |
| Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史、下载和隐私窗口,网页运行在独立会话中。 |
+6
View File
@@ -115,6 +115,12 @@ Windows 和 Web 已接入群设备核验、MLS Welcome 校验、文字加解密
Windows 和 Web 可发送和下载最多 8 MiB 的群附件,并在本机预览常见图片和视频。接口验收覆盖大小限制、上传幂等、未发布附件隔离、发送设备绑定、移除和重新入群后的历史下载隔离。双端验收覆盖 Windows 发送文件到 Edge、逐字节核对下载、拒绝篡改的密文,以及 Edge 发送 PNG 到 Windows 预览。群附件分片和断点续传仍待实现。
## 群文件、图片和视频视图
两端可在群聊切换到群文件、群图片或群视频,按文件名搜索,或只显示本机收藏。分类从本机已同步并解密的群消息中读取,优先显示较新的附件,每次展示 24 条,可继续加载本机记录。打开预览或下载时才向服务端请求附件密文。撤回附件会将它移出分类视图。
Windows Electron 和 Edge 验证进入文件视图与搜索时未请求附件密文、下载字节一致、篡改拒绝、PNG 预览、WebM 解码播放、切换视图关闭预览以及图片撤回后从分类移除。
## 加密群语音
双端在群聊录制最长 60 秒的 Opus 语音。取消录音即释放麦克风,不产生上传。语音按群附件加密,群世代变更会取消当前录音或拒绝发送上一个世代的录音。接收设备下载并验证密文后,通过本机播放器播放、暂停或拖动进度。语音转写及未听提醒待实现。
+59 -5
View File
@@ -10,6 +10,7 @@ import { _electron, chromium } from 'playwright'
const base = process.env.XHCHAT_TEST_URL ?? 'http://127.0.0.1:8080'
const work = (page) => page.getByRole('navigation', { name: '工作区' })
const chat = (page) => page.getByRole('region', { name: '群消息' })
const historyView = (page, name) => chat(page).getByRole('group', { name: '群历史视图' }).getByRole('button', { name, exact: true })
const id = () => randomBytes(6).toString('hex')
async function register(page, username, password) {
@@ -286,9 +287,19 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
await chat(owner).getByRole('button', { name: '下载 ' + filename }).waitFor()
assert.equal(await chat(owner).getByRole('listitem').count(), 1)
await keyword.clear()
const attachmentRequests = []
const trackAttachment = (request) => { if (request.url().includes('/attachments/')) attachmentRequests.push(request.url()) }
owner.on('request', trackAttachment)
await historyView(owner, '群文件').click()
const fileShelf = chat(owner).getByRole('list', { name: '群文件' })
await keyword.fill(filename.toUpperCase())
const fileButton = fileShelf.getByRole('button', { name: '下载 ' + filename })
await fileButton.waitFor()
assert.deepEqual(attachmentRequests, [])
owner.off('request', trackAttachment)
const fileDownload = owner.waitForEvent('download')
const fileResponse = owner.waitForResponse((item) => item.url().includes('/attachments/') && item.request().method() === 'GET')
await chat(owner).getByRole('button', { name: '下载 ' + filename }).click()
await fileButton.click()
const response = await fileResponse
assert.equal(response.status(), 200)
const ciphertext = await response.body()
@@ -303,23 +314,66 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
tampered[0] ^= 1
await route.fulfill({ status: 200, contentType: 'application/octet-stream', body: tampered })
}, { times: 1 })
await chat(owner).getByRole('button', { name: '下载 ' + filename }).click()
await fileButton.click()
await chat(owner).getByRole('alert').getByText('附件完整性校验失败').waitFor()
await keyword.clear()
await historyView(owner, '消息').click()
const photo = '群图片-' + id() + '.png'
const pixel = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aX2sAAAAASUVORK5CYII=', 'base64')
await sendFile(owner, photo, 'image/png', pixel)
await chat(windows).getByRole('button', { name: '预览 ' + photo }).click()
await historyView(windows, '群图片').click()
const gallery = chat(windows).getByRole('list', { name: '群图片' })
assert.equal(await gallery.getByText(filename, { exact: false }).count(), 0)
await gallery.getByRole('button', { name: '预览 ' + photo }).click()
const image = chat(windows).getByRole('img', { name: photo })
await image.evaluate((element) => element.decode())
assert.equal(await image.evaluate((element) => element.naturalWidth), 1)
await chat(windows).getByRole('button', { name: '关闭预览' }).click()
await historyView(windows, '群视频').click()
await image.waitFor({ state: 'detached' })
await chat(windows).getByText('没有匹配的群视频。', { exact: true }).waitFor()
await historyView(windows, '群图片').click()
await gallery.getByRole('button', { name: '预览 ' + photo }).waitFor()
const photoLine = chat(owner).getByRole('listitem').filter({ hasText: photo })
assert.equal(await photoLine.getByRole('button', { name: '编辑', exact: true }).count(), 0)
await photoLine.getByRole('button', { name: '撤回', exact: true }).click()
await chat(owner).getByRole('button', { name: '确认撤回' }).click()
await chat(windows).getByRole('button', { name: '预览 ' + photo }).waitFor({ state: 'detached' })
await chat(windows).getByText('没有匹配的群图片。', { exact: true }).waitFor()
const clip = '群视频-' + id() + '.webm'
const videoBytes = await owner.evaluate(async () => {
const canvas = document.createElement('canvas')
canvas.width = 32
canvas.height = 24
const paint = canvas.getContext('2d')
const stream = canvas.captureStream(15)
const recorder = new MediaRecorder(stream, { mimeType: 'video/webm;codecs=vp8' })
const recorded = new Promise((resolve, reject) => {
const chunks = []
recorder.ondataavailable = ({ data }) => chunks.push(data)
recorder.onstop = async () => resolve(Array.from(new Uint8Array(await new Blob(chunks).arrayBuffer())))
recorder.onerror = () => reject(new Error('无法生成验收视频'))
})
recorder.start()
for (const color of ['#108760', '#2052a4', '#f5a12f']) {
paint.fillStyle = color
paint.fillRect(0, 0, 32, 24)
await new Promise((resolve) => setTimeout(resolve, 150))
}
recorder.stop()
stream.getTracks().forEach((track) => track.stop())
return recorded
})
await sendFile(owner, clip, 'video/webm', Buffer.from(videoBytes))
await historyView(windows, '群视频').click()
const videos = chat(windows).getByRole('list', { name: '群视频' })
await videos.getByRole('button', { name: '预览 ' + clip }).click()
const player = videos.locator('video')
await player.evaluate((element) => element.play())
assert.equal(await player.evaluate((element) => element.videoWidth), 32)
assert.match(await player.getAttribute('src'), /^blob:/)
await historyView(windows, '消息').click()
await player.waitFor({ state: 'detached' })
const voiceUploads = []
windows.on('request', (request) => {
+36 -7
View File
@@ -22,6 +22,15 @@ const selected = ref<Record<string, string>>({})
const draft = ref('')
const search = ref('')
const favoritesOnly = ref(false)
const historyViews = [
{ id: 'messages', label: '消息', empty: '没有匹配的群消息。' },
{ id: 'files', label: '群文件', empty: '没有匹配的群文件。' },
{ id: 'images', label: '群图片', empty: '没有匹配的群图片。' },
{ id: 'videos', label: '群视频', empty: '没有匹配的群视频。' },
] as const
const historyView = ref<typeof historyViews[number]>(historyViews[0])
const mediaPageSize = 24
const mediaLimit = ref(mediaPageSize)
const replying = ref<GroupLine>()
const editing = ref<GroupLine>()
const recalling = ref<GroupLine>()
@@ -35,10 +44,19 @@ const ready = computed(() => view.value?.ready && view.value.current?.membership
const currentEpoch = computed(() => ready.value ? view.value?.current?.id : undefined)
const lines = computed(() => {
const query = search.value.trim().toLowerCase()
return (view.value?.lines ?? []).filter((line) =>
(!favoritesOnly.value || line.favorite) &&
(!query || [line.text, line.quote, line.attachment?.name].some((part) => part?.toLowerCase().includes(query))))
const mode = historyView.value.id
const matches = (view.value?.lines ?? []).filter((line) => {
if (favoritesOnly.value && !line.favorite) return false
if (mode === 'messages') {
return !query || [line.text, line.quote, line.attachment?.name].some((part) => part?.toLowerCase().includes(query))
}
const attachment = line.attachment
if (line.recalled || !attachment || attachment.voice || !attachment.name.toLowerCase().includes(query)) return false
return mode === 'files' || attachment.media?.startsWith(mode === 'images' ? 'image/' : 'video/')
})
return mode === 'messages' ? matches : matches.reverse()
})
const visibleLines = computed(() => historyView.value.id === 'messages' ? lines.value : lines.value.slice(0, mediaLimit.value))
const members = computed(() => props.group.members.filter((member) => member.state === 'active' && member.id !== session.identity?.user.id))
const canPublish = computed(() => Boolean(view.value && !view.value.current && members.value.every((member) =>
view.value?.devices.some((device) => device.user === member.id && device.id === selected.value[member.id] && pinned(device)))))
@@ -216,6 +234,7 @@ function discard() {
}
watch(() => props.group.membershipVersion, queueRefresh)
watch([historyView, search, favoritesOnly], () => { mediaLimit.value = mediaPageSize })
watch(() => session.groupChange, (event) => { if (event?.groupId === props.group.id) queueRefresh() })
watch(() => session.connection, (connection) => { if (connection === '实时连接已建立') queueRefresh() })
watch(currentEpoch, (epoch, previous) => {
@@ -289,13 +308,18 @@ onBeforeUnmount(() => {
</div>
<button v-if="group.role === 'owner' && !view?.current" class="primary" :disabled="busy || !canPublish" @click="establish">建立群加密会话</button>
</details>
<div v-if="view?.lines.length || search || favoritesOnly" class="group-chat-search">
<div v-if="view" class="group-history-views" role="group" aria-label="群历史视图">
<button v-for="mode in historyViews" :key="mode.id" type="button" class="plain"
:aria-pressed="historyView.id === mode.id" @click="historyView = mode">{{ mode.label }}</button>
</div>
<div v-if="view?.lines.length || search || favoritesOnly || historyView.id !== 'messages'" class="group-chat-search">
<label>搜索本机群消息<input v-model="search" type="search" maxlength="120"></label>
<label class="chat-checkbox"><input v-model="favoritesOnly" type="checkbox">只看收藏</label>
</div>
<p v-if="!lines.length && (search.trim() || favoritesOnly)" class="muted" role="status">没有匹配的群消息。</p>
<ul v-if="lines.length" class="group-chat-lines" aria-label="群消息记录">
<li v-for="line in lines" :key="[line.epoch, line.sender, line.id].join('/')">
<p v-if="!lines.length && (historyView.id !== 'messages' || search.trim() || favoritesOnly)" class="muted" role="status">{{ historyView.empty }}</p>
<ul v-if="visibleLines.length" class="group-chat-lines" :class="{ 'group-media': historyView.id !== 'messages' }"
:aria-label="historyView.id === 'messages' ? '群消息记录' : historyView.label">
<li v-for="line in visibleLines" :key="[historyView.id, line.epoch, line.sender, line.id].join('/')">
<small>{{ line.name }} · {{ new Date(line.time).toLocaleString() }}</small>
<blockquote v-if="line.quote">{{ line.quote }}</blockquote>
<p v-if="line.recalled" class="muted">消息已撤回</p>
@@ -320,6 +344,7 @@ onBeforeUnmount(() => {
</div>
</li>
</ul>
<button v-if="visibleLines.length < lines.length" type="button" class="plain" @click="mediaLimit += mediaPageSize">加载更多本机附件</button>
<ForwardDialog v-if="forwarding && view" :key="[forwarding.epoch, forwarding.sender, forwarding.id].join('/')"
:source-group="group.id" :pinned="view.pinned" :busy="busy" @confirm="forward" @cancel="forwarding = undefined" />
<div v-if="ready && recalling" class="confirmation">
@@ -366,6 +391,10 @@ onBeforeUnmount(() => {
.group-chat-lines p { white-space: pre-wrap; overflow-wrap: anywhere; }
.group-chat-lines blockquote { margin: .25rem 0; padding-inline-start: .5rem; border-inline-start: 2px solid currentColor; overflow-wrap: anywhere; }
.group-chat-search { display: flex; flex-wrap: wrap; align-items: end; gap: .75rem; }
.group-history-views { display: flex; flex-wrap: wrap; gap: .4rem; }
.group-history-views [aria-pressed='true'] { background: var(--green-soft); color: var(--green); }
.group-media { grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr)); align-items: start; }
.group-media li { min-width: 0; padding: .7rem; border: 1px solid var(--line); border-radius: .45rem; }
.group-chat-composer { display: grid; gap: .55rem; }
.group-chat-composer textarea { display: block; width: 100%; resize: vertical; font: inherit; }
.group-chat button { justify-self: start; }