diff --git a/keyserver/src/fetchers/community-fetchers.js b/keyserver/src/fetchers/community-fetchers.js --- a/keyserver/src/fetchers/community-fetchers.js +++ b/keyserver/src/fetchers/community-fetchers.js @@ -6,6 +6,7 @@ } from 'lib/types/community-types.js'; import { fetchPrivilegedThreadInfos } from './thread-fetchers.js'; +import { viewerIsMemberOfThreads } from './thread-permission-fetchers.js'; import { dbQuery, SQL, mergeAndConditions } from '../database/database.js'; import { Viewer } from '../session/viewer.js'; @@ -93,13 +94,23 @@ const [result] = await dbQuery(query); - const threadIDs = new Set(result.map(row => row.id.toString())); + const threadIDs = result.map(row => row.id.toString()); + + const membershipMap = await viewerIsMemberOfThreads(viewer, threadIDs); + + const filteredResult = result.filter( + row => !!row.farcasterChannelID && !membershipMap.get(row.id.toString()), + ); + + const filteredThreadIDs = new Set( + filteredResult.map(row => row.id.toString()), + ); const threadInfosResult = await fetchPrivilegedThreadInfos(viewer, { - threadIDs, + threadIDs: filteredThreadIDs, }); - const communityInfos = result.map( + const communityInfos = filteredResult.map( (row): ServerCommunityInfoWithCommunityName => ({ id: row.id.toString(), farcasterChannelID: row.farcasterChannelID, diff --git a/keyserver/src/fetchers/thread-permission-fetchers.js b/keyserver/src/fetchers/thread-permission-fetchers.js --- a/keyserver/src/fetchers/thread-permission-fetchers.js +++ b/keyserver/src/fetchers/thread-permission-fetchers.js @@ -71,6 +71,19 @@ return checkThread(viewer, threadID, [{ check: 'is_member' }]); } +async function viewerIsMemberOfThreads( + viewer: Viewer, + threadIDs: $ReadOnlyArray, +): Promise> { + const validThreadIDs = await checkThreads(viewer, threadIDs, [ + { check: 'is_member' }, + ]); + + return new Map( + threadIDs.map(threadID => [threadID, validThreadIDs.has(threadID)]), + ); +} + async function viewerHasPositiveRole( viewer: Viewer, threadID: string, @@ -448,6 +461,7 @@ fetchThreadPermissionsBlob, checkThreadPermission, viewerIsMember, + viewerIsMemberOfThreads, checkThreads, getValidThreads, checkThread,