web/lib/javascripts/session.js in vines-0.2.0 vs web/lib/javascripts/session.js in vines-0.2.1

- old
+ new

@@ -27,11 +27,11 @@ return this.handlePresence(el); }, this)), null, 'presence'); callback(true); return this.findRoster(__bind(function() { this.notify('roster'); - this.xmpp.send($pres().tree()); + this.xmpp.send($('<presence/>').get(0)); return this.findCards(); }, this)); } }, this)); }; @@ -57,10 +57,13 @@ return this.xmpp.jid; }; Session.prototype.bareJid = function() { return this.xmpp.jid.split('/')[0]; }; + Session.prototype.uniqueId = function() { + return this.xmpp.getUniqueId(); + }; Session.prototype.avatar = function(jid) { var card; card = this.loadCard(jid); if (card && card.photo) { return "data:" + card.photo.type + ";base64," + card.photo.binval; @@ -104,15 +107,16 @@ } }, this); return this.findCard(jids.shift(), success); }; Session.prototype.findCard = function(jid, callback) { - var handler, iq; + var node; if (!jid) { return; } - handler = function(result) { + node = this.xml("<iq id=\"" + (this.uniqueId()) + "\" to=\"" + jid + "\" type=\"get\">\n <vCard xmlns=\"vcard-temp\"/>\n</iq>"); + return this.sendIQ(node, function(result) { var bin, card, photo, type, vcard; card = $('vCard', result); photo = $('PHOTO', card); type = $('TYPE', photo).text(); bin = $('BINVAL', photo).text(); @@ -124,126 +128,85 @@ jid: jid, photo: photo, retrieved: new Date() }; return callback(card.size() > 0 ? vcard : null); - }; - iq = $iq({ - type: 'get', - to: jid, - id: this.xmpp.getUniqueId() - }).c('vCard', { - xmlns: 'vcard-temp' - }).up(); - return this.xmpp.sendIQ(iq, handler, handler, 5000); + }); }; Session.prototype.parseRoster = function(node) { return $('item', node).map(function() { return new Contact(this); }).get(); }; Session.prototype.findRoster = function(callback) { - var handler, iq; - handler = __bind(function(result) { + var node; + node = $("<iq id='" + (this.uniqueId()) + "' type=\"get\">\n <query xmlns=\"jabber:iq:roster\"/>\n</iq>"); + return this.sendIQ(node.get(0), __bind(function(result) { var contact, contacts, _i, _len; contacts = this.parseRoster(result); for (_i = 0, _len = contacts.length; _i < _len; _i++) { contact = contacts[_i]; this.roster[contact.jid] = contact; } return callback(); - }, this); - iq = $iq({ - type: 'get', - id: this.xmpp.getUniqueId() - }).c('query', { - xmlns: 'jabber:iq:roster' - }).up(); - return this.xmpp.sendIQ(iq, handler, handler, 5000); + }, this)); }; Session.prototype.sendMessage = function(jid, message) { - var stanza; - stanza = $msg({ - to: jid, - from: this.xmpp.jid, - type: 'chat' - }).c('body').t(message).up(); - return this.xmpp.send(stanza.tree()); + var node; + node = this.xml("<message id=\"" + (this.uniqueId()) + "\" to=\"" + jid + "\" type=\"chat\">\n <body></body>\n</message>"); + $('body', node).text(message); + return this.xmpp.send(node); }; Session.prototype.sendPresence = function(away, status) { - var stanza; - stanza = $pres(); + var node; + node = $('<presence/>'); if (away) { - stanza.c('show').t('xa').up(); + node.append($('<show>xa</show>')); if (status !== 'Away') { - stanza.c('status').t(status); + node.append($('<status/>').text(status)); } } else { if (status !== 'Available') { - stanza.c('status').t(status); + node.append($('<status/>').text(status)); } } - return this.xmpp.send(stanza.tree()); + return this.xmpp.send(node.get(0)); }; + Session.prototype.sendIQ = function(node, callback) { + return this.xmpp.sendIQ(node, callback, callback, 5000); + }; Session.prototype.updateContact = function(contact, add) { - var group, iq, _i, _len, _ref; - iq = $iq({ - type: 'set', - id: this.xmpp.getUniqueId() - }).c('query', { - xmlns: 'jabber:iq:roster' - }).c('item', { - jid: contact.jid, - name: contact.name - }); + var group, node, _i, _len, _ref; + node = $("<iq id=\"" + (this.uniqueId()) + "\" type=\"set\">\n <query xmlns=\"jabber:iq:roster\">\n <item name=\"\" jid=\"" + contact.jid + "\"/>\n </query>\n</iq>"); + $('item', node).attr('name', contact.name); _ref = contact.groups; for (_i = 0, _len = _ref.length; _i < _len; _i++) { group = _ref[_i]; - iq.c('group', group).up(); + $('item', node).append($('<group></group>').text(group)); } - this.xmpp.send(iq.up().tree()); + this.xmpp.send(node.get(0)); if (add) { - return this.xmpp.send($pres({ - type: 'subscribe', - to: contact.jid - }).tree()); + return this.sendSubscribe(contact.jid); } }; Session.prototype.removeContact = function(jid) { - var iq; - iq = $iq({ - type: 'set', - id: this.xmpp.getUniqueId() - }).c('query', { - xmlns: 'jabber:iq:roster' - }).c('item', { - jid: jid, - subscription: 'remove' - }).up().up(); - return this.xmpp.send(iq.tree()); + var node; + node = $("<iq id=\"" + (this.uniqueId()) + "\" type=\"set\">\n <query xmlns=\"jabber:iq:roster\">\n <item jid=\"" + jid + "\" subscription=\"remove\"/>\n </query>\n</iq>"); + return this.xmpp.send(node.get(0)); }; Session.prototype.sendSubscribe = function(jid) { - return this.xmpp.send($pres({ - type: 'subscribe', - to: jid, - id: this.xmpp.getUniqueId() - }).tree()); + return this.xmpp.send(this.presence(jid, 'subscribe')); }; Session.prototype.sendSubscribed = function(jid) { - return this.xmpp.send($pres({ - type: 'subscribed', - to: jid, - id: this.xmpp.getUniqueId() - }).tree()); + return this.xmpp.send(this.presence(jid, 'subscribed')); }; Session.prototype.sendUnsubscribed = function(jid) { - return this.xmpp.send($pres({ - type: 'unsubscribed', - to: jid, - id: this.xmpp.getUniqueId() - }).tree()); + return this.xmpp.send(this.presence(jid, 'unsubscribed')); }; + Session.prototype.presence = function(to, type) { + return $("<presence\n id=\"" + (this.uniqueId()) + "\"\n to=\"" + to + "\"\n type=\"" + type + "\"/>").get(0); + }; Session.prototype.handleIq = function(node) { var contact, contacts, ns, old, type, _i, _len; node = $(node); type = node.attr('type'); ns = node.find('query').attr('xmlns'); @@ -315,8 +278,11 @@ for (_i = 0, _len = _ref.length; _i < _len; _i++) { callback = _ref[_i]; _results.push(callback(obj)); } return _results; + }; + Session.prototype.xml = function(xml) { + return $.parseXML(xml).documentElement; }; return Session; })(); \ No newline at end of file