- 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 "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: 2 // a comment takes more time than a like to do, but can be done multiple times
- }
+ if (options.isCount !== true) {
+ if (options.trendingDays) {
+ 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 "score"')
+
+ group = 'GROUP BY "video"."id"'
+ } else if ([ 'best', 'hot' ].includes(options.trendingAlgorithm)) {
+ /**
+ * "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 :)
+ *
+ * notes:
+ * - weights and base score are in number of half-days.
+ * - all comments are counted, regardless of being written by the video author or not
+ * see https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/_sorts.pyx#L47-L58
+ * - we have less interactions than on reddit, so multiply weights by an arbitrary factor
+ */
+ const weights = {
+ like: 3 * 50,
+ dislike: -3 * 50,
+ view: Math.floor((1 / 3) * 50),
+ comment: 2 * 50, // a comment takes more time than a like to do, but can be done multiple times
+ history: -2 * 50
+ }