mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 19:21:04 +01:00
cache fixes
on newRoom and deleteRooms clear cache add some checks for empty groups list
This commit is contained in:
@@ -26,7 +26,7 @@ define('forum/chats/create', [
|
|||||||
save: {
|
save: {
|
||||||
label: '[[global:create]]',
|
label: '[[global:create]]',
|
||||||
className: 'btn-primary',
|
className: 'btn-primary',
|
||||||
callback: async function () {
|
callback: function () {
|
||||||
const roomName = modal.find('[component="chat/room/name"]').val();
|
const roomName = modal.find('[component="chat/room/name"]').val();
|
||||||
const uids = modal.find('[component="chat/room/users"] [component="chat/user"]').find('[data-uid]').map(
|
const uids = modal.find('[component="chat/room/users"] [component="chat/user"]').find('[data-uid]').map(
|
||||||
(i, el) => $(el).attr('data-uid')
|
(i, el) => $(el).attr('data-uid')
|
||||||
@@ -38,16 +38,25 @@ define('forum/chats/create', [
|
|||||||
alerts.error('[[error:no-users-selected]]');
|
alerts.error('[[error:no-users-selected]]');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (type === 'public' && !groups) {
|
if (type === 'public' && !groups.length) {
|
||||||
alerts.error('[[error:no-groups-selected]]');
|
alerts.error('[[error:no-groups-selected]]');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
await createRoom({
|
if (!app.user.uid) {
|
||||||
|
alerts.error('[[error:not-logged-in]]');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
api.post(`/chats`, {
|
||||||
roomName: roomName,
|
roomName: roomName,
|
||||||
uids: uids,
|
uids: uids,
|
||||||
type: type,
|
type: type,
|
||||||
groups: groups,
|
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;
|
return create;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ chatsAPI.create = async function (caller, data) {
|
|||||||
if (!isPublic && !data.uids.length) {
|
if (!isPublic && !data.uids.length) {
|
||||||
throw new Error('[[error:no-users-selected]]');
|
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)));
|
await Promise.all(data.uids.map(async uid => messaging.canMessageUser(caller.uid, uid)));
|
||||||
const roomId = await messaging.newRoom(caller.uid, data);
|
const roomId = await messaging.newRoom(caller.uid, data);
|
||||||
|
|
||||||
|
|||||||
@@ -125,8 +125,15 @@ Messaging.getPublicRooms = async (callerUid, uid) => {
|
|||||||
|
|
||||||
const allRoomIds = await Messaging.getPublicRoomIdsFromSet('chat:rooms:public:order');
|
const allRoomIds = await Messaging.getPublicRoomIdsFromSet('chat:rooms:public:order');
|
||||||
const allRoomData = await Messaging.getRoomsData(allRoomIds);
|
const allRoomData = await Messaging.getRoomsData(allRoomIds);
|
||||||
|
console.log(allRoomIds, allRoomData);
|
||||||
const checks = await Promise.all(
|
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 roomData = allRoomData.filter((room, idx) => room && checks[idx]);
|
||||||
const roomIds = roomData.map(r => r.roomId);
|
const roomIds = roomData.map(r => r.roomId);
|
||||||
|
|||||||
@@ -10,9 +10,10 @@ const groups = require('../groups');
|
|||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
const privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
const meta = require('../meta');
|
const meta = require('../meta');
|
||||||
|
const cache = require('../cache');
|
||||||
const cacheCreate = require('../cacheCreate');
|
const cacheCreate = require('../cacheCreate');
|
||||||
|
|
||||||
const cache = cacheCreate({
|
const roomUidCache = cacheCreate({
|
||||||
name: 'chat:room:uids',
|
name: 'chat:room:uids',
|
||||||
max: 500,
|
max: 500,
|
||||||
ttl: 0,
|
ttl: 0,
|
||||||
@@ -104,6 +105,11 @@ module.exports = function (Messaging) {
|
|||||||
Messaging.addRoomToUsers(roomId, [uid].concat(data.uids), now),
|
Messaging.addRoomToUsers(roomId, [uid].concat(data.uids), now),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
cache.del([
|
||||||
|
'chat:rooms:public:all',
|
||||||
|
'chat:rooms:public:order:all',
|
||||||
|
]);
|
||||||
|
|
||||||
if (!isPublic) {
|
if (!isPublic) {
|
||||||
// chat owner should also get the user-join system message
|
// chat owner should also get the user-join system message
|
||||||
await Messaging.addSystemMessage('user-join', uid, roomId);
|
await Messaging.addSystemMessage('user-join', uid, roomId);
|
||||||
@@ -136,6 +142,11 @@ module.exports = function (Messaging) {
|
|||||||
db.deleteAll(roomIds.map(id => `chat:room:${id}`)),
|
db.deleteAll(roomIds.map(id => `chat:room:${id}`)),
|
||||||
db.sortedSetRemove('chat:rooms', roomIds),
|
db.sortedSetRemove('chat:rooms', roomIds),
|
||||||
db.sortedSetRemove('chat:rooms:public', 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] }]),
|
...groupChats.map(id => [`chat:room:${id}`, { groupChat: 1, userCount: countMap[id] }]),
|
||||||
...privateChats.map(id => [`chat:room:${id}`, { groupChat: 0, 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) => {
|
Messaging.leaveRoom = async (uids, roomId) => {
|
||||||
@@ -301,12 +312,12 @@ module.exports = function (Messaging) {
|
|||||||
|
|
||||||
Messaging.getAllUidsInRoom = async function (roomId) {
|
Messaging.getAllUidsInRoom = async function (roomId) {
|
||||||
const cacheKey = `chat:room:${roomId}:users`;
|
const cacheKey = `chat:room:${roomId}:users`;
|
||||||
let uids = cache.get(cacheKey);
|
let uids = roomUidCache.get(cacheKey);
|
||||||
if (uids !== undefined) {
|
if (uids !== undefined) {
|
||||||
return uids;
|
return uids;
|
||||||
}
|
}
|
||||||
uids = await Messaging.getUidsInRoom(roomId, 0, -1);
|
uids = await Messaging.getUidsInRoom(roomId, 0, -1);
|
||||||
cache.set(cacheKey, uids);
|
roomUidCache.set(cacheKey, uids);
|
||||||
return uids;
|
return uids;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -373,7 +384,9 @@ module.exports = function (Messaging) {
|
|||||||
}
|
}
|
||||||
if (!room ||
|
if (!room ||
|
||||||
(!room.public && !inRoom) ||
|
(!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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user