Skip to content

Commit

Permalink
refactor: adjustments to popular and my feed settings (#2565)
Browse files Browse the repository at this point in the history
  • Loading branch information
idoshamun authored Dec 23, 2024
1 parent 71e0cfb commit 0e32cf1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
6 changes: 6 additions & 0 deletions __tests__/feeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ describe('query anonymousFeed', () => {
fresh_page_size: '4',
offset: 0,
user_id: '1',
source_types: ['machine', 'squad'],
allowed_languages: ['en'],
min_day_range: 14,
})
.reply(200, {
data: [{ post_id: 'p1' }, { post_id: 'p4' }],
Expand Down Expand Up @@ -527,6 +530,9 @@ describe('query anonymousFeed', () => {
blocked_tags: ['python', 'java'],
blocked_sources: ['a', 'b'],
user_id: '1',
source_types: ['machine', 'squad'],
allowed_languages: ['en'],
min_day_range: 14,
})
.reply(200, {
data: [{ post_id: 'p1' }, { post_id: 'p4' }],
Expand Down
62 changes: 51 additions & 11 deletions src/integrations/feed/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ export class SimpleFeedConfigGenerator implements FeedConfigGenerator {
}
}

const mergeSingleFilter = (
base: string[] | undefined,
filter: string[] | undefined,
): string[] | undefined => {
if (!filter) {
return base;
}
return base ? Array.from(new Set([...base, ...filter])) : filter;
};

const addFiltersToConfig = ({
config,
filters,
Expand All @@ -85,16 +95,28 @@ const addFiltersToConfig = ({
const baseConfig = { ...config, ...filters.flags };

if (filters.includeTags?.length && opts.includeAllowedTags) {
baseConfig.allowed_tags = filters.includeTags;
baseConfig.allowed_tags = mergeSingleFilter(
baseConfig.allowed_tags,
filters.includeTags,
);
}
if (filters.blockedTags?.length && opts.includeBlockedTags) {
baseConfig.blocked_tags = filters.blockedTags;
baseConfig.blocked_tags = mergeSingleFilter(
baseConfig.blocked_tags,
filters.blockedTags,
);
}
if (filters.excludeSources?.length && opts.includeBlockedSources) {
baseConfig.blocked_sources = filters.excludeSources;
baseConfig.blocked_sources = mergeSingleFilter(
baseConfig.blocked_sources,
filters.excludeSources,
);
}
if (filters.sourceIds?.length && opts.includeSourceMemberships) {
baseConfig.squad_ids = filters.sourceIds;
baseConfig.squad_ids = mergeSingleFilter(
baseConfig.squad_ids,
filters.sourceIds,
);
}
if (filters.excludeTypes?.length && opts.includePostTypes) {
baseConfig.allowed_post_types = (
Expand All @@ -107,24 +129,42 @@ const addFiltersToConfig = ({
);
}
if (filters.blockedContentCuration?.length && opts.includeContentCuration) {
baseConfig.allowed_content_curations = AllowedContentCurationTypes.filter(
(type) => !filters.blockedContentCuration!.includes(type),
baseConfig.allowed_content_curations = mergeSingleFilter(
baseConfig.allowed_content_curations,
AllowedContentCurationTypes.filter(
(type) => !filters.blockedContentCuration!.includes(type),
),
);
}
if (filters.blockedWords?.length && opts.includeBlockedWords) {
baseConfig.blocked_title_words = filters.blockedWords;
baseConfig.blocked_title_words = mergeSingleFilter(
baseConfig.blocked_title_words,
filters.blockedWords,
);
}
if (filters.followingSources?.length && opts.includeFollowedSources) {
baseConfig.followed_sources = filters.followingSources;
baseConfig.followed_sources = mergeSingleFilter(
baseConfig.followed_sources,
filters.followingSources,
);
}
if (filters.followingSources?.length && opts.includeAllowedSources) {
baseConfig.allowed_sources = filters.followingSources;
baseConfig.allowed_sources = mergeSingleFilter(
baseConfig.allowed_sources,
filters.followingSources,
);
}
if (filters.followingUsers?.length && opts.includeFollowedUsers) {
baseConfig.followed_user_ids = filters.followingUsers;
baseConfig.followed_user_ids = mergeSingleFilter(
baseConfig.followed_user_ids,
filters.followingUsers,
);
}
if (filters.followingUsers?.length && opts.includeAllowedUsers) {
baseConfig.allowed_author_ids = filters.followingUsers;
baseConfig.allowed_author_ids = mergeSingleFilter(
baseConfig.allowed_author_ids,
filters.followingUsers,
);
}

return baseConfig;
Expand Down
4 changes: 3 additions & 1 deletion src/integrations/feed/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,16 @@ const opts: Options = {
includePostTypes: true,
includeContentCuration: true,
includeBlockedWords: true,
includeFollowedSources: true,
includeFollowedUsers: true,
};

export const feedGenerators: Partial<Record<FeedVersion, FeedGenerator>> =
Object.freeze({
popular: new FeedGenerator(
popularFeedClient,
new FeedPreferencesConfigGenerator(
{},
{ ...baseFeedConfig, min_day_range: 14 },
{
includePostTypes: true,
includeBlockedSources: true,
Expand Down

0 comments on commit 0e32cf1

Please sign in to comment.