增加加密引用回复和本机消息管理

This commit is contained in:
xhgit committed 2026-11-13 19:17:13 +08:00
1 parent c24960d22a
commit c27ad52dab
7 files changed
+121 -8

No files matched your search

+23 -3
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, sealAttachment, uploadAttachment } from './attachments'
import { decodeMessage, encodeAttachment, encodeReply, sealAttachment, uploadAttachment } from './attachments'
import type { Attachment } from './attachments'
export interface Peer {
@@ -27,6 +27,8 @@ export interface ChatLine {
peer: string
text: string
attachment?: Attachment
quote?: string
favorite?: boolean
time: string
own: boolean
}
@@ -149,7 +151,7 @@ async function flush(state: ChatState, save: (state: ChatState) => Promise<void>
return unavailable
}
export function send(identity: ChatIdentity, user: string, peer: Peer, content: string | File, call: Call) {
export function send(identity: ChatIdentity, user: string, peer: Peer, content: string | File, call: Call, reply?: ChatLine) {
return update(identity, async (state, save, factory) => {
const current = (await directory(call, user)).find((item) => item.id === peer.id)
if (!current || state.pinned[current.id] !== current.publicKey) throw new Error('发送前请核验对方设备安全码')
@@ -162,6 +164,8 @@ export function send(identity: ChatIdentity, user: string, peer: Peer, content:
throw new Error('消息须为 1 至 8000 字节')
}
if (state.lines.length >= 1000) throw new Error('本机预览版消息容量已达上限')
const quote = reply && reply.peer === current.id ? (reply.text || reply.attachment?.name || '').slice(0, 240) : undefined
if (quote && typeof content === 'string') plaintext = encodeReply(text, quote)
const engine = factory.DeviceMls.restore(decode(state.engine))
try {
let group = state.groups[current.id] ? decode(state.groups[current.id]) : undefined
@@ -184,7 +188,7 @@ export function send(identity: ChatIdentity, user: string, peer: Peer, content:
const id = crypto.randomUUID()
const ciphertext = engine.encrypt(group, aad(id, identity.device.id, current.id), plaintext)
state.outbox.push({ messageId: id, recipientDevice: current.id, ciphertext: base64(ciphertext) })
state.lines.push({ id, peer: current.id, text, attachment, time: new Date().toISOString(), own: true })
state.lines.push({ id, peer: current.id, text, attachment, quote: attachment ? undefined : quote, time: new Date().toISOString(), own: true })
state.engine = base64(engine.snapshot())
await save(state)
await flush(state, save, call)
@@ -194,6 +198,22 @@ export function send(identity: ChatIdentity, user: string, peer: Peer, content:
})
}
export function localMessageAction(identity: ChatIdentity, line: ChatLine, action: 'favorite' | 'remove') {
return update(identity, async (state, save) => {
const index = state.lines.findIndex((item) => item.id === line.id && item.peer === line.peer && item.own === line.own)
if (index < 0) return
if (action === 'remove') {
if (state.outbox.some((item) => item.messageId === line.id && item.recipientDevice === line.peer)) {
throw new Error('待发送消息暂不能删除')
}
state.lines.splice(index, 1)
} else {
state.lines[index]!.favorite = !state.lines[index]!.favorite
}
await save(state)
})
}
export function synchronize(identity: ChatIdentity, user: string, call: Call) {
return update(identity, async (state, save, factory) => {
const peers = await directory(call, user)