Skip to content

Commit

Permalink
cache fixes
Browse files Browse the repository at this point in the history
on newRoom and deleteRooms clear cache
add some checks for empty groups list
  • Loading branch information
barisusakli committed Jul 12, 2023
1 parent 18a1663 commit 547bde8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
25 changes: 13 additions & 12 deletions public/src/client/chats/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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;
},
},
},
Expand All @@ -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;
});
3 changes: 3 additions & 0 deletions src/api/chats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 8 additions & 1 deletion src/messaging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 18 additions & 5 deletions src/messaging/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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',
]);
};

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 547bde8

Please sign in to comment.