ActivityPub-federated video streaming platform using P2P directly in your web browser
Support hot sort for search endpoint
Chocobozzz9aa557649e94
diff --git a/packages/tests/src/api/search/search-index.ts b/packages/tests/src/api/search/search-index.tsindex 39fbd02176..7dcd430479 100644--- a/packages/tests/src/api/search/search-index.ts+++ b/packages/tests/src/api/search/search-index.ts@@ -148,6 +148,10 @@ describe('Test index search', function () {await check(baseSearch)})+ it('Should sort results', async function () {+ await check({ ...baseSearch, sort: '-hot' }, true)+ })+it('Should search by start date', async function () {const search = { ...baseSearch, startDate: '2018-10-01T10:54:46.396Z' }await check(search, false)diff --git a/packages/tests/src/api/search/search-videos.ts b/packages/tests/src/api/search/search-videos.tsindex 3559c4fbfb..ef95966b9b 100644--- a/packages/tests/src/api/search/search-videos.ts+++ b/packages/tests/src/api/search/search-videos.ts@@ -377,6 +377,26 @@ describe('Test videos search', function () {expect(videos[3].name).to.equal('1111 2222 3333')})+ it('Should make an advanced search and sort results', async function () {+ const query = {+ search: '1111 2222 3333',+ languageOneOf: [ 'pl', 'fr' ],+ durationMax: 4,+ nsfw: 'false' as 'false',+ licenceOneOf: [ 1, 4 ],+ sort: '-hot'+ }++ const body = await command.advancedVideoSearch({ search: query })+ expect(body.total).to.equal(4)++ const videos = body.data+ expect(videos[0].name).to.equal('1111 2222 3333 - 8')+ expect(videos[1].name).to.equal('1111 2222 3333 - 7')+ expect(videos[2].name).to.equal('1111 2222 3333 - 6')+ expect(videos[3].name).to.equal('1111 2222 3333')+ })+it('Should make an advanced search and only show the first result', async function () {const query = {search: '1111 2222 3333',diff --git a/server/core/initializers/constants.ts b/server/core/initializers/constants.tsindex 7f5027d7c1..523c06c0c6 100644--- a/server/core/initializers/constants.ts+++ b/server/core/initializers/constants.ts@@ -128,7 +128,7 @@ export const SORTABLE_COLUMNS = {],// Don't forget to update peertube-search-index with the same values- VIDEOS_SEARCH: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'match' ],+ VIDEOS_SEARCH: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'match', 'hot' ],VIDEO_CHANNELS_SEARCH: [ 'match', 'displayName', 'createdAt' ],VIDEO_PLAYLISTS_SEARCH: [ 'match', 'displayName', 'createdAt' ],diff --git a/server/core/models/video/sql/video/videos-id-list-query-builder.ts b/server/core/models/video/sql/video/videos-id-list-query-builder.tsindex 321b7fed57..62f2601ece 100644--- a/server/core/models/video/sql/video/videos-id-list-query-builder.ts+++ b/server/core/models/video/sql/video/videos-id-list-query-builder.ts@@ -63,8 +63,7 @@ export type BuildVideosListQueryOptions = {videoPlaylistId?: number- trendingAlgorithm?: string // best, hot, or any other algorithm implemented- trendingDays?: number+ trendingDays: number// Used to include user history information, exclude blocked videos, include internal videos, adapt hot algorithm...user?: MUserAccountId@@ -253,10 +252,10 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {// We don't exclude results in this so if we do a count we don't need to add this complex clauseif (options.isCount !== true) {- if (options.trendingDays) {+ if (options.sort.endsWith('trending')) {this.groupForTrending(options.trendingDays)- } else if ([ 'best', 'hot' ].includes(options.trendingAlgorithm)) {- this.groupForHotOrBest(options.trendingAlgorithm, options.user)+ } else if (options.sort.endsWith('hot') || options.sort.endsWith('best')) {+ this.addAttributeForHotOrBest(options.sort, options.user)}}@@ -703,7 +702,7 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {this.group = 'GROUP BY "video"."id"'}- private groupForHotOrBest (trendingAlgorithm: string, user?: MUserAccountId) {+ private addAttributeForHotOrBest (sort: string, user?: MUserAccountId) {/*** "Hotness" is a measure based on absolute view/comment/like/dislike numbers,* with fixed weights only applied to their log values.@@ -733,7 +732,7 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {`+ LOG(GREATEST(1, "video"."comments")) * ${weights.comment} ` + // comments (+)'+ (SELECT (EXTRACT(epoch FROM "video"."publishedAt") - 1446156582) / 47000) ' // base score (in number of half-days)- if (trendingAlgorithm === 'best' && user) {+ if (sort.endsWith('best') && user) {this.joins.push('LEFT JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :bestUser')diff --git a/server/core/models/video/video.ts b/server/core/models/video/video.tsindex dff10b3f6a..362fea2ca7 100644--- a/server/core/models/video/video.ts+++ b/server/core/models/video/video.ts@@ -1105,14 +1105,6 @@ export class VideoModel extends SequelizeModel<VideoModel> {VideoModel.throwIfPrivateIncludeWithoutUser(options)VideoModel.throwIfPrivacyOneOfWithoutUser(options)- const trendingDays = options.sort.endsWith('trending')- ? CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS- : undefined-- let trendingAlgorithm: string- if (options.sort.endsWith('hot')) trendingAlgorithm = 'hot'- if (options.sort.endsWith('best')) trendingAlgorithm = 'best'-const serverActor = await getServerActor()const queryOptions = {@@ -1149,8 +1141,7 @@ export class VideoModel extends SequelizeModel<VideoModel> {]),serverAccountIdForBlock: serverActor.Account.id,- trendingDays,- trendingAlgorithm+ trendingDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS}return VideoModel.getAvailableForApi(queryOptions, options.countVideos)@@ -1240,6 +1231,8 @@ export class VideoModel extends SequelizeModel<VideoModel> {'displayOnlyForFollower','excludeAlreadyWatched']),++ trendingDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS,serverAccountIdForBlock: serverActor.Account.id}@@ -1579,7 +1572,8 @@ export class VideoModel extends SequelizeModel<VideoModel> {displayOnlyForFollower: {actorId: serverActor.id,orLocalVideos: true- }+ },+ trendingDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS}const queryBuilder = new VideosIdListQueryBuilder(VideoModel.sequelize)@@ -1617,7 +1611,8 @@ export class VideoModel extends SequelizeModel<VideoModel> {sort: '-total',videoChannelId: channelId,displayOnlyForFollower: null,- serverAccountIdForBlock: null+ serverAccountIdForBlock: null,+ trendingDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS}const queryBuilder = new VideosIdListQueryBuilder(VideoModel.sequelize)