From 547bde893905877faf938909a3edbe847d8f066b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 12 Jul 2023 16:35:46 -0400 Subject: [PATCH] cache fixes on newRoom and deleteRooms clear cache add some checks for empty groups list --- public/src/client/chats/create.js | 25 +++++++++++++------------ src/api/chats.js | 3 +++ src/messaging/index.js | 9 ++++++++- src/messaging/rooms.js | 23 ++++++++++++++++++----- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/public/src/client/chats/create.js b/public/src/client/chats/create.js index 9f532f020d..4c76248a16 100644 --- a/public/src/client/chats/create.js +++ b/public/src/client/chats/create.js @@ -26,7 +26,7 @@ define('forum/chats/create', [ save: { label: '[[global:create]]', className: 'btn-primary', - callback: async function () { + callback: function () { const roomName = modal.find('[component="chat/room/name"]').val(); const uids = modal.find('[component="chat/room/users"] [component="chat/user"]').find('[data-uid]').map( (i, el) => $(el).attr('data-uid') @@ -38,16 +38,25 @@ define('forum/chats/create', [ alerts.error('[[error:no-users-selected]]'); return false; } - if (type === 'public' && !groups) { + if (type === 'public' && !groups.length) { alerts.error('[[error:no-groups-selected]]'); return false; } - await createRoom({ + if (!app.user.uid) { + alerts.error('[[error:not-logged-in]]'); + return false; + } + + api.post(`/chats`, { roomName: roomName, uids: uids, type: type, groups: groups, - }); + }).then(({ roomId }) => { + ajaxify.go('chats/' + roomId); + modal.modal('hide'); + }).catch(alerts.error); + return false; }, }, }, @@ -73,13 +82,5 @@ define('forum/chats/create', [ }); } - async function createRoom(params) { - if (!app.user.uid) { - return alerts.error('[[error:not-logged-in]]'); - } - const { roomId } = await api.post(`/chats`, params); - ajaxify.go('chats/' + roomId); - } - return create; }); diff --git a/src/api/chats.js b/src/api/chats.js index 835117e235..a2d2e3467f 100644 --- a/src/api/chats.js +++ b/src/api/chats.js @@ -52,6 +52,9 @@ chatsAPI.create = async function (caller, data) { if (!isPublic && !data.uids.length) { throw new Error('[[error:no-users-selected]]'); } + if (isPublic && (!Array.isArray(data.groups) || !data.groups.length)) { + throw new Error('[[error:no-groups-selected]]'); + } await Promise.all(data.uids.map(async uid => messaging.canMessageUser(caller.uid, uid))); const roomId = await messaging.newRoom(caller.uid, data); diff --git a/src/messaging/index.js b/src/messaging/index.js index faede05c4e..fb87dd07a8 100644 --- a/src/messaging/index.js +++ b/src/messaging/index.js @@ -125,8 +125,15 @@ Messaging.getPublicRooms = async (callerUid, uid) => { const allRoomIds = await Messaging.getPublicRoomIdsFromSet('chat:rooms:public:order'); const allRoomData = await Messaging.getRoomsData(allRoomIds); + console.log(allRoomIds, allRoomData); const checks = await Promise.all( - allRoomData.map(room => groups.isMemberOfAny(uid, room && room.groups)) + allRoomData.map( + room => room && ( + !Array.isArray(room.groups) || + !room.groups.length || + groups.isMemberOfAny(uid, room && room.groups) + ) + ) ); const roomData = allRoomData.filter((room, idx) => room && checks[idx]); const roomIds = roomData.map(r => r.roomId); diff --git a/src/messaging/rooms.js b/src/messaging/rooms.js index 54edfe3150..93f1635f25 100644 --- a/src/messaging/rooms.js +++ b/src/messaging/rooms.js @@ -10,9 +10,10 @@ const groups = require('../groups'); const plugins = require('../plugins'); const privileges = require('../privileges'); const meta = require('../meta'); +const cache = require('../cache'); const cacheCreate = require('../cacheCreate'); -const cache = cacheCreate({ +const roomUidCache = cacheCreate({ name: 'chat:room:uids', max: 500, ttl: 0, @@ -104,6 +105,11 @@ module.exports = function (Messaging) { Messaging.addRoomToUsers(roomId, [uid].concat(data.uids), now), ]); + cache.del([ + 'chat:rooms:public:all', + 'chat:rooms:public:order:all', + ]); + if (!isPublic) { // chat owner should also get the user-join system message await Messaging.addSystemMessage('user-join', uid, roomId); @@ -136,6 +142,11 @@ module.exports = function (Messaging) { db.deleteAll(roomIds.map(id => `chat:room:${id}`)), db.sortedSetRemove('chat:rooms', roomIds), db.sortedSetRemove('chat:rooms:public', roomIds), + db.sortedSetRemove('chat:rooms:public:order', roomIds), + ]); + cache.del([ + 'chat:rooms:public:all', + 'chat:rooms:public:order:all', ]); }; @@ -252,7 +263,7 @@ module.exports = function (Messaging) { ...groupChats.map(id => [`chat:room:${id}`, { groupChat: 1, userCount: countMap[id] }]), ...privateChats.map(id => [`chat:room:${id}`, { groupChat: 0, userCount: countMap[id] }]), ]); - cache.del(roomIds.map(id => `chat:room:${id}:users`)); + roomUidCache.del(roomIds.map(id => `chat:room:${id}:users`)); } Messaging.leaveRoom = async (uids, roomId) => { @@ -301,12 +312,12 @@ module.exports = function (Messaging) { Messaging.getAllUidsInRoom = async function (roomId) { const cacheKey = `chat:room:${roomId}:users`; - let uids = cache.get(cacheKey); + let uids = roomUidCache.get(cacheKey); if (uids !== undefined) { return uids; } uids = await Messaging.getUidsInRoom(roomId, 0, -1); - cache.set(cacheKey, uids); + roomUidCache.set(cacheKey, uids); return uids; }; @@ -373,7 +384,9 @@ module.exports = function (Messaging) { } if (!room || (!room.public && !inRoom) || - (room.public && !(await groups.isMemberOfAny(uid, room.groups))) + (room.public && ( + Array.isArray(room.groups) && room.groups.length && !(await groups.isMemberOfAny(uid, room.groups))) + ) ) { return null; }