1 import * as Bluebird from 'bluebird'
2 import { maxBy } from 'lodash'
3 import * as magnetUtil from 'magnet-uri'
4 import * as parseTorrent from 'parse-torrent'
5 import { join } from 'path'
6 import * as Sequelize from 'sequelize'
29 } from 'sequelize-typescript'
30 import { UserRight, VideoPrivacy, VideoState } from '../../../shared'
31 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
32 import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
33 import { VideoFilter } from '../../../shared/models/videos/video-query.type'
34 import { createTorrentPromise, peertubeTruncate } from '../../helpers/core-utils'
35 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
36 import { isArray, isBooleanValid } from '../../helpers/custom-validators/misc'
39 isVideoDescriptionValid,
47 } from '../../helpers/custom-validators/videos'
48 import { generateImageFromVideoFile, getVideoFileResolution } from '../../helpers/ffmpeg-utils'
49 import { logger } from '../../helpers/logger'
50 import { getServerActor } from '../../helpers/utils'
58 STATIC_DOWNLOAD_PATHS,
66 } from '../../initializers'
67 import { sendDeleteVideo } from '../../lib/activitypub/send'
68 import { AccountModel } from '../account/account'
69 import { AccountVideoRateModel } from '../account/account-video-rate'
70 import { ActorModel } from '../activitypub/actor'
71 import { AvatarModel } from '../avatar/avatar'
72 import { ServerModel } from '../server/server'
73 import { buildBlockedAccountSQL, buildTrigramSearchIndex, createSimilarityAttribute, getVideoSort, throwIfNotValid } from '../utils'
74 import { TagModel } from './tag'
75 import { VideoAbuseModel } from './video-abuse'
76 import { VideoChannelModel } from './video-channel'
77 import { VideoCommentModel } from './video-comment'
78 import { VideoFileModel } from './video-file'
79 import { VideoShareModel } from './video-share'
80 import { VideoTagModel } from './video-tag'
81 import { ScheduleVideoUpdateModel } from './schedule-video-update'
82 import { VideoCaptionModel } from './video-caption'
83 import { VideoBlacklistModel } from './video-blacklist'
84 import { remove, writeFile } from 'fs-extra'
85 import { VideoViewModel } from './video-views'
86 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
88 videoFilesModelToFormattedJSON,
89 VideoFormattingJSONOptions,
90 videoModelToActivityPubObject,
91 videoModelToFormattedDetailsJSON,
92 videoModelToFormattedJSON
93 } from './video-format-utils'
94 import * as validator from 'validator'
95 import { UserVideoHistoryModel } from '../account/user-video-history'
96 import { UserModel } from '../account/user'
98 // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
99 const indexes: Sequelize.DefineIndexesOptions[] = [
100 buildTrigramSearchIndex('video_name_trigram', 'name'),
102 { fields: [ 'createdAt' ] },
103 { fields: [ 'publishedAt' ] },
104 { fields: [ 'duration' ] },
105 { fields: [ 'category' ] },
106 { fields: [ 'licence' ] },
107 { fields: [ 'nsfw' ] },
108 { fields: [ 'language' ] },
109 { fields: [ 'waitTranscoding' ] },
110 { fields: [ 'state' ] },
111 { fields: [ 'remote' ] },
112 { fields: [ 'views' ] },
113 { fields: [ 'likes' ] },
114 { fields: [ 'channelId' ] },
125 export enum ScopeNames {
126 AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
128 WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS',
129 WITH_TAGS = 'WITH_TAGS',
130 WITH_FILES = 'WITH_FILES',
131 WITH_SCHEDULED_UPDATE = 'WITH_SCHEDULED_UPDATE',
132 WITH_BLACKLISTED = 'WITH_BLACKLISTED',
133 WITH_USER_HISTORY = 'WITH_USER_HISTORY'
136 type ForAPIOptions = {
141 type AvailableForListIDsOptions = {
142 serverAccountId: number
144 includeLocalVideos: boolean
146 categoryOneOf?: number[]
148 licenceOneOf?: number[]
149 languageOneOf?: string[]
154 videoChannelId?: number
155 trendingDays?: number
160 [ ScopeNames.FOR_API ]: (options: ForAPIOptions) => {
161 const accountInclude = {
162 attributes: [ 'id', 'name' ],
163 model: AccountModel.unscoped(),
167 attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
168 model: ActorModel.unscoped(),
172 attributes: [ 'host' ],
173 model: ServerModel.unscoped(),
177 model: AvatarModel.unscoped(),
185 const videoChannelInclude = {
186 attributes: [ 'name', 'description', 'id' ],
187 model: VideoChannelModel.unscoped(),
191 attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
192 model: ActorModel.unscoped(),
196 attributes: [ 'host' ],
197 model: ServerModel.unscoped(),
201 model: AvatarModel.unscoped(),
210 const query: IFindOptions<VideoModel> = {
213 [ Sequelize.Op.any ]: options.ids
216 include: [ videoChannelInclude ]
219 if (options.withFiles === true) {
221 model: VideoFileModel.unscoped(),
228 [ ScopeNames.AVAILABLE_FOR_LIST_IDS ]: (options: AvailableForListIDsOptions) => {
229 const query: IFindOptions<VideoModel> = {
231 attributes: [ 'id' ],
234 [ Sequelize.Op.and ]: [
236 [ Sequelize.Op.notIn ]: Sequelize.literal(
237 '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
243 [ Sequelize.Op.notIn ]: Sequelize.literal(
245 'SELECT id FROM "videoChannel" WHERE "accountId" IN (' +
246 buildBlockedAccountSQL(options.serverAccountId, options.user ? options.user.Account.id : undefined) +
255 // Only list public/published videos
256 if (!options.filter || options.filter !== 'all-local') {
257 const privacyWhere = {
258 // Always list public videos
259 privacy: VideoPrivacy.PUBLIC,
260 // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding
261 [ Sequelize.Op.or ]: [
263 state: VideoState.PUBLISHED
266 [ Sequelize.Op.and ]: {
267 state: VideoState.TO_TRANSCODE,
268 waitTranscoding: false
274 Object.assign(query.where, privacyWhere)
277 if (options.filter || options.accountId || options.videoChannelId) {
278 const videoChannelInclude: IIncludeOptions = {
280 model: VideoChannelModel.unscoped(),
284 if (options.videoChannelId) {
285 videoChannelInclude.where = {
286 id: options.videoChannelId
290 if (options.filter || options.accountId) {
291 const accountInclude: IIncludeOptions = {
293 model: AccountModel.unscoped(),
297 if (options.filter) {
298 accountInclude.include = [
301 model: ActorModel.unscoped(),
303 where: VideoModel.buildActorWhereWithFilter(options.filter)
308 if (options.accountId) {
309 accountInclude.where = { id: options.accountId }
312 videoChannelInclude.include = [ accountInclude ]
315 query.include.push(videoChannelInclude)
318 if (options.actorId) {
319 let localVideosReq = ''
320 if (options.includeLocalVideos === true) {
321 localVideosReq = ' UNION ALL ' +
322 'SELECT "video"."id" AS "id" FROM "video" ' +
323 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
324 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
325 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
326 'WHERE "actor"."serverId" IS NULL'
329 // Force actorId to be a number to avoid SQL injections
330 const actorIdNumber = parseInt(options.actorId.toString(), 10)
331 query.where[ 'id' ][ Sequelize.Op.and ].push({
332 [ Sequelize.Op.in ]: Sequelize.literal(
334 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
335 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
336 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
338 'SELECT "video"."id" AS "id" FROM "video" ' +
339 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
340 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
341 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
342 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
343 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
350 if (options.withFiles === true) {
351 query.where[ 'id' ][ Sequelize.Op.and ].push({
352 [ Sequelize.Op.in ]: Sequelize.literal(
353 '(SELECT "videoId" FROM "videoFile")'
358 // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN()
359 if (options.tagsAllOf || options.tagsOneOf) {
360 const createTagsIn = (tags: string[]) => {
361 return tags.map(t => VideoModel.sequelize.escape(t))
365 if (options.tagsOneOf) {
366 query.where[ 'id' ][ Sequelize.Op.and ].push({
367 [ Sequelize.Op.in ]: Sequelize.literal(
369 'SELECT "videoId" FROM "videoTag" ' +
370 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
371 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsOneOf) + ')' +
377 if (options.tagsAllOf) {
378 query.where[ 'id' ][ Sequelize.Op.and ].push({
379 [ Sequelize.Op.in ]: Sequelize.literal(
381 'SELECT "videoId" FROM "videoTag" ' +
382 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
383 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsAllOf) + ')' +
384 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length +
391 if (options.nsfw === true || options.nsfw === false) {
392 query.where[ 'nsfw' ] = options.nsfw
395 if (options.categoryOneOf) {
396 query.where[ 'category' ] = {
397 [ Sequelize.Op.or ]: options.categoryOneOf
401 if (options.licenceOneOf) {
402 query.where[ 'licence' ] = {
403 [ Sequelize.Op.or ]: options.licenceOneOf
407 if (options.languageOneOf) {
408 query.where[ 'language' ] = {
409 [ Sequelize.Op.or ]: options.languageOneOf
413 if (options.trendingDays) {
414 query.include.push(VideoModel.buildTrendingQuery(options.trendingDays))
416 query.subQuery = false
421 [ ScopeNames.WITH_ACCOUNT_DETAILS ]: {
424 model: () => VideoChannelModel.unscoped(),
429 exclude: [ 'privateKey', 'publicKey' ]
431 model: () => ActorModel.unscoped(),
435 attributes: [ 'host' ],
436 model: () => ServerModel.unscoped(),
440 model: () => AvatarModel.unscoped(),
446 model: () => AccountModel.unscoped(),
450 model: () => ActorModel.unscoped(),
452 exclude: [ 'privateKey', 'publicKey' ]
457 attributes: [ 'host' ],
458 model: () => ServerModel.unscoped(),
462 model: () => AvatarModel.unscoped(),
473 [ ScopeNames.WITH_TAGS ]: {
474 include: [ () => TagModel ]
476 [ ScopeNames.WITH_BLACKLISTED ]: {
479 attributes: [ 'id', 'reason' ],
480 model: () => VideoBlacklistModel,
485 [ ScopeNames.WITH_FILES ]: {
488 model: () => VideoFileModel.unscoped(),
490 [ 'separate' as any ]: true, // We may have multiple files, having multiple redundancies so let's separate this join
494 attributes: [ 'fileUrl' ],
495 model: () => VideoRedundancyModel.unscoped(),
502 [ ScopeNames.WITH_SCHEDULED_UPDATE ]: {
505 model: () => ScheduleVideoUpdateModel.unscoped(),
510 [ ScopeNames.WITH_USER_HISTORY ]: (userId: number) => {
514 attributes: [ 'currentTime' ],
515 model: UserVideoHistoryModel.unscoped(),
529 export class VideoModel extends Model<VideoModel> {
532 @Default(DataType.UUIDV4)
534 @Column(DataType.UUID)
538 @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
544 @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
550 @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
556 @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
557 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max))
561 @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
566 @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean'))
572 @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
573 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
578 @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support'))
579 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max))
583 @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
613 @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
614 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
619 commentsEnabled: boolean
623 waitTranscoding: boolean
627 @Is('VideoState', value => throwIfNotValid(value, isVideoStateValid, 'state'))
638 @Default(Sequelize.NOW)
642 @ForeignKey(() => VideoChannelModel)
646 @BelongsTo(() => VideoChannelModel, {
652 VideoChannel: VideoChannelModel
654 @BelongsToMany(() => TagModel, {
655 foreignKey: 'videoId',
656 through: () => VideoTagModel,
661 @HasMany(() => VideoAbuseModel, {
668 VideoAbuses: VideoAbuseModel[]
670 @HasMany(() => VideoFileModel, {
678 VideoFiles: VideoFileModel[]
680 @HasMany(() => VideoShareModel, {
687 VideoShares: VideoShareModel[]
689 @HasMany(() => AccountVideoRateModel, {
696 AccountVideoRates: AccountVideoRateModel[]
698 @HasMany(() => VideoCommentModel, {
706 VideoComments: VideoCommentModel[]
708 @HasMany(() => VideoViewModel, {
715 VideoViews: VideoViewModel[]
717 @HasMany(() => UserVideoHistoryModel, {
724 UserVideoHistories: UserVideoHistoryModel[]
726 @HasOne(() => ScheduleVideoUpdateModel, {
733 ScheduleVideoUpdate: ScheduleVideoUpdateModel
735 @HasOne(() => VideoBlacklistModel, {
742 VideoBlacklist: VideoBlacklistModel
744 @HasMany(() => VideoCaptionModel, {
751 [ 'separate' as any ]: true
753 VideoCaptions: VideoCaptionModel[]
756 static async sendDelete (instance: VideoModel, options) {
757 if (instance.isOwned()) {
758 if (!instance.VideoChannel) {
759 instance.VideoChannel = await instance.$get('VideoChannel', {
763 include: [ ActorModel ]
766 transaction: options.transaction
767 }) as VideoChannelModel
770 return sendDeleteVideo(instance, options.transaction)
777 static async removeFiles (instance: VideoModel) {
778 const tasks: Promise<any>[] = []
780 logger.info('Removing files of video %s.', instance.url)
782 tasks.push(instance.removeThumbnail())
784 if (instance.isOwned()) {
785 if (!Array.isArray(instance.VideoFiles)) {
786 instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[]
789 tasks.push(instance.removePreview())
791 // Remove physical files and torrents
792 instance.VideoFiles.forEach(file => {
793 tasks.push(instance.removeFile(file))
794 tasks.push(instance.removeTorrent(file))
798 // Do not wait video deletion because we could be in a transaction
801 logger.error('Some errors when removing files of video %s in before destroy hook.', instance.uuid, { err })
808 return VideoModel.scope(ScopeNames.WITH_FILES).findAll()
811 static listLocal () {
818 return VideoModel.scope(ScopeNames.WITH_FILES).findAll(query)
821 static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
822 function getRawQuery (select: string) {
823 const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
824 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
825 'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' +
826 'WHERE "Account"."actorId" = ' + actorId
827 const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
828 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
829 'WHERE "VideoShare"."actorId" = ' + actorId
831 return `(${queryVideo}) UNION (${queryVideoShare})`
834 const rawQuery = getRawQuery('"Video"."id"')
835 const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
841 order: getVideoSort('createdAt', [ 'Tags', 'name', 'ASC' ]),
844 [ Sequelize.Op.in ]: Sequelize.literal('(' + rawQuery + ')')
846 [ Sequelize.Op.or ]: [
847 { privacy: VideoPrivacy.PUBLIC },
848 { privacy: VideoPrivacy.UNLISTED }
853 attributes: [ 'language' ],
854 model: VideoCaptionModel.unscoped(),
858 attributes: [ 'id', 'url' ],
859 model: VideoShareModel.unscoped(),
861 // We only want videos shared by this actor
863 [ Sequelize.Op.and ]: [
866 [ Sequelize.Op.not ]: null
876 attributes: [ 'id', 'url' ],
877 model: ActorModel.unscoped()
882 model: VideoChannelModel.unscoped(),
886 attributes: [ 'name' ],
887 model: AccountModel.unscoped(),
891 attributes: [ 'id', 'url', 'followersUrl' ],
892 model: ActorModel.unscoped(),
898 attributes: [ 'id', 'url', 'followersUrl' ],
899 model: ActorModel.unscoped(),
909 return Bluebird.all([
910 // FIXME: typing issue
911 VideoModel.findAll(query as any),
912 VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
913 ]).then(([ rows, totals ]) => {
914 // totals: totalVideos + totalVideoShares
916 let totalVideoShares = 0
917 if (totals[ 0 ]) totalVideos = parseInt(totals[ 0 ].total, 10)
918 if (totals[ 1 ]) totalVideoShares = parseInt(totals[ 1 ].total, 10)
920 const total = totalVideos + totalVideoShares
928 static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) {
929 const query: IFindOptions<VideoModel> = {
932 order: getVideoSort(sort),
935 model: VideoChannelModel,
948 model: ScheduleVideoUpdateModel,
952 model: VideoBlacklistModel,
958 if (withFiles === true) {
960 model: VideoFileModel.unscoped(),
965 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
973 static async listForApi (options: {
978 includeLocalVideos: boolean,
980 categoryOneOf?: number[],
981 licenceOneOf?: number[],
982 languageOneOf?: string[],
983 tagsOneOf?: string[],
984 tagsAllOf?: string[],
985 filter?: VideoFilter,
987 videoChannelId?: number,
989 trendingDays?: number,
991 }, countVideos = true) {
992 if (options.filter && options.filter === 'all-local' && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) {
993 throw new Error('Try to filter all-local but no user has not the see all videos right')
996 const query: IFindOptions<VideoModel> = {
997 offset: options.start,
998 limit: options.count,
999 order: getVideoSort(options.sort)
1002 let trendingDays: number
1003 if (options.sort.endsWith('trending')) {
1004 trendingDays = CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
1006 query.group = 'VideoModel.id'
1009 const serverActor = await getServerActor()
1011 // actorId === null has a meaning, so just check undefined
1012 const actorId = options.actorId !== undefined ? options.actorId : serverActor.id
1014 const queryOptions = {
1016 serverAccountId: serverActor.Account.id,
1018 categoryOneOf: options.categoryOneOf,
1019 licenceOneOf: options.licenceOneOf,
1020 languageOneOf: options.languageOneOf,
1021 tagsOneOf: options.tagsOneOf,
1022 tagsAllOf: options.tagsAllOf,
1023 filter: options.filter,
1024 withFiles: options.withFiles,
1025 accountId: options.accountId,
1026 videoChannelId: options.videoChannelId,
1027 includeLocalVideos: options.includeLocalVideos,
1032 return VideoModel.getAvailableForApi(query, queryOptions, countVideos)
1035 static async searchAndPopulateAccountAndServer (options: {
1036 includeLocalVideos: boolean
1041 startDate?: string // ISO 8601
1042 endDate?: string // ISO 8601
1044 categoryOneOf?: number[]
1045 licenceOneOf?: number[]
1046 languageOneOf?: string[]
1047 tagsOneOf?: string[]
1048 tagsAllOf?: string[]
1049 durationMin?: number // seconds
1050 durationMax?: number // seconds
1052 filter?: VideoFilter
1056 if (options.startDate || options.endDate) {
1057 const publishedAtRange = {}
1059 if (options.startDate) publishedAtRange[ Sequelize.Op.gte ] = options.startDate
1060 if (options.endDate) publishedAtRange[ Sequelize.Op.lte ] = options.endDate
1062 whereAnd.push({ publishedAt: publishedAtRange })
1065 if (options.durationMin || options.durationMax) {
1066 const durationRange = {}
1068 if (options.durationMin) durationRange[ Sequelize.Op.gte ] = options.durationMin
1069 if (options.durationMax) durationRange[ Sequelize.Op.lte ] = options.durationMax
1071 whereAnd.push({ duration: durationRange })
1074 const attributesInclude = []
1075 const escapedSearch = VideoModel.sequelize.escape(options.search)
1076 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
1077 if (options.search) {
1081 [ Sequelize.Op.in ]: Sequelize.literal(
1083 'SELECT "video"."id" FROM "video" ' +
1085 'lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
1086 'lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
1088 'SELECT "video"."id" FROM "video" LEFT JOIN "videoTag" ON "videoTag"."videoId" = "video"."id" ' +
1089 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
1090 'WHERE "tag"."name" = ' + escapedSearch +
1097 attributesInclude.push(createSimilarityAttribute('VideoModel.name', options.search))
1100 // Cannot search on similarity if we don't have a search
1101 if (!options.search) {
1102 attributesInclude.push(
1103 Sequelize.literal('0 as similarity')
1107 const query: IFindOptions<VideoModel> = {
1109 include: attributesInclude
1111 offset: options.start,
1112 limit: options.count,
1113 order: getVideoSort(options.sort),
1115 [ Sequelize.Op.and ]: whereAnd
1119 const serverActor = await getServerActor()
1120 const queryOptions = {
1121 actorId: serverActor.id,
1122 serverAccountId: serverActor.Account.id,
1123 includeLocalVideos: options.includeLocalVideos,
1125 categoryOneOf: options.categoryOneOf,
1126 licenceOneOf: options.licenceOneOf,
1127 languageOneOf: options.languageOneOf,
1128 tagsOneOf: options.tagsOneOf,
1129 tagsAllOf: options.tagsAllOf,
1131 filter: options.filter
1134 return VideoModel.getAvailableForApi(query, queryOptions)
1137 static load (id: number | string, t?: Sequelize.Transaction) {
1138 const where = VideoModel.buildWhereIdOrUUID(id)
1144 return VideoModel.findOne(options)
1147 static loadOnlyId (id: number | string, t?: Sequelize.Transaction) {
1148 const where = VideoModel.buildWhereIdOrUUID(id)
1151 attributes: [ 'id' ],
1156 return VideoModel.findOne(options)
1159 static loadWithFile (id: number, t?: Sequelize.Transaction, logging?: boolean) {
1160 return VideoModel.scope(ScopeNames.WITH_FILES)
1161 .findById(id, { transaction: t, logging })
1164 static loadByUUIDWithFile (uuid: string) {
1172 .scope([ ScopeNames.WITH_FILES ])
1176 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
1177 const query: IFindOptions<VideoModel> = {
1184 return VideoModel.findOne(query)
1187 static loadByUrlAndPopulateAccount (url: string, transaction?: Sequelize.Transaction) {
1188 const query: IFindOptions<VideoModel> = {
1195 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
1198 static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Sequelize.Transaction, userId?: number) {
1199 const where = VideoModel.buildWhereIdOrUUID(id)
1202 order: [ [ 'Tags', 'name', 'ASC' ] ],
1208 ScopeNames.WITH_TAGS,
1209 ScopeNames.WITH_BLACKLISTED,
1210 ScopeNames.WITH_FILES,
1211 ScopeNames.WITH_ACCOUNT_DETAILS,
1212 ScopeNames.WITH_SCHEDULED_UPDATE
1216 scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] } as any) // FIXME: typings
1224 static async getStats () {
1225 const totalLocalVideos = await VideoModel.count({
1230 const totalVideos = await VideoModel.count()
1232 let totalLocalVideoViews = await VideoModel.sum('views', {
1237 // Sequelize could return null...
1238 if (!totalLocalVideoViews) totalLocalVideoViews = 0
1242 totalLocalVideoViews,
1247 static incrementViews (id: number, views: number) {
1248 return VideoModel.increment('views', {
1256 static checkVideoHasInstanceFollow (videoId: number, followerActorId: number) {
1257 // Instances only share videos
1258 const query = 'SELECT 1 FROM "videoShare" ' +
1259 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
1260 'WHERE "actorFollow"."actorId" = $followerActorId AND "videoShare"."videoId" = $videoId ' +
1264 type: Sequelize.QueryTypes.SELECT,
1265 bind: { followerActorId, videoId },
1269 return VideoModel.sequelize.query(query, options)
1270 .then(results => results.length === 1)
1273 // threshold corresponds to how many video the field should have to be returned
1274 static async getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) {
1275 const serverActor = await getServerActor()
1276 const actorId = serverActor.id
1278 const scopeOptions: AvailableForListIDsOptions = {
1279 serverAccountId: serverActor.Account.id,
1281 includeLocalVideos: true
1284 const query: IFindOptions<VideoModel> = {
1285 attributes: [ field ],
1288 having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col(field)), {
1289 [ Sequelize.Op.gte ]: threshold
1290 }) as any, // FIXME: typings
1291 order: [ this.sequelize.random() ]
1294 return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST_IDS, scopeOptions ] })
1296 .then(rows => rows.map(r => r[ field ]))
1299 static buildTrendingQuery (trendingDays: number) {
1303 model: VideoViewModel,
1307 [ Sequelize.Op.gte ]: new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
1313 private static buildActorWhereWithFilter (filter?: VideoFilter) {
1314 if (filter && (filter === 'local' || filter === 'all-local')) {
1323 private static async getAvailableForApi (
1324 query: IFindOptions<VideoModel>,
1325 options: AvailableForListIDsOptions,
1330 ScopeNames.AVAILABLE_FOR_LIST_IDS, options
1334 // Remove trending sort on count, because it uses a group by
1335 const countOptions = Object.assign({}, options, { trendingDays: undefined })
1336 const countQuery = Object.assign({}, query, { attributes: undefined, group: undefined })
1337 const countScope = {
1339 ScopeNames.AVAILABLE_FOR_LIST_IDS, countOptions
1343 const [ count, rowsId ] = await Promise.all([
1344 countVideos ? VideoModel.scope(countScope).count(countQuery) : Promise.resolve(undefined),
1345 VideoModel.scope(idsScope).findAll(query)
1347 const ids = rowsId.map(r => r.id)
1349 if (ids.length === 0) return { data: [], total: count }
1352 const apiScope: any[] = [
1354 method: [ ScopeNames.FOR_API, { ids, withFiles: options.withFiles } as ForAPIOptions ]
1359 apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] })
1362 const secondQuery = {
1365 attributes: query.attributes,
1366 order: [ // Keep original order
1368 ids.map(id => `"VideoModel".id = ${id} DESC`).join(', ')
1372 const rows = await VideoModel.scope(apiScope).findAll(secondQuery)
1380 static getCategoryLabel (id: number) {
1381 return VIDEO_CATEGORIES[ id ] || 'Misc'
1384 static getLicenceLabel (id: number) {
1385 return VIDEO_LICENCES[ id ] || 'Unknown'
1388 static getLanguageLabel (id: string) {
1389 return VIDEO_LANGUAGES[ id ] || 'Unknown'
1392 static getPrivacyLabel (id: number) {
1393 return VIDEO_PRIVACIES[ id ] || 'Unknown'
1396 static getStateLabel (id: number) {
1397 return VIDEO_STATES[ id ] || 'Unknown'
1400 static buildWhereIdOrUUID (id: number | string) {
1401 return validator.isInt('' + id) ? { id } : { uuid: id }
1404 getOriginalFile () {
1405 if (Array.isArray(this.VideoFiles) === false) return undefined
1407 // The original file is the file that have the higher resolution
1408 return maxBy(this.VideoFiles, file => file.resolution)
1411 getVideoFilename (videoFile: VideoFileModel) {
1412 return this.uuid + '-' + videoFile.resolution + videoFile.extname
1415 getThumbnailName () {
1416 // We always have a copy of the thumbnail
1417 const extension = '.jpg'
1418 return this.uuid + extension
1422 const extension = '.jpg'
1423 return this.uuid + extension
1426 getTorrentFileName (videoFile: VideoFileModel) {
1427 const extension = '.torrent'
1428 return this.uuid + '-' + videoFile.resolution + extension
1432 return this.remote === false
1435 createPreview (videoFile: VideoFileModel) {
1436 return generateImageFromVideoFile(
1437 this.getVideoFilePath(videoFile),
1438 CONFIG.STORAGE.PREVIEWS_DIR,
1439 this.getPreviewName(),
1444 createThumbnail (videoFile: VideoFileModel) {
1445 return generateImageFromVideoFile(
1446 this.getVideoFilePath(videoFile),
1447 CONFIG.STORAGE.THUMBNAILS_DIR,
1448 this.getThumbnailName(),
1453 getTorrentFilePath (videoFile: VideoFileModel) {
1454 return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1457 getVideoFilePath (videoFile: VideoFileModel) {
1458 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1461 async createTorrentAndSetInfoHash (videoFile: VideoFileModel) {
1463 // Keep the extname, it's used by the client to stream the file inside a web browser
1464 name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`,
1465 createdBy: 'PeerTube',
1467 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
1468 [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
1470 urlList: [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ]
1473 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
1475 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1476 logger.info('Creating torrent %s.', filePath)
1478 await writeFile(filePath, torrent)
1480 const parsedTorrent = parseTorrent(torrent)
1481 videoFile.infoHash = parsedTorrent.infoHash
1484 getEmbedStaticPath () {
1485 return '/videos/embed/' + this.uuid
1488 getThumbnailStaticPath () {
1489 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
1492 getPreviewStaticPath () {
1493 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
1496 toFormattedJSON (options?: VideoFormattingJSONOptions): Video {
1497 return videoModelToFormattedJSON(this, options)
1500 toFormattedDetailsJSON (): VideoDetails {
1501 return videoModelToFormattedDetailsJSON(this)
1504 getFormattedVideoFilesJSON (): VideoFile[] {
1505 return videoFilesModelToFormattedJSON(this, this.VideoFiles)
1508 toActivityPubObject (): VideoTorrentObject {
1509 return videoModelToActivityPubObject(this)
1512 getTruncatedDescription () {
1513 if (!this.description) return null
1515 const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
1516 return peertubeTruncate(this.description, maxLength)
1519 getOriginalFileResolution () {
1520 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1522 return getVideoFileResolution(originalFilePath)
1525 getDescriptionAPIPath () {
1526 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1529 removeThumbnail () {
1530 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1531 return remove(thumbnailPath)
1532 .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err }))
1536 const previewPath = join(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1537 return remove(previewPath)
1538 .catch(err => logger.warn('Cannot delete preview %s.', previewPath, { err }))
1541 removeFile (videoFile: VideoFileModel) {
1542 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1543 return remove(filePath)
1544 .catch(err => logger.warn('Cannot delete file %s.', filePath, { err }))
1547 removeTorrent (videoFile: VideoFileModel) {
1548 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1549 return remove(torrentPath)
1550 .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))
1554 if (this.isOwned()) return false
1556 const now = Date.now()
1557 const createdAtTime = this.createdAt.getTime()
1558 const updatedAtTime = this.updatedAt.getTime()
1560 return (now - createdAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL &&
1561 (now - updatedAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL
1565 this.changed('updatedAt', true)
1574 if (this.isOwned()) {
1575 baseUrlHttp = CONFIG.WEBSERVER.URL
1576 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1578 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1579 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1582 return { baseUrlHttp, baseUrlWs }
1585 generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1586 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1587 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1588 let urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1590 const redundancies = videoFile.RedundancyVideos
1591 if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
1593 const magnetHash = {
1597 infoHash: videoFile.infoHash,
1601 return magnetUtil.encode(magnetHash)
1604 getThumbnailUrl (baseUrlHttp: string) {
1605 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1608 getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1609 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1612 getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1613 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1616 getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1617 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1620 getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1621 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile)