增加加密引用回复和本机消息管理
This commit is contained in:
1 parent
c24960d22a
commit
c27ad52dab
7 files changed
+121
-8
No files matched your search
@@ -53,8 +53,16 @@ export function encodeAttachment(attachment: Attachment) {
|
||||
return message
|
||||
}
|
||||
|
||||
export function decodeMessage(message: Uint8Array): { text: string; attachment?: Attachment } {
|
||||
export function decodeMessage(message: Uint8Array): { text: string; attachment?: Attachment; quote?: string } {
|
||||
if (message[0] !== 255) return { text: decoder.decode(message) }
|
||||
if (message[1] === 2 && message.length <= 18_000) {
|
||||
const value = JSON.parse(decoder.decode(message.subarray(2))) as { text?: unknown; quote?: unknown } | null
|
||||
if (!value || typeof value.text !== 'string' || !value.text.trim() || typeof value.quote !== 'string' ||
|
||||
!value.quote.trim() || value.quote.length > 240 || encoder.encode(value.text).length > 8000) {
|
||||
throw new Error('引用消息格式无效')
|
||||
}
|
||||
return { text: value.text, quote: value.quote }
|
||||
}
|
||||
if (message[1] !== 1 || message.length > 4096) throw new Error('附件描述格式无效')
|
||||
const value = JSON.parse(decoder.decode(message.subarray(2))) as Attachment | null
|
||||
if (!value || typeof value.id !== 'string' || !/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(value.id) ||
|
||||
@@ -67,6 +75,15 @@ export function decodeMessage(message: Uint8Array): { text: string; attachment?:
|
||||
return { text: '', attachment: value }
|
||||
}
|
||||
|
||||
export function encodeReply(text: string, quote: string) {
|
||||
const body = encoder.encode(JSON.stringify({ text, quote }))
|
||||
if (body.length + 2 > 18_000) throw new Error('引用消息过长')
|
||||
const message = new Uint8Array(body.length + 2)
|
||||
message.set([255, 2])
|
||||
message.set(body, 2)
|
||||
return message
|
||||
}
|
||||
|
||||
export async function downloadAttachment(attachment: Attachment, call: Call) {
|
||||
const ciphertext = await call<ArrayBuffer>(attachmentPath(attachment.id), 'GET', undefined, true)
|
||||
if (ciphertext.byteLength !== attachment.size + 16) throw new Error('附件完整性校验失败')
|
||||
|
||||
Reference in new issue
Block a user