@@ -104,16 +104,22 @@ async function getChannelsAndGroups(user, canViewAnon, searchTerm, sort, paginat
104
104
} ;
105
105
}
106
106
107
- const getChannelsCountForTeam = mem ( ( teamId ) => Promise . await ( Rooms . findByTeamId ( teamId , { projection : { _id : 1 } } ) . count ( ) ) , {
108
- maxAge : 2000 ,
109
- } ) ;
107
+ const getChannelsCountForTeam = mem (
108
+ ( teamId ) => {
109
+ return Rooms . findByTeamId ( teamId , { projection : { _id : 1 } } ) ;
110
+ } ,
111
+ {
112
+ maxAge : 2000 ,
113
+ } ,
114
+ ) ;
110
115
111
116
async function getTeams ( user , searchTerm , sort , pagination ) {
112
117
if ( ! user ) {
113
118
return ;
114
119
}
115
120
116
121
const userSubs = Subscriptions . cachedFindByUserId ( user . _id ) . fetch ( ) ;
122
+
117
123
const ids = userSubs . map ( ( sub ) => sub . rid ) ;
118
124
const { cursor, totalCount } = Rooms . findPaginatedContainingNameOrFNameInIdsAsTeamMain (
119
125
searchTerm ? new RegExp ( searchTerm , 'i' ) : null ,
@@ -143,19 +149,23 @@ async function getTeams(user, searchTerm, sort, pagination) {
143
149
} ,
144
150
) ;
145
151
146
- const [ rooms , total ] = await Promise . all ( [
147
- cursor
148
- . map ( ( room ) => ( {
149
- ...room ,
150
- roomsCount : getChannelsCountForTeam ( room . teamId ) ,
151
- } ) )
152
- . toArray ( ) ,
153
- totalCount ,
154
- ] ) ;
152
+ const roomsPromises = cursor . map ( ( room ) => room ) . toArray ( ) ;
153
+
154
+ const [ rooms ] = await Promise . all ( [ roomsPromises ] ) ;
155
+
156
+ const teams = [ ] ;
157
+
158
+ for await ( const room of rooms ) {
159
+ const result = await getChannelsCountForTeam ( room . teamId ) . toArray ( ) ;
160
+
161
+ teams . push ( { ...room , roomsCount : result . length } ) ;
162
+ }
163
+
164
+ const [ total ] = await Promise . all ( [ totalCount ] ) ;
155
165
156
166
return {
157
167
total,
158
- results : rooms ,
168
+ results : teams ,
159
169
} ;
160
170
}
161
171
0 commit comments