Refactored rosterChanged method.

Roster changes are listened to on the rosterview and the applicable chatbox is
updated.

Custom status messages are now remembered upon page load.
This commit is contained in:
JC Brand 2013-03-25 13:08:27 +02:00
parent 7046656370
commit cdcbe41d59

View File

@ -444,43 +444,45 @@
} }
}, },
closeChat: function () { onChange: function (item, changed) {
this.model.destroy(); if (_.has(changed.changes, 'chat_status')) {
var chat_status = item.get('chat_status'),
fullname = item.get('fullname');
if (this.$el.is(':visible')) {
if (chat_status === 'offline') {
this.insertStatusNotification(fullname+' '+'has gone offline');
} else if (chat_status === 'away') {
this.insertStatusNotification(fullname+' '+'has gone away');
} else if ((chat_status === 'dnd')) {
this.insertStatusNotification(fullname+' '+'is busy');
} else if (chat_status === 'online') {
this.$el.find('div.chat-event').remove();
}
}
} if (_.has(changed.changes, 'status')) {
this.showStatusMessage(item.get('status'));
}
}, },
rosterChanged: function (item, changed) { showStatusMessage: function (msg) {
// FIXME: This event handler should go onto the roster itself, then it this.$el.find('p.user-custom-message').text(msg).attr('title', msg);
// will be called once (for the roster) and not once per open },
// chatbox
var fullname = this.model.get('fullname'), closeChat: function () {
chat_status = item.get('chat_status'); this.model.destroy();
if (item.get('jid') === this.model.get('jid')) {
if (_.has(changed.changes, 'chat_status')) {
if (this.$el.is(':visible')) {
if (chat_status === 'offline') {
this.insertStatusNotification(fullname+' '+'has gone offline');
} else if (chat_status === 'away') {
this.insertStatusNotification(fullname+' '+'has gone away');
} else if ((chat_status === 'dnd')) {
this.insertStatusNotification(fullname+' '+'is busy');
} else if (chat_status === 'online') {
this.$el.find('div.chat-event').remove();
}
}
} else if (_.has(changed.changes, 'status')) {
this.$el.find('p.user-custom-message').text(item.get('status')).attr('title', item.get('status'));
}
}
}, },
initialize: function (){ initialize: function (){
this.model.messages.on('add', this.showMessage, this); this.model.messages.on('add', this.showMessage, this);
this.model.on('show', this.show, this); this.model.on('show', this.show, this);
this.model.on('destroy', function (model, response, options) { this.$el.hide('fast'); }, this); this.model.on('destroy', function (model, response, options) { this.$el.hide('fast'); }, this);
xmppchat.roster.on('change', this.rosterChanged, this); this.model.on('change', this.onChange, this);
this.$el.appendTo(xmppchat.chatboxesview.$el); this.$el.appendTo(xmppchat.chatboxesview.$el);
this.render().show().model.messages.fetch({add: true}); this.render().show().model.messages.fetch({add: true});
if (this.model.get('status')) {
this.showStatusMessage(this.model.get('status'));
}
xmppchat.clearMsgCounter(); xmppchat.clearMsgCounter();
}, },
@ -1309,21 +1311,6 @@
return 0; return 0;
}, },
clearResources: function (bare_jid) {
var item = this.getItem(bare_jid);
if (item) {
item.set({'resources': []});
}
},
getTotalResources: function (bare_jid) {
var item = this.getItem(bare_jid);
if (item) {
return _.size(item.get('resources'));
}
return 0;
},
subscribeBack: function (jid) { subscribeBack: function (jid) {
// XXX: Why the distinction between jid and bare_jid? // XXX: Why the distinction between jid and bare_jid?
var bare_jid = Strophe.getBareJidFromJid(jid); var bare_jid = Strophe.getBareJidFromJid(jid);
@ -1387,15 +1374,11 @@
rosterHandler: function (items) { rosterHandler: function (items) {
this.cleanCache(items); this.cleanCache(items);
_.each(items, function (item, index, items) { _.each(items, function (item, index, items) {
if (this.isSelf(item.jid)) { if (this.isSelf(item.jid)) { return; }
return;
}
var model = this.getItem(item.jid); var model = this.getItem(item.jid);
if (!model) { if (!model) {
is_last = false; is_last = false;
if (index === (items.length-1)) { if (index === (items.length-1)) { is_last = true; }
is_last = true;
}
xmppchat.getVCard( xmppchat.getVCard(
item.jid, item.jid,
$.proxy(function (jid, fullname, img, img_type, url) { $.proxy(function (jid, fullname, img, img_type, url) {
@ -1421,8 +1404,6 @@
is_last: is_last is_last: is_last
}); });
}, this)); }, this));
} else { } else {
if ((item.subscription === 'none') && (item.ask === null)) { if ((item.subscription === 'none') && (item.ask === null)) {
// This user is no longer in our roster // This user is no longer in our roster
@ -1446,7 +1427,7 @@
show = $presence.find('show'), show = $presence.find('show'),
chat_status = show.text() || 'online', chat_status = show.text() || 'online',
status_message = $presence.find('status'), status_message = $presence.find('status'),
item, model; item;
if (this.isSelf(bare_jid)) { if (this.isSelf(bare_jid)) {
if ((xmppchat.connection.jid !== jid)&&(presence_type !== 'unavailabe')) { if ((xmppchat.connection.jid !== jid)&&(presence_type !== 'unavailabe')) {
@ -1460,16 +1441,14 @@
return true; // Ignore MUC return true; // Ignore MUC
} }
if (status_message.text() && (presence_type !== 'unavailable')) { item = this.getItem(bare_jid);
model = this.getItem(bare_jid); if (status_message.text() != item.get('status')) {
model.set({'status': status_message.text()}); item.set({'status': status_message.text()});
} }
if ((presence_type === 'error') || (presence_type === 'subscribed') || (presence_type === 'unsubscribe')) { if ((presence_type === 'error') || (presence_type === 'subscribed') || (presence_type === 'unsubscribe')) {
return true; return true;
} else if (presence_type === 'subscribe') { } else if (presence_type === 'subscribe') {
item = this.getItem(bare_jid);
// TODO see if auto_subscribe is truly an unresolved variable
if (xmppchat.auto_subscribe) { if (xmppchat.auto_subscribe) {
if ((!item) || (item.get('subscription') != 'to')) { if ((!item) || (item.get('subscription') != 'to')) {
this.subscribeBack(jid); this.subscribeBack(jid);
@ -1510,16 +1489,14 @@
this.unsubscribe(jid); this.unsubscribe(jid);
} else if (presence_type === 'unavailable') { } else if (presence_type === 'unavailable') {
if (this.removeResource(bare_jid, resource) === 0) { if (this.removeResource(bare_jid, resource) === 0) {
model = this.getItem(bare_jid); if (item) {
if (model) { item.set({'chat_status': 'offline'});
model.set({'chat_status': 'offline'});
} }
} }
} else { } else {
// presence_type is undefined // presence_type is undefined
this.addResource(bare_jid, resource); this.addResource(bare_jid, resource);
model = this.getItem(bare_jid); item.set({'chat_status': chat_status});
model.set({'chat_status': chat_status});
} }
return true; return true;
} }
@ -1551,7 +1528,8 @@
this.render(item); this.render(item);
}, this); }, this);
this.model.on('change', function (item) { this.model.on('change', function (item, changed) {
this.updateChatBox(item, changed);
this.render(item); this.render(item);
}, this); }, this);
@ -1570,6 +1548,19 @@
this.initialSort(); this.initialSort();
}, },
updateChatBox: function (item, changed) {
var chatbox = xmppchat.chatboxes.get(item.get('jid')),
changes = {};
if (!chatbox) { return; }
if (_.has(changed.changes, 'chat_status')) {
changes.chat_status = item.get('chat_status');
}
if (_.has(changed.changes, 'status')) {
changes.status = item.get('status');
}
chatbox.save(changes);
},
template: _.template('<dt id="xmpp-contact-requests">Contact requests</dt>' + template: _.template('<dt id="xmpp-contact-requests">Contact requests</dt>' +
'<dt id="xmpp-contacts">My contacts</dt>' + '<dt id="xmpp-contacts">My contacts</dt>' +
'<dt id="pending-xmpp-contacts">Pending contacts</dt>'), '<dt id="pending-xmpp-contacts">Pending contacts</dt>'),