Skip to content

Commit

Permalink
[keyserver] return threadInfo in fetchAllCommunityInfosWithNames
Browse files Browse the repository at this point in the history
Summary:
Depends on D13853

we return threadInfos here so that we can populate the community directory on clients using the ThreadList component

Test Plan: tested in next diff by calling fetchAllCommunityInfosWithNames and getting a working modal with the ThreadList correctly populated

Reviewers: ashoat

Reviewed By: ashoat

Subscribers: tomek

Differential Revision: https://phab.comm.dev/D13854
  • Loading branch information
vdhanan committed Nov 20, 2024
1 parent 1490b84 commit 183ac09
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 24 deletions.
26 changes: 18 additions & 8 deletions keyserver/src/fetchers/community-fetchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ServerCommunityInfoWithCommunityName,
} from 'lib/types/community-types.js';

import { fetchPrivilegedThreadInfos } from './thread-fetchers.js';
import { dbQuery, SQL, mergeAndConditions } from '../database/database.js';
import { Viewer } from '../session/viewer.js';

Expand Down Expand Up @@ -80,9 +81,9 @@ async function fetchCommunityFarcasterChannelTag(
return communityInfo?.farcasterChannelID;
}

async function fetchAllCommunityInfosWithNames(): Promise<
$ReadOnlyArray<ServerCommunityInfoWithCommunityName>,
> {
async function fetchAllCommunityInfosWithNames(
viewer: Viewer,
): Promise<$ReadOnlyArray<ServerCommunityInfoWithCommunityName>> {
const query = SQL`
SELECT c.id, t.name as communityName,
c.farcaster_channel_id as farcasterChannelID
Expand All @@ -92,11 +93,20 @@ async function fetchAllCommunityInfosWithNames(): Promise<

const [result] = await dbQuery(query);

const communityInfos = result.map(row => ({
id: row.id.toString(),
farcasterChannelID: row.farcasterChannelID,
communityName: row.communityName,
}));
const threadIDs = new Set(result.map(row => row.id.toString()));

const threadInfosResult = await fetchPrivilegedThreadInfos(viewer, {
threadIDs,
});

const communityInfos = result.map(
(row): ServerCommunityInfoWithCommunityName => ({
id: row.id.toString(),
farcasterChannelID: row.farcasterChannelID,
communityName: row.communityName,
threadInfo: threadInfosResult.threadInfos[row.id.toString()] ?? null,
}),
);

return communityInfos;
}
Expand Down
7 changes: 4 additions & 3 deletions keyserver/src/responders/community-responders.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type {
FetchCommunityInfosResponse,
FetchAllCommunityInfosWithNamesResponse,
ServerFetchAllCommunityInfosWithNamesResponse,
} from 'lib/types/community-types.js';

import {
Expand All @@ -26,12 +26,13 @@ async function fetchCommunityInfosResponder(

async function fetchAllCommunityInfosWithNamesResponder(
viewer: Viewer,
): Promise<FetchAllCommunityInfosWithNamesResponse> {
): Promise<ServerFetchAllCommunityInfosWithNamesResponse> {
if (!viewer.loggedIn) {
return { allCommunityInfosWithNames: [] };
}

const allCommunityInfosWithNames = await fetchAllCommunityInfosWithNames();
const allCommunityInfosWithNames =
await fetchAllCommunityInfosWithNames(viewer);

return { allCommunityInfosWithNames };
}
Expand Down
3 changes: 2 additions & 1 deletion keyserver/src/scripts/delete-all-fc-channel-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ async function deleteAllFCChannelTags() {
const admin = await thisKeyserverAdmin();
const adminViewer = createScriptViewer(admin.id);

const allCommunityInfosWithNames = await fetchAllCommunityInfosWithNames();
const allCommunityInfosWithNames =
await fetchAllCommunityInfosWithNames(adminViewer);

const deleteFarcasterChannelTagPromises = allCommunityInfosWithNames
.map(communityInfoWithName => {
Expand Down
4 changes: 2 additions & 2 deletions lib/actions/community-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { CallKeyserverEndpoint } from '../keyserver-conn/keyserver-conn-typ
import type {
ServerCommunityInfo,
FetchCommunityInfosResponse,
FetchAllCommunityInfosWithNamesResponse,
ClientFetchAllCommunityInfosWithNamesResponse,
CreateOrUpdateFarcasterChannelTagRequest,
CreateOrUpdateFarcasterChannelTagResponse,
DeleteFarcasterChannelTagRequest,
Expand Down Expand Up @@ -69,7 +69,7 @@ const fetchAllCommunityInfosWithNamesActionTypes = Object.freeze({
const fetchAllCommunityInfosWithNames =
(
callSingleKeyserverEndpoint: CallSingleKeyserverEndpoint,
): (() => Promise<FetchAllCommunityInfosWithNamesResponse>) =>
): (() => Promise<ClientFetchAllCommunityInfosWithNamesResponse>) =>
() =>
callSingleKeyserverEndpoint('fetch_all_community_infos_with_names');
const createOrUpdateFarcasterChannelTagActionTypes = Object.freeze({
Expand Down
25 changes: 21 additions & 4 deletions lib/types/community-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import t, { type TInterface } from 'tcomb';

import type { RawMessageInfo } from './message-types.js';
import type { ChangeThreadSettingsResult } from './thread-types.js';
import type { ThinRawThreadInfo } from './minimally-encoded-thread-permissions-types.js';
import type {
ChangeThreadSettingsResult,
LegacyThinRawThreadInfo,
} from './thread-types.js';
import type { ServerUpdateInfo } from './update-types.js';
import { thinRawThreadInfoValidator } from '../permissions/minimally-encoded-raw-thread-info-validators.js';
import { tID, tShape } from '../utils/validation-utils.js';

export type CommunityInfo = {
Expand Down Expand Up @@ -36,23 +41,35 @@ export const serverCommunityInfoValidator: TInterface<ServerCommunityInfo> =
export type ServerCommunityInfoWithCommunityName = $ReadOnly<{
...ServerCommunityInfo,
+communityName: string,
+threadInfo: LegacyThinRawThreadInfo | ThinRawThreadInfo | null,
}>;

export const serverCommunityInfoWithCommunityNameValidator: TInterface<ServerCommunityInfoWithCommunityName> =
tShape<ServerCommunityInfoWithCommunityName>({
export type ClientCommunityInfoWithCommunityName = $ReadOnly<{
...ServerCommunityInfo,
+communityName: string,
+threadInfo: ThinRawThreadInfo | null,
}>;

export const clientCommunityInfoWithCommunityNameValidator: TInterface<ClientCommunityInfoWithCommunityName> =
tShape<ClientCommunityInfoWithCommunityName>({
id: tID,
farcasterChannelID: t.maybe(t.String),
communityName: t.String,
threadInfo: t.maybe(thinRawThreadInfoValidator),
});

export type FetchCommunityInfosResponse = {
+communityInfos: $ReadOnlyArray<ServerCommunityInfo>,
};

export type FetchAllCommunityInfosWithNamesResponse = {
export type ServerFetchAllCommunityInfosWithNamesResponse = {
+allCommunityInfosWithNames: $ReadOnlyArray<ServerCommunityInfoWithCommunityName>,
};

export type ClientFetchAllCommunityInfosWithNamesResponse = {
+allCommunityInfosWithNames: $ReadOnlyArray<ClientCommunityInfoWithCommunityName>,
};

export type CreateOrUpdateFarcasterChannelTagRequest = {
+commCommunityID: string,
+farcasterChannelID: string,
Expand Down
4 changes: 2 additions & 2 deletions lib/types/redux-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type {
CommunityStore,
AddCommunityPayload,
FetchCommunityInfosResponse,
FetchAllCommunityInfosWithNamesResponse,
ClientFetchAllCommunityInfosWithNamesResponse,
CreateOrUpdateFarcasterChannelTagResponse,
DeleteFarcasterChannelTagPayload,
} from './community-types.js';
Expand Down Expand Up @@ -1474,7 +1474,7 @@ export type BaseAction = $ReadOnly<{
}
| {
+type: 'FETCH_ALL_COMMUNITY_INFOS_WITH_NAMES_SUCCESS',
+payload: FetchAllCommunityInfosWithNamesResponse,
+payload: ClientFetchAllCommunityInfosWithNamesResponse,
+loadingInfo: LoadingInfo,
}
| {
Expand Down
8 changes: 4 additions & 4 deletions lib/types/validators/community-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import t, { type TInterface } from 'tcomb';
import { tShape } from '../../utils/validation-utils.js';
import {
serverCommunityInfoValidator,
serverCommunityInfoWithCommunityNameValidator,
clientCommunityInfoWithCommunityNameValidator,
type FetchCommunityInfosResponse,
type FetchAllCommunityInfosWithNamesResponse,
type ClientFetchAllCommunityInfosWithNamesResponse,
} from '../community-types.js';

const fetchCommunityInfosResponseValidator: TInterface<FetchCommunityInfosResponse> =
tShape({
communityInfos: t.list(serverCommunityInfoValidator),
});

const fetchAllCommunityInfosWithNamesResponseValidator: TInterface<FetchAllCommunityInfosWithNamesResponse> =
const fetchAllCommunityInfosWithNamesResponseValidator: TInterface<ClientFetchAllCommunityInfosWithNamesResponse> =
tShape({
allCommunityInfosWithNames: t.list(
serverCommunityInfoWithCommunityNameValidator,
clientCommunityInfoWithCommunityNameValidator,
),
});

Expand Down

0 comments on commit 183ac09

Please sign in to comment.