From 16b2a1b2a95b2100f92bf52f429b641119d1063e Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 3 May 2018 18:34:28 +0200 Subject: [PATCH] Avatar/VCard refactoring - Refer to VCards instead of Avatars where appropriate - Fetch VCards for non-groupchat messages as well --- src/converse-chatboxes.js | 8 +++++--- src/converse-core.js | 17 ----------------- src/converse-message-view.js | 8 +++----- src/converse-muc.js | 8 ++++---- src/converse-vcard.js | 19 ++++++++++++++++++- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/converse-chatboxes.js b/src/converse-chatboxes.js index b1e7f3dee..ad16fa9a1 100644 --- a/src/converse-chatboxes.js +++ b/src/converse-chatboxes.js @@ -22,6 +22,8 @@ converse.plugins.add('converse-chatboxes', { + dependencies: ["converse-vcard"], + overrides: { // Overrides mentioned here will be picked up by converse.js's // plugin architecture they will replace existing methods on the @@ -95,9 +97,9 @@ }, initialize () { - this.avatar = _converse.avatars.findWhere({'jid': this.get('from')}); - if (_.isNil(this.avatar)) { - this.avatar = _converse.avatars.create({'jid': this.get('from')}); + this.vcard = _converse.vcards.findWhere({'jid': this.get('from')}); + if (_.isNil(this.vcard)) { + this.vcard = _converse.vcards.create({'jid': this.get('from')}); } if (this.get('file')) { diff --git a/src/converse-core.js b/src/converse-core.js index 569eaf044..2875d46f9 100644 --- a/src/converse-core.js +++ b/src/converse-core.js @@ -268,17 +268,6 @@ }); - _converse.Avatars = Backbone.Collection.extend({ - model: _converse.ModelWithDefaultAvatar, - - initialize () { - this.on('add', (avatar) => { - _converse.api.vcard.update(avatar); - }); - } - }); - - _converse.initialize = function (settings, callback) { "use strict"; settings = !_.isUndefined(settings) ? settings : {}; @@ -1820,11 +1809,6 @@ _converse.emit('connectionInitialized'); }; - this.initAvatars = function () { - _converse.avatars = new _converse.Avatars(); - _converse.avatars.browserStorage = new Backbone.BrowserStorage.local(b64_sha1(`converse.avatars`)); - _converse.avatars.fetch(); - }; this._tearDown = function () { /* Remove those views which are only allowed with a valid @@ -1895,7 +1879,6 @@ function finishInitialization () { _converse.initPlugins(); _converse.initConnection(); - _converse.initAvatars(); _converse.setUpXMLLogging(); _converse.logIn(); _converse.registerGlobalEventHandlers(); diff --git a/src/converse-message-view.js b/src/converse-message-view.js index 48865af14..51d56ed1a 100644 --- a/src/converse-message-view.js +++ b/src/converse-message-view.js @@ -53,9 +53,7 @@ }) } }); - this.model.avatar.on('change:image', () => { - this.renderAvatar(); - }); + this.model.vcard.on('change:image', () => this.renderAvatar()); this.model.on('change:fullname', this.render, this); this.model.on('change:progress', this.renderFileUploadProgresBar, this); this.model.on('change:type', this.render, this); @@ -128,8 +126,8 @@ if (_.isNull(canvas_el)) { return; } - const image_type = this.model.avatar.get('image_type'), - image = this.model.avatar.get('image'), + const image_type = this.model.vcard.get('image_type'), + image = this.model.vcard.get('image'), img_src = "data:" + image_type + ";base64," + image, img = new Image(); diff --git a/src/converse-muc.js b/src/converse-muc.js index c0ad97f6e..a8849d725 100644 --- a/src/converse-muc.js +++ b/src/converse-muc.js @@ -996,12 +996,12 @@ }, onAvatarChanged () { - this.avatar = _converse.avatars.findWhere({'jid': this.get('from')}); - if (!this.avatar) { return; } + const vcard = _converse.vcards.findWhere({'jid': this.get('from')}); + if (!vcard) { return; } const hash = this.get('image_hash'); - if (hash && this.avatar.get('image_hash') !== hash) { - _converse.api.vcard.update(this.avatar); + if (hash && vcard.get('image_hash') !== hash) { + _converse.api.vcard.update(vcard); } } }); diff --git a/src/converse-vcard.js b/src/converse-vcard.js index 9c3006d0c..84599a705 100644 --- a/src/converse-vcard.js +++ b/src/converse-vcard.js @@ -10,7 +10,7 @@ define(["converse-core", "crypto", "strophe.vcard"], factory); }(this, function (converse, CryptoJS) { "use strict"; - const { Promise, Strophe, SHA1, _, moment, sizzle } = converse.env; + const { Backbone, Promise, Strophe, SHA1, _, b64_sha1, moment, sizzle } = converse.env; const u = converse.env.utils; @@ -110,6 +110,15 @@ */ const { _converse } = this; + _converse.VCards = Backbone.Collection.extend({ + model: _converse.ModelWithDefaultAvatar, + + initialize () { + this.on('add', (model) => _converse.api.vcard.update(model)); + } + }); + + _converse.createRequestingContactFromVCard = function (presence, vcard) { const bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from')); let fullname = vcard.fullname; @@ -134,6 +143,14 @@ }; /* Event handlers */ + _converse.initVCardCollection = function () { + _converse.vcards = new _converse.VCards(); + _converse.vcards.browserStorage = new Backbone.BrowserStorage.local(b64_sha1(`converse.vcards`)); + _converse.vcards.fetch(); + } + _converse.api.listen.on('connectionInitialized', _converse.initVCardCollection); + + _converse.on('addClientFeatures', () => { _converse.connection.disco.addFeature(Strophe.NS.VCARD); });