From 5bcbcbe338ef5a1ed14f084311d013fbb25dabcf Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Fri, 22 Jan 2021 00:12:44 +0100 Subject: modularize abstract video list header and implement video hotness recommendation variant --- server/initializers/constants.ts | 2 +- server/middlewares/sort.ts | 2 +- server/models/video/video-query-builder.ts | 42 +++++++++++++++++++++++++++--- server/models/video/video.ts | 2 ++ 4 files changed, 42 insertions(+), 6 deletions(-) (limited to 'server') diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 84a515857..89491708e 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -72,7 +72,7 @@ const SORTABLE_COLUMNS = { FOLLOWERS: [ 'createdAt', 'state', 'score' ], FOLLOWING: [ 'createdAt', 'redundancyAllowed', 'state' ], - VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending' ], + VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending', 'hot' ], // Don't forget to update peertube-search-index with the same values VIDEOS_SEARCH: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'match' ], diff --git a/server/middlewares/sort.ts b/server/middlewares/sort.ts index 609046a46..0600ccd15 100644 --- a/server/middlewares/sort.ts +++ b/server/middlewares/sort.ts @@ -16,7 +16,7 @@ function setBlacklistSort (req: express.Request, res: express.Response, next: ex // Set model we want to sort onto if (req.query.sort === '-createdAt' || req.query.sort === 'createdAt' || req.query.sort === '-id' || req.query.sort === 'id') { - // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter ... + // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter... newSort.sortModel = undefined } else { newSort.sortModel = 'Video' diff --git a/server/models/video/video-query-builder.ts b/server/models/video/video-query-builder.ts index 9e5b6febb..65b72fe1c 100644 --- a/server/models/video/video-query-builder.ts +++ b/server/models/video/video-query-builder.ts @@ -32,6 +32,8 @@ export type BuildVideosQueryOptions = { videoPlaylistId?: number trendingDays?: number + hot?: boolean + user?: MUserAccountId historyOfUser?: MUserId @@ -239,14 +241,46 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) } } - // We don't exclude results in this if so if we do a count we don't need to add this complex clauses + // We don't exclude results in this so if we do a count we don't need to add this complex clause if (options.trendingDays && options.isCount !== true) { const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * options.trendingDays) joins.push('LEFT JOIN "videoView" ON "video"."id" = "videoView"."videoId" AND "videoView"."startDate" >= :viewsGteDate') replacements.viewsGteDate = viewsGteDate - attributes.push('COALESCE(SUM("videoView"."views"), 0) AS "videoViewsSum"') + attributes.push('COALESCE(SUM("videoView"."views"), 0) AS "score"') + + group = 'GROUP BY "video"."id"' + } else if (options.hot && options.isCount !== true) { + /** + * "Hotness" is a measure based on absolute view/comment/like/dislike numbers, + * with fixed weights only applied to their log values. + * + * This algorithm gives little chance for an old video to have a good score, + * for which recent spikes in interactions could be a sign of "hotness" and + * justify a better score. However there are multiple ways to achieve that + * goal, which is left for later. Yes, this is a TODO :) + * + * note: weights and base score are in number of half-days. + * see https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/_sorts.pyx#L47-L58 + */ + const weights = { + like: 3, + dislike: 3, + view: 1 / 12, + comment: 6 + } + + joins.push('LEFT JOIN "videoComment" ON "video"."id" = "videoComment"."videoId"') + + attributes.push( + `LOG(GREATEST(1, "video"."likes" - 1)) * ${weights.like} ` + // likes (+) + `- LOG(GREATEST(1, "video"."dislikes" - 1)) * ${weights.dislike} ` + // dislikes (-) + `+ LOG("video"."views" + 1) * ${weights.view} ` + // views (+) + `+ LOG(GREATEST(1, COUNT(DISTINCT "videoComment"."id") - 1)) * ${weights.comment} ` + // comments (+) + '+ (SELECT EXTRACT(epoch FROM "video"."publishedAt") / 47000) ' + // base score (in number of half-days) + 'AS "score"' + ) group = 'GROUP BY "video"."id"' } @@ -372,8 +406,8 @@ function buildOrder (value: string) { if (field.toLowerCase() === 'random') return 'ORDER BY RANDOM()' - if (field.toLowerCase() === 'trending') { // Sort by aggregation - return `ORDER BY "videoViewsSum" ${direction}, "video"."views" ${direction}` + if ([ 'trending', 'hot' ].includes(field.toLowerCase())) { // Sort by aggregation + return `ORDER BY "score" ${direction}, "video"."views" ${direction}` } let firstSort: string diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 9b0aa809e..c56fbfbf2 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1090,6 +1090,7 @@ export class VideoModel extends Model { const trendingDays = options.sort.endsWith('trending') ? CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS : undefined + const hot = options.sort.endsWith('hot') const serverActor = await getServerActor() @@ -1119,6 +1120,7 @@ export class VideoModel extends Model { user: options.user, historyOfUser: options.historyOfUser, trendingDays, + hot, search: options.search } -- cgit v1.2.3