Add initial vcard support

This commit is contained in:
JC Brand 2013-03-03 12:16:44 +02:00
parent 65bbe800e2
commit e3fa8325b8

View File

@ -38,6 +38,10 @@
'Libraries/strophe.roster': { 'Libraries/strophe.roster': {
deps: ['Libraries/strophe', 'jquery'] deps: ['Libraries/strophe', 'jquery']
},
'Libraries/strophe.vcard': {
deps: ['Libraries/strophe', 'jquery']
} }
} }
}); });
@ -49,7 +53,8 @@
"Libraries/sjcl", "Libraries/sjcl",
"Libraries/backbone", "Libraries/backbone",
"Libraries/strophe.muc", "Libraries/strophe.muc",
"Libraries/strophe.roster" "Libraries/strophe.roster",
"Libraries/strophe.vcard"
], function (Burry, _s) { ], function (Burry, _s) {
var store = new Burry.Store('collective.xmpp.chat'); var store = new Burry.Store('collective.xmpp.chat');
// Init underscore.str // Init underscore.str
@ -1089,25 +1094,27 @@
return Strophe.getDomainFromJid(jid) === xmppchat.connection.muc_domain; return Strophe.getDomainFromJid(jid) === xmppchat.connection.muc_domain;
}, },
createChatBox: function (jid, data) { createChatRoom: function (jid) {
var box, view; var box = new xmppchat.ChatRoom(jid, xmppchat.fullname);
if (this.isChatRoom(jid)) { var view = new xmppchat.ChatRoomView({
box = new xmppchat.ChatRoom(jid, xmppchat.fullname); 'model': box
view = new xmppchat.ChatRoomView({ });
'model': box this.views[jid] = view.render();
}); view.$el.appendTo(this.$el);
} else { this.options.model.add(box);
box = new xmppchat.ChatBox({ return view;
'id': jid, },
'jid': jid,
'fullname': data.fullname, createChatBox: function (roster_item) {
'portrait_url': data.portrait_url, var jid = roster_item.get('jid');
'user_profile_url': data.user_profile_url var box = new xmppchat.ChatBox({
}); 'id': jid,
view = new xmppchat.ChatBoxView({ 'jid': jid,
model: box 'fullname': roster_item.get('fullname'),
}); 'portrait_url': '',
} 'user_profile_url': '',
});
var view = new xmppchat.ChatBoxView({model: box});
this.views[jid] = view.render(); this.views[jid] = view.render();
view.$el.appendTo(this.$el); view.$el.appendTo(this.$el);
this.options.model.add(box); this.options.model.add(box);
@ -1121,17 +1128,15 @@
} }
}, },
openChat: function (jid) { openChat: function (roster_item) {
var view; var view, jid = roster_item.get('jid');
jid = Strophe.getBareJidFromJid(jid); jid = Strophe.getBareJidFromJid(jid);
if (this.model.get(jid)) { if (this.model.get(jid)) {
this.showChat(jid); this.showChat(jid);
} else if (this.isChatRoom(jid)) { } else if (this.isChatRoom(jid)) {
view = this.createChatBox(jid); view = this.createChatRoom(jid);
} else { } else {
$.getJSON(portal_url + "/xmpp-userinfo?user_id=" + Strophe.getNodeFromJid(jid), $.proxy(function (data) { view = this.createChatBox(roster_item);
view = this.createChatBox(jid, data);
}, this));
} }
}, },
@ -1224,8 +1229,21 @@
}); });
xmppchat.RosterItem = Backbone.Model.extend({ xmppchat.RosterItem = Backbone.Model.extend({
/*
var img = $vcard.find('BINVAL').text();
var type = $vcard.find('TYPE').text();
img_src = 'data:'+type+';base64,'+img;
//display image using localStorage
var ctx = $('#example').get(0).getContext('2d');
var img = new Image(); // Create new Image object
img.onload = function(){
// execute drawImage statements here
ctx.drawImage(img,0,0)
}
img.src = img_src;
*/
initialize: function (jid, subscription, ask, name) { initialize: function (jid, subscription, ask, name, img, img_type) {
var user_id = Strophe.getNodeFromJid(jid); var user_id = Strophe.getNodeFromJid(jid);
if (!name) { if (!name) {
name = user_id; name = user_id;
@ -1256,7 +1274,7 @@
}, },
openChat: function (ev) { openChat: function (ev) {
xmppchat.chatboxesview.openChat(this.model.get('jid')); xmppchat.chatboxesview.openChat(this.model);
ev.preventDefault(); ev.preventDefault();
}, },
@ -1411,12 +1429,15 @@
return Backbone.Collection.prototype.get.call(this, id); return Backbone.Collection.prototype.get.call(this, id);
}, },
addRosterItem: function (jid, subscription, ask, name, options) { addRosterItem: function (jid, subscription, ask, name, img, img_type, options) {
var model = new xmppchat.RosterItem(jid, subscription, ask, name); var model = new xmppchat.RosterItem(jid, subscription, ask, name, img, img_type);
model.options = options || {}; model.options = options || {};
this.add(model); this.add(model);
}, },
getRosterItem: function (jid) {
},
addResource: function (bare_jid, resource) { addResource: function (bare_jid, resource) {
var item = this.getItem(bare_jid), var item = this.getItem(bare_jid),
resources; resources;
@ -1479,7 +1500,7 @@
rosterHandler: function (items) { rosterHandler: function (items) {
var model, item, i, items_length = items.length, var model, item, i, items_length = items.length,
last_item = items[items_length - 1], last_item = items[items_length - 1],
options = {}; options = {}, vcard, img_src;
for (i=0; i<items_length; i+=1) { for (i=0; i<items_length; i+=1) {
item = items[i]; item = items[i];
model = this.getItem(item.jid); model = this.getItem(item.jid);
@ -1487,7 +1508,13 @@
if (item === last_item) { if (item === last_item) {
options.isLast = true; options.isLast = true;
} }
this.addRosterItem(item.jid, item.subscription, item.ask, item.name, options); xmppchat.connection.vcard.get($.proxy(function (iq) {
$vcard = $(iq).find('vCard');
var fullname = $vcard.find('FN').text();
var img = $vcard.find('BINVAL').text();
var img_type = $vcard.find('TYPE').text();
this.addRosterItem(item.jid, item.subscription, item.ask, fullname, img, img_type, options);
}, this), item.jid)
} else { } else {
// only modify model attributes if they are different from the // only modify model attributes if they are different from the
// ones that were already set when the rosterItem was added // ones that were already set when the rosterItem was added
@ -1556,9 +1583,13 @@
if ((item) && (item.get('subscription') != 'none')) { if ((item) && (item.get('subscription') != 'none')) {
xmppchat.connection.roster.authorize(bare_jid); xmppchat.connection.roster.authorize(bare_jid);
} else { } else {
$.getJSON(portal_url + "/xmpp-userinfo?user_id=" + Strophe.getNodeFromJid(jid), $.proxy(function (data) { xmppchat.connection.vcard.get($.proxy(function (iq) {
this.addRosterItem(bare_jid, 'none', 'request', data.fullname); $vcard = $(iq).find('vCard');
}, this)); var fullname = $vcard.find('BINVAL').text();
var img = $vcard.find('BINVAL').text();
var img_type = $vcard.find('TYPE').text();
this.addRosterItem(bare_jid, 'none', 'request', fullname, img, img_type, options);
}, this), jid)
} }
} }
@ -1897,8 +1928,8 @@
$(document).unbind('jarnxmpp.connected'); $(document).unbind('jarnxmpp.connected');
$(document).bind('jarnxmpp.connected', $.proxy(function (ev, connection) { $(document).bind('jarnxmpp.connected', $.proxy(function (ev, connection) {
this.connection = connection; this.connection = connection;
// this.connection.xmlInput = function (body) { console.log(body); }; this.connection.xmlInput = function (body) { console.log(body); };
// this.connection.xmlOutput = function (body) { console.log(body); }; this.connection.xmlOutput = function (body) { console.log(body); };
this.connection.bare_jid = Strophe.getBareJidFromJid(this.connection.jid); this.connection.bare_jid = Strophe.getBareJidFromJid(this.connection.jid);
this.connection.domain = Strophe.getDomainFromJid(this.connection.jid); this.connection.domain = Strophe.getDomainFromJid(this.connection.jid);
this.connection.muc_domain = 'conference.' + this.connection.domain; this.connection.muc_domain = 'conference.' + this.connection.domain;