Commit 630454f8 authored by JC Brand's avatar JC Brand

Refactored the roster code to make it more sane

parent 0fee92bf
......@@ -1063,13 +1063,8 @@
}
});
xmppchat.Roster = (function (_, $, console) {
var ob = {},
Collection = Backbone.Collection.extend({
RosterItems = Backbone.Collection.extend({
model: xmppchat.RosterItem,
stropheRoster: xmppchat.connection.roster,
initialize: function () {
this._connection = xmppchat.connection;
},
......@@ -1100,12 +1095,24 @@
return rank;
},
rosterSuggestedItem: function (msg) {
alert('hello');
$(msg).find('item').each(function () {
var jid = $(this).attr('jid');
var action = $(this).attr('action');
if (action === 'add') {
xmppchat.connection.send($pres({to: jid, type: 'subscribe'}));
}
});
return true;
},
isSelf: function (jid) {
return (Strophe.getBareJidFromJid(jid) === Strophe.getBareJidFromJid(xmppchat.connection.jid));
},
getRoster: function () {
return xmppchat.connection.roster.get(xmppchat.roster.rosterHandler);
return xmppchat.connection.roster.get($.proxy(this.rosterHandler, this));
},
getItem: function (id) {
......@@ -1171,28 +1178,22 @@
}
}
return count;
}
});
var collection = new Collection();
_.extend(ob, collection);
_.extend(ob, Backbone.Events);
},
ob.rosterHandler = function (items) {
rosterHandler: function (items) {
var model, item;
for (var i=0; i<items.length; i++) {
item = items[i];
model = ob.getItem(item.jid);
model = this.getItem(item.jid);
if (!model) {
ob.addRosterItem(item.jid, item.subscription, item.ask, item.name);
this.addRosterItem(item.jid, item.subscription, item.ask, item.name);
} else {
model.set({'subscription': item.subscription, 'ask': item.ask});
}
}
};
},
ob.presenceHandler = function (presence) {
presenceHandler: function (presence) {
var jid = $(presence).attr('from'),
bare_jid = Strophe.getBareJidFromJid(jid),
resource = Strophe.getResourceFromJid(jid),
......@@ -1201,12 +1202,12 @@
status_message = $(presence).find('status'),
item;
if ((($(presence).find('x').attr('xmlns') || '').indexOf(Strophe.NS.MUC) === 0) || (ob.isSelf(bare_jid))) {
if ((($(presence).find('x').attr('xmlns') || '').indexOf(Strophe.NS.MUC) === 0) || (this.isSelf(bare_jid))) {
// Ignore MUC or self-addressed stanzas
return true;
}
if ((status_message.length > 0)&&(status_message.text())) {
model = ob.getItem(bare_jid);
model = this.getItem(bare_jid);
model.set({'status': status_message.text()});
}
......@@ -1215,13 +1216,13 @@
(presence_type === 'unsubscribe')) {
return true;
} else if (presence_type === 'subscribe') {
item = ob.getItem(bare_jid);
item = this.getItem(bare_jid);
if ((item) && (item.get('subscription') != 'none')) {
xmppchat.connection.roster.authorize(bare_jid);
} else {
$.getJSON(portal_url + "/xmpp-userinfo?user_id=" + Strophe.getNodeFromJid(jid), function (data) {
ob.addRosterItem(bare_jid, 'none', 'request', data.fullname);
});
$.getJSON(portal_url + "/xmpp-userinfo?user_id=" + Strophe.getNodeFromJid(jid), $.proxy(function (data) {
this.addRosterItem(bare_jid, 'none', 'request', data.fullname);
}, this));
}
} else if (presence_type === 'unsubscribed') {
/* Upon receiving the presence stanza of type "unsubscribed",
......@@ -1249,21 +1250,20 @@
}
if ((presence_type !== 'offline')&&(presence_type !== 'unavailable')) {
ob.addResource(bare_jid, resource);
model = ob.getItem(bare_jid);
this.addResource(bare_jid, resource);
model = this.getItem(bare_jid);
model.set({'presence_type': presence_type});
} else {
if (ob.removeResource(bare_jid, resource) === 0) {
model = ob.getItem(bare_jid);
if (this.removeResource(bare_jid, resource) === 0) {
model = this.getItem(bare_jid);
model.set({'presence_type': presence_type});
}
}
}
return true;
};
return ob;
});
},
});
xmppchat.RosterView= (function (roster, _, $, console) {
var View = Backbone.View.extend({
......@@ -1469,6 +1469,8 @@
this.username = chatdata.attr('username');
$(document).unbind('jarnxmpp.connected');
$(document).bind('jarnxmpp.connected', $.proxy(function () {
// this.connection.xmlInput = function (body) { console.log(body); };
// this.connection.xmlOutput = function (body) { console.log(body); };
......@@ -1478,16 +1480,24 @@
// XXX: Better if configurable?
this.connection.muc_domain = 'conference.' + this.connection.domain;
this.roster = this.Roster(_, $, console);
this.roster = new RosterItems();
_.extend(this.roster, this.Roster);
this.rosterview = Backbone.View.extend(this.RosterView(this.roster, _, $, console));
this.connection.addHandler(
$.proxy(this.roster.rosterSuggestedItem, this.roster),
'http://jabber.org/protocol/rosterx', 'message', null);
this.connection.addHandler(
$.proxy(function (presence) {
this.roster.presenceHandler(presence);
this.presenceHandler(presence);
return true;
}, this), null, 'presence', null);
}, this.roster), null, 'presence', null);
this.connection.roster.registerCallback(
$.proxy(this.roster.rosterHandler, this.roster),
null, 'presence', null);
this.connection.roster.registerCallback(this.roster.rosterHandler);
this.roster.getRoster();
this.chatboxes = new this.ChatBoxes();
......
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