Skip to content

Commit

Permalink
refactor: move filter to query-side
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-ext committed Apr 1, 2024
1 parent 1d953c0 commit 19e2876
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 37 deletions.
8 changes: 5 additions & 3 deletions app/api/moderation/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { SignalizedProfile } from '../stores/profiles';

import { ContextProfileList, getModerationUI, type ModerationOptions } from '.';
import { type ModerationOptions, ContextProfileList, getModerationUI } from '.';
import { moderateProfile } from './entities/profile';

export const moderateProfileList = (
profiles: SignalizedProfile[] | undefined,
opts: ModerationOptions,
opts: ModerationOptions | undefined,
): SignalizedProfile[] | undefined => {
if (profiles) {
if (opts && profiles) {
return profiles.filter((profile) => {
const causes = moderateProfile(profile, opts);
const ui = getModerationUI(causes, ContextProfileList);

return ui.f.length === 0;
});
}

return profiles;
};
7 changes: 6 additions & 1 deletion app/api/queries/get-likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { multiagent } from '../globals/agent';

import { mergeProfile } from '../stores/profiles';

import { moderateProfileList } from '../moderation/utils';

export const getLikesKey = (uid: At.DID, uri: string, limit = 25) => {
return ['getLikes', uid, uri, limit] as const;
};
Expand All @@ -26,6 +28,9 @@ export const getLikes = async (ctx: QC<ReturnType<typeof getLikesKey>, string |

return {
cursor: data.cursor,
profiles: data.likes.map((like) => mergeProfile(uid, like.actor)),
profiles: moderateProfileList(
data.likes.map((like) => mergeProfile(uid, like.actor)),
ctx.meta?.moderation,
),
};
};
7 changes: 6 additions & 1 deletion app/api/queries/get-post-reposts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { multiagent } from '../globals/agent';

import { mergeProfile } from '../stores/profiles';

import { moderateProfileList } from '../moderation/utils';

export const getPostRepostsKey = (uid: At.DID, uri: string, limit = 25) => {
return ['getPostReposts', uid, uri, limit] as const;
};
Expand All @@ -26,6 +28,9 @@ export const getPostReposts = async (ctx: QC<ReturnType<typeof getPostRepostsKey

return {
cursor: data.cursor,
profiles: data.repostedBy.map((actor) => mergeProfile(uid, actor)),
profiles: moderateProfileList(
data.repostedBy.map((actor) => mergeProfile(uid, actor)),
ctx.meta?.moderation,
),
};
};
7 changes: 6 additions & 1 deletion app/api/queries/get-profile-followers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { multiagent } from '../globals/agent';

import { getCachedProfile, mergeProfile } from '../stores/profiles';

import { moderateProfileList } from '../moderation/utils';

export const getProfileFollowersKey = (uid: At.DID, actor: string, limit = 25) => {
return ['getProfileFollowers', uid, actor, limit] as const;
};
Expand All @@ -29,7 +31,10 @@ export const getProfileFollowers = async (
return {
cursor: data.cursor,
subject: mergeProfile(uid, data.subject),
profiles: data.followers.map((actor) => mergeProfile(uid, actor)),
profiles: moderateProfileList(
data.followers.map((actor) => mergeProfile(uid, actor)),
ctx.meta?.moderation,
),
};
};

Expand Down
7 changes: 6 additions & 1 deletion app/api/queries/get-profile-follows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { multiagent } from '../globals/agent';

import { getCachedProfile, mergeProfile } from '../stores/profiles';

import { moderateProfileList } from '../moderation/utils';

export const getProfileFollowsKey = (uid: At.DID, actor: string, limit = 25) => {
return ['getProfileFollows', uid, actor, limit] as const;
};
Expand All @@ -29,7 +31,10 @@ export const getProfileFollows = async (
return {
cursor: data.cursor,
subject: mergeProfile(uid, data.subject),
profiles: data.follows.map((actor) => mergeProfile(uid, actor)),
profiles: moderateProfileList(
data.follows.map((actor) => mergeProfile(uid, actor)),
ctx.meta?.moderation,
),
};
};

Expand Down
10 changes: 4 additions & 6 deletions app/desktop/components/panes/dialogs/FeedLikedByPaneDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import type { At } from '~/api/atp-schema';
import { getFeedInfo, getFeedInfoKey, getInitialFeedInfo } from '~/api/queries/get-feed-info';
import { getLikes, getLikesKey } from '~/api/queries/get-likes';

import { moderateProfileList } from '~/api/moderation/utils';

import { getModerationOptions } from '~/com/globals/shared';

import ProfileList from '~/com/components/lists/ProfileList';
Expand Down Expand Up @@ -48,6 +46,9 @@ const FeedLikedByPaneDialog = (props: FeedLikedByPaneDialogProps) => {
queryFn: getLikes,
initialPageParam: undefined,
getNextPageParam: (last) => last.cursor,
meta: {
moderation: getModerationOptions(),
},
};
});

