Commit d5c8acea authored by JC Brand's avatar JC Brand

Create `avatar` objects for all messages, not just groupchat

parent e77f6274
...@@ -95,11 +95,9 @@ ...@@ -95,11 +95,9 @@
}, },
initialize () { initialize () {
if (this.get('type') === 'groupchat') { this.avatar = _converse.avatars.findWhere({'jid': this.get('from')});
this.avatar = _converse.avatars.findWhere({'muc_jid': this.get('from')});
if (_.isNil(this.avatar)) { if (_.isNil(this.avatar)) {
this.avatar = _converse.avatars.create({'muc_jid': this.get('from')}); this.avatar = _converse.avatars.create({'jid': this.get('from')});
}
} }
if (this.get('file')) { if (this.get('file')) {
......
...@@ -241,6 +241,44 @@ ...@@ -241,6 +241,44 @@
_converse.router = new Backbone.Router(); _converse.router = new Backbone.Router();
_converse.ModelWithDefaultAvatar = Backbone.Model.extend({
defaults: {
'image': _converse.DEFAULT_IMAGE,
'image_type': _converse.DEFAULT_IMAGE_TYPE
},
set (key, val, options) {
// Override Backbone.Model.prototype.set to make sure that the
// default `image` and `image_type` values are maintained.
let attrs;
if (typeof key === 'object') {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
if (_.has(attrs, 'image') && _.isUndefined(attrs['image'])) {
attrs['image'] = _converse.DEFAULT_IMAGE;
attrs['image_type'] = _converse.DEFAULT_IMAGE_TYPE;
return Backbone.Model.prototype.set.call(this, attrs, options);
} else {
return Backbone.Model.prototype.set.apply(this, arguments);
}
}
});
_converse.Avatars = Backbone.Collection.extend({
model: _converse.ModelWithDefaultAvatar,
initialize () {
this.on('add', (avatar) => {
_converse.api.vcard.update(avatar);
});
}
});
_converse.initialize = function (settings, callback) { _converse.initialize = function (settings, callback) {
"use strict"; "use strict";
settings = !_.isUndefined(settings) ? settings : {}; settings = !_.isUndefined(settings) ? settings : {};
...@@ -1504,33 +1542,6 @@ ...@@ -1504,33 +1542,6 @@
this.connfeedback = new this.ConnectionFeedback(); this.connfeedback = new this.ConnectionFeedback();
this.ModelWithDefaultAvatar = Backbone.Model.extend({
defaults: {
'image': _converse.DEFAULT_IMAGE,
'image_type': _converse.DEFAULT_IMAGE_TYPE
},
set (key, val, options) {
// Override Backbone.Model.prototype.set to make sure that the
// default `image` and `image_type` values are maintained.
let attrs;
if (typeof key === 'object') {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
if (_.has(attrs, 'image') && _.isUndefined(attrs['image'])) {
attrs['image'] = _converse.DEFAULT_IMAGE;
attrs['image_type'] = _converse.DEFAULT_IMAGE_TYPE;
return Backbone.Model.prototype.set.call(this, attrs, options);
} else {
return Backbone.Model.prototype.set.apply(this, arguments);
}
}
});
this.XMPPStatus = this.ModelWithDefaultAvatar.extend({ this.XMPPStatus = this.ModelWithDefaultAvatar.extend({
defaults () { defaults () {
...@@ -1809,6 +1820,12 @@ ...@@ -1809,6 +1820,12 @@
_converse.emit('connectionInitialized'); _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 () { this._tearDown = function () {
/* Remove those views which are only allowed with a valid /* Remove those views which are only allowed with a valid
* connection. * connection.
...@@ -1878,6 +1895,7 @@ ...@@ -1878,6 +1895,7 @@
function finishInitialization () { function finishInitialization () {
_converse.initPlugins(); _converse.initPlugins();
_converse.initConnection(); _converse.initConnection();
_converse.initAvatars();
_converse.setUpXMLLogging(); _converse.setUpXMLLogging();
_converse.logIn(); _converse.logIn();
_converse.registerGlobalEventHandlers(); _converse.registerGlobalEventHandlers();
......
...@@ -53,9 +53,9 @@ ...@@ -53,9 +53,9 @@
}) })
} }
}); });
if (this.model.get('type') === 'groupchat') { this.model.avatar.on('change:image', () => {
this.model.avatar.on('change:image', this.renderAvatar, this); this.renderAvatar();
} });
this.model.on('change:fullname', this.render, this); this.model.on('change:fullname', this.render, this);
this.model.on('change:progress', this.renderFileUploadProgresBar, this); this.model.on('change:progress', this.renderFileUploadProgresBar, this);
this.model.on('change:type', this.render, this); this.model.on('change:type', this.render, this);
...@@ -128,21 +128,11 @@ ...@@ -128,21 +128,11 @@
if (_.isNull(canvas_el)) { if (_.isNull(canvas_el)) {
return; return;
} }
let image, image_type; const image_type = this.model.avatar.get('image_type'),
image = this.model.avatar.get('image'),
if (this.chatbox.get('type') === 'chatroom') { img_src = "data:" + image_type + ";base64," + image,
image_type = this.model.avatar.get('image_type');
image = this.model.avatar.get('image');
} else if (this.model.get('sender') === 'me') {
image_type = _converse.xmppstatus.get('image_type');
image = _converse.xmppstatus.get('image');
} else {
image_type = this.chatbox.get('image_type');
image = this.chatbox.get('image');
}
const img_src = "data:" + image_type + ";base64," + image,
img = new Image(); img = new Image();
img.onload = () => { img.onload = () => {
const ctx = canvas_el.getContext('2d'), const ctx = canvas_el.getContext('2d'),
ratio = img.width / img.height; ratio = img.width / img.height;
......
...@@ -67,7 +67,7 @@ ...@@ -67,7 +67,7 @@
* *
* NB: These plugins need to have already been loaded via require.js. * NB: These plugins need to have already been loaded via require.js.
*/ */
dependencies: ["converse-controlbox"], dependencies: ["converse-chatboxes", "converse-controlbox"],
overrides: { overrides: {
// Overrides mentioned here will be picked up by converse.js's // Overrides mentioned here will be picked up by converse.js's
...@@ -996,9 +996,7 @@ ...@@ -996,9 +996,7 @@
}, },
onAvatarChanged () { onAvatarChanged () {
this.avatar = _converse.avatars.findWhere({ this.avatar = _converse.avatars.findWhere({'jid': this.get('from')});
'muc_jid': this.get('from')
});
if (!this.avatar) { return; } if (!this.avatar) { return; }
const hash = this.get('image_hash'); const hash = this.get('image_hash');
...@@ -1026,15 +1024,6 @@ ...@@ -1026,15 +1024,6 @@
}); });
_converse.Avatars = Backbone.Collection.extend({
model: _converse.ModelWithDefaultAvatar,
initialize () {
this.on('add', (avatar) => _converse.api.vcard.update(avatar));
}
});
_converse.RoomsPanelModel = Backbone.Model.extend({ _converse.RoomsPanelModel = Backbone.Model.extend({
defaults: { defaults: {
'muc_domain': '', 'muc_domain': '',
...@@ -1144,15 +1133,6 @@ ...@@ -1144,15 +1133,6 @@
} }
/************************ BEGIN Event Handlers ************************/ /************************ BEGIN Event Handlers ************************/
_converse.initAvatars = function () {
if (_.isUndefined(_converse.avatars)) {
_converse.avatars = new _converse.Avatars();
_converse.avatars.browserStorage = new Backbone.BrowserStorage.local(b64_sha1(`converse.avatars`));
_converse.avatars.fetch();
}
}
_converse.api.listen.on('connectionInitialized', _converse.initAvatars);
_converse.on('addClientFeatures', () => { _converse.on('addClientFeatures', () => {
if (_converse.allow_muc) { if (_converse.allow_muc) {
_converse.connection.disco.addFeature(Strophe.NS.MUC); _converse.connection.disco.addFeature(Strophe.NS.MUC);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment