Skip to content

Commit

Permalink
feat: add feedToFilters logic for blocked users (#2597)
Browse files Browse the repository at this point in the history
Co-authored-by: capJavert <dev@kickass.website>
  • Loading branch information
ilasw and capJavert authored Jan 16, 2025
1 parent 7f9c7d1 commit b1c9e16
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 10 deletions.
3 changes: 3 additions & 0 deletions __tests__/__snapshots__/feeds.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Object {
"excludeSourceTypes": Array [],
"excludeSources": Array [],
"excludeTypes": Array [],
"excludeUsers": Array [],
"followingSources": Array [],
"followingUsers": Array [],
"includeTags": Array [],
Expand All @@ -27,6 +28,7 @@ Object {
],
"excludeSources": Array [],
"excludeTypes": Array [],
"excludeUsers": Array [],
"followingSources": Array [],
"followingUsers": Array [],
"includeTags": Array [],
Expand All @@ -42,6 +44,7 @@ Object {
"excludeSourceTypes": Array [],
"excludeSources": Array [],
"excludeTypes": Array [],
"excludeUsers": Array [],
"followingSources": Array [],
"followingUsers": Array [],
"includeTags": Array [],
Expand Down
30 changes: 30 additions & 0 deletions __tests__/feeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ describe('query anonymousFeed', () => {
const filters = await feedToFilters(con, '1', '1');
delete filters.sourceIds;
delete filters.excludeTypes;
delete filters.excludeUsers;
const res = await client.query(QUERY, {
variables: { ...variables, filters },
});
Expand Down Expand Up @@ -2960,6 +2961,35 @@ describe('function feedToFilters', () => {
expect(filters.followingUsers).toEqual(['2', '3']);
});

it('should return filters having excluded users based on content preference', async () => {
loggedUser = '1';
await saveAdvancedSettingsFiltersFixtures();
await con.getRepository(ContentPreferenceUser).save([
{
feedId: '1',
userId: '1',
status: ContentPreferenceStatus.Follow,
referenceId: '2',
},
{
feedId: '1',
userId: '1',
status: ContentPreferenceStatus.Subscribed,
referenceId: '3',
},
{
feedId: '1',
userId: '1',
status: ContentPreferenceStatus.Blocked,
referenceId: '4',
type: ContentPreferenceType.User,
},
]);
const filters = await feedToFilters(con, '1', '1');
expect(filters.followingUsers).toEqual(['2', '3']);
expect(filters.excludeUsers).toEqual(['4']);
});

it('should return filters having excluded content types based on advanced settings', async () => {
loggedUser = '1';
await saveAdvancedSettingsFiltersFixtures();
Expand Down
2 changes: 2 additions & 0 deletions __tests__/integrations/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ describe('FeedPreferencesConfigGenerator', () => {
includeBlockedWords: true,
includeFollowedUsers: true,
includeFollowedSources: true,
includeBlockedUsers: true,
},
);

Expand All @@ -327,6 +328,7 @@ describe('FeedPreferencesConfigGenerator', () => {
blocked_sources: expect.arrayContaining(['a', 'b']),
blocked_tags: expect.arrayContaining(['python', 'java']),
blocked_title_words: expect.arrayContaining(['word-abc', 'word-def']),
blocked_author_ids: expect.arrayContaining(['4']),
followed_sources: expect.arrayContaining(['c', 'p']),
followed_user_ids: expect.arrayContaining(['2', '3']),
allowed_post_types: postTypes.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ Object {
exports[`personalizedDigestEmail worker should generate personalized digest for user in timezone ahead UTC 2`] = `
Object {
"allowed_tags": Array [],
"blocked_author_ids": Array [],
"blocked_sources": Array [],
"blocked_tags": Array [],
"date_from": Any<String>,
Expand Down Expand Up @@ -328,6 +329,7 @@ Object {
exports[`personalizedDigestEmail worker should generate personalized digest for user in timezone behind UTC 2`] = `
Object {
"allowed_tags": Array [],
"blocked_author_ids": Array [],
"blocked_sources": Array [],
"blocked_tags": Array [],
"date_from": Any<String>,
Expand Down Expand Up @@ -449,6 +451,7 @@ Object {
exports[`personalizedDigestEmail worker should generate personalized digest for user with provided config 2`] = `
Object {
"allowed_tags": Array [],
"blocked_author_ids": Array [],
"blocked_sources": Array [],
"blocked_tags": Array [],
"date_from": Any<String>,
Expand Down Expand Up @@ -570,6 +573,7 @@ Object {
exports[`personalizedDigestEmail worker should generate personalized digest for user with subscription 2`] = `
Object {
"allowed_tags": Array [],
"blocked_author_ids": Array [],
"blocked_sources": Array [],
"blocked_tags": Array [],
"date_from": Any<String>,
Expand Down
22 changes: 12 additions & 10 deletions src/common/feedGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type RawFiltersData = {
| null;
tags: Pick<ContentPreferenceKeyword, 'keywordId' | 'status'>[] | null;
words: Pick<ContentPreferenceWord, 'referenceId'>[] | null;
users: Pick<ContentPreferenceUser, 'referenceId'>[] | null;
users: Pick<ContentPreferenceUser, 'referenceId' | 'status'>[] | null;
sources: Pick<ContentPreferenceSource, 'sourceId' | 'status'>[] | null;
memberships: { sourceId: SourceMember['sourceId']; hide: boolean }[] | null;
feeds: Pick<Feed, 'flags' | 'type'>[] | null;
Expand Down Expand Up @@ -136,12 +136,11 @@ const getRawFiltersData = async (
),
rawFilterSelect(con, 'users', (qb) =>
qb
.select('"referenceId"')
.select(['"referenceId"', 'status'])
.from(ContentPreference, 'u')
.where('"feedId" = $1')
.andWhere('"userId" = $2')
.andWhere(`type = '${ContentPreferenceType.User}'`)
.andWhere(`status != '${ContentPreferenceStatus.Blocked}'`),
.andWhere(`type = '${ContentPreferenceType.User}'`),
),
rawFilterSelect(con, 'sources', (qb) =>
qb
Expand Down Expand Up @@ -253,15 +252,17 @@ const wordsToFilters = ({

const usersToFilters = ({
users,
}: RawFiltersData): {
followingUsers: string[];
} => {
}: RawFiltersData): Record<'followingUsers' | 'excludeUsers', string[]> => {
return (users || []).reduce<ReturnType<typeof usersToFilters>>(
(acc, value) => {
acc.followingUsers.push(value.referenceId);
(acc, { referenceId, status }) => {
if (status === ContentPreferenceStatus.Blocked) {
acc.excludeUsers.push(referenceId);
} else {
acc.followingUsers.push(referenceId);
}
return acc;
},
{ followingUsers: [] },
{ followingUsers: [], excludeUsers: [] },
);
};

Expand Down Expand Up @@ -663,6 +664,7 @@ export interface AnonymousFeedFilters {
blockedWords?: string[];
excludeSourceTypes?: string[];
followingUsers?: string[];
excludeUsers?: string[];
followingSources?: string[];
flags?: FeedFlagsFilters;
}
Expand Down
1 change: 1 addition & 0 deletions src/common/personalizedDigest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export const getPersonalizedDigestEmailPayload = async ({
) || [],
page_size: feature.maxPosts,
total_pages: 1,
blocked_author_ids: feedConfig.excludeUsers,
};
const feedResponse = await personalizedDigestFeedClient.fetchFeed(
{ log: logger },
Expand Down
8 changes: 8 additions & 0 deletions src/integrations/feed/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Options = {
includeBlockedTags?: boolean;
includeAllowedSources?: boolean;
includeBlockedSources?: boolean;
includeBlockedUsers?: boolean;
includeAllowedUsers?: boolean;
includeSourceMemberships?: boolean;
includePostTypes?: boolean;
Expand Down Expand Up @@ -167,6 +168,13 @@ const addFiltersToConfig = ({
);
}

if (filters.excludeUsers?.length && opts.includeBlockedUsers) {
baseConfig.blocked_author_ids = mergeSingleFilter(
baseConfig.blocked_author_ids,
filters.excludeUsers,
);
}

return baseConfig;
};

Expand Down
2 changes: 2 additions & 0 deletions src/integrations/feed/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const opts: Options = {
includeBlockedWords: true,
includeFollowedSources: true,
includeFollowedUsers: true,
includeBlockedUsers: true,
};

export const feedGenerators: Partial<Record<FeedVersion, FeedGenerator>> =
Expand All @@ -113,6 +114,7 @@ export const feedGenerators: Partial<Record<FeedVersion, FeedGenerator>> =
includeBlockedTags: true,
includeContentCuration: true,
includeBlockedWords: true,
includeBlockedUsers: true,
},
),
'popular',
Expand Down
1 change: 1 addition & 0 deletions src/integrations/feed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type FeedConfig = {
allowed_content_curations?: string[];
blocked_title_words?: string[];
allowed_author_ids?: string[];
blocked_author_ids?: string[];
followed_user_ids?: string[];
followed_sources?: string[];
squad_ids?: string[];
Expand Down
1 change: 1 addition & 0 deletions src/schema/feeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,7 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
includeContentCuration: true,
includeBlockedWords: true,
includeAllowedUsers: true,
includeBlockedUsers: true,
includeAllowedSources: true,
feedId: feedId,
},
Expand Down

0 comments on commit b1c9e16

Please sign in to comment.