mirror of https://github.com/Chocobozzz/PeerTube
Refactor include checks
parent
71d4af1efc
commit
20a206c3d1
|
@ -31,7 +31,6 @@ import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '
|
||||||
import { AccountModel } from '../../models/account/account'
|
import { AccountModel } from '../../models/account/account'
|
||||||
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
|
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
|
||||||
import { ActorFollowModel } from '../../models/actor/actor-follow'
|
import { ActorFollowModel } from '../../models/actor/actor-follow'
|
||||||
import { VideoModel } from '../../models/video/video'
|
|
||||||
import { VideoCaptionModel } from '../../models/video/video-caption'
|
import { VideoCaptionModel } from '../../models/video/video-caption'
|
||||||
import { VideoCommentModel } from '../../models/video/video-comment'
|
import { VideoCommentModel } from '../../models/video/video-comment'
|
||||||
import { VideoPlaylistModel } from '../../models/video/video-playlist'
|
import { VideoPlaylistModel } from '../../models/video/video-playlist'
|
||||||
|
|
|
@ -5,8 +5,7 @@ import {
|
||||||
MVideoFullLight,
|
MVideoFullLight,
|
||||||
MVideoId,
|
MVideoId,
|
||||||
MVideoImmutable,
|
MVideoImmutable,
|
||||||
MVideoThumbnail,
|
MVideoThumbnail
|
||||||
MVideoWithRights
|
|
||||||
} from '@server/types/models'
|
} from '@server/types/models'
|
||||||
import { Hooks } from '../plugins/hooks'
|
import { Hooks } from '../plugins/hooks'
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,7 @@ import {
|
||||||
MVideoFullLight,
|
MVideoFullLight,
|
||||||
MVideoId,
|
MVideoId,
|
||||||
MVideoImmutable,
|
MVideoImmutable,
|
||||||
MVideoThumbnail,
|
MVideoThumbnail
|
||||||
MVideoWithRights
|
|
||||||
} from '@server/types/models'
|
} from '@server/types/models'
|
||||||
import { HttpStatusCode } from '@shared/core-utils'
|
import { HttpStatusCode } from '@shared/core-utils'
|
||||||
import { UserRight } from '@shared/models'
|
import { UserRight } from '@shared/models'
|
||||||
|
|
|
@ -10,11 +10,21 @@ import { VideoTables } from './shared/video-tables'
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
export type GetType =
|
||||||
|
'api' |
|
||||||
|
'full-light' |
|
||||||
|
'account-blacklist-files' |
|
||||||
|
'all-files' |
|
||||||
|
'thumbnails' |
|
||||||
|
'thumbnails-blacklist' |
|
||||||
|
'id' |
|
||||||
|
'blacklist-rights'
|
||||||
|
|
||||||
export type BuildVideoGetQueryOptions = {
|
export type BuildVideoGetQueryOptions = {
|
||||||
id?: number | string
|
id?: number | string
|
||||||
url?: string
|
url?: string
|
||||||
|
|
||||||
type: 'api' | 'full-light' | 'account-blacklist-files' | 'all-files' | 'thumbnails' | 'thumbnails-blacklist' | 'id' | 'blacklist-rights'
|
type: GetType
|
||||||
|
|
||||||
userId?: number
|
userId?: number
|
||||||
transaction?: Transaction
|
transaction?: Transaction
|
||||||
|
@ -29,6 +39,8 @@ export class VideosModelGetQueryBuilder {
|
||||||
|
|
||||||
private readonly videoModelBuilder: VideoModelBuilder
|
private readonly videoModelBuilder: VideoModelBuilder
|
||||||
|
|
||||||
|
private static readonly videoFilesInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files', 'all-files' ])
|
||||||
|
|
||||||
constructor (protected readonly sequelize: Sequelize) {
|
constructor (protected readonly sequelize: Sequelize) {
|
||||||
this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
|
this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
|
||||||
this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
|
this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
|
||||||
|
@ -41,11 +53,11 @@ export class VideosModelGetQueryBuilder {
|
||||||
const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
|
const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
|
||||||
this.videoQueryBuilder.queryVideos(options),
|
this.videoQueryBuilder.queryVideos(options),
|
||||||
|
|
||||||
this.shouldQueryVideoFiles(options)
|
VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
|
||||||
? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options)
|
? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options)
|
||||||
: Promise.resolve(undefined),
|
: Promise.resolve(undefined),
|
||||||
|
|
||||||
this.shouldQueryVideoFiles(options)
|
VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
|
||||||
? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
|
? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
|
||||||
: Promise.resolve(undefined)
|
: Promise.resolve(undefined)
|
||||||
])
|
])
|
||||||
|
@ -59,10 +71,6 @@ export class VideosModelGetQueryBuilder {
|
||||||
if (videos.length === 0) return null
|
if (videos.length === 0) return null
|
||||||
return videos[0]
|
return videos[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
private shouldQueryVideoFiles (options: BuildVideoGetQueryOptions) {
|
|
||||||
return [ 'api', 'full-light', 'account-blacklist-files', 'all-files' ].includes(options.type)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder {
|
export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder {
|
||||||
|
@ -71,6 +79,30 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild
|
||||||
protected webtorrentFilesQuery: string
|
protected webtorrentFilesQuery: string
|
||||||
protected streamingPlaylistFilesQuery: string
|
protected streamingPlaylistFilesQuery: string
|
||||||
|
|
||||||
|
private static readonly trackersInclude = new Set<GetType>([ 'api' ])
|
||||||
|
private static readonly liveInclude = new Set<GetType>([ 'api', 'full-light' ])
|
||||||
|
private static readonly scheduleUpdateInclude = new Set<GetType>([ 'api', 'full-light' ])
|
||||||
|
private static readonly tagsInclude = new Set<GetType>([ 'api', 'full-light' ])
|
||||||
|
private static readonly userHistoryInclude = new Set<GetType>([ 'api', 'full-light' ])
|
||||||
|
private static readonly accountInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files' ])
|
||||||
|
private static readonly ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])
|
||||||
|
|
||||||
|
private static readonly blacklistedInclude = new Set<GetType>([
|
||||||
|
'api',
|
||||||
|
'full-light',
|
||||||
|
'account-blacklist-files',
|
||||||
|
'thumbnails-blacklist',
|
||||||
|
'blacklist-rights'
|
||||||
|
])
|
||||||
|
|
||||||
|
private static readonly thumbnailsInclude = new Set<GetType>([
|
||||||
|
'api',
|
||||||
|
'full-light',
|
||||||
|
'account-blacklist-files',
|
||||||
|
'thumbnails',
|
||||||
|
'thumbnails-blacklist'
|
||||||
|
])
|
||||||
|
|
||||||
constructor (protected readonly sequelize: Sequelize) {
|
constructor (protected readonly sequelize: Sequelize) {
|
||||||
super('get')
|
super('get')
|
||||||
}
|
}
|
||||||
|
@ -86,40 +118,40 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild
|
||||||
'"video".*': ''
|
'"video".*': ''
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.shouldIncludeThumbnails(options)) {
|
if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) {
|
||||||
this.includeThumbnails()
|
this.includeThumbnails()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.shouldIncludeBlacklisted(options)) {
|
if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) {
|
||||||
this.includeBlacklisted()
|
this.includeBlacklisted()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.shouldIncludeAccount(options)) {
|
if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) {
|
||||||
this.includeChannels()
|
this.includeChannels()
|
||||||
this.includeAccounts()
|
this.includeAccounts()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.shouldIncludeTags(options)) {
|
if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) {
|
||||||
this.includeTags()
|
this.includeTags()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.shouldIncludeScheduleUpdate(options)) {
|
if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) {
|
||||||
this.includeScheduleUpdate()
|
this.includeScheduleUpdate()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.shouldIncludeLive(options)) {
|
if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) {
|
||||||
this.includeLive()
|
this.includeLive()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.userId && this.shouldIncludeUserHistory(options)) {
|
if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) {
|
||||||
this.includeUserHistory(options.userId)
|
this.includeUserHistory(options.userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.shouldIncludeOwnerUser(options)) {
|
if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) {
|
||||||
this.includeOwnerUser()
|
this.includeOwnerUser()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.shouldIncludeTrackers(options)) {
|
if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) {
|
||||||
this.includeTrackers()
|
this.includeTrackers()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +161,7 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildQuery (options: BuildVideoGetQueryOptions) {
|
private buildQuery (options: BuildVideoGetQueryOptions) {
|
||||||
const order = this.shouldIncludeTags(options)
|
const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)
|
||||||
? 'ORDER BY "Tags"."name" ASC'
|
? 'ORDER BY "Tags"."name" ASC'
|
||||||
: ''
|
: ''
|
||||||
|
|
||||||
|
@ -137,40 +169,4 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild
|
||||||
|
|
||||||
return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`
|
return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`
|
||||||
}
|
}
|
||||||
|
|
||||||
private shouldIncludeTrackers (options: BuildVideoGetQueryOptions) {
|
|
||||||
return options.type === 'api'
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldIncludeLive (options: BuildVideoGetQueryOptions) {
|
|
||||||
return [ 'api', 'full-light' ].includes(options.type)
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldIncludeScheduleUpdate (options: BuildVideoGetQueryOptions) {
|
|
||||||
return [ 'api', 'full-light' ].includes(options.type)
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldIncludeTags (options: BuildVideoGetQueryOptions) {
|
|
||||||
return [ 'api', 'full-light' ].includes(options.type)
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldIncludeUserHistory (options: BuildVideoGetQueryOptions) {
|
|
||||||
return [ 'api', 'full-light' ].includes(options.type)
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldIncludeAccount (options: BuildVideoGetQueryOptions) {
|
|
||||||
return [ 'api', 'full-light', 'account-blacklist-files' ].includes(options.type)
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldIncludeBlacklisted (options: BuildVideoGetQueryOptions) {
|
|
||||||
return [ 'api', 'full-light', 'account-blacklist-files', 'thumbnails-blacklist', 'blacklist-rights' ].includes(options.type)
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldIncludeOwnerUser (options: BuildVideoGetQueryOptions) {
|
|
||||||
return options.type === 'blacklist-rights'
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldIncludeThumbnails (options: BuildVideoGetQueryOptions) {
|
|
||||||
return [ 'api', 'full-light', 'account-blacklist-files', 'thumbnails', 'thumbnails-blacklist' ].includes(options.type)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,11 +37,9 @@ import {
|
||||||
MVideoBlacklist,
|
MVideoBlacklist,
|
||||||
MVideoCaptionVideo,
|
MVideoCaptionVideo,
|
||||||
MVideoFullLight,
|
MVideoFullLight,
|
||||||
MVideoIdThumbnail,
|
|
||||||
MVideoRedundancyVideo,
|
MVideoRedundancyVideo,
|
||||||
MVideoShareActor,
|
MVideoShareActor,
|
||||||
MVideoThumbnail,
|
MVideoThumbnail
|
||||||
MVideoWithRights
|
|
||||||
} from '../../types/models'
|
} from '../../types/models'
|
||||||
|
|
||||||
declare module 'express' {
|
declare module 'express' {
|
||||||
|
|
Loading…
Reference in New Issue