Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: favorite categories segmentation logic #1177

Merged
merged 6 commits into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/criteria/default/favorite-categories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { setMatchingFunction } from '../utils';

setMatchingFunction( 'favorite_categories', ( config, ras ) => {
let match = false;
const views = ras.getUniqueActivitiesBy( 'article_view', 'post_id' );

if ( 1 >= views.length ) {
return match;
}

const categories = views.reduce( ( c, v ) => {
if ( v.data?.categories?.length ) {
c.push( ...v.data.categories );
Expand All @@ -14,6 +20,17 @@ setMatchingFunction( 'favorite_categories', ( config, ras ) => {
}, {} );
const countsArray = Object.entries( counts );
countsArray.sort( ( a, b ) => b[ 1 ] - a[ 1 ] );
/* TODO: Decide how to rank categories. */
return false;

if ( ! countsArray || ! countsArray.length ) {
return match;
}

// Must have viewed at least 2 categories or 2 posts within the same category in order to rank.
if ( ! countsArray[ 1 ] || countsArray[ 0 ][ 1 ] > countsArray[ 1 ][ 1 ] ) {
if ( -1 < config.value.indexOf( parseInt( countsArray[ 0 ][ 0 ] ) ) ) {
match = true;
}
}

return match;
} );