diff --git a/keyserver/src/fetchers/community-fetchers.js b/keyserver/src/fetchers/community-fetchers.js index 06f5a18c7..12c68ba1b 100644 --- a/keyserver/src/fetchers/community-fetchers.js +++ b/keyserver/src/fetchers/community-fetchers.js @@ -1,68 +1,96 @@ // @flow -import type { ServerCommunityInfo } from 'lib/types/community-types.js'; +import type { + ServerCommunityInfo, + ServerCommunityInfoWithCommunityName, +} from 'lib/types/community-types.js'; import { dbQuery, SQL } from '../database/database.js'; import { Viewer } from '../session/viewer.js'; type ServerCommunityInfoWithHolder = $ReadOnly<{ ...ServerCommunityInfo, +blobHolder: ?string, }>; async function fetchCommunityInfos( viewer: Viewer, communityIDs?: $ReadOnlyArray, ): Promise<$ReadOnlyArray> { if (!viewer.loggedIn) { return []; } const query = SQL` SELECT c.id, c.farcaster_channel_id as farcasterChannelID, c.blob_holder as blobHolder FROM communities c INNER JOIN memberships m ON c.id = m.thread AND m.user = ${viewer.userID} WHERE m.role > 0 `; if (communityIDs && communityIDs.length > 0) { query.append(SQL` AND c.id IN (${communityIDs}) `); } const [result] = await dbQuery(query); const communityInfos = result.map(row => ({ id: row.id.toString(), farcasterChannelID: row.farcasterChannelID, blobHolder: row.blobHolder, })); return communityInfos; } async function fetchCommunityFarcasterChannelTag( viewer: Viewer, communityID: string, ): Promise { if (!viewer.loggedIn) { return null; } const query = SQL` SELECT c.farcaster_channel_id as farcasterChannelID FROM communities c WHERE c.id = ${communityID} `; const [result] = await dbQuery(query); const communityInfo = result[0]; return communityInfo?.farcasterChannelID; } -export { fetchCommunityInfos, fetchCommunityFarcasterChannelTag }; +async function fetchAllCommunityInfosWithNames(): Promise< + $ReadOnlyArray, +> { + const query = SQL` + SELECT c.id, t.name as communityName, + c.farcaster_channel_id as farcasterChannelID + FROM communities c + INNER JOIN threads t ON c.id = t.id + `; + + const [result] = await dbQuery(query); + + const communityInfos = result.map(row => ({ + id: row.id.toString(), + farcasterChannelID: row.farcasterChannelID, + communityName: row.communityName, + })); + + return communityInfos; +} + +export { + fetchCommunityInfos, + fetchCommunityFarcasterChannelTag, + fetchAllCommunityInfosWithNames, +}; diff --git a/lib/types/community-types.js b/lib/types/community-types.js index 0a55aa1d0..411bfdc07 100644 --- a/lib/types/community-types.js +++ b/lib/types/community-types.js @@ -1,69 +1,74 @@ // @flow import t, { type TInterface } from 'tcomb'; import { tID, tShape } from '../utils/validation-utils.js'; export type CommunityInfo = { +farcasterChannelID: ?string, }; export type CommunityInfos = { +[threadID: string]: CommunityInfo }; export type CommunityStore = { +communityInfos: CommunityInfos, }; export type AddCommunityPayload = { +id: string, +newCommunityInfo: CommunityInfo, }; export type ServerCommunityInfo = { +id: string, +farcasterChannelID: ?string, }; export const serverCommunityInfoValidator: TInterface = tShape({ id: tID, farcasterChannelID: t.maybe(t.String), }); +export type ServerCommunityInfoWithCommunityName = $ReadOnly<{ + ...ServerCommunityInfo, + +communityName: string, +}>; + export type FetchCommunityInfosResponse = { +communityInfos: $ReadOnlyArray, }; export type CreateOrUpdateFarcasterChannelTagRequest = { +commCommunityID: string, +farcasterChannelID: string, }; export type CreateOrUpdateFarcasterChannelTagResponse = { +commCommunityID: string, +farcasterChannelID: string, }; export type DeleteFarcasterChannelTagRequest = { +commCommunityID: string, +farcasterChannelID: string, }; export type DeleteFarcasterChannelTagPayload = { +commCommunityID: string, }; export type OngoingJoinCommunityData = { +resolve: () => mixed, +reject: () => mixed, +communityID: string, +threadID: ?string, }; export type JoinCommunityStep = | 'inactive' | 'add_keyserver' | 'auth_to_keyserver' | 'join_community' | 'join_thread' | 'finished';