pl-fe: account relationships migration

Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
nicole mikołajczyk
2025-10-23 15:41:01 +02:00
parent 593b35b21f
commit a922287246
20 changed files with 148 additions and 198 deletions

View File

@ -6,8 +6,7 @@ import { useClient } from 'pl-fe/hooks/use-client';
import { useFeatures } from 'pl-fe/hooks/use-features';
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
import { type Account, normalizeAccount } from 'pl-fe/normalizers/account';
import { useRelationship } from './use-relationship';
import { useRelationshipQuery } from 'pl-fe/queries/accounts/use-relationship';
import type { Account as BaseAccount } from 'pl-api';
@ -29,9 +28,9 @@ const useAccountLookup = (acct: string | undefined, opts: UseAccountLookupOpts =
);
const {
relationship,
data: relationship,
isLoading: isRelationshipLoading,
} = useRelationship(entity?.id, { enabled: withRelationship });
} = useRelationshipQuery(withRelationship ? entity?.id : undefined);
const isBlocked = entity?.relationship?.blocked_by === true;
const isUnavailable = (me === entity?.id) ? false : (isBlocked && !features.blockersVisible);

View File

@ -7,8 +7,7 @@ import { useClient } from 'pl-fe/hooks/use-client';
import { useFeatures } from 'pl-fe/hooks/use-features';
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
import { type Account, normalizeAccount } from 'pl-fe/normalizers/account';
import { useRelationship } from './use-relationship';
import { useRelationshipQuery } from 'pl-fe/queries/accounts/use-relationship';
import type { Account as BaseAccount } from 'pl-api';
@ -31,9 +30,9 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => {
const meta = useAppSelector((state) => accountId ? state.accounts_meta[accountId] : undefined);
const {
relationship,
data: relationship,
isLoading: isRelationshipLoading,
} = useRelationship(accountId, { enabled: withRelationship });
} = useRelationshipQuery(withRelationship ? entity?.id : undefined);
const isBlocked = entity?.relationship?.blocked_by === true;
const isUnavailable = (me === entity?.id) ? false : (isBlocked && !features.blockersVisible);

View File

@ -1,88 +0,0 @@
import { importEntities } from 'pl-fe/entity-store/actions';
import { Entities } from 'pl-fe/entity-store/entities';
import { useTransaction } from 'pl-fe/entity-store/hooks/use-transaction';
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
import { useClient } from 'pl-fe/hooks/use-client';
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
interface FollowOpts {
reblogs?: boolean;
notify?: boolean;
languages?: string[];
}
const useFollow = () => {
const client = useClient();
const dispatch = useAppDispatch();
const { isLoggedIn } = useLoggedIn();
const { transaction } = useTransaction();
const followEffect = (accountId: string) => {
transaction({
Accounts: {
[accountId]: (account) => ({
...account,
followers_count: account.followers_count + 1,
}),
},
Relationships: {
[accountId]: (relationship) => ({
...relationship,
following: true,
}),
},
});
};
const unfollowEffect = (accountId: string) => {
transaction({
Accounts: {
[accountId]: (account) => ({
...account,
followers_count: Math.max(0, account.followers_count - 1),
}),
},
Relationships: {
[accountId]: (relationship) => ({
...relationship,
following: false,
requested: false,
}),
},
});
};
const follow = async (accountId: string, options: FollowOpts = {}) => {
if (!isLoggedIn) return;
followEffect(accountId);
try {
const response = await client.accounts.followAccount(accountId, options);
if (response.id) {
dispatch(importEntities([response], Entities.RELATIONSHIPS));
}
} catch (e) {
unfollowEffect(accountId);
}
};
const unfollow = async (accountId: string) => {
if (!isLoggedIn) return;
unfollowEffect(accountId);
try {
await client.accounts.unfollowAccount(accountId);
} catch (e) {
followEffect(accountId);
}
};
return {
follow,
unfollow,
followEffect,
unfollowEffect,
};
};
export { useFollow };

View File

@ -1,31 +0,0 @@
import * as v from 'valibot';
import { Entities } from 'pl-fe/entity-store/entities';
import { useEntity } from 'pl-fe/entity-store/hooks/use-entity';
import { useClient } from 'pl-fe/hooks/use-client';
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
import type { Relationship } from 'pl-api';
interface UseRelationshipOpts {
enabled?: boolean;
}
const useRelationship = (accountId: string | undefined, opts: UseRelationshipOpts = {}) => {
const client = useClient();
const { isLoggedIn } = useLoggedIn();
const { enabled = false } = opts;
const { entity: relationship, ...result } = useEntity<Relationship>(
[Entities.RELATIONSHIPS, accountId!],
() => client.accounts.getRelationships([accountId!]),
{
enabled: enabled && isLoggedIn && !!accountId,
schema: v.pipe(v.any(), v.transform(arr => arr[0])),
},
);
return { relationship, ...result };
};
export { useRelationship };

View File

@ -8,9 +8,6 @@ import { getLocale } from 'pl-fe/actions/settings';
import { updateStatus } from 'pl-fe/actions/statuses';
import { deleteFromTimelines, processTimelineUpdate } from 'pl-fe/actions/timelines';
import { useStatContext } from 'pl-fe/contexts/stat-context';
import { importEntities } from 'pl-fe/entity-store/actions';
import { Entities } from 'pl-fe/entity-store/entities';
import { selectEntity } from 'pl-fe/entity-store/selectors';
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
import messages from 'pl-fe/messages';
@ -73,16 +70,12 @@ const updateFollowRelationships = (update: FollowRelationshipUpdate) =>
const state = getState();
const me = state.me;
const relationship = selectEntity<Relationship>(state, Entities.RELATIONSHIPS, update.following.id);
if (update.follower.id === me && relationship) {
const updated = {
if (update.follower.id === me) {
queryClient.setQueryData<Relationship>(['accountRelationships', update.following.id], (relationship) => relationship ? ({
...relationship,
...followStateToRelationship(update.state),
};
// Add a small delay to deal with API race conditions.
setTimeout(() => dispatch(importEntities([updated], Entities.RELATIONSHIPS)), 300);
}) : undefined);
}
};