X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Fmodels%2Fvideo%2Fvideo.ts;h=38447797e6b5d98ac9551ce53dba771660719bd8;hb=74dc3bca2b14f5fd3fe80c394dfc34177a46db77;hp=7a102b058c6679381046391da70003e77d8aceed;hpb=418d092afa81e2c8fe8ac6838fc4b5eb0af6a782;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 7a102b058..38447797e 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -51,10 +51,9 @@ import { getServerActor } from '../../helpers/utils' import { ACTIVITY_PUB, API_VERSION, - CONFIG, CONSTRAINTS_FIELDS, - HLS_PLAYLIST_DIRECTORY, HLS_REDUNDANCY_DIRECTORY, + HLS_STREAMING_PLAYLIST_DIRECTORY, PREVIEWS_SIZE, REMOTE_SCHEME, STATIC_DOWNLOAD_PATHS, @@ -64,8 +63,9 @@ import { VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES, - VIDEO_STATES -} from '../../initializers' + VIDEO_STATES, + WEBSERVER +} from '../../initializers/constants' import { sendDeleteVideo } from '../../lib/activitypub/send' import { AccountModel } from '../account/account' import { AccountVideoRateModel } from '../account/account-video-rate' @@ -78,11 +78,12 @@ import { buildWhereIdOrUUID, createSimilarityAttribute, getVideoSort, + isOutdated, throwIfNotValid } from '../utils' import { TagModel } from './tag' import { VideoAbuseModel } from './video-abuse' -import { VideoChannelModel, ScopeNames as VideoChannelScopeNames } from './video-channel' +import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel' import { VideoCommentModel } from './video-comment' import { VideoFileModel } from './video-file' import { VideoShareModel } from './video-share' @@ -105,6 +106,7 @@ import { UserModel } from '../account/user' import { VideoImportModel } from './video-import' import { VideoStreamingPlaylistModel } from './video-streaming-playlist' import { VideoPlaylistElementModel } from './video-playlist-element' +import { CONFIG } from '../../initializers/config' // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation const indexes: Sequelize.DefineIndexesOptions[] = [ @@ -225,7 +227,8 @@ type AvailableForListIDsOptions = { }, include: [ { - model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY) + model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }), + required: true } ] } @@ -240,7 +243,10 @@ type AvailableForListIDsOptions = { if (options.videoPlaylistId) { query.include.push({ model: VideoPlaylistElementModel.unscoped(), - required: true + required: true, + where: { + videoPlaylistId: options.videoPlaylistId + } }) } @@ -304,6 +310,8 @@ type AvailableForListIDsOptions = { videoPlaylistId: options.videoPlaylistId } }) + + query.subQuery = false } if (options.filter || options.accountId || options.videoChannelId) { @@ -1535,18 +1543,7 @@ export class VideoModel extends Model { if (ids.length === 0) return { data: [], total: count } - // FIXME: typings - const apiScope: any[] = [ - { - method: [ ScopeNames.FOR_API, { ids, withFiles: options.withFiles } as ForAPIOptions ] - } - ] - - if (options.user) { - apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] }) - } - - const secondQuery = { + const secondQuery: IFindOptions = { offset: 0, limit: query.limit, attributes: query.attributes, @@ -1556,6 +1553,29 @@ export class VideoModel extends Model { ) ] } + + // FIXME: typing + const apiScope: any[] = [] + + if (options.user) { + apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] }) + + // Even if the relation is n:m, we know that a user only have 0..1 video history + // So we won't have multiple rows for the same video + // A subquery adds some bugs in our query so disable it + secondQuery.subQuery = false + } + + apiScope.push({ + method: [ + ScopeNames.FOR_API, { + ids, + withFiles: options.withFiles, + videoPlaylistId: options.videoPlaylistId + } as ForAPIOptions + ] + }) + const rows = await VideoModel.scope(apiScope).findAll(secondQuery) return { @@ -1646,10 +1666,10 @@ export class VideoModel extends Model { name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`, createdBy: 'PeerTube', announceList: [ - [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ], - [ CONFIG.WEBSERVER.URL + '/tracker/announce' ] + [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ], + [ WEBSERVER.URL + '/tracker/announce' ] ], - urlList: [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ] + urlList: [ WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ] } const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options) @@ -1739,7 +1759,7 @@ export class VideoModel extends Model { } removeStreamingPlaylist (isRedundancy = false) { - const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_PLAYLIST_DIRECTORY + const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_STREAMING_PLAYLIST_DIRECTORY const filePath = join(baseDir, this.uuid) return remove(filePath) @@ -1749,12 +1769,7 @@ export class VideoModel extends Model { isOutdated () { if (this.isOwned()) return false - const now = Date.now() - const createdAtTime = this.createdAt.getTime() - const updatedAtTime = this.updatedAt.getTime() - - return (now - createdAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL && - (now - updatedAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL + return isOutdated(this, ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL) } setAsRefreshed () { @@ -1768,8 +1783,8 @@ export class VideoModel extends Model { let baseUrlWs if (this.isOwned()) { - baseUrlHttp = CONFIG.WEBSERVER.URL - baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + baseUrlHttp = WEBSERVER.URL + baseUrlWs = WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT } else { baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host