mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 03:55:55 +01:00
optimizing browsing users
This commit is contained in:
@@ -89,6 +89,7 @@ var socket,
|
||||
socket.on('event:connect', function (data) {
|
||||
app.username = data.username;
|
||||
app.userslug = data.userslug;
|
||||
app.picture = data.picture;
|
||||
app.uid = data.uid;
|
||||
app.isAdmin = data.isAdmin;
|
||||
|
||||
@@ -142,8 +143,6 @@ var socket,
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
app.enterRoom('global');
|
||||
|
||||
app.cacheBuster = config['cache-buster'];
|
||||
|
||||
bootbox.setDefaults({
|
||||
@@ -205,8 +204,11 @@ var socket,
|
||||
}
|
||||
|
||||
socket.emit('meta.rooms.enter', {
|
||||
'enter': room,
|
||||
'leave': app.currentRoom
|
||||
enter: room,
|
||||
leave: app.currentRoom,
|
||||
username: app.username,
|
||||
userslug: app.userslug,
|
||||
picture: app.picture
|
||||
});
|
||||
|
||||
app.currentRoom = room;
|
||||
|
||||
@@ -10,75 +10,35 @@ define('forum/topic/browsing', function() {
|
||||
|
||||
Browsing.onUpdateUsersInRoom = function(data) {
|
||||
if(data && data.room.indexOf('topic_' + ajaxify.variables.get('topic_id')) !== -1) {
|
||||
var activeEl = $('.thread_active_users');
|
||||
|
||||
// remove users that are no longer here
|
||||
activeEl.find('a').each(function(index, element) {
|
||||
if(element) {
|
||||
var uid = $(element).attr('data-uid');
|
||||
var absent = data.users.every(function(user) {
|
||||
return parseInt(user.uid, 10) !== parseInt(uid, 10);
|
||||
});
|
||||
|
||||
if (absent) {
|
||||
$(element).parent().remove();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var i=0, icon;
|
||||
// add self
|
||||
for(i = 0; i<data.users.length; ++i) {
|
||||
if(parseInt(data.users[i].uid, 10) === parseInt(app.uid, 10)) {
|
||||
icon = createUserIcon(data.users[i].uid, data.users[i].picture, data.users[i].userslug, data.users[i].username);
|
||||
activeEl.prepend(icon);
|
||||
data.users.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
for(var i=0; i<data.users.length; ++i) {
|
||||
addUserIcon(data.users[i]);
|
||||
}
|
||||
// add other users
|
||||
for(i=0; i<data.users.length; ++i) {
|
||||
icon = createUserIcon(data.users[i].uid, data.users[i].picture, data.users[i].userslug, data.users[i].username);
|
||||
activeEl.append(icon);
|
||||
if(activeEl.children().length > 8) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
activeEl.find('a[data-uid] img').tooltip({
|
||||
placement: 'top'
|
||||
});
|
||||
|
||||
var remainingUsers = data.users.length - 9;
|
||||
remainingUsers = remainingUsers < 0 ? 0 : remainingUsers;
|
||||
var anonymousCount = parseInt(data.anonymousCount, 10);
|
||||
activeEl.find('.anonymous-box').remove();
|
||||
if(anonymousCount || remainingUsers) {
|
||||
|
||||
var anonLink = $('<div class="anonymous-box inline-block"><i class="fa fa-user"></i></div>');
|
||||
activeEl.append(anonLink);
|
||||
|
||||
var title = '';
|
||||
if(remainingUsers && anonymousCount) {
|
||||
title = '[[topic:more_users_and_guests, ' + remainingUsers + ', ' + anonymousCount + ']]';
|
||||
} else if(remainingUsers) {
|
||||
title = '[[topic:more_users, ' + remainingUsers + ']]';
|
||||
} else {
|
||||
title = '[[topic:more_guests, ' + anonymousCount + ']]';
|
||||
}
|
||||
|
||||
translator.translate(title, function(translated) {
|
||||
$('.anonymous-box').tooltip({
|
||||
placement: 'top',
|
||||
title: translated
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getReplyingUsers();
|
||||
}
|
||||
};
|
||||
|
||||
Browsing.onUserEnter = function(data) {
|
||||
var activeEl = $('.thread_active_users');
|
||||
var user = activeEl.find('a[data-uid="' + data.uid + '"]');
|
||||
if (!user.length) {
|
||||
addUserIcon(data);
|
||||
} else {
|
||||
user.attr('data-count', parseInt(user.attr('data-count'), 10) + 1);
|
||||
}
|
||||
};
|
||||
|
||||
Browsing.onUserLeave = function(uid) {
|
||||
var activeEl = $('.thread_active_users');
|
||||
var user = activeEl.find('a[data-uid="' + uid + '"]');
|
||||
if (user.length) {
|
||||
var count = Math.max(0, parseInt(user.attr('data-count'), 10) - 1);
|
||||
user.attr('data-count', count);
|
||||
if (count <= 0) {
|
||||
user.remove();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Browsing.onUserOnline = function(err, data) {
|
||||
updateOnlineIcon($('.username-field[data-username="' + data.username + '"]'), data);
|
||||
|
||||
@@ -100,17 +60,26 @@ define('forum/topic/browsing', function() {
|
||||
if (user.length && !data.online) {
|
||||
user.parent().remove();
|
||||
} else if(!user.length && data.online && data.rooms.indexOf('topic_' + ajaxify.variables.get('topic_id')) !== -1) {
|
||||
user = createUserIcon(data.uid, data.picture, data.userslug, data.username);
|
||||
activeEl.append(user);
|
||||
activeEl.find('a[data-uid] img').tooltip({
|
||||
placement: 'top'
|
||||
});
|
||||
addUserIcon(user);
|
||||
}
|
||||
}
|
||||
|
||||
function addUserIcon(user) {
|
||||
if (!user.userslug) {
|
||||
return;
|
||||
}
|
||||
var activeEl = $('.thread_active_users');
|
||||
var userEl = createUserIcon(user.uid, user.picture, user.userslug, user.username);
|
||||
activeEl.append(userEl);
|
||||
activeEl.find('a[data-uid] img').tooltip({
|
||||
placement: 'top'
|
||||
});
|
||||
}
|
||||
|
||||
function createUserIcon(uid, picture, userslug, username) {
|
||||
if(!$('.thread_active_users').find('[data-uid="' + uid + '"]').length) {
|
||||
return $('<div class="inline-block"><a data-uid="' + uid + '" href="' + config.relative_path + '/user/' + userslug + '"><img title="' + username + '" src="'+ picture +'"/></a></div>');
|
||||
console.log('are u fucking kidming', arguments);
|
||||
return $('<div class="inline-block"><a data-uid="' + uid + '" data-count="1" href="' + config.relative_path + '/user/' + userslug + '"><img title="' + username + '" src="'+ picture +'"/></a></div>');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ define('forum/topic/events', ['forum/topic/browsing', 'forum/topic/postTools', '
|
||||
|
||||
var events = {
|
||||
'event:update_users_in_room': browsing.onUpdateUsersInRoom,
|
||||
'event:user_enter': browsing.onUserEnter,
|
||||
'event:user_leave': browsing.onUserLeave,
|
||||
'user.isOnline': browsing.onUserOnline,
|
||||
'event:voted': updatePostVotesAndUserReputation,
|
||||
'event:favourited': updateFavouriteCount,
|
||||
|
||||
@@ -162,16 +162,20 @@ Sockets.init = function(server) {
|
||||
|
||||
async.parallel({
|
||||
user: function(next) {
|
||||
user.getUserFields(uid, ['username', 'userslug'], next);
|
||||
user.getUserFields(uid, ['username', 'userslug', 'picture'], next);
|
||||
},
|
||||
isAdmin: function(next) {
|
||||
user.isAdministrator(uid, next);
|
||||
}
|
||||
}, function(err, userData) {
|
||||
if (err || !userData.user) {
|
||||
return;
|
||||
}
|
||||
socket.emit('event:connect', {
|
||||
status: 1,
|
||||
username: userData.user ? userData.user.username : 'guest',
|
||||
userslug: userData.user ? userData.user.userslug : '',
|
||||
username: userData.user.username,
|
||||
userslug: userData.user.userslug,
|
||||
picture: userData.user.picture,
|
||||
isAdmin: userData.isAdmin,
|
||||
uid: uid
|
||||
});
|
||||
@@ -208,7 +212,7 @@ Sockets.init = function(server) {
|
||||
|
||||
for(var roomName in io.sockets.manager.roomClients[socket.id]) {
|
||||
if (roomName.indexOf('topic') !== -1) {
|
||||
updateRoomBrowsingText(roomName.slice(1));
|
||||
io.sockets.in(roomName.slice(1)).emit('event:user_leave', socket.uid);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -368,15 +372,17 @@ Sockets.isUsersOnline = function(uids, callback) {
|
||||
};
|
||||
|
||||
Sockets.updateRoomBrowsingText = updateRoomBrowsingText;
|
||||
function updateRoomBrowsingText(roomName) {
|
||||
function updateRoomBrowsingText(roomName, selfUid) {
|
||||
|
||||
if (!roomName) {
|
||||
return;
|
||||
}
|
||||
|
||||
var uids = Sockets.getUidsInRoom(roomName),
|
||||
anonymousCount = Sockets.getAnonCountInRoom(roomName);
|
||||
|
||||
var uids = Sockets.getUidsInRoom(roomName);
|
||||
uids = uids.slice(0, 9);
|
||||
if (selfUid) {
|
||||
uids = [selfUid].concat(uids);
|
||||
}
|
||||
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], function(err, users) {
|
||||
if(!err) {
|
||||
users = users.filter(function(user) {
|
||||
@@ -385,7 +391,6 @@ function updateRoomBrowsingText(roomName) {
|
||||
|
||||
io.sockets.in(roomName).emit('event:update_users_in_room', {
|
||||
users: users,
|
||||
anonymousCount: anonymousCount,
|
||||
room: roomName
|
||||
});
|
||||
}
|
||||
|
||||
@@ -62,24 +62,23 @@ SocketMeta.getUsageStats = function(socket, data, callback) {
|
||||
/* Rooms */
|
||||
|
||||
SocketMeta.rooms.enter = function(socket, data, callback) {
|
||||
if(!data) {
|
||||
if (!data) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
if (data.leave) {
|
||||
socket.leave(data.leave);
|
||||
if (socket.uid && data.leave.indexOf('topic') !== -1) {
|
||||
websockets.in(data.leave).emit('event:user_leave', socket.uid);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.enter) {
|
||||
socket.join(data.enter);
|
||||
}
|
||||
|
||||
if (data.leave && data.leave !== data.enter && data.leave.indexOf('topic') !== -1) {
|
||||
module.parent.exports.updateRoomBrowsingText(data.leave);
|
||||
}
|
||||
|
||||
if (data.enter.indexOf('topic') !== -1) {
|
||||
module.parent.exports.updateRoomBrowsingText(data.enter);
|
||||
if (socket.uid && data.enter.indexOf('topic') !== -1) {
|
||||
data.uid = socket.uid;
|
||||
websockets.in(data.enter).emit('event:user_enter', data);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ SocketTopics.enter = function(socket, tid, callback) {
|
||||
SocketTopics.markAsRead(socket, tid);
|
||||
topics.markTopicNotificationsRead(tid, socket.uid);
|
||||
topics.increaseViewCount(tid);
|
||||
websockets.updateRoomBrowsingText('topic_' + tid);
|
||||
};
|
||||
|
||||
SocketTopics.postcount = function(socket, tid, callback) {
|
||||
|
||||
Reference in New Issue
Block a user