diff --git a/apps/admin-x-activitypub/src/api/activitypub.ts b/apps/admin-x-activitypub/src/api/activitypub.ts index 1c84aadb8f..ab37d24788 100644 --- a/apps/admin-x-activitypub/src/api/activitypub.ts +++ b/apps/admin-x-activitypub/src/api/activitypub.ts @@ -234,9 +234,10 @@ export class ActivityPubAPI { }; } - async follow(username: string): Promise { + async follow(username: string): Promise { const url = new URL(`.ghost/activitypub/actions/follow/${username}`, this.apiUrl); - await this.fetchJSON(url, 'POST'); + const json = await this.fetchJSON(url, 'POST'); + return json as Actor; } async getActor(url: string): Promise { diff --git a/apps/admin-x-activitypub/src/hooks/useActivityPubQueries.ts b/apps/admin-x-activitypub/src/hooks/useActivityPubQueries.ts index 85f3481816..13fce3194b 100644 --- a/apps/admin-x-activitypub/src/hooks/useActivityPubQueries.ts +++ b/apps/admin-x-activitypub/src/hooks/useActivityPubQueries.ts @@ -282,13 +282,40 @@ export function useSearchForUser(handle: string, query: string) { } export function useFollow(handle: string, onSuccess: () => void, onError: () => void) { + const queryClient = useQueryClient(); return useMutation({ async mutationFn(username: string) { const siteUrl = await getSiteUrl(); const api = createActivityPubAPI(handle, siteUrl); return api.follow(username); }, - onSuccess, + onSuccess(followedActor, fullHandle) { + queryClient.setQueryData([`profile:${fullHandle}`], (currentProfile: unknown) => { + if (!currentProfile) { + return currentProfile; + } + return { + ...currentProfile, + isFollowing: true + }; + }); + + queryClient.setQueryData(['following:index'], (currentFollowing?: unknown[]) => { + if (!currentFollowing) { + return currentFollowing; + } + return [followedActor].concat(currentFollowing); + }); + + queryClient.setQueryData(['followingCount:index'], (currentFollowingCount?: number) => { + if (!currentFollowingCount) { + return 1; + } + return currentFollowingCount + 1; + }); + + onSuccess(); + }, onError }); }