支持向已核验设备加密转发文字和附件

This commit is contained in:
xhgit committed 2026-11-13 19:28:24 +08:00
1 parent f5f116a266
commit 0b89634af2
6 files changed
+81 -6

No files matched your search

+6 -1
View File
@@ -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<ArrayBuffer>(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
+8 -1
View File
@@ -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)
+57 -2
View File
@@ -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<ChatLine>()
const deleting = ref<ChatLine>()
const editing = ref<ChatLine>()
const recalling = ref<ChatLine>()
const forwarding = ref<ChatLine>()
const destinations = ref<Contact[]>([])
const destination = ref('')
const destinationDevice = ref('')
const forwardDevices = ref<Peer[]>([])
const error = ref('')
const busy = ref(false)
const view = ref<Awaited<ReturnType<typeof synchronize>>>()
@@ -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))
<button v-if="!line.recalled" type="button" class="plain" :disabled="busy" @click="localAction(line, 'favorite')">
{{ line.favorite ? '取消收藏' : '收藏' }}
</button>
<button v-if="!line.recalled" type="button" class="plain" :disabled="busy" @click="beginForward(line)">转发</button>
<button v-if="line.own && !line.recalled && !line.attachment" type="button" class="plain" :disabled="busy" @click="edit(line)">编辑</button>
<button v-if="line.own && !line.recalled" type="button" class="plain" :disabled="busy" @click="recalling = line">撤回</button>
<button type="button" class="plain" :disabled="busy || view?.unsent.includes(line.id)" @click="deleting = line">本机删除</button>
@@ -221,6 +260,22 @@ onBeforeUnmount(() => clearInterval(timer))
<button type="button" class="plain" :disabled="busy" @click="recall">确认撤回</button>
<button type="button" class="plain" @click="recalling = undefined">取消</button>
</div>
<form v-if="forwarding" class="contact-preferences confirmation" @submit.prevent="submitForward">
<label>转发给
<select v-model="destination" :disabled="busy" @change="loadForwardDevices">
<option value="">选择联系人</option>
<option v-for="contact in destinations" :key="contact.id" :value="contact.id">{{ contact.alias || contact.username }}</option>
</select>
</label>
<label v-if="forwardDevices.length">接收设备
<select v-model="destinationDevice" :disabled="busy">
<option v-for="device in forwardDevices" :key="device.id" :value="device.id">{{ device.name }}</option>
</select>
</label>
<p v-else-if="destination && !busy" class="muted">请先在对方聊天中核验接收设备。</p>
<button class="primary" :disabled="busy || !destinationDevice">确认转发</button>
<button type="button" class="plain" :disabled="busy" @click="forwarding = undefined">取消</button>
</form>
<form class="chat-compose" @submit.prevent="submit">
<div v-if="editing" class="chat-quote">
<span>正在编辑消息</span>