Skip to content

Commit b591912

Browse files
authored
Updated the local state after following an account (#21509)
refs https://linear.app/ghost/issue/AP-523 We want to preempt the Accept activity from our Follows, so we make the assumption that it's succeeded. What this means is that we have to update our `following`, `followingCount` as well as the fetched profile to set the `isFollowing` property. This gives a more fluid user experience when following accounts and keeps our state up to date. Accounts where the follow request has to be accepted manually, are a little trickier as we don't currently have easy access to the "requested but not accepted state"
1 parent c0a00ca commit b591912

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

apps/admin-x-activitypub/src/api/activitypub.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,10 @@ export class ActivityPubAPI {
234234
};
235235
}
236236

237-
async follow(username: string): Promise<void> {
237+
async follow(username: string): Promise<Actor> {
238238
const url = new URL(`.ghost/activitypub/actions/follow/${username}`, this.apiUrl);
239-
await this.fetchJSON(url, 'POST');
239+
const json = await this.fetchJSON(url, 'POST');
240+
return json as Actor;
240241
}
241242

242243
async getActor(url: string): Promise<Actor> {

apps/admin-x-activitypub/src/hooks/useActivityPubQueries.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,40 @@ export function useSearchForUser(handle: string, query: string) {
282282
}
283283

284284
export function useFollow(handle: string, onSuccess: () => void, onError: () => void) {
285+
const queryClient = useQueryClient();
285286
return useMutation({
286287
async mutationFn(username: string) {
287288
const siteUrl = await getSiteUrl();
288289
const api = createActivityPubAPI(handle, siteUrl);
289290
return api.follow(username);
290291
},
291-
onSuccess,
292+
onSuccess(followedActor, fullHandle) {
293+
queryClient.setQueryData([`profile:${fullHandle}`], (currentProfile: unknown) => {
294+
if (!currentProfile) {
295+
return currentProfile;
296+
}
297+
return {
298+
...currentProfile,
299+
isFollowing: true
300+
};
301+
});
302+
303+
queryClient.setQueryData(['following:index'], (currentFollowing?: unknown[]) => {
304+
if (!currentFollowing) {
305+
return currentFollowing;
306+
}
307+
return [followedActor].concat(currentFollowing);
308+
});
309+
310+
queryClient.setQueryData(['followingCount:index'], (currentFollowingCount?: number) => {
311+
if (!currentFollowingCount) {
312+
return 1;
313+
}
314+
return currentFollowingCount + 1;
315+
});
316+
317+
onSuccess();
318+
},
292319
onError
293320
});
294321
}

0 commit comments

Comments
 (0)