2018-05-11 13:31:48 +02:00
|
|
|
// Converse.js
|
2019-03-04 17:47:45 +01:00
|
|
|
// https://conversejs.org
|
2018-05-11 13:31:48 +02:00
|
|
|
//
|
2019-02-18 19:17:06 +01:00
|
|
|
// Copyright (c) 2013-2019, the Converse.js developers
|
2018-05-11 13:31:48 +02:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-caps
|
|
|
|
*/
|
2019-05-29 17:40:16 +02:00
|
|
|
import SHA1 from 'strophe.js/src/sha1';
|
2018-10-23 03:41:38 +02:00
|
|
|
import converse from "@converse/headless/converse-core";
|
2018-05-11 13:31:48 +02:00
|
|
|
|
2019-05-29 17:40:16 +02:00
|
|
|
const { Strophe, $build, _ } = converse.env;
|
2018-05-11 13:31:48 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
Strophe.addNamespace('CAPS', "http://jabber.org/protocol/caps");
|
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
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
function generateVerificationString (_converse) {
|
|
|
|
const identities = _converse.api.disco.own.identities.get(),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
let S = _.reduce(
|
|
|
|
identities,
|
|
|
|
(result, id) => `${result}${id.category}/${id.type}/${_.get(id, 'lang', '')}/${id.name}<`,
|
|
|
|
"");
|
|
|
|
|
|
|
|
features.sort();
|
|
|
|
S = _.reduce(features, (result, feature) => `${result}${feature}<`, S);
|
2019-05-29 17:40:16 +02:00
|
|
|
return SHA1.b64_sha1(S);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createCapsNode (_converse) {
|
|
|
|
return $build("c", {
|
|
|
|
'xmlns': Strophe.NS.CAPS,
|
|
|
|
'hash': "sha-1",
|
|
|
|
'node': "https://conversejs.org",
|
|
|
|
'ver': generateVerificationString(_converse)
|
|
|
|
}).nodeTree;
|
|
|
|
}
|
|
|
|
|
|
|
|
converse.plugins.add('converse-caps', {
|
|
|
|
|
|
|
|
overrides: {
|
|
|
|
// Overrides mentioned here will be picked up by converse.js's
|
|
|
|
// plugin architecture they will replace existing methods on the
|
|
|
|
// relevant objects or classes.
|
|
|
|
XMPPStatus: {
|
|
|
|
constructPresence () {
|
|
|
|
const presence = this.__super__.constructPresence.apply(this, arguments);
|
|
|
|
presence.root().cnode(createCapsNode(this.__super__._converse));
|
|
|
|
return presence;
|
2018-05-11 13:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|