From 2760b454a761f6af3138b2fb5f34340772ab0d1e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 27 Oct 2021 14:37:04 +0200 Subject: Deprecate filter video query Introduce include and isLocal instead --- .../shared/abstract-videos-model-query-builder.ts | 28 +++++++++ .../models/video/sql/shared/video-model-builder.ts | 72 +++++++++++++++++++--- server/models/video/sql/shared/video-tables.ts | 4 ++ 3 files changed, 95 insertions(+), 9 deletions(-) (limited to 'server/models/video/sql/shared') diff --git a/server/models/video/sql/shared/abstract-videos-model-query-builder.ts b/server/models/video/sql/shared/abstract-videos-model-query-builder.ts index 0d7e64574..29827db2a 100644 --- a/server/models/video/sql/shared/abstract-videos-model-query-builder.ts +++ b/server/models/video/sql/shared/abstract-videos-model-query-builder.ts @@ -1,3 +1,5 @@ +import { createSafeIn } from '@server/models/utils' +import { MUserAccountId } from '@server/types/models' import validator from 'validator' import { AbstractVideosQueryBuilder } from './abstract-videos-query-builder' import { VideoTables } from './video-tables' @@ -188,6 +190,32 @@ export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder } } + protected includeBlockedOwnerAndServer (serverAccountId: number, user?: MUserAccountId) { + const blockerIds = [ serverAccountId ] + if (user) blockerIds.push(user.Account.id) + + const inClause = createSafeIn(this.sequelize, blockerIds) + + this.addJoin( + 'LEFT JOIN "accountBlocklist" AS "VideoChannel->Account->AccountBlocklist" ' + + 'ON "VideoChannel->Account"."id" = "VideoChannel->Account->AccountBlocklist"."targetAccountId" ' + + 'AND "VideoChannel->Account->AccountBlocklist"."accountId" IN (' + inClause + ')' + ) + + this.addJoin( + 'LEFT JOIN "serverBlocklist" AS "VideoChannel->Account->Actor->Server->ServerBlocklist" ' + + 'ON "VideoChannel->Account->Actor->Server->ServerBlocklist"."targetServerId" = "VideoChannel->Account->Actor"."serverId" ' + + 'AND "VideoChannel->Account->Actor->Server->ServerBlocklist"."accountId" IN (' + inClause + ') ' + ) + + this.attributes = { + ...this.attributes, + + ...this.buildAttributesObject('VideoChannel->Account->AccountBlocklist', this.tables.getBlocklistAttributes()), + ...this.buildAttributesObject('VideoChannel->Account->Actor->Server->ServerBlocklist', this.tables.getBlocklistAttributes()) + } + } + protected includeScheduleUpdate () { this.addJoin( 'LEFT OUTER JOIN "scheduleVideoUpdate" AS "ScheduleVideoUpdate" ON "video"."id" = "ScheduleVideoUpdate"."videoId"' diff --git a/server/models/video/sql/shared/video-model-builder.ts b/server/models/video/sql/shared/video-model-builder.ts index 33a0181e9..0eac95661 100644 --- a/server/models/video/sql/shared/video-model-builder.ts +++ b/server/models/video/sql/shared/video-model-builder.ts @@ -1,11 +1,14 @@ import { AccountModel } from '@server/models/account/account' +import { AccountBlocklistModel } from '@server/models/account/account-blocklist' import { ActorModel } from '@server/models/actor/actor' import { ActorImageModel } from '@server/models/actor/actor-image' import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy' import { ServerModel } from '@server/models/server/server' +import { ServerBlocklistModel } from '@server/models/server/server-blocklist' import { TrackerModel } from '@server/models/server/tracker' import { UserVideoHistoryModel } from '@server/models/user/user-video-history' +import { VideoInclude } from '@shared/models' import { ScheduleVideoUpdateModel } from '../../schedule-video-update' import { TagModel } from '../../tag' import { ThumbnailModel } from '../../thumbnail' @@ -33,6 +36,8 @@ export class VideoModelBuilder { private thumbnailsDone: Set private historyDone: Set private blacklistDone: Set + private accountBlocklistDone: Set + private serverBlocklistDone: Set private liveDone: Set private redundancyDone: Set private scheduleVideoUpdateDone: Set @@ -51,7 +56,14 @@ export class VideoModelBuilder { } - buildVideosFromRows (rows: SQLRow[], rowsWebTorrentFiles?: SQLRow[], rowsStreamingPlaylist?: SQLRow[]) { + buildVideosFromRows (options: { + rows: SQLRow[] + include?: VideoInclude + rowsWebTorrentFiles?: SQLRow[] + rowsStreamingPlaylist?: SQLRow[] + }) { + const { rows, rowsWebTorrentFiles, rowsStreamingPlaylist, include } = options + this.reinit() for (const row of rows) { @@ -77,6 +89,15 @@ export class VideoModelBuilder { this.setBlacklisted(row, videoModel) this.setScheduleVideoUpdate(row, videoModel) this.setLive(row, videoModel) + } else { + if (include & VideoInclude.BLACKLISTED) { + this.setBlacklisted(row, videoModel) + } + + if (include & VideoInclude.BLOCKED_OWNER) { + this.setBlockedOwner(row, videoModel) + this.setBlockedServer(row, videoModel) + } } } @@ -91,15 +112,18 @@ export class VideoModelBuilder { this.videoStreamingPlaylistMemo = {} this.videoFileMemo = {} - this.thumbnailsDone = new Set() - this.historyDone = new Set() - this.blacklistDone = new Set() - this.liveDone = new Set() - this.redundancyDone = new Set() - this.scheduleVideoUpdateDone = new Set() + this.thumbnailsDone = new Set() + this.historyDone = new Set() + this.blacklistDone = new Set() + this.liveDone = new Set() + this.redundancyDone = new Set() + this.scheduleVideoUpdateDone = new Set() + + this.accountBlocklistDone = new Set() + this.serverBlocklistDone = new Set() - this.trackersDone = new Set() - this.tagsDone = new Set() + this.trackersDone = new Set() + this.tagsDone = new Set() this.videos = [] } @@ -162,6 +186,8 @@ export class VideoModelBuilder { const accountModel = new AccountModel(this.grab(row, this.tables.getAccountAttributes(), 'VideoChannel.Account'), this.buildOpts) accountModel.Actor = this.buildActor(row, 'VideoChannel.Account') + accountModel.BlockedBy = [] + channelModel.Account = accountModel videoModel.VideoChannel = channelModel @@ -180,6 +206,8 @@ export class VideoModelBuilder { ? new ServerModel(this.grab(row, this.tables.getServerAttributes(), serverPrefix), this.buildOpts) : null + if (serverModel) serverModel.BlockedBy = [] + const actorModel = new ActorModel(this.grab(row, this.tables.getActorAttributes(), actorPrefix), this.buildOpts) actorModel.Avatar = avatarModel actorModel.Server = serverModel @@ -297,6 +325,32 @@ export class VideoModelBuilder { this.blacklistDone.add(id) } + private setBlockedOwner (row: SQLRow, videoModel: VideoModel) { + const id = row['VideoChannel.Account.AccountBlocklist.id'] + if (!id) return + + const key = `${videoModel.id}-${id}` + if (this.accountBlocklistDone.has(key)) return + + const attributes = this.grab(row, this.tables.getBlocklistAttributes(), 'VideoChannel.Account.AccountBlocklist') + videoModel.VideoChannel.Account.BlockedBy.push(new AccountBlocklistModel(attributes, this.buildOpts)) + + this.accountBlocklistDone.add(key) + } + + private setBlockedServer (row: SQLRow, videoModel: VideoModel) { + const id = row['VideoChannel.Account.Actor.Server.ServerBlocklist.id'] + if (!id || this.serverBlocklistDone.has(id)) return + + const key = `${videoModel.id}-${id}` + if (this.serverBlocklistDone.has(key)) return + + const attributes = this.grab(row, this.tables.getBlocklistAttributes(), 'VideoChannel.Account.Actor.Server.ServerBlocklist') + videoModel.VideoChannel.Account.Actor.Server.BlockedBy.push(new ServerBlocklistModel(attributes, this.buildOpts)) + + this.serverBlocklistDone.add(key) + } + private setScheduleVideoUpdate (row: SQLRow, videoModel: VideoModel) { const id = row['ScheduleVideoUpdate.id'] if (!id || this.scheduleVideoUpdateDone.has(id)) return diff --git a/server/models/video/sql/shared/video-tables.ts b/server/models/video/sql/shared/video-tables.ts index 75823864d..042b9d5da 100644 --- a/server/models/video/sql/shared/video-tables.ts +++ b/server/models/video/sql/shared/video-tables.ts @@ -139,6 +139,10 @@ export class VideoTables { return [ 'id', 'reason', 'unfederated' ] } + getBlocklistAttributes () { + return [ 'id' ] + } + getScheduleUpdateAttributes () { return [ 'id', -- cgit v1.2.3