Remove duplicates uid in the user list

pull/1/head
yflory 8 years ago
parent cfc5d634f4
commit f34492be94

@ -242,20 +242,26 @@ define([
return $userlist[0]; return $userlist[0];
}; };
// TODO deduplicate users by uid
var getOtherUsers = function(myUserName, userList, userData) { var getOtherUsers = function(myUserName, userList, userData) {
var i = 0; var i = 0; // duplicates counter
var list = []; var list = [];
userList.forEach(function(user) { var myUid = userData[myUserName] ? userData[myUserName].uid : undefined;
if(user !== myUserName) { var uids = [];
var data = (userData) ? (userData[user] || null) : null; userList.forEach(function(user) {
var userName = (data) ? data.name : null; if(user !== myUserName) {
if(userName) { var data = (userData) ? (userData[user] || null) : null;
list.push(userName); var userName = (data) ? data.name : null;
} var userId = (data) ? data.uid : null;
} if (userName && uids.indexOf(userId) === -1 && (!myUid || userId !== myUid)) {
}); uids.push(userId);
return list; list.push(userName);
} else { i++; }
}
});
return {
list: list,
duplicates: i
};
}; };
var arrayIntersect = function(a, b) { var arrayIntersect = function(a, b) {
@ -293,11 +299,13 @@ define([
// may contain data about users that have already left the channel. // may contain data about users that have already left the channel.
userList = readOnly === -1 ? userList : arrayIntersect(userList, Object.keys(userData)); userList = readOnly === -1 ? userList : arrayIntersect(userList, Object.keys(userData));
var numberOfEditUsers = userList.length;
var numberOfViewUsers = numberOfUsers - numberOfEditUsers;
// Names of editing users // Names of editing users
var editUsersNames = getOtherUsers(myUserName, userList, userData); var others = getOtherUsers(myUserName, userList, userData);
var editUsersNames = others.list;
var duplicates = others.duplicates;
var numberOfEditUsers = userList.length - duplicates;
var numberOfViewUsers = numberOfUsers - numberOfEditUsers - duplicates;
// Number of anonymous editing users // Number of anonymous editing users
var anonymous = numberOfEditUsers - editUsersNames.length; var anonymous = numberOfEditUsers - editUsersNames.length;
@ -307,21 +315,19 @@ define([
var $editUsers = $userButtons.find('.' + USERLIST_CLS); var $editUsers = $userButtons.find('.' + USERLIST_CLS);
$editUsers.html('').append($usersTitle); $editUsers.html('').append($usersTitle);
var editUsersList = '';
var $editUsersList = $('<pre>'); var $editUsersList = $('<pre>');
if (readOnly !== 1) { if (readOnly !== 1) { // Yourself - edit
$editUsers.append('<span class="yourself">' + Messages.yourself + '</span>'); $editUsers.append('<span class="yourself">' + Messages.yourself + '</span>');
anonymous--; anonymous--;
} }
if (editUsersNames.length > 0) { // Editors
$editUsersList.text(editUsersNames.join('\n')); // .text() to avoid XSS $editUsersList.text(editUsersNames.join('\n')); // .text() to avoid XSS
$editUsers.append($editUsersList); $editUsers.append($editUsersList);
} if (anonymous > 0) { // Anonymous editors
if (anonymous > 0) {
var text = anonymous === 1 ? Messages.anonymousUser : Messages.anonymousUsers; var text = anonymous === 1 ? Messages.anonymousUser : Messages.anonymousUsers;
$editUsers.append('<span class="anonymous">' + anonymous + ' ' + text + '</span>'); $editUsers.append('<span class="anonymous">' + anonymous + ' ' + text + '</span>');
} }
if (numberOfViewUsers > 0) { if (numberOfViewUsers > 0) { // Viewers
var viewText = '<span class="viewer">'; var viewText = '<span class="viewer">';
if (numberOfEditUsers > 0) { if (numberOfEditUsers > 0) {
$editUsers.append('<br>'); $editUsers.append('<br>');

Loading…
Cancel
Save