增加会话导航与可选加密已读回执
This commit is contained in:
1 parent
6786158a3e
commit
149028267e
8 files changed
+218
-17
No files matched your search
+65
-5
@@ -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, readAttachment, sealAttachment, uploadAttachment } from './attachments'
|
||||
import { decodeMessage, encodeAttachment, encodeMutation, encodeRead, encodeReply, readAttachment, sealAttachment, uploadAttachment } from './attachments'
|
||||
import type { Attachment, Mutation } from './attachments'
|
||||
|
||||
export interface Peer {
|
||||
@@ -31,6 +31,8 @@ export interface ChatLine {
|
||||
favorite?: boolean
|
||||
recalled?: boolean
|
||||
edited?: boolean
|
||||
read?: boolean
|
||||
contact?: string
|
||||
time: string
|
||||
own: boolean
|
||||
}
|
||||
@@ -45,6 +47,7 @@ interface ChatState {
|
||||
groups: Record<string, string>
|
||||
package?: { lastPublished: number; item: Package }
|
||||
rejected: number
|
||||
shareRead?: boolean
|
||||
}
|
||||
|
||||
export type Call = <T>(path: string, method?: string, body?: object, binary?: boolean) => Promise<T>
|
||||
@@ -233,7 +236,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, quote: attachment ? undefined : quote, time: new Date().toISOString(), own: true })
|
||||
state.lines.push({ id, peer: current.id, contact: user, 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)
|
||||
@@ -266,9 +269,60 @@ export async function forwardMessage(identity: ChatIdentity, line: ChatLine, rec
|
||||
await send(identity, recipient, device, content, call)
|
||||
}
|
||||
|
||||
export function recentConversations(identity: ChatIdentity) {
|
||||
return withVault<ChatState, Record<string, string>>(identity.device.id, async (state) => {
|
||||
const recent: Record<string, string> = {}
|
||||
for (const line of state?.lines ?? []) {
|
||||
if (line.contact && (!recent[line.contact] || recent[line.contact] < line.time)) recent[line.contact] = line.time
|
||||
}
|
||||
return recent
|
||||
}, true)
|
||||
}
|
||||
|
||||
export function markRead(identity: ChatIdentity, user: string, peer: Peer, visible: string[], call: Call) {
|
||||
return update(identity, async (state, save, factory) => {
|
||||
if (!state.shareRead) return
|
||||
const selected = new Set(visible)
|
||||
const lines = state.lines.filter((line) => line.peer === peer.id && !line.own && !line.read && !line.recalled && selected.has(line.id)).slice(0, 64)
|
||||
const group = state.groups[peer.id]
|
||||
if (!lines.length || !group || state.pinned[peer.id] !== peer.publicKey) return
|
||||
const current = (await directory(call, user)).find((device) => device.id === peer.id)
|
||||
if (!current || current.publicKey !== peer.publicKey) return
|
||||
await verifiedPeer(current, user)
|
||||
const engine = factory.DeviceMls.restore(decode(state.engine))
|
||||
try {
|
||||
const id = crypto.randomUUID()
|
||||
const bytes = engine.encrypt(decode(group), aad(id, identity.device.id, peer.id), encodeRead(lines.map((line) => line.id)))
|
||||
state.outbox.push({ messageId: id, recipientDevice: peer.id, ciphertext: base64(bytes) })
|
||||
for (const line of lines) line.read = true
|
||||
state.engine = base64(engine.snapshot())
|
||||
await save(state)
|
||||
await flush(state, save, call)
|
||||
} finally {
|
||||
engine.free()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function setReadReceipts(identity: ChatIdentity, enabled: boolean) {
|
||||
return update(identity, async (state, save) => {
|
||||
state.shareRead = enabled
|
||||
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)
|
||||
const peerIDs = new Set(peers.map((peer) => peer.id))
|
||||
let backfilled = false
|
||||
for (const line of state.lines) {
|
||||
if (!line.contact && peerIDs.has(line.peer)) {
|
||||
line.contact = user
|
||||
backfilled = true
|
||||
}
|
||||
}
|
||||
if (backfilled) await save(state)
|
||||
for (let page = 0; page < 4; page++) {
|
||||
const inbox = await call<Incoming[]>('/inbox?after=' + state.cursor)
|
||||
if (!inbox.length) break
|
||||
@@ -292,12 +346,18 @@ export function synchronize(identity: ChatIdentity, user: string, call: Call) {
|
||||
} else {
|
||||
const plaintext = engine.decrypt(peer.id, key, aad(item.messageId, peer.id, identity.device.id), wire)
|
||||
const content = decodeMessage(plaintext)
|
||||
if (content.mutation) {
|
||||
if (content.read) {
|
||||
const ids = new Set(content.read)
|
||||
for (const line of state.lines) {
|
||||
if (line.own && line.peer === peer.id && ids.has(line.id)) line.read = true
|
||||
}
|
||||
} else if (content.mutation) {
|
||||
const mutation = content.mutation
|
||||
const changed = state.lines.find((line) => !line.own && line.peer === peer.id && line.id === mutation.id)
|
||||
if (changed) applyMutation(changed, mutation)
|
||||
} else if (state.lines.length < 1000) {
|
||||
state.lines.push({ id: item.messageId, peer: peer.id, ...content, time: item.createdAt, own: false })
|
||||
state.lines.push({ id: item.messageId, peer: peer.id, contact: user, text: content.text,
|
||||
attachment: content.attachment, quote: content.quote, time: item.createdAt, own: false })
|
||||
}
|
||||
}
|
||||
state.engine = base64(engine.snapshot())
|
||||
@@ -316,7 +376,7 @@ export function synchronize(identity: ChatIdentity, user: string, call: Call) {
|
||||
} catch (cause) {
|
||||
deliveryError = cause instanceof APIError && cause.status === 404 ? '部分待发消息的设备已不可用' : '部分消息等待重试'
|
||||
}
|
||||
return { lines: state.lines, pinned: state.pinned, peers, rejected: state.rejected, deliveryError,
|
||||
return { lines: state.lines, pinned: state.pinned, peers, rejected: state.rejected, deliveryError, shareRead: state.shareRead === true,
|
||||
unsent: state.outbox.map((item) => item.mutationTarget ?? item.messageId) }
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user