1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-22 09:12:19 -05:00

Fix: avatar bindings

This commit is contained in:
Sébastien Hut 2015-03-20 20:36:40 +01:00 committed by Sébastien Hut
parent fe22d0e98e
commit d67440a7cc
8 changed files with 66 additions and 30 deletions

View File

@ -102,6 +102,10 @@ module.exports = {
cb(); cb();
}, },
function (cb) { function (cb) {
app.whenConnected(function () {
me.publishAvatar();
});
function start() { function start() {
// start our router and show the appropriate page // start our router and show the appropriate page
app.history.start({pushState: true, root: '/'}); app.history.start({pushState: true, root: '/'});

View File

@ -2,18 +2,18 @@
"use strict"; "use strict";
var crypto = require('crypto'); var crypto = require('crypto');
module.exports.getGravatar = function (jid) {
function fallback(jid) {
var gID = crypto.createHash('md5').update(jid).digest('hex'); var gID = crypto.createHash('md5').update(jid).digest('hex');
return { return {
uri: 'https://gravatar.com/avatar/' + gID + '?s=80&d=mm' uri: 'https://gravatar.com/avatar/' + gID + '?s=80&d=mm'
}; };
} };
var getGravatar = module.exports.getGravatar;
module.exports = function (jid, id, type, source, cb) { module.exports.fetch = function (jid, id, type, source, cb) {
if (!id) { if (!id) {
return cb(fallback(jid)); return cb(getGravatar(jid));
} }
app.storage.avatars.get(id, function (err, avatar) { app.storage.avatars.get(id, function (err, avatar) {
@ -22,14 +22,14 @@ module.exports = function (jid, id, type, source, cb) {
} }
if (!type) { if (!type) {
return cb(fallback(jid)); return cb(getGravatar(jid));
} }
app.whenConnected(function () { app.whenConnected(function () {
if (source == 'vcard') { if (source == 'vcard') {
app.api.getVCard(jid, function (err, resp) { app.api.getVCard(jid, function (err, resp) {
if (err) { if (err) {
return cb(fallback(jid)); return cb(getGravatar(jid));
} }
type = resp.vCardTemp.photo.type || type; type = resp.vCardTemp.photo.type || type;
@ -49,7 +49,7 @@ module.exports = function (jid, id, type, source, cb) {
} else { } else {
app.api.getAvatar(jid, id, function (err, resp) { app.api.getAvatar(jid, id, function (err, resp) {
if (err) { if (err) {
return cb(fallback(jid)); return cb(getGravatar(jid));
} }
var data = resp.pubsub.retrieve.item.avatarData; var data = resp.pubsub.retrieve.item.avatarData;

View File

@ -10,7 +10,7 @@ var Resources = require('./resources');
var Messages = require('./messages'); var Messages = require('./messages');
var Message = require('./message'); var Message = require('./message');
var logger = require('andlog'); var logger = require('andlog');
var fetchAvatar = require('../helpers/fetchAvatar'); var avatarHandler = require('../helpers/avatarHandler');
module.exports = HumanModel.define({ module.exports = HumanModel.define({
@ -231,8 +231,10 @@ module.exports = HumanModel.define({
} }
}, },
setAvatar: function (id, type, source) { setAvatar: function (id, type, source) {
if (!this.avatar) this.avatar = avatarHandler.getGravatar(this.jid).uri;
var self = this; var self = this;
fetchAvatar(this.jid, id, type, source, function (avatar) { avatarHandler.fetch(this.jid, id, type, source, function (avatar) {
if (source == 'vcard' && self.avatarSource == 'pubsub') return; if (source == 'vcard' && self.avatarSource == 'pubsub') return;
self.avatarID = avatar.id; self.avatarID = avatar.id;
self.avatar = avatar.uri; self.avatar = avatar.uri;

View File

@ -9,7 +9,8 @@ var Contact = require('./contact');
var MUCs = require('./mucs'); var MUCs = require('./mucs');
var MUC = require('./muc'); var MUC = require('./muc');
var ContactRequests = require('./contactRequests'); var ContactRequests = require('./contactRequests');
var fetchAvatar = require('../helpers/fetchAvatar'); var avatarHandler = require('../helpers/avatarHandler');
var crypto = require('crypto');
module.exports = HumanModel.define({ module.exports = HumanModel.define({
initialize: function (opts) { initialize: function (opts) {
@ -105,12 +106,34 @@ module.exports = HumanModel.define({
return this.avatar; return this.avatar;
}, },
setAvatar: function (id, type, source) { setAvatar: function (id, type, source) {
if (!this.avatar) this.avatar = avatarHandler.getGravatar('').uri;
var self = this; var self = this;
fetchAvatar('', id, type, source, function (avatar) { avatarHandler.fetch('', id, type, source, function (avatar) {
self.avatarID = avatar.id; self.avatarID = avatar.id;
self.avatar = avatar.uri; self.avatar = avatar.uri;
}); });
}, },
publishAvatar: function (data) {
if (!data) data = this.avatar;
if (!data || data.indexOf('https://') != -1) return;
var resampler = new Resample(data, 80, 80, function (data) {
var b64Data = data.split(',')[1];
var id = crypto.createHash('sha1').update(atob(b64Data)).digest('hex');
app.storage.avatars.add({id: id, uri: data});
client.publishAvatar(id, b64Data, function (err, res) {
if (err) return;
client.useAvatars([{
id: id,
width: 80,
height: 80,
type: 'image/png',
bytes: b64Data.length
}]);
});
});
},
hasLdapUsers: function () { hasLdapUsers: function () {
return app.ldapUsers.length > 0 ? 'hasLdapUsers' : ''; return app.ldapUsers.length > 0 ? 'hasLdapUsers' : '';
}, },

View File

@ -9,6 +9,7 @@ var HumanModel = require('human-model');
var Resources = require('./resources'); var Resources = require('./resources');
var Messages = require('./messages'); var Messages = require('./messages');
var Message = require('./message'); var Message = require('./message');
var avatarHandler = require('../helpers/avatarHandler');
module.exports = HumanModel.define({ module.exports = HumanModel.define({
initialize: function (attrs) { initialize: function (attrs) {
@ -90,7 +91,7 @@ module.exports = HumanModel.define({
return nickname != this.getName(jid) ? nickname : ''; return nickname != this.getName(jid) ? nickname : '';
}, },
getAvatar: function (jid) { getAvatar: function (jid) {
var avatar = ""; var avatar = avatarHandler.getGravatar('').uri;
var xmppContact = me.getContact(jid.split('/')[1]); var xmppContact = me.getContact(jid.split('/')[1]);
if (xmppContact) { if (xmppContact) {
avatar = xmppContact.avatar; avatar = xmppContact.avatar;

View File

@ -25,6 +25,7 @@ module.exports = BasePage.extend({
this.listenTo(this.model, 'refresh', this.renderCollection); this.listenTo(this.model, 'refresh', this.renderCollection);
app.state.bind('change:connected', this.connectionChange, this); app.state.bind('change:connected', this.connectionChange, this);
this.model.bind('change:avatar', this.handleAvatarChanged, this);
this.render(); this.render();
}, },
@ -37,7 +38,6 @@ module.exports = BasePage.extend({
'click .mute': 'handleMuteClick' 'click .mute': 'handleMuteClick'
}, },
srcBindings: { srcBindings: {
avatar: 'header .avatar',
streamUrl: 'video.remote' streamUrl: 'video.remote'
}, },
textBindings: { textBindings: {
@ -233,6 +233,11 @@ module.exports = BasePage.extend({
var resources = val || this.model.jingleResources; var resources = val || this.model.jingleResources;
this.$('button.call').prop('disabled', !resources.length); this.$('button.call').prop('disabled', !resources.length);
}, },
handleAvatarChanged: function (contact, uri) {
if (!me.isMe(contact.jid)) {
$('.' + contact.jid.substr(0, contact.jid.indexOf('@')) + ' .messageAvatar img').attr('src', uri);
}
},
appendModel: function (model, preload) { appendModel: function (model, preload) {
var newEl, first, last; var newEl, first, last;
var msgDate = Date.create(model.timestamp); var msgDate = Date.create(model.timestamp);
@ -259,6 +264,7 @@ module.exports = BasePage.extend({
this.staydown.checkdown(); this.staydown.checkdown();
} else { } else {
newEl = $(model.templateHtml); newEl = $(model.templateHtml);
if (!me.isMe(model.sender.jid)) newEl.addClass(model.sender.jid.substr(0, model.sender.jid.indexOf('@')));
this.staydown.append(newEl[0]); this.staydown.append(newEl[0]);
this.lastModel = model; this.lastModel = model;
} }
@ -283,6 +289,7 @@ module.exports = BasePage.extend({
first.addClass('chatGroup'); first.addClass('chatGroup');
} else { } else {
newEl = $(model.templateHtml); newEl = $(model.templateHtml);
if (!me.isMe(model.sender.jid)) newEl.addClass(model.sender.jid.substr(0, model.sender.jid.indexOf('@')));
firstEl.after(newEl[0]); firstEl.after(newEl[0]);
this.firstModel = model; this.firstModel = model;
} }

View File

@ -90,6 +90,7 @@ module.exports = BasePage.extend({
this.listenTo(this, 'rosterItemClicked', this.rosterItemSelected); this.listenTo(this, 'rosterItemClicked', this.rosterItemSelected);
this.listenTo(this.model.messages, 'add', this.handleChatAdded); this.listenTo(this.model.messages, 'add', this.handleChatAdded);
this.listenTo(this.model.resources, 'add', this.handleResourceAdded);
$(window).on('resize', _.bind(this.resizeInput, this)); $(window).on('resize', _.bind(this.resizeInput, this));
@ -114,6 +115,17 @@ module.exports = BasePage.extend({
handleChatAdded: function (model) { handleChatAdded: function (model) {
this.appendModel(model, true); this.appendModel(model, true);
}, },
handleResourceAdded: function (model) {
var xmppContact = me.getContact(model.id.split('/')[1]);
if (xmppContact) {
xmppContact.bind('change:avatar', this.handleAvatarChanged, this);
}
},
handleAvatarChanged: function(contact, uri) {
if (!me.isMe(contact.jid)) {
$('.' + contact.jid.substr(0, contact.jid.indexOf('@')) + ' .messageAvatar img').attr('src', uri);
}
},
handlePageLoaded: function () { handlePageLoaded: function () {
this.staydown.checkdown(); this.staydown.checkdown();
this.resizeInput(); this.resizeInput();
@ -354,6 +366,7 @@ module.exports = BasePage.extend({
this.staydown.checkdown(); this.staydown.checkdown();
} else { } else {
newEl = $(model.templateHtml); newEl = $(model.templateHtml);
newEl.addClass(model.sender.getNickname(model.from.full));
this.staydown.append(newEl[0]); this.staydown.append(newEl[0]);
this.lastModel = model; this.lastModel = model;
} }
@ -378,6 +391,7 @@ module.exports = BasePage.extend({
first.addClass('chatGroup'); first.addClass('chatGroup');
} else { } else {
newEl = $(model.templateHtml); newEl = $(model.templateHtml);
newEl.addClass(model.sender.getNickname(model.from.full));
firstEl.after(newEl[0]); firstEl.after(newEl[0]);
this.firstModel = model; this.firstModel = model;
} }

View File

@ -1,7 +1,6 @@
/*global app, me, client, Resample*/ /*global app, me, client, Resample*/
"use strict"; "use strict";
var crypto = require('crypto');
var BasePage = require('./base'); var BasePage = require('./base');
var templates = require('../templates'); var templates = require('../templates');
var LDAPUserItem = require('../views/ldapUserItem'); var LDAPUserItem = require('../views/ldapUserItem');
@ -79,21 +78,7 @@ module.exports = BasePage.extend({
if (file.type.match('image.*')) { if (file.type.match('image.*')) {
var fileTracker = new FileReader(); var fileTracker = new FileReader();
fileTracker.onload = function () { fileTracker.onload = function () {
var resampler = new Resample(this.result, 80, 80, function (data) { me.publishAvatar(this.result);
var b64Data = data.split(',')[1];
var id = crypto.createHash('sha1').update(atob(b64Data)).digest('hex');
app.storage.avatars.add({id: id, uri: data});
client.publishAvatar(id, b64Data, function (err, res) {
if (err) return;
client.useAvatars([{
id: id,
width: 80,
height: 80,
type: 'image/png',
bytes: b64Data.length
}]);
});
});
}; };
fileTracker.readAsDataURL(file); fileTracker.readAsDataURL(file);
} }