aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/sql
diff options
context:
space:
mode:
authorWicklow <123956049+wickloww@users.noreply.github.com>2023-04-12 07:32:20 +0000
committerGitHub <noreply@github.com>2023-04-12 09:32:20 +0200
commit2a4c0d8bbe29178ae90e776bb9453f86e6d23bd9 (patch)
treedfe4b6e1e06f617f8968285ca394e73fedefe6b2 /server/models/video/sql
parent0cda019c1d1f77e06e524362880c38e93b1f5c70 (diff)
downloadPeerTube-2a4c0d8bbe29178ae90e776bb9453f86e6d23bd9.tar.gz
PeerTube-2a4c0d8bbe29178ae90e776bb9453f86e6d23bd9.tar.zst
PeerTube-2a4c0d8bbe29178ae90e776bb9453f86e6d23bd9.zip
Feature/filter already watched videos (#5739)
* filter already watched videos * Updated code based on review comments
Diffstat (limited to 'server/models/video/sql')
-rw-r--r--server/models/video/sql/video/videos-id-list-query-builder.ts22
1 files changed, 22 insertions, 0 deletions
diff --git a/server/models/video/sql/video/videos-id-list-query-builder.ts b/server/models/video/sql/video/videos-id-list-query-builder.ts
index 62f1855c7..cba77c1d1 100644
--- a/server/models/video/sql/video/videos-id-list-query-builder.ts
+++ b/server/models/video/sql/video/videos-id-list-query-builder.ts
@@ -78,6 +78,8 @@ export type BuildVideosListQueryOptions = {
78 78
79 transaction?: Transaction 79 transaction?: Transaction
80 logging?: boolean 80 logging?: boolean
81
82 excludeAlreadyWatched?: boolean
81} 83}
82 84
83export class VideosIdListQueryBuilder extends AbstractRunQuery { 85export class VideosIdListQueryBuilder extends AbstractRunQuery {
@@ -260,6 +262,14 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
260 this.whereDurationMax(options.durationMax) 262 this.whereDurationMax(options.durationMax)
261 } 263 }
262 264
265 if (options.excludeAlreadyWatched) {
266 if (exists(options.user.id)) {
267 this.whereExcludeAlreadyWatched(options.user.id)
268 } else {
269 throw new Error('Cannot use excludeAlreadyWatched parameter when auth token is not provided')
270 }
271 }
272
263 this.whereSearch(options.search) 273 this.whereSearch(options.search)
264 274
265 if (options.isCount === true) { 275 if (options.isCount === true) {
@@ -598,6 +608,18 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
598 this.replacements.durationMax = durationMax 608 this.replacements.durationMax = durationMax
599 } 609 }
600 610
611 private whereExcludeAlreadyWatched (userId: number) {
612 this.and.push(
613 'NOT EXISTS (' +
614 ' SELECT 1' +
615 ' FROM "userVideoHistory"' +
616 ' WHERE "video"."id" = "userVideoHistory"."videoId"' +
617 ' AND "userVideoHistory"."userId" = :excludeAlreadyWatchedUserId' +
618 ')'
619 )
620 this.replacements.excludeAlreadyWatchedUserId = userId
621 }
622
601 private groupForTrending (trendingDays: number) { 623 private groupForTrending (trendingDays: number) {
602 const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays) 624 const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
603 625