From 71460b5c02b0e598cc7cc79d06752b43efdd8426 Mon Sep 17 00:00:00 2001 From: Pierre Lehnen Date: Mon, 22 Mar 2021 16:46:03 -0300 Subject: [PATCH] [FIX] rooms.adminRooms to properly return teams regardless of the other filters --- app/api/server/lib/rooms.js | 10 +++++-- app/models/server/raw/Rooms.js | 51 ++++++++++++++++------------------ 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/app/api/server/lib/rooms.js b/app/api/server/lib/rooms.js index feac9602ad646..63885f1e568c0 100644 --- a/app/api/server/lib/rooms.js +++ b/app/api/server/lib/rooms.js @@ -25,6 +25,8 @@ export async function findAdminRooms({ uid, filter, types = [], pagination: { of msgs: 1, archived: 1, tokenpass: 1, + teamId: 1, + teamMain: 1, }; const name = filter && filter.trim(); @@ -39,12 +41,14 @@ export async function findAdminRooms({ uid, filter, types = [], pagination: { of limit: count, }; - let cursor = Rooms.findByNameContaining(name, discussion, includeTeams, options); + let cursor; if (name && showTypes.length) { - cursor = Rooms.findByNameContainingAndTypes(name, showTypes, discussion, options); + cursor = Rooms.findByNameContainingAndTypes(name, showTypes, discussion, includeTeams, options); } else if (showTypes.length) { - cursor = Rooms.findByTypes(showTypes, discussion, options); + cursor = Rooms.findByTypes(showTypes, discussion, includeTeams, options); + } else { + cursor = Rooms.findByNameContaining(name, discussion, includeTeams, options); } const total = await cursor.count(); diff --git a/app/models/server/raw/Rooms.js b/app/models/server/raw/Rooms.js index b8dd71b3f6abe..cb34016ba5927 100644 --- a/app/models/server/raw/Rooms.js +++ b/app/models/server/raw/Rooms.js @@ -42,8 +42,15 @@ export class RoomsRaw extends BaseRaw { return statistic; } - findByNameContainingAndTypes(name, types, discussion = false, options = {}) { + findByNameContainingAndTypes(name, types, discussion = false, teams = false, options = {}) { const nameRegex = new RegExp(escapeRegExp(name).trim(), 'i'); + + const teamCondition = teams ? {} : { + teamMain: { + $exists: false, + }, + }; + const query = { t: { $in: types, @@ -56,19 +63,24 @@ export class RoomsRaw extends BaseRaw { usernames: nameRegex, }, ], + ...teamCondition, }; return this.find(query, options); } - findByTypes(types, discussion = false, options = {}) { + findByTypes(types, discussion = false, teams = false, options = {}) { + const teamCondition = teams ? {} : { + teamMain: { + $exists: false, + }, + }; + const query = { t: { $in: types, }, prid: { $exists: discussion }, - teamId: { - $exists: false, - }, + ...teamCondition, }; return this.find(query, options); } @@ -76,37 +88,22 @@ export class RoomsRaw extends BaseRaw { findByNameContaining(name, discussion = false, teams = false, options = {}) { const nameRegex = new RegExp(escapeRegExp(name).trim(), 'i'); - const teamCondition = teams ? { - $or: [ - { - teamId: { - $exists: false, - }, - }, - { - teamMain: true, - }, - ], - } : { - teamId: { + const teamCondition = teams ? {} : { + teamMain: { $exists: false, }, }; const query = { prid: { $exists: discussion }, - $and: [ + $or: [ + { name: nameRegex }, { - $or: [ - { name: nameRegex }, - { - t: 'd', - usernames: nameRegex, - }, - ], + t: 'd', + usernames: nameRegex, }, - teamCondition, ], + ...teamCondition, }; return this.find(query, options);