@@ -153,16 +153,36 @@ export const getViewableThreadsByUser = async (
153153 . map ( userChannel => userChannel ( 'channelId' ) )
154154 . run ( ) ;
155155
156+ const getCurrentUserCommunityIds = db
157+ . table ( 'usersCommunities' )
158+ . getAll ( currentUser , { index : 'userId' } )
159+ . filter ( { isMember : true } )
160+ . map ( userCommunity => userCommunity ( 'communityId' ) )
161+ . run ( ) ;
162+
156163 // get a list of the channels where the user posted a thread
157164 const getPublishedChannelIds = db
158165 . table ( 'threads' )
159166 . getAll ( evalUser , { index : 'creatorId' } )
160167 . map ( thread => thread ( 'channelId' ) )
161168 . run ( ) ;
162169
163- const [ currentUsersChannelIds , publishedChannelIds ] = await Promise . all ( [
170+ const getPublishedCommunityIds = db
171+ . table ( 'threads' )
172+ . getAll ( evalUser , { index : 'creatorId' } )
173+ . map ( thread => thread ( 'communityId' ) )
174+ . run ( ) ;
175+
176+ const [
177+ currentUsersChannelIds ,
178+ publishedChannelIds ,
179+ currentUsersCommunityIds ,
180+ publishedCommunityIds ,
181+ ] = await Promise . all ( [
164182 getCurrentUsersChannelIds ,
165183 getPublishedChannelIds ,
184+ getCurrentUserCommunityIds ,
185+ getPublishedCommunityIds ,
166186 ] ) ;
167187
168188 // get a list of all the channels that are public
@@ -173,16 +193,32 @@ export const getViewableThreadsByUser = async (
173193 . map ( channel => channel ( 'id' ) )
174194 . run ( ) ;
175195
176- const allIds = [ ...currentUsersChannelIds , ...publicChannelIds ] ;
196+ const publicCommunityIds = await db
197+ . table ( 'communities' )
198+ . getAll ( ...publishedCommunityIds )
199+ . filter ( { isPrivate : false } )
200+ . map ( community => community ( 'id' ) )
201+ . run ( ) ;
202+
203+ const allIds = [
204+ ...currentUsersChannelIds ,
205+ ...currentUsersCommunityIds ,
206+ ...publicChannelIds ,
207+ ...publicCommunityIds ,
208+ ] ;
177209 const distinctIds = allIds . filter ( ( x , i , a ) => a . indexOf ( x ) == i ) ;
178- const validIds = intersection ( distinctIds , publishedChannelIds ) ;
210+ let validChannelIds = intersection ( distinctIds , publishedChannelIds ) ;
211+ let validCommunityIds = intersection ( distinctIds , publishedCommunityIds ) ;
179212
180213 // takes ~70ms for a heavy load
181214 return await db
182215 . table ( 'threads' )
183216 . getAll ( evalUser , { index : 'creatorId' } )
184217 . filter ( thread => db . not ( thread . hasFields ( 'deletedAt' ) ) )
185- . filter ( thread => db . expr ( validIds ) . contains ( thread ( 'channelId' ) ) )
218+ . filter ( thread => db . expr ( validChannelIds ) . contains ( thread ( 'channelId' ) ) )
219+ . filter ( thread =>
220+ db . expr ( validCommunityIds ) . contains ( thread ( 'communityId' ) )
221+ )
186222 . orderBy ( db . desc ( 'lastActive' ) , db . desc ( 'createdAt' ) )
187223 . skip ( after || 0 )
188224 . limit ( first )
@@ -203,6 +239,10 @@ export const getPublicThreadsByUser = (evalUser: string, options: PaginationOpti
203239 . filter ( { right : { isPrivate : false } } )
204240 . without ( 'right' )
205241 . zip ( )
242+ . eqJoin ( 'communityId' , db . table ( 'communities' ) )
243+ . filter ( { right : { isPrivate : false } } )
244+ . without ( 'right' )
245+ . zip ( )
206246 . orderBy ( db . desc ( 'lastActive' ) , db . desc ( 'createdAt' ) )
207247 . skip ( after || 0 )
208248 . limit ( first || 10 )
@@ -223,6 +263,13 @@ export const getViewableParticipantThreadsByUser = async (
223263 . map ( userChannel => userChannel ( 'channelId' ) )
224264 . run ( ) ;
225265
266+ const getCurrentUserCommunityIds = db
267+ . table ( 'usersCommunities' )
268+ . getAll ( currentUser , { index : 'userId' } )
269+ . filter ( { isMember : true } )
270+ . map ( userCommunity => userCommunity ( 'communityId' ) )
271+ . run ( ) ;
272+
226273 // get a list of the channels where the user participated in a thread
227274 const getParticipantChannelIds = db
228275 . table ( 'usersThreads' )
@@ -233,16 +280,36 @@ export const getViewableParticipantThreadsByUser = async (
233280 . pluck ( 'channelId' , 'threadId' )
234281 . run ( ) ;
235282
236- const [ currentUsersChannelIds , participantChannelIds ] = await Promise . all ( [
283+ const getParticipantCommunityIds = db
284+ . table ( 'usersThreads' )
285+ . getAll ( evalUser , { index : 'userId' } )
286+ . filter ( { isParticipant : true } )
287+ . eqJoin ( 'threadId' , db . table ( 'threads' ) )
288+ . zip ( )
289+ . pluck ( 'communityId' , 'threadId' )
290+ . run ( ) ;
291+
292+ const [
293+ currentUsersChannelIds ,
294+ participantChannelIds ,
295+ currentUsersCommunityIds ,
296+ participantCommunityIds ,
297+ ] = await Promise . all ( [
237298 getCurrentUsersChannelIds ,
238299 getParticipantChannelIds ,
300+ getCurrentUserCommunityIds ,
301+ getParticipantCommunityIds ,
239302 ] ) ;
240303
241304 const participantThreadIds = participantChannelIds . map ( c => c . threadId ) ;
242305 const distinctParticipantChannelIds = participantChannelIds
243306 . map ( c => c . channelId )
244307 . filter ( ( x , i , a ) => a . indexOf ( x ) == i ) ;
245308
309+ const distinctParticipantCommunityIds = participantCommunityIds
310+ . map ( c => c . communityId )
311+ . filter ( ( x , i , a ) => a . indexOf ( x ) == i ) ;
312+
246313 // get a list of all the channels that are public
247314 const publicChannelIds = await db
248315 . table ( 'channels' )
@@ -251,15 +318,37 @@ export const getViewableParticipantThreadsByUser = async (
251318 . map ( channel => channel ( 'id' ) )
252319 . run ( ) ;
253320
254- const allIds = [ ...currentUsersChannelIds , ...publicChannelIds ] ;
321+ const publicCommunityIds = await db
322+ . table ( 'communities' )
323+ . getAll ( ...distinctParticipantCommunityIds )
324+ . filter ( { isPrivate : false } )
325+ . map ( community => community ( 'id' ) )
326+ . run ( ) ;
327+
328+ const allIds = [
329+ ...currentUsersChannelIds ,
330+ ...publicChannelIds ,
331+ ...currentUsersCommunityIds ,
332+ ...publicCommunityIds ,
333+ ] ;
255334 const distinctIds = allIds . filter ( ( x , i , a ) => a . indexOf ( x ) == i ) ;
256- const validIds = intersection ( distinctIds , distinctParticipantChannelIds ) ;
335+ let validChannelIds = intersection (
336+ distinctIds ,
337+ distinctParticipantChannelIds
338+ ) ;
339+ let validCommunityIds = intersection (
340+ distinctIds ,
341+ distinctParticipantCommunityIds
342+ ) ;
257343
258344 return await db
259345 . table ( 'threads' )
260346 . getAll ( ...participantThreadIds )
261347 . filter ( thread => db . not ( thread . hasFields ( 'deletedAt' ) ) )
262- . filter ( thread => db . expr ( validIds ) . contains ( thread ( 'channelId' ) ) )
348+ . filter ( thread => db . expr ( validChannelIds ) . contains ( thread ( 'channelId' ) ) )
349+ . filter ( thread =>
350+ db . expr ( validCommunityIds ) . contains ( thread ( 'communityId' ) )
351+ )
263352 . orderBy ( db . desc ( 'lastActive' ) , db . desc ( 'createdAt' ) )
264353 . skip ( after || 0 )
265354 . limit ( first )
@@ -293,6 +382,10 @@ export const getPublicParticipantThreadsByUser = (evalUser: string, options: Pag
293382 . filter ( { right : { isPrivate : false } } )
294383 . without ( 'right' )
295384 . zip ( )
385+ . eqJoin ( 'communityId' , db . table ( 'communities' ) )
386+ . filter ( { right : { isPrivate : false } } )
387+ . without ( 'right' )
388+ . zip ( )
296389 . orderBy ( db . desc ( 'lastActive' ) , db . desc ( 'createdAt' ) )
297390 . skip ( after || 0 )
298391 . limit ( first || 10 )
0 commit comments