2022-07-14 20:34:15 +02:00
|
|
|
import { _converse, converse } from '@converse/headless/core.js';
|
2022-07-14 20:40:58 +02:00
|
|
|
import { arrayBufferToBase64, stringToArrayBuffer } from '@converse/headless/utils/arraybuffer.js';
|
2018-05-11 13:31:48 +02:00
|
|
|
|
2020-03-06 15:19:48 +01:00
|
|
|
const { Strophe, $build } = converse.env;
|
2018-05-11 13:31:48 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
function propertySort (array, property) {
|
|
|
|
return array.sort((a, b) => { return a[property] > b[property] ? -1 : 1 });
|
|
|
|
}
|
2018-05-11 13:31:48 +02:00
|
|
|
|
2022-07-14 20:34:15 +02:00
|
|
|
async function generateVerificationString () {
|
2019-10-24 14:29:15 +02:00
|
|
|
const identities = _converse.api.disco.own.identities.get();
|
|
|
|
const features = _converse.api.disco.own.features.get();
|
2018-05-11 13:31:48 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
if (identities.length > 1) {
|
|
|
|
propertySort(identities, "category");
|
|
|
|
propertySort(identities, "type");
|
|
|
|
propertySort(identities, "lang");
|
2018-05-11 13:31:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 11:13:13 +02:00
|
|
|
let S = identities.reduce((result, id) => `${result}${id.category}/${id.type}/${id?.lang ?? ''}/${id.name}<`, "");
|
2018-10-23 03:41:38 +02:00
|
|
|
features.sort();
|
2020-03-06 15:19:48 +01:00
|
|
|
S = features.reduce((result, feature) => `${result}${feature}<`, S);
|
2022-07-14 20:34:15 +02:00
|
|
|
|
2022-07-14 20:40:58 +02:00
|
|
|
const ab = await crypto.subtle.digest('SHA-1', stringToArrayBuffer(S));
|
2022-07-14 20:34:15 +02:00
|
|
|
return arrayBufferToBase64(ab);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 20:34:15 +02:00
|
|
|
async function createCapsNode () {
|
2018-10-23 03:41:38 +02:00
|
|
|
return $build("c", {
|
|
|
|
'xmlns': Strophe.NS.CAPS,
|
|
|
|
'hash': "sha-1",
|
|
|
|
'node': "https://conversejs.org",
|
2022-07-14 20:34:15 +02:00
|
|
|
'ver': await generateVerificationString()
|
2023-02-02 21:59:38 +01:00
|
|
|
}).tree();
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2022-07-14 20:34:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a stanza, adds a XEP-0115 CAPS element
|
2023-02-23 07:03:50 +01:00
|
|
|
* @param { Element } stanza
|
2022-07-14 20:34:15 +02:00
|
|
|
*/
|
|
|
|
export async function addCapsNode (stanza) {
|
|
|
|
const caps_el = await createCapsNode();
|
2022-07-14 21:17:32 +02:00
|
|
|
stanza.root().cnode(caps_el).up();
|
|
|
|
return stanza;
|
2022-07-14 20:34:15 +02:00
|
|
|
}
|