Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
Fix #208 nick assignment
Browse files Browse the repository at this point in the history
Thanks @PaulSD for the PR. I made minor changes to the commit,
hope you don't mind :)
  • Loading branch information
mweibel committed Jan 13, 2014
1 parent 042a6f2 commit 162c882
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
28 changes: 17 additions & 11 deletions candy.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2351,31 +2351,36 @@ Candy.Core.Event = (function(self, Strophe, $) {
roomJid = Strophe.getBareJidFromJid(from),
presenceType = msg.attr('type'),
status = msg.find('status'),
nickAssign = false,
nickChange = false;

if(status.length) {
// check if status code indicates a nick change
// check if status code indicates a nick assignment or nick change
for(var i = 0, l = status.length; i < l; i++) {
var $status = $(status[i]);
if($status.attr('code') === '303') {
var $status = $(status[i]),
code = $status.attr('code');
if(code === '303') {
nickChange = true;
} else if(code === '210') {
nickAssign = true;
}
}
}

// Current User left a room
if(Strophe.getResourceFromJid(from) === Candy.Core.getUser().getNick() && presenceType === 'unavailable' && nickChange === false) {
self.Jabber.Room.Leave(msg);
return true;
}

// Current User joined a room
var room = Candy.Core.getRoom(roomJid);
if(!room) {
Candy.Core.getRooms()[roomJid] = new Candy.Core.ChatRoom(roomJid);
room = Candy.Core.getRoom(roomJid);
}

// Current User left a room
var currentUser = room.getUser() ? room.getUser() : Candy.Core.getUser();
if(Strophe.getResourceFromJid(from) === currentUser.getNick() && presenceType === 'unavailable' && nickChange === false) {
self.Jabber.Room.Leave(msg);
return true;
}

var roster = room.getRoster(),
action, user,
nick,
Expand All @@ -2389,8 +2394,9 @@ Candy.Core.Event = (function(self, Strophe, $) {
nick = Strophe.getResourceFromJid(from);
user = new Candy.Core.ChatUser(from, nick, item.attr('affiliation'), item.attr('role'));
// Room existed but client (myself) is not yet registered
if(room.getUser() === null && Candy.Core.getUser().getNick() === nick) {
if(room.getUser() === null && (Candy.Core.getUser().getNick() === nick || nickAssign)) {
room.setUser(user);
currentUser = user;
}
roster.add(user);
action = 'join';
Expand Down Expand Up @@ -2432,7 +2438,7 @@ Candy.Core.Event = (function(self, Strophe, $) {
'roomName': room.getName(),
'user': user,
'action': action,
'currentUser': Candy.Core.getUser()
'currentUser': currentUser
});
return true;
},
Expand Down
Loading

0 comments on commit 162c882

Please sign in to comment.