From f1899d0977b10a1db788f2d7a4c07099e8674c60 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Tue, 9 Apr 2019 20:34:23 +0200 Subject: [PATCH] Looser coupling with converse-vcard To make it easier to remove it without breaking stuff. --- dist/converse.js | 104 +++++++++++++++++-------- src/converse-chatboxviews.js | 16 ++-- src/headless/converse-chatboxes.js | 25 ++++-- src/headless/converse-core.js | 5 -- src/headless/converse-roster.js | 18 ++++- src/headless/converse-vcard.js | 11 +++ src/headless/dist/converse-headless.js | 83 +++++++++++++------- 7 files changed, 181 insertions(+), 81 deletions(-) diff --git a/dist/converse.js b/dist/converse.js index 89d9721de6..fe5f66c4f0 100644 --- a/dist/converse.js +++ b/dist/converse.js @@ -48641,19 +48641,24 @@ const AvatarMixin = { return; } - const image_type = this.model.vcard.get('image_type'), - image = this.model.vcard.get('image'); - canvas_el.outerHTML = templates_avatar_svg__WEBPACK_IMPORTED_MODULE_4___default()({ + const data = { 'classes': canvas_el.getAttribute('class'), 'width': canvas_el.width, - 'height': canvas_el.height, - 'image': "data:" + image_type + ";base64," + image - }); + 'height': canvas_el.height + }; + + if (this.model.vcard) { + const image_type = this.model.vcard.get('image_type'), + image = this.model.vcard.get('image'); + data['image'] = "data:" + image_type + ";base64," + image; + } + + canvas_el.outerHTML = templates_avatar_svg__WEBPACK_IMPORTED_MODULE_4___default()(data); } }; _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-chatboxviews', { - dependencies: ["converse-chatboxes"], + dependencies: ["converse-chatboxes", "converse-vcard"], overrides: { // Overrides mentioned here will be picked up by converse.js's // plugin architecture they will replace existing methods on the @@ -61849,6 +61854,11 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha }, setVCard() { + if (!_converse.vcards) { + // VCards aren't supported + return; + } + if (this.get('type') === 'error') { return; } else if (this.get('type') === 'groupchat') { @@ -61870,12 +61880,12 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha getDisplayName() { if (this.get('type') === 'groupchat') { return this.get('nick'); + } else if (this.contact) { + return this.contact.getDisplayName(); + } else if (this.vcard) { + return this.vcard.getDisplayName(); } else { - if (this.contact) { - return this.contact.getDisplayName(); - } - - return this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('from'); + return this.get('from'); } }, @@ -62015,22 +62025,25 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha // This happens when the controlbox is in browser storage, // but we're in embedded mode. return; - } - - this.vcard = _converse.vcards.findWhere({ - 'jid': jid - }) || _converse.vcards.create({ - 'jid': jid - }); // XXX: this creates a dependency on converse-roster, which we + } // XXX: this creates a dependency on converse-roster, which we // probably shouldn't have here, so we should probably move // ChatBox out of converse-chatboxes + this.presence = _converse.presences.findWhere({ 'jid': jid }) || _converse.presences.create({ 'jid': jid }); + if (_converse.vcards) { + this.vcard = _converse.vcards.findWhere({ + 'jid': jid + }) || _converse.vcards.create({ + 'jid': jid + }); + } + if (this.get('type') === _converse.PRIVATE_CHAT_TYPE) { this.setRosterContact(jid); } @@ -62070,9 +62083,11 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha getDisplayName() { if (this.contact) { return this.contact.getDisplayName(); + } else if (this.vcard) { + return this.vcard.getDisplayName(); + } else { + return this.get('jid'); } - - return this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('jid'); }, getUpdatedMessageAttributes(message, stanza) { @@ -64254,16 +64269,6 @@ _converse.initialize = async function (settings, callback) { }, initialize() { - this.vcard = _converse.vcards.findWhere({ - 'jid': this.get('jid') - }); - - if (_lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.isNil(this.vcard)) { - this.vcard = _converse.vcards.create({ - 'jid': this.get('jid') - }); - } - this.on('change:status', item => { const status = this.get('status'); this.sendPresence(status); @@ -68820,6 +68825,11 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins }, setVCard() { + if (!_converse.vcards) { + // VCards aren't supported + return; + } + const jid = this.get('jid'); this.vcard = _converse.vcards.findWhere({ 'jid': jid @@ -68873,11 +68883,21 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins }, getDisplayName() { - return this.get('nickname') || this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('jid'); + if (this.get('nickname')) { + return this.get('nickname'); + } else if (this.vcard) { + return this.vcard.getDisplayName(); + } else { + return this.get('jid'); + } }, getFullname() { - return this.vcard.get('fullname'); + if (this.vcard) { + return this.vcard.get('fullname'); + } else { + return this.get('jid'); + } }, /** @@ -69828,6 +69848,10 @@ _converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins.add('converse-vca } else { return Backbone.Model.prototype.set.apply(this, arguments); } + }, + + getDisplayName() { + return this.get('nickname') || this.get('fullname') || this.get('jid'); } }); @@ -69925,6 +69949,18 @@ _converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins.add('converse-vca _converse.api.listen.on('sessionInitialized', _converse.initVCardCollection); + _converse.api.listen.on('statusInitialized', () => { + const vcards = _converse.vcards; + + const jid = _converse.xmppstatus.get('jid'); + + _converse.xmppstatus.vcard = vcards.findWhere({ + 'jid': jid + }) || vcards.create({ + 'jid': jid + }); + }); + _converse.api.listen.on('addClientFeatures', () => { _converse.api.disco.own.features.add(Strophe.NS.VCARD); }); @@ -94517,7 +94553,7 @@ __p += '\n \n '; __p += '
'; } ; __p += '\n '; - if (o.received) { ; + if (o.received && !o.is_me_message) { ; __p += ' '; } ; __p += '\n '; diff --git a/src/converse-chatboxviews.js b/src/converse-chatboxviews.js index 7a9f4c43db..3c0fcc1d41 100644 --- a/src/converse-chatboxviews.js +++ b/src/converse-chatboxviews.js @@ -23,22 +23,24 @@ const AvatarMixin = { if (_.isNull(canvas_el)) { return; } - const image_type = this.model.vcard.get('image_type'), - image = this.model.vcard.get('image'); - - canvas_el.outerHTML = tpl_avatar({ + const data = { 'classes': canvas_el.getAttribute('class'), 'width': canvas_el.width, 'height': canvas_el.height, - 'image': "data:" + image_type + ";base64," + image, - }); + } + if (this.model.vcard) { + const image_type = this.model.vcard.get('image_type'), + image = this.model.vcard.get('image'); + data['image'] = "data:" + image_type + ";base64," + image; + } + canvas_el.outerHTML = tpl_avatar(data); }, }; converse.plugins.add('converse-chatboxviews', { - dependencies: ["converse-chatboxes"], + dependencies: ["converse-chatboxes", "converse-vcard"], overrides: { // Overrides mentioned here will be picked up by converse.js's diff --git a/src/headless/converse-chatboxes.js b/src/headless/converse-chatboxes.js index 9a932d0162..a3093f57b0 100644 --- a/src/headless/converse-chatboxes.js +++ b/src/headless/converse-chatboxes.js @@ -118,6 +118,10 @@ converse.plugins.add('converse-chatboxes', { }, setVCard () { + if (!_converse.vcards) { + // VCards aren't supported + return; + } if (this.get('type') === 'error') { return; } else if (this.get('type') === 'groupchat') { @@ -135,11 +139,12 @@ converse.plugins.add('converse-chatboxes', { getDisplayName () { if (this.get('type') === 'groupchat') { return this.get('nick'); + } else if (this.contact) { + return this.contact.getDisplayName(); + } else if (this.vcard) { + return this.vcard.getDisplayName(); } else { - if (this.contact) { - return this.contact.getDisplayName(); - } - return this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('from'); + return this.get('from'); } }, @@ -271,12 +276,15 @@ converse.plugins.add('converse-chatboxes', { // but we're in embedded mode. return; } - - this.vcard = _converse.vcards.findWhere({'jid': jid}) || _converse.vcards.create({'jid': jid}); // XXX: this creates a dependency on converse-roster, which we // probably shouldn't have here, so we should probably move // ChatBox out of converse-chatboxes this.presence = _converse.presences.findWhere({'jid': jid}) || _converse.presences.create({'jid': jid}); + + if (_converse.vcards) { + this.vcard = _converse.vcards.findWhere({'jid': jid}) || _converse.vcards.create({'jid': jid}); + } + if (this.get('type') === _converse.PRIVATE_CHAT_TYPE) { this.setRosterContact(jid); } @@ -317,8 +325,11 @@ converse.plugins.add('converse-chatboxes', { getDisplayName () { if (this.contact) { return this.contact.getDisplayName(); + } else if (this.vcard) { + return this.vcard.getDisplayName(); + } else { + return this.get('jid'); } - return this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('jid'); }, getUpdatedMessageAttributes (message, stanza) { diff --git a/src/headless/converse-core.js b/src/headless/converse-core.js index 648b20f060..fff615b168 100644 --- a/src/headless/converse-core.js +++ b/src/headless/converse-core.js @@ -1059,11 +1059,6 @@ _converse.initialize = async function (settings, callback) { }, initialize () { - this.vcard = _converse.vcards.findWhere({'jid': this.get('jid')}); - if (_.isNil(this.vcard)) { - this.vcard = _converse.vcards.create({'jid': this.get('jid')}); - } - this.on('change:status', (item) => { const status = this.get('status'); this.sendPresence(status); diff --git a/src/headless/converse-roster.js b/src/headless/converse-roster.js index 93e8c0b0ac..af85a3b8ee 100644 --- a/src/headless/converse-roster.js +++ b/src/headless/converse-roster.js @@ -211,6 +211,10 @@ converse.plugins.add('converse-roster', { }, setVCard () { + if (!_converse.vcards) { + // VCards aren't supported + return; + } const jid = this.get('jid'); this.vcard = _converse.vcards.findWhere({'jid': jid}) || _converse.vcards.create({'jid': jid}); }, @@ -258,11 +262,21 @@ converse.plugins.add('converse-roster', { }, getDisplayName () { - return this.get('nickname') || this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('jid'); + if (this.get('nickname')) { + return this.get('nickname'); + } else if (this.vcard) { + return this.vcard.getDisplayName(); + } else { + return this.get('jid'); + } }, getFullname () { - return this.vcard.get('fullname'); + if (this.vcard) { + return this.vcard.get('fullname'); + } else { + return this.get('jid'); + } }, /** diff --git a/src/headless/converse-vcard.js b/src/headless/converse-vcard.js index b62989bc51..0426278c21 100644 --- a/src/headless/converse-vcard.js +++ b/src/headless/converse-vcard.js @@ -43,6 +43,10 @@ converse.plugins.add('converse-vcard', { } else { return Backbone.Model.prototype.set.apply(this, arguments); } + }, + + getDisplayName () { + return this.get('nickname') || this.get('fullname') || this.get('jid'); } }); @@ -124,6 +128,13 @@ converse.plugins.add('converse-vcard', { _converse.api.listen.on('sessionInitialized', _converse.initVCardCollection); + _converse.api.listen.on('statusInitialized', () => { + const vcards = _converse.vcards; + const jid = _converse.xmppstatus.get('jid'); + _converse.xmppstatus.vcard = vcards.findWhere({'jid': jid}) || vcards.create({'jid': jid}); + }); + + _converse.api.listen.on('addClientFeatures', () => { _converse.api.disco.own.features.add(Strophe.NS.VCARD); }); diff --git a/src/headless/dist/converse-headless.js b/src/headless/dist/converse-headless.js index d870993e7d..8551c8de9c 100644 --- a/src/headless/dist/converse-headless.js +++ b/src/headless/dist/converse-headless.js @@ -40371,6 +40371,11 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha }, setVCard() { + if (!_converse.vcards) { + // VCards aren't supported + return; + } + if (this.get('type') === 'error') { return; } else if (this.get('type') === 'groupchat') { @@ -40392,12 +40397,12 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha getDisplayName() { if (this.get('type') === 'groupchat') { return this.get('nick'); + } else if (this.contact) { + return this.contact.getDisplayName(); + } else if (this.vcard) { + return this.vcard.getDisplayName(); } else { - if (this.contact) { - return this.contact.getDisplayName(); - } - - return this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('from'); + return this.get('from'); } }, @@ -40537,22 +40542,25 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha // This happens when the controlbox is in browser storage, // but we're in embedded mode. return; - } - - this.vcard = _converse.vcards.findWhere({ - 'jid': jid - }) || _converse.vcards.create({ - 'jid': jid - }); // XXX: this creates a dependency on converse-roster, which we + } // XXX: this creates a dependency on converse-roster, which we // probably shouldn't have here, so we should probably move // ChatBox out of converse-chatboxes + this.presence = _converse.presences.findWhere({ 'jid': jid }) || _converse.presences.create({ 'jid': jid }); + if (_converse.vcards) { + this.vcard = _converse.vcards.findWhere({ + 'jid': jid + }) || _converse.vcards.create({ + 'jid': jid + }); + } + if (this.get('type') === _converse.PRIVATE_CHAT_TYPE) { this.setRosterContact(jid); } @@ -40592,9 +40600,11 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha getDisplayName() { if (this.contact) { return this.contact.getDisplayName(); + } else if (this.vcard) { + return this.vcard.getDisplayName(); + } else { + return this.get('jid'); } - - return this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('jid'); }, getUpdatedMessageAttributes(message, stanza) { @@ -42776,16 +42786,6 @@ _converse.initialize = async function (settings, callback) { }, initialize() { - this.vcard = _converse.vcards.findWhere({ - 'jid': this.get('jid') - }); - - if (_lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.isNil(this.vcard)) { - this.vcard = _converse.vcards.create({ - 'jid': this.get('jid') - }); - } - this.on('change:status', item => { const status = this.get('status'); this.sendPresence(status); @@ -47342,6 +47342,11 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins }, setVCard() { + if (!_converse.vcards) { + // VCards aren't supported + return; + } + const jid = this.get('jid'); this.vcard = _converse.vcards.findWhere({ 'jid': jid @@ -47395,11 +47400,21 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins }, getDisplayName() { - return this.get('nickname') || this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('jid'); + if (this.get('nickname')) { + return this.get('nickname'); + } else if (this.vcard) { + return this.vcard.getDisplayName(); + } else { + return this.get('jid'); + } }, getFullname() { - return this.vcard.get('fullname'); + if (this.vcard) { + return this.vcard.get('fullname'); + } else { + return this.get('jid'); + } }, /** @@ -48350,6 +48365,10 @@ _converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins.add('converse-vca } else { return Backbone.Model.prototype.set.apply(this, arguments); } + }, + + getDisplayName() { + return this.get('nickname') || this.get('fullname') || this.get('jid'); } }); @@ -48447,6 +48466,18 @@ _converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins.add('converse-vca _converse.api.listen.on('sessionInitialized', _converse.initVCardCollection); + _converse.api.listen.on('statusInitialized', () => { + const vcards = _converse.vcards; + + const jid = _converse.xmppstatus.get('jid'); + + _converse.xmppstatus.vcard = vcards.findWhere({ + 'jid': jid + }) || vcards.create({ + 'jid': jid + }); + }); + _converse.api.listen.on('addClientFeatures', () => { _converse.api.disco.own.features.add(Strophe.NS.VCARD); });