refactor: remove knonwOwner param

isekai-main
Barış Soner Uşaklı 3 years ago
parent 8fb89d761a
commit 42781467d2

@ -69,12 +69,19 @@ module.exports = function (Messaging) {
Messaging.getUserCountInRoom = async roomId => db.sortedSetCard(`chat:room:${roomId}:uids`); Messaging.getUserCountInRoom = async roomId => db.sortedSetCard(`chat:room:${roomId}:uids`);
Messaging.isRoomOwner = async (uid, roomId, knownOwner) => { Messaging.isRoomOwner = async (uids, roomId) => {
const owner = (knownOwner == null) ? await db.getObjectField(`chat:room:${roomId}`, 'owner') : knownOwner; const isArray = Array.isArray(uids);
const isOwner = parseInt(uid, 10) === parseInt(owner, 10); if (!isArray) {
uids = [uids];
}
const owner = await db.getObjectField(`chat:room:${roomId}`, 'owner');
const isOwners = uids.map(uid => parseInt(uid, 10) === parseInt(owner, 10));
const payload = await plugins.hooks.fire('filter:messaging.isRoomOwner', { uid, roomId, owner, isOwner }); const result = await Promise.all(isOwners.map(async (isOwner, index) => {
const payload = await plugins.hooks.fire('filter:messaging.isRoomOwner', { uid: uids[index], roomId, owner, isOwner });
return payload.isOwner; return payload.isOwner;
}));
return isArray ? result : result[0];
}; };
Messaging.addUsersToRoom = async function (uid, uids, roomId) { Messaging.addUsersToRoom = async function (uid, uids, roomId) {
@ -171,15 +178,15 @@ module.exports = function (Messaging) {
Messaging.getUsersInRoom = async (roomId, start, stop) => { Messaging.getUsersInRoom = async (roomId, start, stop) => {
const uids = await Messaging.getUidsInRoom(roomId, start, stop); const uids = await Messaging.getUidsInRoom(roomId, start, stop);
const [users, ownerId] = await Promise.all([ const [users, isOwners] = await Promise.all([
user.getUsersFields(uids, ['uid', 'username', 'picture', 'status']), user.getUsersFields(uids, ['uid', 'username', 'picture', 'status']),
db.getObjectField(`chat:room:${roomId}`, 'owner'), Messaging.isRoomOwner(uids, roomId),
]); ]);
return Promise.all(users.map(async (user) => { return users.map((user, index) => {
user.isOwner = await Messaging.isRoomOwner(user.uid, roomId, ownerId); user.isOwner = isOwners[index];
return user; return user;
})); });
}; };
Messaging.renameRoom = async function (uid, roomId, newName) { Messaging.renameRoom = async function (uid, roomId, newName) {
@ -240,7 +247,7 @@ module.exports = function (Messaging) {
]); ]);
room.messages = messages; room.messages = messages;
room.isOwner = await Messaging.isRoomOwner(uid, room.roomId, room.owner); room.isOwner = await Messaging.isRoomOwner(uid, room.roomId);
room.users = users.filter(user => user && parseInt(user.uid, 10) && parseInt(user.uid, 10) !== parseInt(uid, 10)); room.users = users.filter(user => user && parseInt(user.uid, 10) && parseInt(user.uid, 10) !== parseInt(uid, 10));
room.canReply = canReply; room.canReply = canReply;
room.groupChat = room.hasOwnProperty('groupChat') ? room.groupChat : users.length > 2; room.groupChat = room.hasOwnProperty('groupChat') ? room.groupChat : users.length > 2;

Loading…
Cancel
Save