xmpp.chapril.org-conversejs/src/headless/plugins/caps/utils.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

import { _converse, converse } from '@converse/headless/core.js';
import { arrayBufferToBase64 } from '@converse/headless/utils/arraybuffer.js';
const { Strophe, $build } = converse.env;
2018-10-23 03:41:38 +02:00
function propertySort (array, property) {
return array.sort((a, b) => { return a[property] > b[property] ? -1 : 1 });
}
async function generateVerificationString () {
const identities = _converse.api.disco.own.identities.get();
const features = _converse.api.disco.own.features.get();
2018-10-23 03:41:38 +02:00
if (identities.length > 1) {
propertySort(identities, "category");
propertySort(identities, "type");
propertySort(identities, "lang");
}
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();
S = features.reduce((result, feature) => `${result}${feature}<`, S);
const ab = await crypto.subtle.digest('SHA-1', S);
return arrayBufferToBase64(ab);
2018-10-23 03:41:38 +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",
'ver': await generateVerificationString()
2018-10-23 03:41:38 +02:00
}).nodeTree;
}
/**
* Given a stanza, adds a XEP-0115 CAPS element
* @param { XMLElement } stanza
*/
export async function addCapsNode (stanza) {
const caps_el = await createCapsNode();
return stanza.root().cnode(caps_el).up() && stanza;
}