Add fullname support and also handle special chars*

* Necessary for email as username support
This commit is contained in:
JC Brand 2012-09-13 14:19:19 +02:00
parent 358985c96e
commit 96fab21e0c

View File

@ -93,7 +93,8 @@ xmppchat.ChatBox = Backbone.Model.extend({
initialize: function () { initialize: function () {
this.set({ this.set({
'user_id' : Strophe.getNodeFromJid(this.get('jid')), 'user_id' : Strophe.getNodeFromJid(this.get('jid')),
'box_id' : this.hash(this.get('jid')) 'box_id' : this.hash(this.get('jid')),
'fullname' : this.get('fullname')
}); });
} }
@ -388,7 +389,7 @@ xmppchat.ChatBoxView = Backbone.View.extend({
template: _.template( template: _.template(
'<div class="chat-head chat-head-chatbox">' + '<div class="chat-head chat-head-chatbox">' +
'<div class="chat-title"> <%= user_id %> </div>' + '<div class="chat-title"> <%= fullname %> </div>' +
'<a href="javascript:void(0)" class="chatbox-button close-chatbox-button">X</a>' + '<a href="javascript:void(0)" class="chatbox-button close-chatbox-button">X</a>' +
'<p class="user-custom-message"><p/>' + '<p class="user-custom-message"><p/>' +
'</div>' + '</div>' +
@ -457,7 +458,7 @@ xmppchat.ContactsPanel = Backbone.View.extend({
.attr('id', 'found-users-'+obj.id) .attr('id', 'found-users-'+obj.id)
.append( .append(
$('<a class="subscribe-to-user" href="#" title="Click to add as a chat contact"></a>') $('<a class="subscribe-to-user" href="#" title="Click to add as a chat contact"></a>')
.attr('data-recipient', obj.id+'@'+xmppchat.connection.domain) .attr('data-recipient', Strophe.escapeNode(obj.id)+'@'+xmppchat.connection.domain)
.text(obj.fullname) .text(obj.fullname)
) )
); );
@ -467,10 +468,9 @@ xmppchat.ContactsPanel = Backbone.View.extend({
subscribeToContact: function (ev) { subscribeToContact: function (ev) {
ev.preventDefault(); ev.preventDefault();
// FIXME: Need to escape username var jid = $(ev.target).attr('data-recipient'),
var jid = $(ev.target).attr('data-recipient'); name = $(ev.target).text();
xmppchat.connection.roster.add(jid, '', [], function (iq) { xmppchat.connection.roster.add(jid, name, [], function (iq) {
// XXX: We can set the name here!!!
xmppchat.connection.roster.subscribe(jid); xmppchat.connection.roster.subscribe(jid);
}); });
$(ev.target).parent().remove(); $(ev.target).parent().remove();
@ -784,7 +784,8 @@ xmppchat.ChatBoxesView = Backbone.View.extend({
restoreOpenChats: function () { restoreOpenChats: function () {
var cookie = jQuery.cookie('chats-open-'+xmppchat.username), var cookie = jQuery.cookie('chats-open-'+xmppchat.username),
open_chats = []; open_chats = [],
that = this;
jQuery.cookie('chats-open-'+xmppchat.username, null, {path: '/'}); jQuery.cookie('chats-open-'+xmppchat.username, null, {path: '/'});
if (cookie) { if (cookie) {
@ -792,12 +793,14 @@ xmppchat.ChatBoxesView = Backbone.View.extend({
if (_.indexOf(open_chats, 'online-users-container') != -1) { if (_.indexOf(open_chats, 'online-users-container') != -1) {
this.renderChat('online-users-container'); this.renderChat('online-users-container');
} }
for (var i=0; i<open_chats.length; i++) { _.each(open_chats, function (jid) {
if (open_chats[i] === 'online-users-container') { if (jid != 'online-users-container') {
continue; // XXX: Can this be optimised somehow?
$.getJSON(portal_url + "/xmpp-userinfo?user_id=" + Strophe.getNodeFromJid(jid), function (data) {
that.renderChat(jid, data.fullname);
});
} }
this.renderChat(open_chats[i]); });
}
} }
}, },
@ -805,7 +808,7 @@ xmppchat.ChatBoxesView = Backbone.View.extend({
return Strophe.getDomainFromJid(jid) === xmppchat.connection.muc_domain; return Strophe.getDomainFromJid(jid) === xmppchat.connection.muc_domain;
}, },
renderChat: function (jid) { renderChat: function (jid, name) {
var box, view; var box, view;
if (jid === 'online-users-container') { if (jid === 'online-users-container') {
box = new xmppchat.ControlBox({'id': jid, 'jid': jid}); box = new xmppchat.ControlBox({'id': jid, 'jid': jid});
@ -819,7 +822,7 @@ xmppchat.ChatBoxesView = Backbone.View.extend({
'model': box 'model': box
}); });
} else { } else {
box = new xmppchat.ChatBox({'id': jid, 'jid': jid}); box = new xmppchat.ChatBox({'id': jid, 'jid': jid, 'fullname': name});
view = new xmppchat.ChatBoxView({ view = new xmppchat.ChatBoxView({
model: box model: box
}); });
@ -838,9 +841,9 @@ xmppchat.ChatBoxesView = Backbone.View.extend({
} }
}, },
openChat: function (jid) { openChat: function (jid, name) {
if (!this.model.get(jid)) { if (!this.model.get(jid)) {
this.renderChat(jid); this.renderChat(jid, name);
} else { } else {
this.showChat(jid); this.showChat(jid);
} }
@ -888,10 +891,11 @@ xmppchat.ChatBoxesView = Backbone.View.extend({
xmppchat.RosterItem = Backbone.Model.extend({ xmppchat.RosterItem = Backbone.Model.extend({
initialize: function (jid, subscription, ask) { initialize: function (jid, subscription, ask, name) {
// FIXME: the fullname is set to user_id for now...
var user_id = Strophe.getNodeFromJid(jid); var user_id = Strophe.getNodeFromJid(jid);
if (!name) {
name = user_id;
}
this.set({ this.set({
'id': jid, 'id': jid,
'jid': jid, 'jid': jid,
@ -899,7 +903,7 @@ xmppchat.RosterItem = Backbone.Model.extend({
'bare_jid': Strophe.getBareJidFromJid(jid), 'bare_jid': Strophe.getBareJidFromJid(jid),
'user_id': user_id, 'user_id': user_id,
'subscription': subscription, 'subscription': subscription,
'fullname': user_id, 'fullname': name,
'resources': [], 'resources': [],
'presence_type': 'offline', 'presence_type': 'offline',
'status': 'offline' 'status': 'offline'
@ -912,8 +916,7 @@ xmppchat.RosterItemView = Backbone.View.extend({
tagName: 'dd', tagName: 'dd',
openChat: function () { openChat: function () {
var jid = this.model.get('jid'); xmppchat.chatboxesview.openChat(this.model.get('jid'), this.model.get('fullname'));
xmppchat.chatboxesview.openChat(jid);
}, },
removeContact: function () { removeContact: function () {
@ -945,8 +948,11 @@ xmppchat.RosterItemView = Backbone.View.extend({
}, },
acceptRequest: function () { acceptRequest: function () {
xmppchat.connection.roster.authorize(this.model.get('jid')); var jid = this.model.get('jid');
xmppchat.connection.roster.subscribe(this.model.get('jid')); xmppchat.connection.roster.authorize(jid);
xmppchat.connection.roster.add(jid, this.model.get('fullname'), [], function (iq) {
xmppchat.connection.roster.subscribe(jid);
}, this);
}, },
declineRequest: function () { declineRequest: function () {
@ -1068,8 +1074,8 @@ xmppchat.Roster = (function (_, $, console) {
return Backbone.Collection.prototype.get.call(this, id); return Backbone.Collection.prototype.get.call(this, id);
}, },
addRosterItem: function (jid, subscription, ask) { addRosterItem: function (jid, subscription, ask, name) {
var model = new xmppchat.RosterItem(jid, subscription, ask); var model = new xmppchat.RosterItem(jid, subscription, ask, name);
this.add(model); this.add(model);
}, },
@ -1141,7 +1147,7 @@ xmppchat.Roster = (function (_, $, console) {
item = items[i]; item = items[i];
model = ob.getItem(item.jid); model = ob.getItem(item.jid);
if (!model) { if (!model) {
ob.addRosterItem(item.jid, item.subscription, item.ask); ob.addRosterItem(item.jid, item.subscription, item.ask, item.name);
} else { } else {
model.set({'subscription': item.subscription, 'ask': item.ask}); model.set({'subscription': item.subscription, 'ask': item.ask});
} }
@ -1175,7 +1181,9 @@ xmppchat.Roster = (function (_, $, console) {
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 {
ob.addRosterItem(bare_jid, 'none', 'request'); $.getJSON(portal_url + "/xmpp-userinfo?user_id=" + Strophe.getNodeFromJid(jid), function (data) {
ob.addRosterItem(bare_jid, 'none', 'request', data.fullname);
});
} }
} else if (presence_type === 'unsubscribed') { } else if (presence_type === 'unsubscribed') {
/* Upon receiving the presence stanza of type "unsubscribed", /* Upon receiving the presence stanza of type "unsubscribed",