2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-vcard
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright The Converse.js contributors
|
2019-09-19 16:54:55 +02:00
|
|
|
* @license Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2019-10-15 12:55:11 +02:00
|
|
|
import "./converse-status";
|
2020-05-18 10:54:37 +02:00
|
|
|
import log from "@converse/headless/log";
|
|
|
|
import tpl_vcard from "./templates/vcard.html";
|
2020-06-08 16:08:50 +02:00
|
|
|
import { Collection } from "@converse/skeletor/src/collection";
|
|
|
|
import { Model } from '@converse/skeletor/src/model.js';
|
2020-05-18 10:54:37 +02:00
|
|
|
import { _converse, api, converse } from "./converse-core";
|
2020-06-08 16:08:50 +02:00
|
|
|
import { has, isString } from "lodash-es";
|
2017-12-02 18:00:18 +01:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
const { Strophe, $iq, dayjs } = converse.env;
|
2018-10-23 03:41:38 +02:00
|
|
|
const u = converse.env.utils;
|
2016-03-16 12:16:32 +01:00
|
|
|
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
converse.plugins.add('converse-vcard', {
|
2018-05-15 09:47:52 +02:00
|
|
|
|
2019-10-15 12:55:11 +02:00
|
|
|
dependencies: ["converse-status", "converse-roster"],
|
2019-09-04 19:11:26 +02:00
|
|
|
|
|
|
|
overrides: {
|
|
|
|
XMPPStatus: {
|
|
|
|
getNickname () {
|
|
|
|
const { _converse } = this.__super__;
|
|
|
|
const nick = this.__super__.getNickname.apply(this);
|
|
|
|
if (!nick && _converse.xmppstatus.vcard) {
|
2019-09-05 12:49:10 +02:00
|
|
|
return _converse.xmppstatus.vcard.get('nickname');
|
2019-09-04 19:11:26 +02:00
|
|
|
} else {
|
|
|
|
return nick;
|
|
|
|
}
|
2019-09-05 12:49:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getFullname (){
|
|
|
|
const { _converse } = this.__super__;
|
|
|
|
const fullname = this.__super__.getFullname.apply(this);
|
|
|
|
if (!fullname && _converse.xmppstatus.vcard) {
|
|
|
|
return _converse.xmppstatus.vcard.get('fullname');
|
|
|
|
} else {
|
|
|
|
return fullname;
|
|
|
|
}
|
2019-09-04 19:11:26 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
RosterContact: {
|
|
|
|
getDisplayName () {
|
|
|
|
if (!this.get('nickname') && this.vcard) {
|
|
|
|
return this.vcard.getDisplayName();
|
|
|
|
} else {
|
|
|
|
return this.__super__.getDisplayName.apply(this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getFullname () {
|
|
|
|
if (this.vcard) {
|
|
|
|
return this.vcard.get('fullname');
|
|
|
|
} else {
|
|
|
|
return this.__super__.getFullname.apply(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.promises.add('VCardsInitialized');
|
2019-10-24 17:55:20 +02:00
|
|
|
|
2019-09-04 19:11:26 +02:00
|
|
|
|
2020-06-03 13:32:08 +02:00
|
|
|
/**
|
|
|
|
* Represents a VCard
|
|
|
|
* @class
|
|
|
|
* @namespace _converse.VCard
|
|
|
|
* @memberOf _converse
|
|
|
|
*/
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.VCard = Model.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
defaults: {
|
|
|
|
'image': _converse.DEFAULT_IMAGE,
|
|
|
|
'image_type': _converse.DEFAULT_IMAGE_TYPE
|
|
|
|
},
|
2018-05-03 18:34:28 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
set (key, val, options) {
|
2019-09-19 16:54:55 +02:00
|
|
|
// Override Model.prototype.set to make sure that the
|
2018-10-23 03:41:38 +02:00
|
|
|
// default `image` and `image_type` values are maintained.
|
|
|
|
let attrs;
|
|
|
|
if (typeof key === 'object') {
|
|
|
|
attrs = key;
|
|
|
|
options = val;
|
|
|
|
} else {
|
|
|
|
(attrs = {})[key] = val;
|
2018-05-08 17:48:37 +02:00
|
|
|
}
|
2019-12-18 12:42:40 +01:00
|
|
|
if (has(attrs, 'image') && !attrs['image']) {
|
2018-10-23 03:41:38 +02:00
|
|
|
attrs['image'] = _converse.DEFAULT_IMAGE;
|
|
|
|
attrs['image_type'] = _converse.DEFAULT_IMAGE_TYPE;
|
2019-09-19 16:54:55 +02:00
|
|
|
return Model.prototype.set.call(this, attrs, options);
|
2018-08-27 21:54:18 +02:00
|
|
|
} else {
|
2019-09-19 16:54:55 +02:00
|
|
|
return Model.prototype.set.apply(this, arguments);
|
2018-05-08 17:48:37 +02:00
|
|
|
}
|
2019-04-09 20:34:23 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getDisplayName () {
|
|
|
|
return this.get('nickname') || this.get('fullname') || this.get('jid');
|
2018-05-08 17:48:37 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
2018-05-08 17:48:37 +02:00
|
|
|
|
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.VCards = Collection.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
model: _converse.VCard,
|
2018-05-08 17:48:37 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
initialize () {
|
2020-07-09 22:29:06 +02:00
|
|
|
this.on('add', vcard => (vcard.get('jid') && api.vcard.update(vcard)));
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-10-25 07:32:44 +02:00
|
|
|
async function onVCardData (jid, iq) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const vcard = iq.querySelector('vCard');
|
|
|
|
let result = {};
|
2019-08-05 01:39:57 +02:00
|
|
|
if (vcard !== null) {
|
2018-10-23 03:41:38 +02:00
|
|
|
result = {
|
|
|
|
'stanza': iq,
|
2020-06-03 13:26:38 +02:00
|
|
|
'fullname': vcard.querySelector('FN')?.textContent,
|
|
|
|
'nickname': vcard.querySelector('NICKNAME')?.textContent,
|
|
|
|
'image': vcard.querySelector('PHOTO BINVAL')?.textContent,
|
|
|
|
'image_type': vcard.querySelector('PHOTO TYPE')?.textContent,
|
|
|
|
'url': vcard.querySelector('URL')?.textContent,
|
|
|
|
'role': vcard.querySelector('ROLE')?.textContent,
|
|
|
|
'email': vcard.querySelector('EMAIL USERID')?.textContent,
|
2019-05-05 19:05:45 +02:00
|
|
|
'vcard_updated': (new Date()).toISOString(),
|
2018-10-23 03:41:38 +02:00
|
|
|
'vcard_error': undefined
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (result.image) {
|
|
|
|
const buffer = u.base64ToArrayBuffer(result['image']);
|
2018-10-25 07:32:44 +02:00
|
|
|
const ab = await crypto.subtle.digest('SHA-1', buffer);
|
|
|
|
result['image_hash'] = u.arrayBufferToHex(ab);
|
2018-05-08 17:48:37 +02:00
|
|
|
}
|
2018-10-25 07:32:44 +02:00
|
|
|
return result;
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2018-05-08 17:48:37 +02:00
|
|
|
|
2019-12-18 12:42:40 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
function createStanza (type, jid, vcard_el) {
|
|
|
|
const iq = $iq(jid ? {'type': type, 'to': jid} : {'type': type});
|
|
|
|
if (!vcard_el) {
|
|
|
|
iq.c("vCard", {'xmlns': Strophe.NS.VCARD});
|
|
|
|
} else {
|
|
|
|
iq.cnode(vcard_el);
|
2018-05-03 18:34:28 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
return iq;
|
|
|
|
}
|
2018-05-03 18:34:28 +02:00
|
|
|
|
2019-12-18 12:42:40 +01:00
|
|
|
|
2018-10-25 07:32:44 +02:00
|
|
|
async function getVCard (_converse, jid) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const to = Strophe.getBareJidFromJid(jid) === _converse.bare_jid ? null : jid;
|
2018-10-25 07:32:44 +02:00
|
|
|
let iq;
|
|
|
|
try {
|
2020-03-31 13:15:57 +02:00
|
|
|
iq = await api.sendIQ(createStanza("get", to))
|
2018-10-25 07:32:44 +02:00
|
|
|
} catch (iq) {
|
|
|
|
return {
|
|
|
|
'stanza': iq,
|
|
|
|
'jid': jid,
|
2019-05-05 19:05:45 +02:00
|
|
|
'vcard_error': (new Date()).toISOString()
|
2018-10-25 07:32:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return onVCardData(jid, iq);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 12:42:40 +01:00
|
|
|
|
|
|
|
async function setVCardOnModel (model) {
|
|
|
|
let jid;
|
|
|
|
if (model instanceof _converse.Message) {
|
|
|
|
if (model.get('type') === 'error') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
jid = model.get('from');
|
|
|
|
} else {
|
|
|
|
jid = model.get('jid');
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('VCardsInitialized');
|
2019-12-18 12:42:40 +01:00
|
|
|
model.vcard = _converse.vcards.findWhere({'jid': jid});
|
|
|
|
if (!model.vcard) {
|
|
|
|
model.vcard = _converse.vcards.create({'jid': jid});
|
|
|
|
}
|
|
|
|
model.vcard.on('change', () => model.trigger('vcard:change'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getVCardForChatroomOccupant (message) {
|
2020-03-24 12:26:34 +01:00
|
|
|
const chatbox = message?.collection?.chatbox;
|
2019-12-18 12:42:40 +01:00
|
|
|
const nick = Strophe.getResourceFromJid(message.get('from'));
|
|
|
|
|
|
|
|
if (chatbox && chatbox.get('nick') === nick) {
|
|
|
|
return _converse.xmppstatus.vcard;
|
|
|
|
} else {
|
|
|
|
const jid = message.occupant && message.occupant.get('jid') || message.get('from');
|
|
|
|
if (jid) {
|
|
|
|
return _converse.vcards.findWhere({jid}) || _converse.vcards.create({jid});
|
|
|
|
} else {
|
|
|
|
log.error(`Could not assign VCard for message because no JID found! msgid: ${message.get('msgid')}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function setVCardOnMUCMessage (message) {
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('VCardsInitialized');
|
2019-12-18 12:42:40 +01:00
|
|
|
if (['error', 'info'].includes(message.get('type'))) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
message.vcard = getVCardForChatroomOccupant(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-24 17:55:20 +02:00
|
|
|
_converse.initVCardCollection = async function () {
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.vcards = new _converse.VCards();
|
2019-10-24 17:55:20 +02:00
|
|
|
_converse.vcards.browserStorage = _converse.createStore(`${_converse.bare_jid}-converse.vcards`);
|
|
|
|
await new Promise(resolve => {
|
|
|
|
_converse.vcards.fetch({
|
|
|
|
'success': resolve,
|
|
|
|
'error': resolve
|
|
|
|
}, {'silent': true});
|
|
|
|
});
|
2019-04-09 20:34:23 +02:00
|
|
|
const vcards = _converse.vcards;
|
2019-07-01 10:44:59 +02:00
|
|
|
if (_converse.session) {
|
|
|
|
const jid = _converse.session.get('bare_jid');
|
|
|
|
_converse.xmppstatus.vcard = vcards.findWhere({'jid': jid}) || vcards.create({'jid': jid});
|
|
|
|
}
|
2019-10-24 17:55:20 +02:00
|
|
|
/**
|
|
|
|
* Triggered as soon as the `_converse.vcards` collection has been initialized and populated from cache.
|
|
|
|
* @event _converse#VCardsInitialized
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('VCardsInitialized');
|
2019-10-24 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 20:34:23 +02:00
|
|
|
|
2019-12-18 12:42:40 +01:00
|
|
|
function clearVCardsSession () {
|
2019-10-24 17:55:20 +02:00
|
|
|
if (_converse.shouldClearCache()) {
|
2020-03-31 13:15:57 +02:00
|
|
|
api.promises.add('VCardsInitialized');
|
2019-10-24 17:55:20 +02:00
|
|
|
if (_converse.vcards) {
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.vcards.clearStore();
|
2019-10-24 17:55:20 +02:00
|
|
|
delete _converse.vcards;
|
|
|
|
}
|
2019-08-01 10:26:35 +02:00
|
|
|
}
|
2019-12-18 12:42:40 +01:00
|
|
|
}
|
2019-04-09 20:34:23 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-12-18 12:42:40 +01:00
|
|
|
/************************ BEGIN Event Handlers ************************/
|
2019-09-04 19:11:26 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('chatBoxInitialized', m => setVCardOnModel(m));
|
|
|
|
api.listen.on('chatRoomInitialized', m => setVCardOnModel(m));
|
|
|
|
api.listen.on('chatRoomMessageInitialized', m => setVCardOnMUCMessage(m));
|
|
|
|
api.listen.on('addClientFeatures', () => api.disco.own.features.add(Strophe.NS.VCARD));
|
|
|
|
api.listen.on('clearSession', () => clearVCardsSession());
|
|
|
|
api.listen.on('messageInitialized', m => setVCardOnModel(m));
|
|
|
|
api.listen.on('rosterContactInitialized', m => setVCardOnModel(m));
|
|
|
|
api.listen.on('statusInitialized', _converse.initVCardCollection);
|
2019-09-04 19:11:26 +02:00
|
|
|
|
|
|
|
|
2019-04-29 09:07:15 +02:00
|
|
|
/************************ BEGIN API ************************/
|
|
|
|
Object.assign(_converse.api, {
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
|
|
|
* The XEP-0054 VCard API
|
|
|
|
*
|
|
|
|
* This API lets you access and update user VCards
|
|
|
|
*
|
|
|
|
* @namespace _converse.api.vcard
|
|
|
|
* @memberOf _converse.api
|
|
|
|
*/
|
|
|
|
'vcard': {
|
2018-09-02 15:07:14 +02:00
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
* Enables setting new values for a VCard.
|
2018-09-02 15:07:14 +02:00
|
|
|
*
|
2020-06-03 13:32:08 +02:00
|
|
|
* Sends out an IQ stanza to set the user's VCard and if
|
|
|
|
* successful, it updates the {@link _converse.VCard}
|
|
|
|
* for the passed in JID.
|
|
|
|
*
|
2018-10-23 03:41:38 +02:00
|
|
|
* @method _converse.api.vcard.set
|
|
|
|
* @param {string} jid The JID for which the VCard should be set
|
|
|
|
* @param {object} data A map of VCard keys and values
|
|
|
|
* @example
|
|
|
|
* _converse.api.vcard.set({
|
|
|
|
* 'jid': _converse.bare_jid,
|
|
|
|
* 'fn': 'John Doe',
|
|
|
|
* 'nickname': 'jdoe'
|
|
|
|
* }).then(() => {
|
|
|
|
* // Succes
|
|
|
|
* }).catch(() => {
|
|
|
|
* // Failure
|
|
|
|
* }).
|
|
|
|
*/
|
2020-06-03 13:32:08 +02:00
|
|
|
async set (jid, data) {
|
2019-09-04 19:11:26 +02:00
|
|
|
if (!jid) {
|
|
|
|
throw Error("No jid provided for the VCard data");
|
|
|
|
}
|
|
|
|
const vcard_el = Strophe.xmlHtmlNode(tpl_vcard(data)).firstElementChild;
|
2020-06-03 13:32:08 +02:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = await api.sendIQ(createStanza("set", jid, vcard_el));
|
|
|
|
} catch (e) {
|
|
|
|
throw (e);
|
|
|
|
}
|
|
|
|
await api.vcard.update(jid, true);
|
|
|
|
return result;
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method _converse.api.vcard.get
|
2019-09-19 16:54:55 +02:00
|
|
|
* @param {Model|string} model Either a `Model` instance, or a string JID.
|
|
|
|
* If a `Model` instance is passed in, then it must have either a `jid`
|
2018-10-23 03:41:38 +02:00
|
|
|
* attribute or a `muc_jid` attribute.
|
|
|
|
* @param {boolean} [force] A boolean indicating whether the vcard should be
|
|
|
|
* fetched even if it's been fetched before.
|
|
|
|
* @returns {promise} A Promise which resolves with the VCard data for a particular JID or for
|
2019-09-19 16:54:55 +02:00
|
|
|
* a `Model` instance which represents an entity with a JID (such as a roster contact,
|
2018-10-23 03:41:38 +02:00
|
|
|
* chat or chatroom occupant).
|
2018-09-02 15:07:14 +02:00
|
|
|
*
|
2018-10-23 03:41:38 +02:00
|
|
|
* @example
|
|
|
|
* _converse.api.waitUntil('rosterContactsFetched').then(() => {
|
|
|
|
* _converse.api.vcard.get('someone@example.org').then(
|
|
|
|
* (vcard) => {
|
|
|
|
* // Do something with the vcard...
|
|
|
|
* }
|
|
|
|
* );
|
|
|
|
* });
|
2018-09-02 15:07:14 +02:00
|
|
|
*/
|
2018-05-29 12:00:23 +02:00
|
|
|
get (model, force) {
|
2019-12-18 12:42:40 +01:00
|
|
|
if (isString(model)) {
|
2018-10-23 03:41:38 +02:00
|
|
|
return getVCard(_converse, model);
|
|
|
|
} else if (force ||
|
|
|
|
!model.get('vcard_updated') ||
|
2019-05-06 11:16:56 +02:00
|
|
|
!dayjs(model.get('vcard_error')).isSame(new Date(), "day")) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
const jid = model.get('jid');
|
|
|
|
if (!jid) {
|
2020-07-09 22:29:06 +02:00
|
|
|
log.error("No JID to get vcard for");
|
2018-05-01 11:33:32 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
return getVCard(_converse, jid);
|
|
|
|
} else {
|
|
|
|
return Promise.resolve({});
|
2017-12-02 18:00:18 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-09-19 16:54:55 +02:00
|
|
|
* Fetches the VCard associated with a particular `Model` instance
|
2018-10-23 03:41:38 +02:00
|
|
|
* (by using its `jid` or `muc_jid` attribute) and then updates the model with the
|
|
|
|
* returned VCard data.
|
|
|
|
*
|
|
|
|
* @method _converse.api.vcard.update
|
2019-09-19 16:54:55 +02:00
|
|
|
* @param {Model} model A `Model` instance
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {boolean} [force] A boolean indicating whether the vcard should be
|
|
|
|
* fetched again even if it's been fetched before.
|
|
|
|
* @returns {promise} A promise which resolves once the update has completed.
|
|
|
|
* @example
|
2019-10-04 18:26:33 +02:00
|
|
|
* _converse.api.waitUntil('rosterContactsFetched').then(async () => {
|
|
|
|
* const chatbox = await _converse.chatboxes.getChatBox('someone@example.org');
|
2018-10-23 03:41:38 +02:00
|
|
|
* _converse.api.vcard.update(chatbox);
|
|
|
|
* });
|
|
|
|
*/
|
2019-09-04 19:11:26 +02:00
|
|
|
async update (model, force) {
|
2019-10-24 17:55:20 +02:00
|
|
|
const data = await this.get(model, force);
|
2020-06-03 13:32:08 +02:00
|
|
|
model = isString(model) ? _converse.vcards.findWhere({'jid': model}) : model;
|
|
|
|
if (!model) {
|
|
|
|
log.error(`Could not find a VCard model for ${model}`);
|
|
|
|
return;
|
|
|
|
}
|
2019-10-24 17:55:20 +02:00
|
|
|
delete data['stanza']
|
|
|
|
model.save(data);
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|