only update user list if its open

isekai-main
Barış Soner Uşaklı 2 years ago
parent cfa00ece35
commit f0775651b9

@ -29,7 +29,6 @@ define('forum/chats', [
let newMessage = false; let newMessage = false;
let chatNavWrapper = null; let chatNavWrapper = null;
let userListEl = null;
$(window).on('action:ajaxify.start', function () { $(window).on('action:ajaxify.start', function () {
Chats.destroyAutoComplete(ajaxify.data.roomId); Chats.destroyAutoComplete(ajaxify.data.roomId);
@ -48,7 +47,6 @@ define('forum/chats', [
socket.emit('modules.chats.enterPublic', ajaxify.data.publicRooms.map(r => r.roomId)); socket.emit('modules.chats.enterPublic', ajaxify.data.publicRooms.map(r => r.roomId));
const env = utils.findBootstrapEnvironment(); const env = utils.findBootstrapEnvironment();
chatNavWrapper = $('[component="chat/nav-wrapper"]'); chatNavWrapper = $('[component="chat/nav-wrapper"]');
userListEl = $('[component="chat/user/list"]');
if (!Chats.initialised) { if (!Chats.initialised) {
Chats.addSocketListeners(); Chats.addSocketListeners();
Chats.addGlobalEventListeners(); Chats.addGlobalEventListeners();
@ -469,7 +467,6 @@ define('forum/chats', [
const mainWrapper = components.get('chat/main-wrapper'); const mainWrapper = components.get('chat/main-wrapper');
mainWrapper.html(html); mainWrapper.html(html);
chatNavWrapper = $('[component="chat/nav-wrapper"]'); chatNavWrapper = $('[component="chat/nav-wrapper"]');
userListEl = $('[component="chat/user/list"]');
html.find('.timeago').timeago(); html.find('.timeago').timeago();
ajaxify.data = { ...ajaxify.data, ...payload, roomId: roomId }; ajaxify.data = { ...ajaxify.data, ...payload, roomId: roomId };
ajaxify.updateTitle(ajaxify.data.title); ajaxify.updateTitle(ajaxify.data.title);
@ -528,10 +525,6 @@ define('forum/chats', [
Chats.increasePublicRoomUnreadCount(chatNavWrapper.find('[data-roomid=' + data.roomId + ']')); Chats.increasePublicRoomUnreadCount(chatNavWrapper.find('[data-roomid=' + data.roomId + ']'));
}); });
socket.on('event:chats.user-online', function (data) {
userListEl.find(`[data-uid="${data.uid}"]`).toggleClass('online', !!data.state);
});
socket.on('event:user_status_change', function (data) { socket.on('event:user_status_change', function (data) {
app.updateUserStatus($('.chats-list [data-uid="' + data.uid + '"] [component="user/status"]'), data.status); app.updateUserStatus($('.chats-list [data-uid="' + data.uid + '"] [component="user/status"]'), data.status);
}); });

@ -4,6 +4,8 @@
define('forum/chats/user-list', ['api'], function (api) { define('forum/chats/user-list', ['api'], function (api) {
const userList = {}; const userList = {};
let updateInterval = 0;
userList.init = function (roomId, container) { userList.init = function (roomId, container) {
const userListEl = container.find('[component="chat/user/list"]'); const userListEl = container.find('[component="chat/user/list"]');
if (!userListEl.length) { if (!userListEl.length) {
@ -11,13 +13,41 @@ define('forum/chats/user-list', ['api'], function (api) {
} }
container.find('[component="chat/user/list/btn"]').on('click', () => { container.find('[component="chat/user/list/btn"]').on('click', () => {
userListEl.toggleClass('hidden'); userListEl.toggleClass('hidden');
if (userListEl.hasClass('hidden')) {
stopUpdating();
} else {
startUpdating(roomId, userListEl);
}
}); });
$(window).off('action:ajaxify.start', stopUpdating)
.one('action:ajaxify.start', stopUpdating);
userList.addInfiniteScrollHandler(roomId, userListEl, async (listEl, data) => { userList.addInfiniteScrollHandler(roomId, userListEl, async (listEl, data) => {
listEl.append(await app.parseAndTranslate('partials/chats/user-list', 'users', data)); listEl.append(await app.parseAndTranslate('partials/chats/user-list', 'users', data));
}); });
}; };
function startUpdating(roomId, userListEl) {
updateInterval = setInterval(() => {
updateUserList(roomId, userListEl);
}, 5000);
}
function stopUpdating() {
if (updateInterval) {
clearInterval(updateInterval);
updateInterval = 0;
}
}
async function updateUserList(roomId, userListEl) {
if (ajaxify.data.template.chats && app.isFocused && userListEl.scrollTop() === 0 && !userListEl.hasClass('hidden')) {
const data = await api.get(`/chats/${roomId}/users`, { start: 0 });
userListEl.html(await app.parseAndTranslate('partials/chats/user-list', 'users', data));
}
}
userList.addInfiniteScrollHandler = function (roomId, listEl, callback) { userList.addInfiniteScrollHandler = function (roomId, listEl, callback) {
listEl.on('scroll', utils.debounce(async () => { listEl.on('scroll', utils.debounce(async () => {
const bottom = (listEl[0].scrollHeight - listEl.height()) * 0.85; const bottom = (listEl[0].scrollHeight - listEl.height()) * 0.85;

@ -168,18 +168,21 @@ chatsAPI.mark = async (caller, data) => {
chatsAPI.users = async (caller, data) => { chatsAPI.users = async (caller, data) => {
const start = data.hasOwnProperty('start') ? data.start : 0; const start = data.hasOwnProperty('start') ? data.start : 0;
const stop = start + 39; const stop = start + 39;
const [isOwner, isUserInRoom, users] = await Promise.all([ const io = require('../socket.io');
const [isOwner, isUserInRoom, users, onlineUids] = await Promise.all([
messaging.isRoomOwner(caller.uid, data.roomId), messaging.isRoomOwner(caller.uid, data.roomId),
messaging.isUserInRoom(caller.uid, data.roomId), messaging.isUserInRoom(caller.uid, data.roomId),
messaging.getUsersInRoomFromSet( messaging.getUsersInRoomFromSet(
`chat:room:${data.roomId}:uids:online`, data.roomId, start, stop, true `chat:room:${data.roomId}:uids:online`, data.roomId, start, stop, true
), ),
io.getUidsInRoom(`chat_room_${data.roomId}`),
]); ]);
if (!isUserInRoom) { if (!isUserInRoom) {
throw new Error('[[error:no-privileges]]'); throw new Error('[[error:no-privileges]]');
} }
users.forEach((user) => { users.forEach((user) => {
user.canKick = isOwner && (parseInt(user.uid, 10) !== parseInt(caller.uid, 10)); user.canKick = isOwner && (parseInt(user.uid, 10) !== parseInt(caller.uid, 10));
user.online = parseInt(user.uid, 10) === parseInt(caller.uid, 10) || onlineUids.includes(String(user.uid));
}); });
return { users }; return { users };
}; };

@ -97,10 +97,6 @@ function onConnection(socket) {
socket.on('disconnect', () => { socket.on('disconnect', () => {
onDisconnect(socket); onDisconnect(socket);
}); });
socket.on('disconnecting', () => {
onDisconnecting(socket);
});
} }
function onDisconnect(socket) { function onDisconnect(socket) {
@ -108,19 +104,6 @@ function onDisconnect(socket) {
plugins.hooks.fire('action:sockets.disconnect', { socket: socket }); plugins.hooks.fire('action:sockets.disconnect', { socket: socket });
} }
async function onDisconnecting(socket) {
if (socket.uid > 0) {
for (const roomName of socket.rooms) {
if (roomName.startsWith('chat_room') && !roomName.includes('public')) {
Sockets.server.in(roomName).emit('event:chats.user-online', {
uid: socket.uid,
state: 0,
});
}
}
}
}
async function onConnect(socket) { async function onConnect(socket) {
try { try {
await validateSession(socket, '[[error:invalid-session]]'); await validateSession(socket, '[[error:invalid-session]]');

@ -106,18 +106,12 @@ async function joinLeave(socket, roomIds, method, prefix = 'chat_room') {
Messaging.isUserInRoom(socket.uid, roomIds), Messaging.isUserInRoom(socket.uid, roomIds),
Messaging.getRoomsData(roomIds, ['public', 'groups']), Messaging.getRoomsData(roomIds, ['public', 'groups']),
]); ]);
const io = require('./index');
await Promise.all(roomIds.map(async (roomId, idx) => { await Promise.all(roomIds.map(async (roomId, idx) => {
const isPublic = roomData[idx] && roomData[idx].public; const isPublic = roomData[idx] && roomData[idx].public;
const roomGroups = roomData[idx] && roomData[idx].groups; const roomGroups = roomData[idx] && roomData[idx].groups;
if (isAdmin || (inRooms[idx] && (!isPublic || await groups.isMemberOfAny(socket.uid, roomGroups)))) { if (isAdmin || (inRooms[idx] && (!isPublic || await groups.isMemberOfAny(socket.uid, roomGroups)))) {
socket[method](`${prefix}_${roomId}`); socket[method](`${prefix}_${roomId}`);
if (prefix === 'chat_room') {
io.in(`chat_room_${roomId}`).emit('event:chats.user-online', {
uid: socket.uid,
state: method === 'join' ? 1 : 0,
});
}
} }
})); }));
} }

Loading…
Cancel
Save