mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
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"
This commit is contained in:
parent
c0a00cabe4
commit
b591912c74
2 changed files with 31 additions and 3 deletions
|
@ -234,9 +234,10 @@ export class ActivityPubAPI {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async follow(username: string): Promise<void> {
|
async follow(username: string): Promise<Actor> {
|
||||||
const url = new URL(`.ghost/activitypub/actions/follow/${username}`, this.apiUrl);
|
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<Actor> {
|
async getActor(url: string): Promise<Actor> {
|
||||||
|
|
|
@ -282,13 +282,40 @@ export function useSearchForUser(handle: string, query: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useFollow(handle: string, onSuccess: () => void, onError: () => void) {
|
export function useFollow(handle: string, onSuccess: () => void, onError: () => void) {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
return useMutation({
|
return useMutation({
|
||||||
async mutationFn(username: string) {
|
async mutationFn(username: string) {
|
||||||
const siteUrl = await getSiteUrl();
|
const siteUrl = await getSiteUrl();
|
||||||
const api = createActivityPubAPI(handle, siteUrl);
|
const api = createActivityPubAPI(handle, siteUrl);
|
||||||
return api.follow(username);
|
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
|
onError
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue