update image to xep-0385

This commit is contained in:
koplenov 2026-05-09 19:19:35 +03:00
parent e3c2c780c8
commit 1b3fd93150

View file

@ -129,6 +129,44 @@
return msg_id
}
static async hash_file(file: File, algorithm = 'SHA-256'): Promise<string> {
const buffer = await file.arrayBuffer()
const digest = await crypto.subtle.digest(algorithm, buffer)
const bytes = new Uint8Array(digest)
let binary = ''
const chunk = 0x8000
for (let i = 0; i < bytes.length; i += chunk) {
binary += String.fromCharCode(...bytes.subarray(i, i + chunk))
}
return btoa(binary)
}
send_media_sharing(to: string, body: string, file_name: string, size: number, mime: string, hash_algo: string, hash_value: string, get_url: string, type: 'chat' | 'groupchat' = 'chat', id?: string): string {
const msg_id = id ?? this._id()
const begin = body.indexOf(file_name)
const end = begin >= 0 ? begin + file_name.length : body.length
this._send(
`<message to="${ this._esc(to) }" type="${ this._esc(type) }" id="${ msg_id }">` +
`<body>${ this._esc(body) }</body>` +
`<reference xmlns="urn:xmpp:reference:0" type="data" begin="${ begin >= 0 ? begin : 0 }" end="${ end }">` +
`<media-sharing xmlns="urn:xmpp:sims:1">` +
`<file xmlns="urn:xmpp:jingle:apps:file-transfer:5">` +
`<media-type>${ this._esc(mime) }</media-type>` +
`<name>${ this._esc(file_name) }</name>` +
`<size>${ size }</size>` +
`<hash xmlns="urn:xmpp:hashes:2" algo="${ this._esc(hash_algo) }">${ this._esc(hash_value) }</hash>` +
`</file>` +
`<sources>` +
`<reference xmlns="urn:xmpp:reference:0" type="data" uri="${ this._esc(get_url) }"/>` +
`</sources>` +
`</media-sharing>` +
`</reference>` +
`<markable xmlns="urn:xmpp:chat-markers:0"/>` +
`</message>`
)
return msg_id
}
// XEP-0333: send a chat marker (received/displayed/acknowledged) for a previously-received message id.
send_marker(to: string, kind: 'received' | 'displayed' | 'acknowledged', id: string, type: 'chat' | 'groupchat' = 'chat') {
console.log('[xmpp] → send_marker', { to, kind, id, type })
@ -513,7 +551,18 @@ private _handle_message(el: Element) {
}
const type = el.getAttribute('type') || 'chat';
const body = this._getBody(el);
let body = this._getBody(el);
if (!body) {
const reference = this._find(el, 'urn:xmpp:reference:0', 'reference')
if (reference?.getAttribute('type') === 'data') {
const media = reference.querySelector('media-sharing')
const file = media?.querySelector('file')
const name = file?.querySelector('name')?.textContent
const source = media?.querySelector('sources reference')?.getAttribute('uri')
if (name) body = `Shared file: ${ name }`
else if (source) body = source
}
}
if (!body) return;
// XEP-0333: peer requested chat markers for this message
@ -1946,9 +1995,12 @@ private _handle_message(el: Element) {
if (!this._conn) throw new Error('Not connected')
const { put, get } = await this._conn.request_slot(file.name, file.size, file.type)
await Xmpp_conn.upload(put, file)
const id = `l${ Date.now() }_${ Math.random().toString(36).slice(2) }`
this._conn.send_message(jid, get, id)
this._add_message({ id, from: this.my_jid().split('/')[0], to: jid, body: get, time: Date.now() })
const hash = await Xmpp_conn.hash_file(file)
const body = `Shared file: ${ file.name }`
const id = `l${ Date.now() }_${ Math.random().toString(36).slice(2) }`
const type = this._rooms.has(jid) ? 'groupchat' : 'chat'
this._conn.send_media_sharing(jid, body, file.name, file.size, file.type || 'application/octet-stream', 'sha-256', hash, get, type, id)
this._add_message({ id, from: this.my_jid().split('/')[0], to: jid, body, time: Date.now() })
this._scroll_to_bottom(jid)
}
}