X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo.ts;h=5027e980d94fde0fcd2d823b0712d349e7fb56f0;hb=d8b34ee55b654912f86bb8b472d391ced8c28f64;hp=ae2483b2f920ee2439161f0b2fa3616941b83050;hpb=fd261a8de933779480d631891efd2ac289045f2f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video.ts b/server/models/video/video.ts index ae2483b2f..5027e980d 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1,7 +1,8 @@ import * as Bluebird from 'bluebird' +import { remove } from 'fs-extra' import { maxBy, minBy, pick } from 'lodash' import { join } from 'path' -import { FindOptions, IncludeOptions, Op, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' +import { FindOptions, Includeable, IncludeOptions, Op, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -23,10 +24,19 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { UserRight, VideoPrivacy, VideoState, ResultList } from '../../../shared' -import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' +import { buildNSFWFilter } from '@server/helpers/express-utils' +import { getPrivaciesForFederation, isPrivacyForFederation, isStateForFederation } from '@server/helpers/video' +import { LiveManager } from '@server/lib/live-manager' +import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths' +import { getServerActor } from '@server/models/application/application' +import { ModelCache } from '@server/models/model-cache' +import { VideoFile } from '@shared/models/videos/video-file.model' +import { ResultList, UserRight, VideoPrivacy, VideoState } from '../../../shared' +import { VideoObject } from '../../../shared/models/activitypub/objects' import { Video, VideoDetails } from '../../../shared/models/videos' +import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' import { VideoFilter } from '../../../shared/models/videos/video-query.type' +import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' import { peertubeTruncate } from '../../helpers/core-utils' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { isBooleanValid } from '../../helpers/custom-validators/misc' @@ -41,8 +51,9 @@ import { isVideoStateValid, isVideoSupportValid } from '../../helpers/custom-validators/videos' -import { getVideoFileResolution } from '../../helpers/ffmpeg-utils' +import { getVideoFileResolution } from '../../helpers/ffprobe-utils' import { logger } from '../../helpers/logger' +import { CONFIG } from '../../initializers/config' import { ACTIVITY_PUB, API_VERSION, @@ -59,40 +70,6 @@ import { WEBSERVER } from '../../initializers/constants' import { sendDeleteVideo } from '../../lib/activitypub/send' -import { AccountModel } from '../account/account' -import { AccountVideoRateModel } from '../account/account-video-rate' -import { ActorModel } from '../activitypub/actor' -import { AvatarModel } from '../avatar/avatar' -import { ServerModel } from '../server/server' -import { buildTrigramSearchIndex, buildWhereIdOrUUID, getVideoSort, isOutdated, throwIfNotValid } from '../utils' -import { TagModel } from './tag' -import { VideoAbuseModel } from './video-abuse' -import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' -import { VideoCommentModel } from './video-comment' -import { VideoFileModel } from './video-file' -import { VideoShareModel } from './video-share' -import { VideoTagModel } from './video-tag' -import { ScheduleVideoUpdateModel } from './schedule-video-update' -import { VideoCaptionModel } from './video-caption' -import { VideoBlacklistModel } from './video-blacklist' -import { remove } from 'fs-extra' -import { VideoViewModel } from './video-view' -import { VideoRedundancyModel } from '../redundancy/video-redundancy' -import { - videoFilesModelToFormattedJSON, - VideoFormattingJSONOptions, - videoModelToActivityPubObject, - videoModelToFormattedDetailsJSON, - videoModelToFormattedJSON -} from './video-format-utils' -import { UserVideoHistoryModel } from '../account/user-video-history' -import { VideoImportModel } from './video-import' -import { VideoStreamingPlaylistModel } from './video-streaming-playlist' -import { VideoPlaylistElementModel } from './video-playlist-element' -import { CONFIG } from '../../initializers/config' -import { ThumbnailModel } from './thumbnail' -import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' -import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' import { MChannel, MChannelAccountDefault, @@ -117,16 +94,41 @@ import { MVideoWithAllFiles, MVideoWithFile, MVideoWithRights -} from '../../typings/models' -import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../typings/models/video/video-file' -import { MThumbnail } from '../../typings/models/video/thumbnail' -import { VideoFile } from '@shared/models/videos/video-file.model' -import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths' -import { ModelCache } from '@server/models/model-cache' +} from '../../types/models' +import { MThumbnail } from '../../types/models/video/thumbnail' +import { MVideoFile, MVideoFileRedundanciesOpt, MVideoFileStreamingPlaylistVideo } from '../../types/models/video/video-file' +import { VideoAbuseModel } from '../abuse/video-abuse' +import { AccountModel } from '../account/account' +import { AccountVideoRateModel } from '../account/account-video-rate' +import { UserVideoHistoryModel } from '../account/user-video-history' +import { ActorModel } from '../activitypub/actor' +import { AvatarModel } from '../avatar/avatar' +import { VideoRedundancyModel } from '../redundancy/video-redundancy' +import { ServerModel } from '../server/server' +import { buildTrigramSearchIndex, buildWhereIdOrUUID, getVideoSort, isOutdated, throwIfNotValid } from '../utils' +import { ScheduleVideoUpdateModel } from './schedule-video-update' +import { TagModel } from './tag' +import { ThumbnailModel } from './thumbnail' +import { VideoBlacklistModel } from './video-blacklist' +import { VideoCaptionModel } from './video-caption' +import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' +import { VideoCommentModel } from './video-comment' +import { VideoFileModel } from './video-file' +import { + videoFilesModelToFormattedJSON, + VideoFormattingJSONOptions, + videoModelToActivityPubObject, + videoModelToFormattedDetailsJSON, + videoModelToFormattedJSON +} from './video-format-utils' +import { VideoImportModel } from './video-import' +import { VideoLiveModel } from './video-live' +import { VideoPlaylistElementModel } from './video-playlist-element' import { buildListQuery, BuildVideosQueryOptions, wrapForAPIResults } from './video-query-builder' -import { buildNSFWFilter } from '@server/helpers/express-utils' -import { getServerActor } from '@server/models/application/application' -import { getPrivaciesForFederation, isPrivacyForFederation } from "@server/helpers/video" +import { VideoShareModel } from './video-share' +import { VideoStreamingPlaylistModel } from './video-streaming-playlist' +import { VideoTagModel } from './video-tag' +import { VideoViewModel } from './video-view' export enum ScopeNames { AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS', @@ -140,7 +142,8 @@ export enum ScopeNames { WITH_STREAMING_PLAYLISTS = 'WITH_STREAMING_PLAYLISTS', WITH_USER_ID = 'WITH_USER_ID', WITH_IMMUTABLE_ATTRIBUTES = 'WITH_IMMUTABLE_ATTRIBUTES', - WITH_THUMBNAILS = 'WITH_THUMBNAILS' + WITH_THUMBNAILS = 'WITH_THUMBNAILS', + WITH_LIVE = 'WITH_LIVE' } export type ForAPIOptions = { @@ -187,26 +190,26 @@ export type AvailableForListIDsOptions = { attributes: [ 'id', 'url', 'uuid', 'remote' ] }, [ScopeNames.FOR_API]: (options: ForAPIOptions) => { - const query: FindOptions = { - include: [ - { - model: VideoChannelModel.scope({ - method: [ - VideoChannelScopeNames.SUMMARY, { - withAccount: true, - withAccountBlockerIds: options.withAccountBlockerIds - } as SummaryOptions - ] - }), - required: true - }, - { - attributes: [ 'type', 'filename' ], - model: ThumbnailModel, - required: false - } - ] - } + const include: Includeable[] = [ + { + model: VideoChannelModel.scope({ + method: [ + VideoChannelScopeNames.SUMMARY, { + withAccount: true, + withAccountBlockerIds: options.withAccountBlockerIds + } as SummaryOptions + ] + }), + required: true + }, + { + attributes: [ 'type', 'filename' ], + model: ThumbnailModel, + required: false + } + ] + + const query: FindOptions = {} if (options.ids) { query.where = { @@ -217,14 +220,14 @@ export type AvailableForListIDsOptions = { } if (options.withFiles === true) { - query.include.push({ + include.push({ model: VideoFileModel, required: true }) } if (options.videoPlaylistId) { - query.include.push({ + include.push({ model: VideoPlaylistElementModel.unscoped(), required: true, where: { @@ -233,6 +236,8 @@ export type AvailableForListIDsOptions = { }) } + query.include = include + return query }, [ScopeNames.WITH_THUMBNAILS]: { @@ -243,6 +248,14 @@ export type AvailableForListIDsOptions = { } ] }, + [ScopeNames.WITH_LIVE]: { + include: [ + { + model: VideoLiveModel.unscoped(), + required: false + } + ] + }, [ScopeNames.WITH_USER_ID]: { include: [ { @@ -466,7 +479,7 @@ export type AvailableForListIDsOptions = { } ] }) -export class VideoModel extends Model { +export class VideoModel extends Model { @AllowNull(false) @Default(DataType.UUIDV4) @@ -500,7 +513,7 @@ export class VideoModel extends Model { @AllowNull(false) @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy')) @Column - privacy: number + privacy: VideoPrivacy @AllowNull(false) @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean')) @@ -549,6 +562,11 @@ export class VideoModel extends Model { @Column remote: boolean + @AllowNull(false) + @Default(false) + @Column + isLive: boolean + @AllowNull(false) @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max)) @@ -719,6 +737,15 @@ export class VideoModel extends Model { }) VideoBlacklist: VideoBlacklistModel + @HasOne(() => VideoLiveModel, { + foreignKey: { + name: 'videoId', + allowNull: false + }, + onDelete: 'cascade' + }) + VideoLive: VideoLiveModel + @HasOne(() => VideoImportModel, { foreignKey: { name: 'videoId', @@ -794,6 +821,15 @@ export class VideoModel extends Model { return undefined } + @BeforeDestroy + static stopLiveIfNeeded (instance: VideoModel) { + if (!instance.isLive) return + + logger.info('Stopping live of video %s after video deletion.', instance.uuid) + + return LiveManager.Instance.stopSessionOf(instance.id) + } + @BeforeDestroy static invalidateCache (instance: VideoModel) { ModelCache.Instance.invalidateCache('video', instance.id) @@ -803,14 +839,14 @@ export class VideoModel extends Model { static async saveEssentialDataToAbuses (instance: VideoModel, options) { const tasks: Promise[] = [] - logger.info('Saving video abuses details of video %s.', instance.url) - if (!Array.isArray(instance.VideoAbuses)) { instance.VideoAbuses = await instance.$get('VideoAbuses') if (instance.VideoAbuses.length === 0) return undefined } + logger.info('Saving video abuses details of video %s.', instance.url) + const details = instance.toFormattedDetailsJSON() for (const abuse of instance.VideoAbuses) { @@ -826,7 +862,7 @@ export class VideoModel extends Model { return undefined } - static listLocal (): Bluebird { + static listLocal (): Promise { const query = { where: { remote: false @@ -920,6 +956,17 @@ export class VideoModel extends Model { } ] }, + { + model: VideoStreamingPlaylistModel.unscoped(), + required: false, + include: [ + { + model: VideoFileModel, + required: false + } + ] + }, + VideoLiveModel.unscoped(), VideoFileModel, TagModel ] @@ -943,6 +990,20 @@ export class VideoModel extends Model { }) } + static async listPublishedLiveIds () { + const options = { + attributes: [ 'id' ], + where: { + isLive: true, + state: VideoState.PUBLISHED + } + } + + const result = await VideoModel.findAll(options) + + return result.map(v => v.id) + } + static listUserVideosForApi ( accountId: number, start: number, @@ -1026,8 +1087,9 @@ export class VideoModel extends Model { user?: MUserAccountId historyOfUser?: MUserId countVideos?: boolean + search?: string }) { - if (options.filter && options.filter === 'all-local' && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) { + if ((options.filter === 'all-local' || options.filter === 'all') && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) { throw new Error('Try to filter all-local but no user has not the see all videos right') } @@ -1062,7 +1124,8 @@ export class VideoModel extends Model { includeLocalVideos: options.includeLocalVideos, user: options.user, historyOfUser: options.historyOfUser, - trendingDays + trendingDays, + search: options.search } return VideoModel.getAvailableForApi(queryOptions, options.countVideos) @@ -1119,7 +1182,44 @@ export class VideoModel extends Model { return VideoModel.getAvailableForApi(queryOptions) } - static load (id: number | string, t?: Transaction): Bluebird { + static countLocalLives () { + const options = { + where: { + remote: false, + isLive: true, + state: { + [Op.ne]: VideoState.LIVE_ENDED + } + } + } + + return VideoModel.count(options) + } + + static countLivesOfAccount (accountId: number) { + const options = { + where: { + remote: false, + isLive: true, + state: { + [Op.ne]: VideoState.LIVE_ENDED + } + }, + include: [ + { + required: true, + model: VideoChannelModel.unscoped(), + where: { + accountId + } + } + ] + } + + return VideoModel.count(options) + } + + static load (id: number | string, t?: Transaction): Promise { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1129,7 +1229,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadWithBlacklist (id: number | string, t?: Transaction): Bluebird { + static loadWithBlacklist (id: number | string, t?: Transaction): Promise { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1142,7 +1242,7 @@ export class VideoModel extends Model { ]).findOne(options) } - static loadImmutableAttributes (id: number | string, t?: Transaction): Bluebird { + static loadImmutableAttributes (id: number | string, t?: Transaction): Promise { const fun = () => { const query = { where: buildWhereIdOrUUID(id), @@ -1160,7 +1260,7 @@ export class VideoModel extends Model { }) } - static loadWithRights (id: number | string, t?: Transaction): Bluebird { + static loadWithRights (id: number | string, t?: Transaction): Promise { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1174,7 +1274,7 @@ export class VideoModel extends Model { ]).findOne(options) } - static loadOnlyId (id: number | string, t?: Transaction): Bluebird { + static loadOnlyId (id: number | string, t?: Transaction): Promise { const where = buildWhereIdOrUUID(id) const options = { @@ -1186,7 +1286,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadWithFiles (id: number | string, t?: Transaction, logging?: boolean): Bluebird { + static loadWithFiles (id: number | string, t?: Transaction, logging?: boolean): Promise { const where = buildWhereIdOrUUID(id) const query = { @@ -1202,7 +1302,7 @@ export class VideoModel extends Model { ]).findOne(query) } - static loadByUUID (uuid: string): Bluebird { + static loadByUUID (uuid: string): Promise { const options = { where: { uuid @@ -1212,7 +1312,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadByUrl (url: string, transaction?: Transaction): Bluebird { + static loadByUrl (url: string, transaction?: Transaction): Promise { const query: FindOptions = { where: { url @@ -1223,7 +1323,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(query) } - static loadByUrlImmutableAttributes (url: string, transaction?: Transaction): Bluebird { + static loadByUrlImmutableAttributes (url: string, transaction?: Transaction): Promise { const fun = () => { const query: FindOptions = { where: { @@ -1243,7 +1343,7 @@ export class VideoModel extends Model { }) } - static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Bluebird { + static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Promise { const query: FindOptions = { where: { url @@ -1260,7 +1360,7 @@ export class VideoModel extends Model { ]).findOne(query) } - static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number): Bluebird { + static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number): Promise { const where = buildWhereIdOrUUID(id) const options = { @@ -1276,7 +1376,8 @@ export class VideoModel extends Model { ScopeNames.WITH_SCHEDULED_UPDATE, ScopeNames.WITH_WEBTORRENT_FILES, ScopeNames.WITH_STREAMING_PLAYLISTS, - ScopeNames.WITH_THUMBNAILS + ScopeNames.WITH_THUMBNAILS, + ScopeNames.WITH_LIVE ] if (userId) { @@ -1292,7 +1393,7 @@ export class VideoModel extends Model { id: number | string t?: Transaction userId?: number - }): Bluebird { + }): Promise { const { id, t, userId } = parameters const where = buildWhereIdOrUUID(id) @@ -1308,6 +1409,7 @@ export class VideoModel extends Model { ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_SCHEDULED_UPDATE, ScopeNames.WITH_THUMBNAILS, + ScopeNames.WITH_LIVE, { method: [ ScopeNames.WITH_WEBTORRENT_FILES, true ] }, { method: [ ScopeNames.WITH_STREAMING_PLAYLISTS, true ] } ] @@ -1390,7 +1492,7 @@ export class VideoModel extends Model { return VideoModel.update({ support: videoChannel.support }, options) } - static getAllIdsFromChannel (videoChannel: MChannelId): Bluebird { + static getAllIdsFromChannel (videoChannel: MChannelId): Promise { const query = { attributes: [ 'id' ], where: { @@ -1472,7 +1574,8 @@ export class VideoModel extends Model { } private static buildAPIResult (rows: any[]) { - const memo: { [ id: number ]: VideoModel } = {} + const videosMemo: { [ id: number ]: VideoModel } = {} + const videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel } = {} const thumbnailsDone = new Set() const historyDone = new Set() @@ -1484,6 +1587,7 @@ export class VideoModel extends Model { const actorKeys = [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ] const serverKeys = [ 'id', 'host' ] const videoFileKeys = [ 'id', 'createdAt', 'updatedAt', 'resolution', 'size', 'extname', 'infoHash', 'fps', 'videoId' ] + const videoStreamingPlaylistKeys = [ 'id' ] const videoKeys = [ 'id', 'uuid', @@ -1500,6 +1604,7 @@ export class VideoModel extends Model { 'likes', 'dislikes', 'remote', + 'isLive', 'url', 'commentsEnabled', 'downloadEnabled', @@ -1529,7 +1634,7 @@ export class VideoModel extends Model { } for (const row of rows) { - if (!memo[row.id]) { + if (!videosMemo[row.id]) { // Build Channel const channel = row.VideoChannel const channelModel = new VideoChannelModel(pick(channel, [ 'id', 'name', 'description', 'actorId' ])) @@ -1547,13 +1652,14 @@ export class VideoModel extends Model { videoModel.UserVideoHistories = [] videoModel.Thumbnails = [] videoModel.VideoFiles = [] + videoModel.VideoStreamingPlaylists = [] - memo[row.id] = videoModel + videosMemo[row.id] = videoModel // Don't take object value to have a sorted array videos.push(videoModel) } - const videoModel = memo[row.id] + const videoModel = videosMemo[row.id] if (row.userVideoHistory?.id && !historyDone.has(row.userVideoHistory.id)) { const historyModel = new UserVideoHistoryModel(pick(row.userVideoHistory, [ 'id', 'currentTime' ])) @@ -1575,6 +1681,24 @@ export class VideoModel extends Model { videoFilesDone.add(row.VideoFiles.id) } + + if (row.VideoStreamingPlaylists?.id && !videoStreamingPlaylistMemo[row.VideoStreamingPlaylists.id]) { + const streamingPlaylist = new VideoStreamingPlaylistModel(pick(row.VideoStreamingPlaylists, videoStreamingPlaylistKeys)) + streamingPlaylist.VideoFiles = [] + + videoModel.VideoStreamingPlaylists.push(streamingPlaylist) + + videoStreamingPlaylistMemo[streamingPlaylist.id] = streamingPlaylist + } + + if (row.VideoStreamingPlaylists?.VideoFiles?.id && !videoFilesDone.has(row.VideoStreamingPlaylists.VideoFiles.id)) { + const streamingPlaylist = videoStreamingPlaylistMemo[row.VideoStreamingPlaylists.id] + + const videoFileModel = new VideoFileModel(pick(row.VideoStreamingPlaylists.VideoFiles, videoFileKeys)) + streamingPlaylist.VideoFiles.push(videoFileModel) + + videoFilesDone.add(row.VideoStreamingPlaylists.VideoFiles.id) + } } return videos @@ -1605,8 +1729,7 @@ export class VideoModel extends Model { } isBlocked () { - return (this.VideoChannel.Account.Actor.Server && this.VideoChannel.Account.Actor.Server.isBlocked()) || - this.VideoChannel.Account.isBlocked() + return this.VideoChannel.Account.Actor.Server?.isBlocked() || this.VideoChannel.Account.isBlocked() } getQualityFileBy (this: T, fun: (files: MVideoFile[], it: (file: MVideoFile) => number) => MVideoFile) { @@ -1718,10 +1841,20 @@ export class VideoModel extends Model { getFormattedVideoFilesJSON (): VideoFile[] { const { baseUrlHttp, baseUrlWs } = this.getBaseUrls() - return videoFilesModelToFormattedJSON(this, baseUrlHttp, baseUrlWs, this.VideoFiles) + let files: MVideoFileRedundanciesOpt[] = [] + + if (Array.isArray(this.VideoFiles)) { + files = files.concat(this.VideoFiles) + } + + for (const p of (this.VideoStreamingPlaylists || [])) { + files = files.concat(p.VideoFiles || []) + } + + return videoFilesModelToFormattedJSON(this, baseUrlHttp, baseUrlWs, files) } - toActivityPubObject (this: MVideoAP): VideoTorrentObject { + toActivityPubObject (this: MVideoAP): VideoObject { return videoModelToActivityPubObject(this) } @@ -1808,6 +1941,10 @@ export class VideoModel extends Model { return isPrivacyForFederation(this.privacy) } + hasStateForFederation () { + return isStateForFederation(this.state) + } + isNewVideo (newPrivacy: VideoPrivacy) { return this.hasPrivacyForFederation() === false && isPrivacyForFederation(newPrivacy) === true }