Expand All @@ -66,10 +67,7 @@ const FeedLikedByPaneDialog = (props: FeedLikedByPaneDialogProps) => {

<div class="flex min-h-0 grow flex-col overflow-y-auto">
<ProfileList
profiles={moderateProfileList(
likes.data?.pages.flatMap((page) => page.profiles),
getModerationOptions(),
)}
profiles={likes.data?.pages.flatMap((page) => page.profiles)}
fetching={likes.isFetching}
error={likes.error}
hasMore={likes.hasNextPage}
Expand Down
10 changes: 4 additions & 6 deletions app/desktop/components/panes/dialogs/PostLikedByPaneDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import type { At } from '~/api/atp-schema';

import { getLikes, getLikesKey } from '~/api/queries/get-likes';

import { moderateProfileList } from '~/api/moderation/utils';

import { getModerationOptions } from '~/com/globals/shared';

import ProfileList from '~/com/components/lists/ProfileList';
Expand Down Expand Up @@ -36,6 +34,9 @@ const PostLikedByPaneDialog = (props: PostLikedByDialogProps) => {
queryFn: getLikes,
initialPageParam: undefined,
getNextPageParam: (last) => last.cursor,
meta: {
moderation: getModerationOptions(),
},
};
});

Expand All @@ -45,10 +46,7 @@ const PostLikedByPaneDialog = (props: PostLikedByDialogProps) => {

<div class="flex min-h-0 grow flex-col overflow-y-auto">
<ProfileList
profiles={moderateProfileList(
likes.data?.pages.flatMap((page) => page.profiles),
getModerationOptions(),
)}
profiles={likes.data?.pages.flatMap((page) => page.profiles)}
fetching={likes.isFetching}
error={likes.error}
hasMore={likes.hasNextPage}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import type { At } from '~/api/atp-schema';

import { getPostReposts, getPostRepostsKey } from '~/api/queries/get-post-reposts';

import { moderateProfileList } from '~/api/moderation/utils';

import { getModerationOptions } from '~/com/globals/shared';

import ProfileList from '~/com/components/lists/ProfileList';
Expand Down Expand Up @@ -36,6 +34,9 @@ const PostRepostedByPaneDialog = (props: PostRepostedByDialogProps) => {
queryFn: getPostReposts,
initialPageParam: undefined,
getNextPageParam: (last) => last.cursor,
meta: {
moderation: getModerationOptions(),
},
};
});

Expand All @@ -45,10 +46,7 @@ const PostRepostedByPaneDialog = (props: PostRepostedByDialogProps) => {

<div class="flex min-h-0 grow flex-col overflow-y-auto">
<ProfileList
profiles={moderateProfileList(
reposts.data?.pages.flatMap((page) => page.profiles),
getModerationOptions(),
)}
profiles={reposts.data?.pages.flatMap((page) => page.profiles)}
fetching={reposts.isFetching}
error={reposts.error}
hasMore={reposts.hasNextPage}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
getProfileFollowersKey,
} from '~/api/queries/get-profile-followers';

import { moderateProfileList } from '~/api/moderation/utils';

import { getModerationOptions } from '~/com/globals/shared';

import { ProfileFollowAccessory } from '~/com/components/items/ProfileItem';
Expand Down Expand Up @@ -42,6 +40,9 @@ const ProfileFollowersPaneDialog = (props: ProfileFollowersPaneDialogProps) => {
initialPageParam: undefined,
getNextPageParam: (last) => last.cursor,
placeholderData: () => getInitialProfileFollowers(key),
meta: {
moderation: getModerationOptions(),
},
};
});

Expand All @@ -65,10 +66,7 @@ const ProfileFollowersPaneDialog = (props: ProfileFollowersPaneDialogProps) => {
<div class="flex min-h-0 grow flex-col overflow-y-auto">
<ProfileList
asideAccessory={ProfileFollowAccessory}
profiles={moderateProfileList(
followers.data?.pages.flatMap((page) => page.profiles),
getModerationOptions(),
)}
profiles={followers.data?.pages.flatMap((page) => page.profiles)}
fetching={followers.isFetching}
error={followers.error}
hasMore={followers.hasNextPage}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
getProfileFollowsKey,
} from '~/api/queries/get-profile-follows';

import { moderateProfileList } from '~/api/moderation/utils';

import { getModerationOptions } from '~/com/globals/shared';

import { ProfileFollowAccessory } from '~/com/components/items/ProfileItem';
Expand Down Expand Up @@ -42,6 +40,9 @@ const ProfileFollowsPaneDialog = (props: ProfileFollowsPaneDialogProps) => {
initialPageParam: undefined,
getNextPageParam: (last) => last.cursor,
placeholderData: () => getInitialProfileFollows(key),
meta: {
moderation: getModerationOptions(),
},
};
});

Expand All @@ -65,10 +66,7 @@ const ProfileFollowsPaneDialog = (props: ProfileFollowsPaneDialogProps) => {
<div class="flex min-h-0 grow flex-col overflow-y-auto">
<ProfileList
asideAccessory={ProfileFollowAccessory}
profiles={moderateProfileList(
follows.data?.pages.flatMap((page) => page.profiles),
getModerationOptions(),
)}
profiles={follows.data?.pages.flatMap((page) => page.profiles)}
fetching={follows.isFetching}
error={follows.error}
hasMore={follows.hasNextPage}
Expand Down

0 comments on commit 19e2876

Please sign in to comment.