File size: 4,032 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import { flatMap } from 'lodash';
import moment from 'moment';
export const RECS_PER_BLOCK = 2;
/**
* Check if two postKeys are for the same siteId or feedId
* @param {Object} postKey1 First post key
* @param {Object} postKey2 Second post key
* @returns {boolean} Returns true if two postKeys are for the same siteId or feedId
*/
export function sameSite( postKey1, postKey2 ) {
return (
postKey1 &&
postKey2 &&
! postKey1.isRecommendationBlock &&
! postKey2.isRecommendationBlock &&
( ( postKey1.blogId && postKey1.blogId === postKey2.blogId ) ||
( postKey1.feedId && postKey1.feedId === postKey2.feedId ) )
);
}
export function sameDay( postKey1, postKey2 ) {
return moment( postKey1.date ).isSame( postKey2.date, 'day' );
}
export function sameXPost( postKey1, postKey2 ) {
return (
postKey1 &&
postKey2 &&
postKey1.xPostMetadata &&
postKey2.xPostMetadata &&
postKey1.xPostMetadata.blogId &&
postKey1.xPostMetadata.blogId === postKey2.xPostMetadata.blogId &&
postKey1.xPostMetadata.postId &&
postKey1.xPostMetadata.postId === postKey2.xPostMetadata.postId
);
}
export function injectRecommendations( posts, recs = [], itemsBetweenRecs ) {
if ( ! recs || recs.length === 0 ) {
return posts;
}
if ( posts.length < itemsBetweenRecs ) {
return posts;
}
let recIndex = 0;
return flatMap( posts, ( post, index ) => {
if ( index && index % itemsBetweenRecs === 0 && recIndex < recs.length ) {
const recBlock = {
isRecommendationBlock: true,
recommendations: recs.slice( recIndex, recIndex + RECS_PER_BLOCK ),
index: recIndex,
};
recIndex += RECS_PER_BLOCK;
return [ recBlock, post ];
}
return post;
} );
}
const MIN_DISTANCE_BETWEEN_RECS = 4; // page size is 7, so one in the middle of every page and one on page boundries, sometimes
const MAX_DISTANCE_BETWEEN_RECS = 30;
export function getDistanceBetweenRecs( totalSubs ) {
// the distance between recs changes based on how many subscriptions the user has.
// We cap it at MAX_DISTANCE_BETWEEN_RECS.
// It grows at the natural log of the number of subs, times a multiplier, offset by a constant.
// This lets the distance between recs grow quickly as you add subs early on, and slow down as you
// become a common user of the reader.
if ( totalSubs <= 0 ) {
// 0 means either we don't know yet, or the user actually has zero subs.
// if a user has zero subs, we don't show posts at all, so just treat 0 as 'unknown' and
// push recs to the max.
return MAX_DISTANCE_BETWEEN_RECS;
}
return Math.min(
Math.max( Math.floor( Math.log( totalSubs ) * Math.LOG2E * 5 - 6 ), MIN_DISTANCE_BETWEEN_RECS ),
MAX_DISTANCE_BETWEEN_RECS
);
}
const MIN_DISTANCE_BETWEEN_PROMPTS = 10;
const MAX_DISTANCE_BETWEEN_PROMPTS = 50;
export function getDistanceBetweenPrompts( totalSubs ) {
// the distance between recs changes based on how many subscriptions the user has.
// We cap it at MAX_DISTANCE_BETWEEN_PROMPTS.
// It grows at the natural log of the number of subs, times a multiplier, offset by a constant.
// This lets the distance between prompts grow quickly as you add subs early on, and slow down as you
// become a common user of the reader.
if ( totalSubs <= 0 ) {
// 0 means either we don't know yet, or the user actually has zero subs.
// if a user has zero subs, we don't show posts at all, so just treat 0 as 'unknown' and
// push recs to the max.
return MAX_DISTANCE_BETWEEN_PROMPTS;
}
return Math.min(
Math.max(
Math.floor( Math.log( totalSubs ) * Math.LOG2E * 7 - 3 ),
MIN_DISTANCE_BETWEEN_PROMPTS
),
MAX_DISTANCE_BETWEEN_PROMPTS
);
}
export function injectPrompts( posts, itemsBetweenPrompts ) {
if ( posts.length < itemsBetweenPrompts ) {
return posts;
}
let promptIndex = 0;
return flatMap( posts, ( post, index ) => {
if ( index && index % itemsBetweenPrompts === 0 ) {
const promptBlock = {
isPromptBlock: true,
index: promptIndex,
};
promptIndex++;
return [ promptBlock, post ];
}
return post;
} );
}
|