X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fsql%2Fvideos-id-list-query-builder.ts;h=5064afafe568946a49cf880638af2df0d284620b;hb=7e7d8e485356402e7652c61c9f004e850b0a1607;hp=7625c003d0fbc6cc4d9a90f3951e7d0798987c91;hpb=421ff4618da64f0849353383f690a014024c40da;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/sql/videos-id-list-query-builder.ts b/server/models/video/sql/videos-id-list-query-builder.ts index 7625c003d..5064afafe 100644 --- a/server/models/video/sql/videos-id-list-query-builder.ts +++ b/server/models/video/sql/videos-id-list-query-builder.ts @@ -4,8 +4,8 @@ import { exists } from '@server/helpers/custom-validators/misc' import { WEBSERVER } from '@server/initializers/constants' import { buildDirectionAndField, createSafeIn } from '@server/models/utils' import { MUserAccountId, MUserId } from '@server/types/models' -import { VideoFilter, VideoPrivacy, VideoState } from '@shared/models' -import { AbstractVideosQueryBuilder } from './shared/abstract-videos-query-builder' +import { VideoInclude, VideoPrivacy, VideoState } from '@shared/models' +import { AbstractRunQuery } from './shared/abstract-run-query' /** * @@ -13,21 +13,27 @@ import { AbstractVideosQueryBuilder } from './shared/abstract-videos-query-build * */ +export type DisplayOnlyForFollowerOptions = { + actorId: number + orLocalVideos: boolean +} + export type BuildVideosListQueryOptions = { attributes?: string[] - serverAccountId: number - followerActorId: number - includeLocalVideos: boolean + serverAccountIdForBlock: number + + displayOnlyForFollower: DisplayOnlyForFollowerOptions count: number start: number sort: string nsfw?: boolean - filter?: VideoFilter host?: string isLive?: boolean + isLocal?: boolean + include?: VideoInclude categoryOneOf?: number[] licenceOneOf?: number[] @@ -37,7 +43,7 @@ export type BuildVideosListQueryOptions = { uuids?: string[] - withFiles?: boolean + hasFiles?: boolean accountId?: number videoChannelId?: number @@ -66,7 +72,7 @@ export type BuildVideosListQueryOptions = { having?: string } -export class VideosIdListQueryBuilder extends AbstractVideosQueryBuilder { +export class VideosIdListQueryBuilder extends AbstractRunQuery { protected replacements: any = {} private attributes: string[] @@ -99,8 +105,9 @@ export class VideosIdListQueryBuilder extends AbstractVideosQueryBuilder { return this.runQuery().then(rows => rows.length !== 0 ? rows[0].total : 0) } - getIdsListQueryAndSort (options: BuildVideosListQueryOptions) { + getQuery (options: BuildVideosListQueryOptions) { this.buildIdsListQuery(options) + return { query: this.query, sort: this.sort, replacements: this.replacements } } @@ -116,23 +123,30 @@ export class VideosIdListQueryBuilder extends AbstractVideosQueryBuilder { 'INNER JOIN "actor" "accountActor" ON "account"."actorId" = "accountActor"."id"' ]) - this.whereNotBlacklisted() + if (!(options.include & VideoInclude.BLACKLISTED)) { + this.whereNotBlacklisted() + } - if (options.serverAccountId) { - this.whereNotBlocked(options.serverAccountId, options.user) + if (options.serverAccountIdForBlock && !(options.include & VideoInclude.BLOCKED_OWNER)) { + this.whereNotBlocked(options.serverAccountIdForBlock, options.user) } - // Only list public/published videos - if (!options.filter || (options.filter !== 'all-local' && options.filter !== 'all')) { - this.whereStateAndPrivacyAvailable(options.user) + // Only list published videos + if (!(options.include & VideoInclude.NOT_PUBLISHED_STATE)) { + this.whereStateAvailable() + } + + // Only list videos with the appropriate priavcy + if (!(options.include & VideoInclude.HIDDEN_PRIVACY)) { + this.wherePrivacyAvailable(options.user) } if (options.videoPlaylistId) { this.joinPlaylist(options.videoPlaylistId) } - if (options.filter && (options.filter === 'local' || options.filter === 'all-local')) { - this.whereOnlyLocal() + if (exists(options.isLocal)) { + this.whereLocal(options.isLocal) } if (options.host) { @@ -147,11 +161,11 @@ export class VideosIdListQueryBuilder extends AbstractVideosQueryBuilder { this.whereChannelId(options.videoChannelId) } - if (options.followerActorId) { - this.whereFollowerActorId(options.followerActorId, options.includeLocalVideos) + if (options.displayOnlyForFollower) { + this.whereFollowerActorId(options.displayOnlyForFollower) } - if (options.withFiles === true) { + if (options.hasFiles === true) { this.whereFileExists() } @@ -282,12 +296,14 @@ export class VideosIdListQueryBuilder extends AbstractVideosQueryBuilder { this.replacements.videoPlaylistId = playlistId } - private whereStateAndPrivacyAvailable (user?: MUserAccountId) { + private whereStateAvailable () { this.and.push( `("video"."state" = ${VideoState.PUBLISHED} OR ` + `("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false))` ) + } + private wherePrivacyAvailable (user?: MUserAccountId) { if (user) { this.and.push( `("video"."privacy" = ${VideoPrivacy.PUBLIC} OR "video"."privacy" = ${VideoPrivacy.INTERNAL})` @@ -299,8 +315,10 @@ export class VideosIdListQueryBuilder extends AbstractVideosQueryBuilder { } } - private whereOnlyLocal () { - this.and.push('"video"."remote" IS FALSE') + private whereLocal (isLocal: boolean) { + const isRemote = isLocal ? 'FALSE' : 'TRUE' + + this.and.push('"video"."remote" IS ' + isRemote) } private whereHost (host: string) { @@ -326,7 +344,7 @@ export class VideosIdListQueryBuilder extends AbstractVideosQueryBuilder { this.replacements.videoChannelId = channelId } - private whereFollowerActorId (followerActorId: number, includeLocalVideos: boolean) { + private whereFollowerActorId (options: { actorId: number, orLocalVideos: boolean }) { let query = '(' + ' EXISTS (' + // Videos shared by actors we follow @@ -342,14 +360,14 @@ export class VideosIdListQueryBuilder extends AbstractVideosQueryBuilder { ' AND "actorFollow"."state" = \'accepted\'' + ' )' - if (includeLocalVideos) { + if (options.orLocalVideos) { query += ' OR "video"."remote" IS FALSE' } query += ')' this.and.push(query) - this.replacements.followerActorId = followerActorId + this.replacements.followerActorId = options.actorId } private whereFileExists () {