From 6b6fc5834e525a34bba8d0a0384fb68b1ee886db Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Tue, 20 Apr 2021 16:33:58 -0300 Subject: [PATCH 01/10] Add groups to the directory channels list --- app/models/server/models/Rooms.js | 6 ++++-- server/methods/browseChannels.js | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/models/server/models/Rooms.js b/app/models/server/models/Rooms.js index 3208a1130add..f783afecccf0 100644 --- a/app/models/server/models/Rooms.js +++ b/app/models/server/models/Rooms.js @@ -528,9 +528,11 @@ export class Rooms extends Base { return this._db.find(query, options); } - findByNameOrFNameAndTypeIncludingTeamRooms(name, type, teamIds, options) { + findByNameOrFNameAndTypeIncludingTeamRooms(name, types, teamIds, options) { const query = { - t: type, + t: { + $in: types, + }, teamMain: { $exists: false, }, diff --git a/server/methods/browseChannels.js b/server/methods/browseChannels.js index 9633e2f6f690..8d8a266e2d0c 100644 --- a/server/methods/browseChannels.js +++ b/server/methods/browseChannels.js @@ -11,6 +11,7 @@ import { getFederationDomain } from '../../app/federation/server/lib/getFederati import { isFederationEnabled } from '../../app/federation/server/lib/isFederationEnabled'; import { federationSearchUsers } from '../../app/federation/server/handler'; import { escapeRegExp } from '../../lib/escapeRegExp'; +import { getSubscribedRoomsForUserWithDetails } from '../../app/lib/server/functions/getRoomsWithSingleOwner'; import { Team } from '../sdk'; const sortChannels = function(field, direction) { @@ -44,7 +45,7 @@ const sortUsers = function(field, direction) { } }; -const getChannels = (user, canViewAnon, searchTerm, sort, pagination) => { +const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) => { if ((!user && !canViewAnon) || (user && !hasPermission(user._id, 'view-c-room'))) { return; } @@ -52,7 +53,7 @@ const getChannels = (user, canViewAnon, searchTerm, sort, pagination) => { const teams = Promise.await(Team.getAllPublicTeams()); const teamIds = teams.map(({ _id }) => _id); - const result = Rooms.findByNameOrFNameAndTypeIncludingTeamRooms(searchTerm, 'c', teamIds, { + const rooms = Rooms.findByNameOrFNameAndTypeIncludingTeamRooms(searchTerm, ['c', 'p'], teamIds, { ...pagination, sort: { featured: -1, @@ -73,18 +74,24 @@ const getChannels = (user, canViewAnon, searchTerm, sort, pagination) => { prid: 1, teamId: 1, }, - }); + }).fetch(); - const total = result.count(); // count ignores the `skip` and `limit` options - const results = result.fetch().map((room) => { + const roomsIds = rooms.map((room) => room._id); + const roomData = getSubscribedRoomsForUserWithDetails(user._id, false, roomsIds); + const results = []; + + for (const room of rooms) { if (room.teamId) { const team = teams.find((team) => team._id === room.teamId); if (team) { room.belongsTo = team.name; } } - return room; - }); + if (roomData.find((data) => data.rid === room._id)) { + results.push(room); + } + } + const total = results.length; return { total, @@ -227,7 +234,7 @@ Meteor.methods({ switch (type) { case 'channels': - return getChannels(user, canViewAnonymous, regex, sortChannels(sortBy, sortDirection), pagination); + return getChannelsAndGroups(user, canViewAnonymous, regex, sortChannels(sortBy, sortDirection), pagination); case 'teams': return getTeams(user, text, sortChannels(sortBy, sortDirection), pagination); case 'users': From 1b231816bd1e8c6efab1a2006348f4c1404cb928 Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Fri, 23 Apr 2021 12:37:39 -0300 Subject: [PATCH 02/10] Update query to obtain private groups --- app/models/server/models/Rooms.js | 43 +++++++++++++++++++++++++------ server/methods/browseChannels.js | 20 +++++--------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/app/models/server/models/Rooms.js b/app/models/server/models/Rooms.js index ae44d3a74861..66bb27a80ef4 100644 --- a/app/models/server/models/Rooms.js +++ b/app/models/server/models/Rooms.js @@ -544,10 +544,13 @@ export class Rooms extends Base { return this._db.find(query, options); } - findByNameOrFNameAndTypeIncludingTeamRooms(name, types, teamIds, options) { + findByNameOrFNameAndRoomIdsIncludingTeamRooms(name, teamIds, roomIds, options) { const query = { t: { - $in: types, + $in: [ + 'p', + 'c', + ], }, teamMain: { $exists: false, @@ -556,14 +559,38 @@ export class Rooms extends Base { { $or: [ { - teamId: { - $exists: false, - }, + $and: [ + { + t: 'p', + }, + roomIds ? { _id: { $in: roomIds } } : false, + ], + }, + { + $and: [ + { + t: 'c', + }, + { + teamId: { + $exists: false, + }, + }, + ], }, { - teamId: { - $in: teamIds, - }, + $and: [ + { + teamId: { + $exists: true, + }, + }, + { + teamId: { + $in: teamIds, + }, + }, + ], }, ], }, diff --git a/server/methods/browseChannels.js b/server/methods/browseChannels.js index 8d8a266e2d0c..d48c102b0eb6 100644 --- a/server/methods/browseChannels.js +++ b/server/methods/browseChannels.js @@ -11,7 +11,6 @@ import { getFederationDomain } from '../../app/federation/server/lib/getFederati import { isFederationEnabled } from '../../app/federation/server/lib/isFederationEnabled'; import { federationSearchUsers } from '../../app/federation/server/handler'; import { escapeRegExp } from '../../lib/escapeRegExp'; -import { getSubscribedRoomsForUserWithDetails } from '../../app/lib/server/functions/getRoomsWithSingleOwner'; import { Team } from '../sdk'; const sortChannels = function(field, direction) { @@ -52,8 +51,9 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = const teams = Promise.await(Team.getAllPublicTeams()); const teamIds = teams.map(({ _id }) => _id); + const userRooms = user.__rooms; - const rooms = Rooms.findByNameOrFNameAndTypeIncludingTeamRooms(searchTerm, ['c', 'p'], teamIds, { + const result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, teamIds, userRooms, { ...pagination, sort: { featured: -1, @@ -74,24 +74,18 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = prid: 1, teamId: 1, }, - }).fetch(); - - const roomsIds = rooms.map((room) => room._id); - const roomData = getSubscribedRoomsForUserWithDetails(user._id, false, roomsIds); - const results = []; + }); - for (const room of rooms) { + const total = result.count(); // count ignores the `skip` and `limit` options + const results = result.fetch().map((room) => { if (room.teamId) { const team = teams.find((team) => team._id === room.teamId); if (team) { room.belongsTo = team.name; } } - if (roomData.find((data) => data.rid === room._id)) { - results.push(room); - } - } - const total = results.length; + return room; + }); return { total, From c1e67b90bdc2792f9e13ae15cf2d3e443ed7ef20 Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Fri, 23 Apr 2021 15:55:20 -0300 Subject: [PATCH 03/10] Improve rooms query - remove unnecessary operators --- app/models/server/models/Rooms.js | 38 +++++++------------------------ 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/app/models/server/models/Rooms.js b/app/models/server/models/Rooms.js index 66bb27a80ef4..7a8a6462db62 100644 --- a/app/models/server/models/Rooms.js +++ b/app/models/server/models/Rooms.js @@ -558,39 +558,17 @@ export class Rooms extends Base { $and: [ { $or: [ + roomIds ? { _id: { $in: roomIds }, t: 'p' } : false, { - $and: [ - { - t: 'p', - }, - roomIds ? { _id: { $in: roomIds } } : false, - ], + t: 'c', + teamId: { + $exists: false, + }, }, { - $and: [ - { - t: 'c', - }, - { - teamId: { - $exists: false, - }, - }, - ], - }, - { - $and: [ - { - teamId: { - $exists: true, - }, - }, - { - teamId: { - $in: teamIds, - }, - }, - ], + teamId: { + $in: teamIds, + }, }, ], }, From 55c9520bfe97eb3bfa7750ea7e01ed313cdd9742 Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Thu, 29 Apr 2021 20:31:56 -0300 Subject: [PATCH 04/10] Remove room's type check --- app/models/server/models/Rooms.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/models/server/models/Rooms.js b/app/models/server/models/Rooms.js index 705fea7c3846..49dab0904e45 100644 --- a/app/models/server/models/Rooms.js +++ b/app/models/server/models/Rooms.js @@ -554,12 +554,6 @@ export class Rooms extends Base { findByNameOrFNameAndRoomIdsIncludingTeamRooms(name, teamIds, roomIds, options) { const query = { - t: { - $in: [ - 'p', - 'c', - ], - }, teamMain: { $exists: false, }, From 0749ffd0be2a6ed5417732706d27f69812030c19 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 12 May 2021 18:05:02 -0300 Subject: [PATCH 05/10] General improvements --- app/models/server/models/Rooms.js | 35 +++++++++++++------------ client/views/directory/ChannelsTable.js | 11 ++++---- server/methods/browseChannels.js | 10 ++++--- server/sdk/types/ITeamService.ts | 1 + 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/app/models/server/models/Rooms.js b/app/models/server/models/Rooms.js index e868c4ab22ad..8455d1a5c27f 100644 --- a/app/models/server/models/Rooms.js +++ b/app/models/server/models/Rooms.js @@ -552,39 +552,40 @@ export class Rooms extends Base { return this._db.find(query, options); } - findByNameOrFNameAndRoomIdsIncludingTeamRooms(name, teamIds, roomIds, options) { + findByNameOrFNameAndRoomIdsIncludingTeamRooms(text, teamIds, roomIds, options) { + const searchTerm = text && new RegExp(text, 'i'); + const query = { - teamMain: { - $exists: false, - }, $and: [ + { teamMain: { $exists: false } }, + { prid: { $exists: false } }, { $or: [ - roomIds ? { _id: { $in: roomIds }, t: 'p' } : false, { t: 'c', - teamId: { - $exists: false, - }, + teamId: { $exists: false }, }, { - teamId: { - $in: teamIds, - }, + t: 'c', + teamId: { $in: teamIds }, }, + ...roomIds?.length > 0 ? [{ + _id: { + $in: roomIds, + }, + }] : [], ], }, - { + ...searchTerm ? [{ $or: [{ - name, + name: searchTerm, }, { - fname: name, + fname: searchTerm, }], - }, + }] : [], ], }; - // do not use cache return this._db.find(query, options); } @@ -604,7 +605,7 @@ export class Rooms extends Base { }; if (text) { - const regex = new RegExp(s.trim(escapeRegExp(text)), 'i'); + const regex = new RegExp(text, 'i'); query.$and.push({ $or: [{ diff --git a/client/views/directory/ChannelsTable.js b/client/views/directory/ChannelsTable.js index 9c817ae378cf..842a880acb14 100644 --- a/client/views/directory/ChannelsTable.js +++ b/client/views/directory/ChannelsTable.js @@ -101,16 +101,17 @@ function ChannelsTable() { ); const channelRoute = useRoute('channel'); + const groupsRoute = useRoute('group'); const { value: data = { result: [] } } = useEndpointData('directory', query); const onClick = useMemo( - () => (name) => (e) => { + () => (name, type) => (e) => { if (e.type === 'click' || e.key === 'Enter') { - channelRoute.push({ name }); + type === 'c' ? channelRoute.push({ name }) : groupsRoute.push({ name }); } }, - [channelRoute], + [channelRoute, groupsRoute], ); const formatDate = useFormatDate(); @@ -122,8 +123,8 @@ function ChannelsTable() { return ( _id); + + const userTeams = Promise.await(Team.listTeamsBySubscriberUserId(user._id, { projection: { teamId: 1 } }))?.map(({ teamId }) => teamId) || []; const userRooms = user.__rooms; - const result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, teamIds, userRooms, { + const result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, [...userTeams, ...teamIds], userRooms, { ...pagination, sort: { featured: -1, @@ -203,7 +205,7 @@ const getUsers = (user, text, workspace, sort, pagination) => { Meteor.methods({ browseChannels({ text = '', workspace = '', type = 'channels', sortBy = 'name', sortDirection = 'asc', page, offset, limit = 10 }) { - const regex = new RegExp(s.trim(escapeRegExp(text)), 'i'); + const searchTerm = s.trim(escapeRegExp(text)); if (!['channels', 'users', 'teams'].includes(type) || !['asc', 'desc'].includes(sortDirection) || ((!page && page !== 0) && (!offset && offset !== 0))) { return; @@ -231,9 +233,9 @@ Meteor.methods({ switch (type) { case 'channels': - return getChannelsAndGroups(user, canViewAnonymous, regex, sortChannels(sortBy, sortDirection), pagination); + return getChannelsAndGroups(user, canViewAnonymous, searchTerm, sortChannels(sortBy, sortDirection), pagination); case 'teams': - return getTeams(user, text, sortChannels(sortBy, sortDirection), pagination); + return getTeams(user, searchTerm, sortChannels(sortBy, sortDirection), pagination); case 'users': return getUsers(user, text, workspace, sortUsers(sortBy, sortDirection), pagination); default: diff --git a/server/sdk/types/ITeamService.ts b/server/sdk/types/ITeamService.ts index c301b966c564..8709ae0c452c 100644 --- a/server/sdk/types/ITeamService.ts +++ b/server/sdk/types/ITeamService.ts @@ -82,4 +82,5 @@ export interface ITeamService { getAllPublicTeams(options: FindOneOptions): Promise>; getMembersByTeamIds(teamIds: Array, options: FindOneOptions): Promise>; update(uid: string, teamId: string, updateData: ITeamUpdateData): Promise; + listTeamsBySubscriberUserId(uid: string, options?: FindOneOptions): Promise | null>; } From 834abcc6b55fe9cafc12f1210ac77db3f8e38da0 Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Thu, 13 May 2021 14:21:58 -0300 Subject: [PATCH 06/10] Fix BelongsTo field --- server/methods/browseChannels.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/methods/browseChannels.js b/server/methods/browseChannels.js index 8f531bbd8367..806c6bdd589e 100644 --- a/server/methods/browseChannels.js +++ b/server/methods/browseChannels.js @@ -52,10 +52,11 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = const teams = Promise.await(Team.getAllPublicTeams()); const teamIds = teams.map(({ _id }) => _id); - const userTeams = Promise.await(Team.listTeamsBySubscriberUserId(user._id, { projection: { teamId: 1 } }))?.map(({ teamId }) => teamId) || []; + const userTeamsIds = Promise.await(Team.listTeamsBySubscriberUserId(user._id, { projection: { teamId: 1 } }))?.map(({ teamId }) => teamId) || []; + const userTeams = Promise.await(Team.listByIds(userTeamsIds, { projection: { _id: 1, name: 1 } })); const userRooms = user.__rooms; - const result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, [...userTeams, ...teamIds], userRooms, { + const result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, [...userTeamsIds, ...teamIds], userRooms, { ...pagination, sort: { featured: -1, @@ -81,7 +82,7 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = const total = result.count(); // count ignores the `skip` and `limit` options const results = result.fetch().map((room) => { if (room.teamId) { - const team = teams.find((team) => team._id === room.teamId); + const team = teams.find((team) => team._id === room.teamId) || userTeams.find((team) => team._id === room.teamId) || Promise.await(Team.getInfoById(room.teamId)); if (team) { room.belongsTo = team.name; } From c5aca1adcaa9b483bf52d8b1c8eb5b628b50de6d Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Wed, 19 May 2021 18:14:04 -0300 Subject: [PATCH 07/10] Update search for teams' names: use a single teams main rooms list --- app/models/server/models/Rooms.js | 4 ++++ server/methods/browseChannels.js | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/server/models/Rooms.js b/app/models/server/models/Rooms.js index 8455d1a5c27f..0c848843fcc1 100644 --- a/app/models/server/models/Rooms.js +++ b/app/models/server/models/Rooms.js @@ -535,6 +535,10 @@ export class Rooms extends Base { return this._db.find(query, options); } + findTeamMainRooms(options) { + return this._db.find({ teamMain: { $exists: true, $eq: true } }, options); + } + findByNameOrFNameAndType(name, type, options) { const query = { t: type, diff --git a/server/methods/browseChannels.js b/server/methods/browseChannels.js index 806c6bdd589e..678a2ebdfeee 100644 --- a/server/methods/browseChannels.js +++ b/server/methods/browseChannels.js @@ -51,9 +51,14 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = const teams = Promise.await(Team.getAllPublicTeams()); const teamIds = teams.map(({ _id }) => _id); + const teamsMains = Rooms.findTeamMainRooms({ + fields: { + name: 1, + teamId: 1, + }, + }).fetch(); const userTeamsIds = Promise.await(Team.listTeamsBySubscriberUserId(user._id, { projection: { teamId: 1 } }))?.map(({ teamId }) => teamId) || []; - const userTeams = Promise.await(Team.listByIds(userTeamsIds, { projection: { _id: 1, name: 1 } })); const userRooms = user.__rooms; const result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, [...userTeamsIds, ...teamIds], userRooms, { @@ -82,7 +87,7 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = const total = result.count(); // count ignores the `skip` and `limit` options const results = result.fetch().map((room) => { if (room.teamId) { - const team = teams.find((team) => team._id === room.teamId) || userTeams.find((team) => team._id === room.teamId) || Promise.await(Team.getInfoById(room.teamId)); + const team = teamsMains.find((mainRoom) => mainRoom.teamId === room.teamId); if (team) { room.belongsTo = team.name; } From ed70cd2cb22934cbe308d29d6d9c8e2c0730c858 Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Thu, 20 May 2021 16:47:20 -0300 Subject: [PATCH 08/10] Fill belongsTo field using the new getTeamsByIds method --- app/models/server/models/Rooms.js | 4 ---- server/methods/browseChannels.js | 20 +++++++++----------- server/sdk/types/ITeamService.ts | 1 + server/services/team/service.ts | 4 ++++ 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/models/server/models/Rooms.js b/app/models/server/models/Rooms.js index 0c848843fcc1..8455d1a5c27f 100644 --- a/app/models/server/models/Rooms.js +++ b/app/models/server/models/Rooms.js @@ -535,10 +535,6 @@ export class Rooms extends Base { return this._db.find(query, options); } - findTeamMainRooms(options) { - return this._db.find({ teamMain: { $exists: true, $eq: true } }, options); - } - findByNameOrFNameAndType(name, type, options) { const query = { t: type, diff --git a/server/methods/browseChannels.js b/server/methods/browseChannels.js index 678a2ebdfeee..a65eaf043246 100644 --- a/server/methods/browseChannels.js +++ b/server/methods/browseChannels.js @@ -50,18 +50,12 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = } const teams = Promise.await(Team.getAllPublicTeams()); - const teamIds = teams.map(({ _id }) => _id); - const teamsMains = Rooms.findTeamMainRooms({ - fields: { - name: 1, - teamId: 1, - }, - }).fetch(); + const publicTeamIds = teams.map(({ _id }) => _id); const userTeamsIds = Promise.await(Team.listTeamsBySubscriberUserId(user._id, { projection: { teamId: 1 } }))?.map(({ teamId }) => teamId) || []; const userRooms = user.__rooms; - const result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, [...userTeamsIds, ...teamIds], userRooms, { + let result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, [...userTeamsIds, ...publicTeamIds], userRooms, { ...pagination, sort: { featured: -1, @@ -83,11 +77,15 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = teamId: 1, }, }); - const total = result.count(); // count ignores the `skip` and `limit` options - const results = result.fetch().map((room) => { + result = result.fetch(); + + const teamIds = result.filter(({ teamId }) => teamId).map(({ teamId }) => teamId); + const teamsMains = Promise.await(Team.getTeamsByIds([...new Set(teamIds)], { projection: { _id: 1, name: 1 } })); + + const results = result.map((room) => { if (room.teamId) { - const team = teamsMains.find((mainRoom) => mainRoom.teamId === room.teamId); + const team = teamsMains.find((mainRoom) => mainRoom._id === room.teamId); if (team) { room.belongsTo = team.name; } diff --git a/server/sdk/types/ITeamService.ts b/server/sdk/types/ITeamService.ts index 8709ae0c452c..85b832c9dee0 100644 --- a/server/sdk/types/ITeamService.ts +++ b/server/sdk/types/ITeamService.ts @@ -81,6 +81,7 @@ export interface ITeamService { autocomplete(uid: string, name: string): Promise>; getAllPublicTeams(options: FindOneOptions): Promise>; getMembersByTeamIds(teamIds: Array, options: FindOneOptions): Promise>; + getTeamsByIds(teamIds: string[], options?: FindOneOptions): Promise; update(uid: string, teamId: string, updateData: ITeamUpdateData): Promise; listTeamsBySubscriberUserId(uid: string, options?: FindOneOptions): Promise | null>; } diff --git a/server/services/team/service.ts b/server/services/team/service.ts index 21335d387dd7..f73f80551fe3 100644 --- a/server/services/team/service.ts +++ b/server/services/team/service.ts @@ -206,6 +206,10 @@ export class TeamService extends ServiceClass implements ITeamService { })); } + async getTeamsByIds(teamIds: string[], options?: FindOneOptions): Promise { + return this.TeamModel.findByIds(teamIds, options).toArray(); + } + async search(userId: string, term: string | RegExp, options?: FindOneOptions): Promise { if (typeof term === 'string') { term = new RegExp(`^${ escapeRegExp(term) }`, 'i'); From e2b2a3cc74dc7b51bb02c4ddfcaa3fb16555b224 Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Thu, 20 May 2021 17:15:22 -0300 Subject: [PATCH 09/10] Replace result by cursor --- server/methods/browseChannels.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/methods/browseChannels.js b/server/methods/browseChannels.js index a65eaf043246..883765cec23c 100644 --- a/server/methods/browseChannels.js +++ b/server/methods/browseChannels.js @@ -55,7 +55,7 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = const userTeamsIds = Promise.await(Team.listTeamsBySubscriberUserId(user._id, { projection: { teamId: 1 } }))?.map(({ teamId }) => teamId) || []; const userRooms = user.__rooms; - let result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, [...userTeamsIds, ...publicTeamIds], userRooms, { + const cursor = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, [...userTeamsIds, ...publicTeamIds], userRooms, { ...pagination, sort: { featured: -1, @@ -77,8 +77,8 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = teamId: 1, }, }); - const total = result.count(); // count ignores the `skip` and `limit` options - result = result.fetch(); + const total = cursor.count(); // count ignores the `skip` and `limit` options + const result = cursor.fetch(); const teamIds = result.filter(({ teamId }) => teamId).map(({ teamId }) => teamId); const teamsMains = Promise.await(Team.getTeamsByIds([...new Set(teamIds)], { projection: { _id: 1, name: 1 } })); From d453e962510b823179acab66f814349cb5270564 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Thu, 20 May 2021 21:32:54 -0300 Subject: [PATCH 10/10] Remove duplicated method --- server/methods/browseChannels.js | 2 +- server/sdk/types/ITeamService.ts | 1 - server/services/team/service.ts | 4 ---- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/server/methods/browseChannels.js b/server/methods/browseChannels.js index 883765cec23c..03bb78922c7c 100644 --- a/server/methods/browseChannels.js +++ b/server/methods/browseChannels.js @@ -81,7 +81,7 @@ const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) = const result = cursor.fetch(); const teamIds = result.filter(({ teamId }) => teamId).map(({ teamId }) => teamId); - const teamsMains = Promise.await(Team.getTeamsByIds([...new Set(teamIds)], { projection: { _id: 1, name: 1 } })); + const teamsMains = Promise.await(Team.listByIds([...new Set(teamIds)], { projection: { _id: 1, name: 1 } })); const results = result.map((room) => { if (room.teamId) { diff --git a/server/sdk/types/ITeamService.ts b/server/sdk/types/ITeamService.ts index 85b832c9dee0..8709ae0c452c 100644 --- a/server/sdk/types/ITeamService.ts +++ b/server/sdk/types/ITeamService.ts @@ -81,7 +81,6 @@ export interface ITeamService { autocomplete(uid: string, name: string): Promise>; getAllPublicTeams(options: FindOneOptions): Promise>; getMembersByTeamIds(teamIds: Array, options: FindOneOptions): Promise>; - getTeamsByIds(teamIds: string[], options?: FindOneOptions): Promise; update(uid: string, teamId: string, updateData: ITeamUpdateData): Promise; listTeamsBySubscriberUserId(uid: string, options?: FindOneOptions): Promise | null>; } diff --git a/server/services/team/service.ts b/server/services/team/service.ts index f73f80551fe3..21335d387dd7 100644 --- a/server/services/team/service.ts +++ b/server/services/team/service.ts @@ -206,10 +206,6 @@ export class TeamService extends ServiceClass implements ITeamService { })); } - async getTeamsByIds(teamIds: string[], options?: FindOneOptions): Promise { - return this.TeamModel.findByIds(teamIds, options).toArray(); - } - async search(userId: string, term: string | RegExp, options?: FindOneOptions): Promise { if (typeof term === 'string') { term = new RegExp(`^${ escapeRegExp(term) }`, 'i');