增加会话导航与可选加密已读回执
This commit is contained in:
1 parent
6786158a3e
commit
149028267e
8 files changed
+218
-17
No files matched your search
@@ -54,10 +54,19 @@ export function encodeAttachment(attachment: Attachment) {
|
||||
}
|
||||
|
||||
export type Mutation = { action: 'edit' | 'recall'; id: string; text?: string }
|
||||
type ChatContent = { text: string; attachment?: Attachment; quote?: string; mutation?: Mutation }
|
||||
type ChatContent = { text: string; attachment?: Attachment; quote?: string; mutation?: Mutation; read?: string[] }
|
||||
const messageID = /^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/
|
||||
|
||||
export function decodeMessage(message: Uint8Array): ChatContent {
|
||||
if (message[0] !== 255) return { text: decoder.decode(message) }
|
||||
if (message[1] === 4 && message.length <= 4096) {
|
||||
const ids: unknown = JSON.parse(decoder.decode(message.subarray(2)))
|
||||
if (!Array.isArray(ids) || ids.length === 0 || ids.length > 64 ||
|
||||
!ids.every((id) => typeof id === 'string' && messageID.test(id))) {
|
||||
throw new Error('已读回执格式无效')
|
||||
}
|
||||
return { text: '', read: ids }
|
||||
}
|
||||
if (message[1] === 3 && message.length <= 18_000) {
|
||||
const value = JSON.parse(decoder.decode(message.subarray(2))) as Mutation | null
|
||||
if (!value || typeof value.id !== 'string' || !/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(value.id) ||
|
||||
@@ -93,6 +102,10 @@ export function encodeMutation(mutation: Mutation) {
|
||||
return encodeContent(3, mutation)
|
||||
}
|
||||
|
||||
export function encodeRead(ids: string[]) {
|
||||
return encodeContent(4, ids)
|
||||
}
|
||||
|
||||
function encodeContent(kind: number, value: object) {
|
||||
const body = encoder.encode(JSON.stringify(value))
|
||||
if (body.length + 2 > 18_000) throw new Error('消息内容过长')
|
||||
|
||||
Reference in new issue
Block a user