Merge branch 'release/v1.2.0'
[oweals/peertube.git] / server / models / video / video.ts
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'
7 import {
8   AllowNull,
9   BeforeDestroy,
10   BelongsTo,
11   BelongsToMany,
12   Column,
13   CreatedAt,
14   DataType,
15   Default,
16   ForeignKey,
17   HasMany,
18   HasOne,
19   IFindOptions,
20   IIncludeOptions,
21   Is,
22   IsInt,
23   IsUUID,
24   Min,
25   Model,
26   Scopes,
27   Table,
28   UpdatedAt
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'
37 import {
38   isVideoCategoryValid,
39   isVideoDescriptionValid,
40   isVideoDurationValid,
41   isVideoLanguageValid,
42   isVideoLicenceValid,
43   isVideoNameValid,
44   isVideoPrivacyValid,
45   isVideoStateValid,
46   isVideoSupportValid
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'
51 import {
52   ACTIVITY_PUB,
53   API_VERSION,
54   CONFIG,
55   CONSTRAINTS_FIELDS,
56   PREVIEWS_SIZE,
57   REMOTE_SCHEME,
58   STATIC_DOWNLOAD_PATHS,
59   STATIC_PATHS,
60   THUMBNAILS_SIZE,
61   VIDEO_CATEGORIES,
62   VIDEO_LANGUAGES,
63   VIDEO_LICENCES,
64   VIDEO_PRIVACIES,
65   VIDEO_STATES
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'
87 import {
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'
97 import { VideoImportModel } from './video-import'
98
99 // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
100 const indexes: Sequelize.DefineIndexesOptions[] = [
101   buildTrigramSearchIndex('video_name_trigram', 'name'),
102
103   { fields: [ 'createdAt' ] },
104   { fields: [ 'publishedAt' ] },
105   { fields: [ 'duration' ] },
106   { fields: [ 'views' ] },
107   { fields: [ 'channelId' ] },
108   {
109     fields: [ 'category' ], // We don't care videos with an unknown category
110     where: {
111       category: {
112         [Sequelize.Op.ne]: null
113       }
114     }
115   },
116   {
117     fields: [ 'licence' ], // We don't care videos with an unknown licence
118     where: {
119       licence: {
120         [Sequelize.Op.ne]: null
121       }
122     }
123   },
124   {
125     fields: [ 'language' ], // We don't care videos with an unknown language
126     where: {
127       language: {
128         [Sequelize.Op.ne]: null
129       }
130     }
131   },
132   {
133     fields: [ 'nsfw' ], // Most of the videos are not NSFW
134     where: {
135       nsfw: true
136     }
137   },
138   {
139     fields: [ 'remote' ], // Only index local videos
140     where: {
141       remote: false
142     }
143   },
144   {
145     fields: [ 'uuid' ],
146     unique: true
147   },
148   {
149     fields: [ 'url' ],
150     unique: true
151   }
152 ]
153
154 export enum ScopeNames {
155   AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
156   FOR_API = 'FOR_API',
157   WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS',
158   WITH_TAGS = 'WITH_TAGS',
159   WITH_FILES = 'WITH_FILES',
160   WITH_SCHEDULED_UPDATE = 'WITH_SCHEDULED_UPDATE',
161   WITH_BLACKLISTED = 'WITH_BLACKLISTED',
162   WITH_USER_HISTORY = 'WITH_USER_HISTORY'
163 }
164
165 type ForAPIOptions = {
166   ids: number[]
167   withFiles?: boolean
168 }
169
170 type AvailableForListIDsOptions = {
171   serverAccountId: number
172   followerActorId: number
173   includeLocalVideos: boolean
174   filter?: VideoFilter
175   categoryOneOf?: number[]
176   nsfw?: boolean
177   licenceOneOf?: number[]
178   languageOneOf?: string[]
179   tagsOneOf?: string[]
180   tagsAllOf?: string[]
181   withFiles?: boolean
182   accountId?: number
183   videoChannelId?: number
184   trendingDays?: number
185   user?: UserModel,
186   historyOfUser?: UserModel
187 }
188
189 @Scopes({
190   [ ScopeNames.FOR_API ]: (options: ForAPIOptions) => {
191     const accountInclude = {
192       attributes: [ 'id', 'name' ],
193       model: AccountModel.unscoped(),
194       required: true,
195       include: [
196         {
197           attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
198           model: ActorModel.unscoped(),
199           required: true,
200           include: [
201             {
202               attributes: [ 'host' ],
203               model: ServerModel.unscoped(),
204               required: false
205             },
206             {
207               model: AvatarModel.unscoped(),
208               required: false
209             }
210           ]
211         }
212       ]
213     }
214
215     const videoChannelInclude = {
216       attributes: [ 'name', 'description', 'id' ],
217       model: VideoChannelModel.unscoped(),
218       required: true,
219       include: [
220         {
221           attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
222           model: ActorModel.unscoped(),
223           required: true,
224           include: [
225             {
226               attributes: [ 'host' ],
227               model: ServerModel.unscoped(),
228               required: false
229             },
230             {
231               model: AvatarModel.unscoped(),
232               required: false
233             }
234           ]
235         },
236         accountInclude
237       ]
238     }
239
240     const query: IFindOptions<VideoModel> = {
241       where: {
242         id: {
243           [ Sequelize.Op.any ]: options.ids
244         }
245       },
246       include: [ videoChannelInclude ]
247     }
248
249     if (options.withFiles === true) {
250       query.include.push({
251         model: VideoFileModel.unscoped(),
252         required: true
253       })
254     }
255
256     return query
257   },
258   [ ScopeNames.AVAILABLE_FOR_LIST_IDS ]: (options: AvailableForListIDsOptions) => {
259     const query: IFindOptions<VideoModel> = {
260       raw: true,
261       attributes: [ 'id' ],
262       where: {
263         id: {
264           [ Sequelize.Op.and ]: [
265             {
266               [ Sequelize.Op.notIn ]: Sequelize.literal(
267                 '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
268               )
269             }
270           ]
271         },
272         channelId: {
273           [ Sequelize.Op.notIn ]: Sequelize.literal(
274             '(' +
275               'SELECT id FROM "videoChannel" WHERE "accountId" IN (' +
276                 buildBlockedAccountSQL(options.serverAccountId, options.user ? options.user.Account.id : undefined) +
277               ')' +
278             ')'
279           )
280         }
281       },
282       include: []
283     }
284
285     // Only list public/published videos
286     if (!options.filter || options.filter !== 'all-local') {
287       const privacyWhere = {
288         // Always list public videos
289         privacy: VideoPrivacy.PUBLIC,
290         // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding
291         [ Sequelize.Op.or ]: [
292           {
293             state: VideoState.PUBLISHED
294           },
295           {
296             [ Sequelize.Op.and ]: {
297               state: VideoState.TO_TRANSCODE,
298               waitTranscoding: false
299             }
300           }
301         ]
302       }
303
304       Object.assign(query.where, privacyWhere)
305     }
306
307     if (options.filter || options.accountId || options.videoChannelId) {
308       const videoChannelInclude: IIncludeOptions = {
309         attributes: [],
310         model: VideoChannelModel.unscoped(),
311         required: true
312       }
313
314       if (options.videoChannelId) {
315         videoChannelInclude.where = {
316           id: options.videoChannelId
317         }
318       }
319
320       if (options.filter || options.accountId) {
321         const accountInclude: IIncludeOptions = {
322           attributes: [],
323           model: AccountModel.unscoped(),
324           required: true
325         }
326
327         if (options.filter) {
328           accountInclude.include = [
329             {
330               attributes: [],
331               model: ActorModel.unscoped(),
332               required: true,
333               where: VideoModel.buildActorWhereWithFilter(options.filter)
334             }
335           ]
336         }
337
338         if (options.accountId) {
339           accountInclude.where = { id: options.accountId }
340         }
341
342         videoChannelInclude.include = [ accountInclude ]
343       }
344
345       query.include.push(videoChannelInclude)
346     }
347
348     if (options.followerActorId) {
349       let localVideosReq = ''
350       if (options.includeLocalVideos === true) {
351         localVideosReq = ' UNION ALL ' +
352           'SELECT "video"."id" AS "id" FROM "video" ' +
353           'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
354           'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
355           'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
356           'WHERE "actor"."serverId" IS NULL'
357       }
358
359       // Force actorId to be a number to avoid SQL injections
360       const actorIdNumber = parseInt(options.followerActorId.toString(), 10)
361       query.where[ 'id' ][ Sequelize.Op.and ].push({
362         [ Sequelize.Op.in ]: Sequelize.literal(
363           '(' +
364           'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
365           'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
366           'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
367           ' UNION ALL ' +
368           'SELECT "video"."id" AS "id" FROM "video" ' +
369           'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
370           'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
371           'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
372           'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
373           'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
374           localVideosReq +
375           ')'
376         )
377       })
378     }
379
380     if (options.withFiles === true) {
381       query.where[ 'id' ][ Sequelize.Op.and ].push({
382         [ Sequelize.Op.in ]: Sequelize.literal(
383           '(SELECT "videoId" FROM "videoFile")'
384         )
385       })
386     }
387
388     // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN()
389     if (options.tagsAllOf || options.tagsOneOf) {
390       const createTagsIn = (tags: string[]) => {
391         return tags.map(t => VideoModel.sequelize.escape(t))
392                    .join(', ')
393       }
394
395       if (options.tagsOneOf) {
396         query.where[ 'id' ][ Sequelize.Op.and ].push({
397           [ Sequelize.Op.in ]: Sequelize.literal(
398             '(' +
399             'SELECT "videoId" FROM "videoTag" ' +
400             'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
401             'WHERE "tag"."name" IN (' + createTagsIn(options.tagsOneOf) + ')' +
402             ')'
403           )
404         })
405       }
406
407       if (options.tagsAllOf) {
408         query.where[ 'id' ][ Sequelize.Op.and ].push({
409           [ Sequelize.Op.in ]: Sequelize.literal(
410             '(' +
411             'SELECT "videoId" FROM "videoTag" ' +
412             'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
413             'WHERE "tag"."name" IN (' + createTagsIn(options.tagsAllOf) + ')' +
414             'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length +
415             ')'
416           )
417         })
418       }
419     }
420
421     if (options.nsfw === true || options.nsfw === false) {
422       query.where[ 'nsfw' ] = options.nsfw
423     }
424
425     if (options.categoryOneOf) {
426       query.where[ 'category' ] = {
427         [ Sequelize.Op.or ]: options.categoryOneOf
428       }
429     }
430
431     if (options.licenceOneOf) {
432       query.where[ 'licence' ] = {
433         [ Sequelize.Op.or ]: options.licenceOneOf
434       }
435     }
436
437     if (options.languageOneOf) {
438       query.where[ 'language' ] = {
439         [ Sequelize.Op.or ]: options.languageOneOf
440       }
441     }
442
443     if (options.trendingDays) {
444       query.include.push(VideoModel.buildTrendingQuery(options.trendingDays))
445
446       query.subQuery = false
447     }
448
449     if (options.historyOfUser) {
450       query.include.push({
451         model: UserVideoHistoryModel,
452         required: true,
453         where: {
454           userId: options.historyOfUser.id
455         }
456       })
457
458       // Even if the relation is n:m, we know that a user only have 0..1 video history
459       // So we won't have multiple rows for the same video
460       // Without this, we would not be able to sort on "updatedAt" column of UserVideoHistoryModel
461       query.subQuery = false
462     }
463
464     return query
465   },
466   [ ScopeNames.WITH_ACCOUNT_DETAILS ]: {
467     include: [
468       {
469         model: () => VideoChannelModel.unscoped(),
470         required: true,
471         include: [
472           {
473             attributes: {
474               exclude: [ 'privateKey', 'publicKey' ]
475             },
476             model: () => ActorModel.unscoped(),
477             required: true,
478             include: [
479               {
480                 attributes: [ 'host' ],
481                 model: () => ServerModel.unscoped(),
482                 required: false
483               },
484               {
485                 model: () => AvatarModel.unscoped(),
486                 required: false
487               }
488             ]
489           },
490           {
491             model: () => AccountModel.unscoped(),
492             required: true,
493             include: [
494               {
495                 model: () => ActorModel.unscoped(),
496                 attributes: {
497                   exclude: [ 'privateKey', 'publicKey' ]
498                 },
499                 required: true,
500                 include: [
501                   {
502                     attributes: [ 'host' ],
503                     model: () => ServerModel.unscoped(),
504                     required: false
505                   },
506                   {
507                     model: () => AvatarModel.unscoped(),
508                     required: false
509                   }
510                 ]
511               }
512             ]
513           }
514         ]
515       }
516     ]
517   },
518   [ ScopeNames.WITH_TAGS ]: {
519     include: [ () => TagModel ]
520   },
521   [ ScopeNames.WITH_BLACKLISTED ]: {
522     include: [
523       {
524         attributes: [ 'id', 'reason' ],
525         model: () => VideoBlacklistModel,
526         required: false
527       }
528     ]
529   },
530   [ ScopeNames.WITH_FILES ]: {
531     include: [
532       {
533         model: () => VideoFileModel.unscoped(),
534         // FIXME: typings
535         [ 'separate' as any ]: true, // We may have multiple files, having multiple redundancies so let's separate this join
536         required: false,
537         include: [
538           {
539             attributes: [ 'fileUrl' ],
540             model: () => VideoRedundancyModel.unscoped(),
541             required: false
542           }
543         ]
544       }
545     ]
546   },
547   [ ScopeNames.WITH_SCHEDULED_UPDATE ]: {
548     include: [
549       {
550         model: () => ScheduleVideoUpdateModel.unscoped(),
551         required: false
552       }
553     ]
554   },
555   [ ScopeNames.WITH_USER_HISTORY ]: (userId: number) => {
556     return {
557       include: [
558         {
559           attributes: [ 'currentTime' ],
560           model: UserVideoHistoryModel.unscoped(),
561           required: false,
562           where: {
563             userId
564           }
565         }
566       ]
567     }
568   }
569 })
570 @Table({
571   tableName: 'video',
572   indexes
573 })
574 export class VideoModel extends Model<VideoModel> {
575
576   @AllowNull(false)
577   @Default(DataType.UUIDV4)
578   @IsUUID(4)
579   @Column(DataType.UUID)
580   uuid: string
581
582   @AllowNull(false)
583   @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
584   @Column
585   name: string
586
587   @AllowNull(true)
588   @Default(null)
589   @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
590   @Column
591   category: number
592
593   @AllowNull(true)
594   @Default(null)
595   @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
596   @Column
597   licence: number
598
599   @AllowNull(true)
600   @Default(null)
601   @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
602   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max))
603   language: string
604
605   @AllowNull(false)
606   @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
607   @Column
608   privacy: number
609
610   @AllowNull(false)
611   @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean'))
612   @Column
613   nsfw: boolean
614
615   @AllowNull(true)
616   @Default(null)
617   @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
618   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
619   description: string
620
621   @AllowNull(true)
622   @Default(null)
623   @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support'))
624   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max))
625   support: string
626
627   @AllowNull(false)
628   @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
629   @Column
630   duration: number
631
632   @AllowNull(false)
633   @Default(0)
634   @IsInt
635   @Min(0)
636   @Column
637   views: number
638
639   @AllowNull(false)
640   @Default(0)
641   @IsInt
642   @Min(0)
643   @Column
644   likes: number
645
646   @AllowNull(false)
647   @Default(0)
648   @IsInt
649   @Min(0)
650   @Column
651   dislikes: number
652
653   @AllowNull(false)
654   @Column
655   remote: boolean
656
657   @AllowNull(false)
658   @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
659   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
660   url: string
661
662   @AllowNull(false)
663   @Column
664   commentsEnabled: boolean
665
666   @AllowNull(false)
667   @Column
668   waitTranscoding: boolean
669
670   @AllowNull(false)
671   @Default(null)
672   @Is('VideoState', value => throwIfNotValid(value, isVideoStateValid, 'state'))
673   @Column
674   state: VideoState
675
676   @CreatedAt
677   createdAt: Date
678
679   @UpdatedAt
680   updatedAt: Date
681
682   @AllowNull(false)
683   @Default(Sequelize.NOW)
684   @Column
685   publishedAt: Date
686
687   @ForeignKey(() => VideoChannelModel)
688   @Column
689   channelId: number
690
691   @BelongsTo(() => VideoChannelModel, {
692     foreignKey: {
693       allowNull: true
694     },
695     hooks: true
696   })
697   VideoChannel: VideoChannelModel
698
699   @BelongsToMany(() => TagModel, {
700     foreignKey: 'videoId',
701     through: () => VideoTagModel,
702     onDelete: 'CASCADE'
703   })
704   Tags: TagModel[]
705
706   @HasMany(() => VideoAbuseModel, {
707     foreignKey: {
708       name: 'videoId',
709       allowNull: false
710     },
711     onDelete: 'cascade'
712   })
713   VideoAbuses: VideoAbuseModel[]
714
715   @HasMany(() => VideoFileModel, {
716     foreignKey: {
717       name: 'videoId',
718       allowNull: false
719     },
720     hooks: true,
721     onDelete: 'cascade'
722   })
723   VideoFiles: VideoFileModel[]
724
725   @HasMany(() => VideoShareModel, {
726     foreignKey: {
727       name: 'videoId',
728       allowNull: false
729     },
730     onDelete: 'cascade'
731   })
732   VideoShares: VideoShareModel[]
733
734   @HasMany(() => AccountVideoRateModel, {
735     foreignKey: {
736       name: 'videoId',
737       allowNull: false
738     },
739     onDelete: 'cascade'
740   })
741   AccountVideoRates: AccountVideoRateModel[]
742
743   @HasMany(() => VideoCommentModel, {
744     foreignKey: {
745       name: 'videoId',
746       allowNull: false
747     },
748     onDelete: 'cascade',
749     hooks: true
750   })
751   VideoComments: VideoCommentModel[]
752
753   @HasMany(() => VideoViewModel, {
754     foreignKey: {
755       name: 'videoId',
756       allowNull: false
757     },
758     onDelete: 'cascade'
759   })
760   VideoViews: VideoViewModel[]
761
762   @HasMany(() => UserVideoHistoryModel, {
763     foreignKey: {
764       name: 'videoId',
765       allowNull: false
766     },
767     onDelete: 'cascade'
768   })
769   UserVideoHistories: UserVideoHistoryModel[]
770
771   @HasOne(() => ScheduleVideoUpdateModel, {
772     foreignKey: {
773       name: 'videoId',
774       allowNull: false
775     },
776     onDelete: 'cascade'
777   })
778   ScheduleVideoUpdate: ScheduleVideoUpdateModel
779
780   @HasOne(() => VideoBlacklistModel, {
781     foreignKey: {
782       name: 'videoId',
783       allowNull: false
784     },
785     onDelete: 'cascade'
786   })
787   VideoBlacklist: VideoBlacklistModel
788
789   @HasOne(() => VideoImportModel, {
790     foreignKey: {
791       name: 'videoId',
792       allowNull: true
793     },
794     onDelete: 'set null'
795   })
796   VideoImport: VideoImportModel
797
798   @HasMany(() => VideoCaptionModel, {
799     foreignKey: {
800       name: 'videoId',
801       allowNull: false
802     },
803     onDelete: 'cascade',
804     hooks: true,
805     [ 'separate' as any ]: true
806   })
807   VideoCaptions: VideoCaptionModel[]
808
809   @BeforeDestroy
810   static async sendDelete (instance: VideoModel, options) {
811     if (instance.isOwned()) {
812       if (!instance.VideoChannel) {
813         instance.VideoChannel = await instance.$get('VideoChannel', {
814           include: [
815             {
816               model: AccountModel,
817               include: [ ActorModel ]
818             }
819           ],
820           transaction: options.transaction
821         }) as VideoChannelModel
822       }
823
824       return sendDeleteVideo(instance, options.transaction)
825     }
826
827     return undefined
828   }
829
830   @BeforeDestroy
831   static async removeFiles (instance: VideoModel) {
832     const tasks: Promise<any>[] = []
833
834     logger.info('Removing files of video %s.', instance.url)
835
836     tasks.push(instance.removeThumbnail())
837
838     if (instance.isOwned()) {
839       if (!Array.isArray(instance.VideoFiles)) {
840         instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[]
841       }
842
843       tasks.push(instance.removePreview())
844
845       // Remove physical files and torrents
846       instance.VideoFiles.forEach(file => {
847         tasks.push(instance.removeFile(file))
848         tasks.push(instance.removeTorrent(file))
849       })
850     }
851
852     // Do not wait video deletion because we could be in a transaction
853     Promise.all(tasks)
854            .catch(err => {
855              logger.error('Some errors when removing files of video %s in before destroy hook.', instance.uuid, { err })
856            })
857
858     return undefined
859   }
860
861   static list () {
862     return VideoModel.scope(ScopeNames.WITH_FILES).findAll()
863   }
864
865   static listLocal () {
866     const query = {
867       where: {
868         remote: false
869       }
870     }
871
872     return VideoModel.scope(ScopeNames.WITH_FILES).findAll(query)
873   }
874
875   static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
876     function getRawQuery (select: string) {
877       const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
878         'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
879         'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' +
880         'WHERE "Account"."actorId" = ' + actorId
881       const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
882         'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
883         'WHERE "VideoShare"."actorId" = ' + actorId
884
885       return `(${queryVideo}) UNION (${queryVideoShare})`
886     }
887
888     const rawQuery = getRawQuery('"Video"."id"')
889     const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
890
891     const query = {
892       distinct: true,
893       offset: start,
894       limit: count,
895       order: getVideoSort('createdAt', [ 'Tags', 'name', 'ASC' ]),
896       where: {
897         id: {
898           [ Sequelize.Op.in ]: Sequelize.literal('(' + rawQuery + ')')
899         },
900         [ Sequelize.Op.or ]: [
901           { privacy: VideoPrivacy.PUBLIC },
902           { privacy: VideoPrivacy.UNLISTED }
903         ]
904       },
905       include: [
906         {
907           attributes: [ 'language' ],
908           model: VideoCaptionModel.unscoped(),
909           required: false
910         },
911         {
912           attributes: [ 'id', 'url' ],
913           model: VideoShareModel.unscoped(),
914           required: false,
915           // We only want videos shared by this actor
916           where: {
917             [ Sequelize.Op.and ]: [
918               {
919                 id: {
920                   [ Sequelize.Op.not ]: null
921                 }
922               },
923               {
924                 actorId
925               }
926             ]
927           },
928           include: [
929             {
930               attributes: [ 'id', 'url' ],
931               model: ActorModel.unscoped()
932             }
933           ]
934         },
935         {
936           model: VideoChannelModel.unscoped(),
937           required: true,
938           include: [
939             {
940               attributes: [ 'name' ],
941               model: AccountModel.unscoped(),
942               required: true,
943               include: [
944                 {
945                   attributes: [ 'id', 'url', 'followersUrl' ],
946                   model: ActorModel.unscoped(),
947                   required: true
948                 }
949               ]
950             },
951             {
952               attributes: [ 'id', 'url', 'followersUrl' ],
953               model: ActorModel.unscoped(),
954               required: true
955             }
956           ]
957         },
958         VideoFileModel,
959         TagModel
960       ]
961     }
962
963     return Bluebird.all([
964       // FIXME: typing issue
965       VideoModel.findAll(query as any),
966       VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
967     ]).then(([ rows, totals ]) => {
968       // totals: totalVideos + totalVideoShares
969       let totalVideos = 0
970       let totalVideoShares = 0
971       if (totals[ 0 ]) totalVideos = parseInt(totals[ 0 ].total, 10)
972       if (totals[ 1 ]) totalVideoShares = parseInt(totals[ 1 ].total, 10)
973
974       const total = totalVideos + totalVideoShares
975       return {
976         data: rows,
977         total: total
978       }
979     })
980   }
981
982   static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) {
983     const query: IFindOptions<VideoModel> = {
984       offset: start,
985       limit: count,
986       order: getVideoSort(sort),
987       include: [
988         {
989           model: VideoChannelModel,
990           required: true,
991           include: [
992             {
993               model: AccountModel,
994               where: {
995                 id: accountId
996               },
997               required: true
998             }
999           ]
1000         },
1001         {
1002           model: ScheduleVideoUpdateModel,
1003           required: false
1004         },
1005         {
1006           model: VideoBlacklistModel,
1007           required: false
1008         }
1009       ]
1010     }
1011
1012     if (withFiles === true) {
1013       query.include.push({
1014         model: VideoFileModel.unscoped(),
1015         required: true
1016       })
1017     }
1018
1019     return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
1020       return {
1021         data: rows,
1022         total: count
1023       }
1024     })
1025   }
1026
1027   static async listForApi (options: {
1028     start: number,
1029     count: number,
1030     sort: string,
1031     nsfw: boolean,
1032     includeLocalVideos: boolean,
1033     withFiles: boolean,
1034     categoryOneOf?: number[],
1035     licenceOneOf?: number[],
1036     languageOneOf?: string[],
1037     tagsOneOf?: string[],
1038     tagsAllOf?: string[],
1039     filter?: VideoFilter,
1040     accountId?: number,
1041     videoChannelId?: number,
1042     followerActorId?: number
1043     trendingDays?: number,
1044     user?: UserModel,
1045     historyOfUser?: UserModel
1046   }, countVideos = true) {
1047     if (options.filter && options.filter === 'all-local' && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) {
1048       throw new Error('Try to filter all-local but no user has not the see all videos right')
1049     }
1050
1051     const query: IFindOptions<VideoModel> = {
1052       offset: options.start,
1053       limit: options.count,
1054       order: getVideoSort(options.sort)
1055     }
1056
1057     let trendingDays: number
1058     if (options.sort.endsWith('trending')) {
1059       trendingDays = CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
1060
1061       query.group = 'VideoModel.id'
1062     }
1063
1064     const serverActor = await getServerActor()
1065
1066     // followerActorId === null has a meaning, so just check undefined
1067     const followerActorId = options.followerActorId !== undefined ? options.followerActorId : serverActor.id
1068
1069     const queryOptions = {
1070       followerActorId,
1071       serverAccountId: serverActor.Account.id,
1072       nsfw: options.nsfw,
1073       categoryOneOf: options.categoryOneOf,
1074       licenceOneOf: options.licenceOneOf,
1075       languageOneOf: options.languageOneOf,
1076       tagsOneOf: options.tagsOneOf,
1077       tagsAllOf: options.tagsAllOf,
1078       filter: options.filter,
1079       withFiles: options.withFiles,
1080       accountId: options.accountId,
1081       videoChannelId: options.videoChannelId,
1082       includeLocalVideos: options.includeLocalVideos,
1083       user: options.user,
1084       historyOfUser: options.historyOfUser,
1085       trendingDays
1086     }
1087
1088     return VideoModel.getAvailableForApi(query, queryOptions, countVideos)
1089   }
1090
1091   static async searchAndPopulateAccountAndServer (options: {
1092     includeLocalVideos: boolean
1093     search?: string
1094     start?: number
1095     count?: number
1096     sort?: string
1097     startDate?: string // ISO 8601
1098     endDate?: string // ISO 8601
1099     nsfw?: boolean
1100     categoryOneOf?: number[]
1101     licenceOneOf?: number[]
1102     languageOneOf?: string[]
1103     tagsOneOf?: string[]
1104     tagsAllOf?: string[]
1105     durationMin?: number // seconds
1106     durationMax?: number // seconds
1107     user?: UserModel,
1108     filter?: VideoFilter
1109   }) {
1110     const whereAnd = []
1111
1112     if (options.startDate || options.endDate) {
1113       const publishedAtRange = {}
1114
1115       if (options.startDate) publishedAtRange[ Sequelize.Op.gte ] = options.startDate
1116       if (options.endDate) publishedAtRange[ Sequelize.Op.lte ] = options.endDate
1117
1118       whereAnd.push({ publishedAt: publishedAtRange })
1119     }
1120
1121     if (options.durationMin || options.durationMax) {
1122       const durationRange = {}
1123
1124       if (options.durationMin) durationRange[ Sequelize.Op.gte ] = options.durationMin
1125       if (options.durationMax) durationRange[ Sequelize.Op.lte ] = options.durationMax
1126
1127       whereAnd.push({ duration: durationRange })
1128     }
1129
1130     const attributesInclude = []
1131     const escapedSearch = VideoModel.sequelize.escape(options.search)
1132     const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
1133     if (options.search) {
1134       whereAnd.push(
1135         {
1136           id: {
1137             [ Sequelize.Op.in ]: Sequelize.literal(
1138               '(' +
1139               'SELECT "video"."id" FROM "video" ' +
1140               'WHERE ' +
1141               'lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
1142               'lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
1143               'UNION ALL ' +
1144               'SELECT "video"."id" FROM "video" LEFT JOIN "videoTag" ON "videoTag"."videoId" = "video"."id" ' +
1145               'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
1146               'WHERE "tag"."name" = ' + escapedSearch +
1147               ')'
1148             )
1149           }
1150         }
1151       )
1152
1153       attributesInclude.push(createSimilarityAttribute('VideoModel.name', options.search))
1154     }
1155
1156     // Cannot search on similarity if we don't have a search
1157     if (!options.search) {
1158       attributesInclude.push(
1159         Sequelize.literal('0 as similarity')
1160       )
1161     }
1162
1163     const query: IFindOptions<VideoModel> = {
1164       attributes: {
1165         include: attributesInclude
1166       },
1167       offset: options.start,
1168       limit: options.count,
1169       order: getVideoSort(options.sort),
1170       where: {
1171         [ Sequelize.Op.and ]: whereAnd
1172       }
1173     }
1174
1175     const serverActor = await getServerActor()
1176     const queryOptions = {
1177       followerActorId: serverActor.id,
1178       serverAccountId: serverActor.Account.id,
1179       includeLocalVideos: options.includeLocalVideos,
1180       nsfw: options.nsfw,
1181       categoryOneOf: options.categoryOneOf,
1182       licenceOneOf: options.licenceOneOf,
1183       languageOneOf: options.languageOneOf,
1184       tagsOneOf: options.tagsOneOf,
1185       tagsAllOf: options.tagsAllOf,
1186       user: options.user,
1187       filter: options.filter
1188     }
1189
1190     return VideoModel.getAvailableForApi(query, queryOptions)
1191   }
1192
1193   static load (id: number | string, t?: Sequelize.Transaction) {
1194     const where = VideoModel.buildWhereIdOrUUID(id)
1195     const options = {
1196       where,
1197       transaction: t
1198     }
1199
1200     return VideoModel.findOne(options)
1201   }
1202
1203   static loadOnlyId (id: number | string, t?: Sequelize.Transaction) {
1204     const where = VideoModel.buildWhereIdOrUUID(id)
1205
1206     const options = {
1207       attributes: [ 'id' ],
1208       where,
1209       transaction: t
1210     }
1211
1212     return VideoModel.findOne(options)
1213   }
1214
1215   static loadWithFile (id: number, t?: Sequelize.Transaction, logging?: boolean) {
1216     return VideoModel.scope(ScopeNames.WITH_FILES)
1217                      .findById(id, { transaction: t, logging })
1218   }
1219
1220   static loadByUUIDWithFile (uuid: string) {
1221     const options = {
1222       where: {
1223         uuid
1224       }
1225     }
1226
1227     return VideoModel
1228       .scope([ ScopeNames.WITH_FILES ])
1229       .findOne(options)
1230   }
1231
1232   static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
1233     const query: IFindOptions<VideoModel> = {
1234       where: {
1235         url
1236       },
1237       transaction
1238     }
1239
1240     return VideoModel.findOne(query)
1241   }
1242
1243   static loadByUrlAndPopulateAccount (url: string, transaction?: Sequelize.Transaction) {
1244     const query: IFindOptions<VideoModel> = {
1245       where: {
1246         url
1247       },
1248       transaction
1249     }
1250
1251     return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
1252   }
1253
1254   static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Sequelize.Transaction, userId?: number) {
1255     const where = VideoModel.buildWhereIdOrUUID(id)
1256
1257     const options = {
1258       order: [ [ 'Tags', 'name', 'ASC' ] ],
1259       where,
1260       transaction: t
1261     }
1262
1263     const scopes = [
1264       ScopeNames.WITH_TAGS,
1265       ScopeNames.WITH_BLACKLISTED,
1266       ScopeNames.WITH_FILES,
1267       ScopeNames.WITH_ACCOUNT_DETAILS,
1268       ScopeNames.WITH_SCHEDULED_UPDATE
1269     ]
1270
1271     if (userId) {
1272       scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] } as any) // FIXME: typings
1273     }
1274
1275     return VideoModel
1276       .scope(scopes)
1277       .findOne(options)
1278   }
1279
1280   static async getStats () {
1281     const totalLocalVideos = await VideoModel.count({
1282       where: {
1283         remote: false
1284       }
1285     })
1286     const totalVideos = await VideoModel.count()
1287
1288     let totalLocalVideoViews = await VideoModel.sum('views', {
1289       where: {
1290         remote: false
1291       }
1292     })
1293     // Sequelize could return null...
1294     if (!totalLocalVideoViews) totalLocalVideoViews = 0
1295
1296     return {
1297       totalLocalVideos,
1298       totalLocalVideoViews,
1299       totalVideos
1300     }
1301   }
1302
1303   static incrementViews (id: number, views: number) {
1304     return VideoModel.increment('views', {
1305       by: views,
1306       where: {
1307         id
1308       }
1309     })
1310   }
1311
1312   static checkVideoHasInstanceFollow (videoId: number, followerActorId: number) {
1313     // Instances only share videos
1314     const query = 'SELECT 1 FROM "videoShare" ' +
1315     'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
1316     'WHERE "actorFollow"."actorId" = $followerActorId AND "videoShare"."videoId" = $videoId ' +
1317     'LIMIT 1'
1318
1319     const options = {
1320       type: Sequelize.QueryTypes.SELECT,
1321       bind: { followerActorId, videoId },
1322       raw: true
1323     }
1324
1325     return VideoModel.sequelize.query(query, options)
1326                      .then(results => results.length === 1)
1327   }
1328
1329   // threshold corresponds to how many video the field should have to be returned
1330   static async getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) {
1331     const serverActor = await getServerActor()
1332     const followerActorId = serverActor.id
1333
1334     const scopeOptions: AvailableForListIDsOptions = {
1335       serverAccountId: serverActor.Account.id,
1336       followerActorId,
1337       includeLocalVideos: true
1338     }
1339
1340     const query: IFindOptions<VideoModel> = {
1341       attributes: [ field ],
1342       limit: count,
1343       group: field,
1344       having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col(field)), {
1345         [ Sequelize.Op.gte ]: threshold
1346       }) as any, // FIXME: typings
1347       order: [ this.sequelize.random() ]
1348     }
1349
1350     return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST_IDS, scopeOptions ] })
1351                      .findAll(query)
1352                      .then(rows => rows.map(r => r[ field ]))
1353   }
1354
1355   static buildTrendingQuery (trendingDays: number) {
1356     return {
1357       attributes: [],
1358       subQuery: false,
1359       model: VideoViewModel,
1360       required: false,
1361       where: {
1362         startDate: {
1363           [ Sequelize.Op.gte ]: new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
1364         }
1365       }
1366     }
1367   }
1368
1369   private static buildActorWhereWithFilter (filter?: VideoFilter) {
1370     if (filter && (filter === 'local' || filter === 'all-local')) {
1371       return {
1372         serverId: null
1373       }
1374     }
1375
1376     return {}
1377   }
1378
1379   private static async getAvailableForApi (
1380     query: IFindOptions<VideoModel>,
1381     options: AvailableForListIDsOptions,
1382     countVideos = true
1383   ) {
1384     const idsScope = {
1385       method: [
1386         ScopeNames.AVAILABLE_FOR_LIST_IDS, options
1387       ]
1388     }
1389
1390     // Remove trending sort on count, because it uses a group by
1391     const countOptions = Object.assign({}, options, { trendingDays: undefined })
1392     const countQuery = Object.assign({}, query, { attributes: undefined, group: undefined })
1393     const countScope = {
1394       method: [
1395         ScopeNames.AVAILABLE_FOR_LIST_IDS, countOptions
1396       ]
1397     }
1398
1399     const [ count, rowsId ] = await Promise.all([
1400       countVideos ? VideoModel.scope(countScope).count(countQuery) : Promise.resolve<number>(undefined),
1401       VideoModel.scope(idsScope).findAll(query)
1402     ])
1403     const ids = rowsId.map(r => r.id)
1404
1405     if (ids.length === 0) return { data: [], total: count }
1406
1407     // FIXME: typings
1408     const apiScope: any[] = [
1409       {
1410         method: [ ScopeNames.FOR_API, { ids, withFiles: options.withFiles } as ForAPIOptions ]
1411       }
1412     ]
1413
1414     if (options.user) {
1415       apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] })
1416     }
1417
1418     const secondQuery = {
1419       offset: 0,
1420       limit: query.limit,
1421       attributes: query.attributes,
1422       order: [ // Keep original order
1423         Sequelize.literal(
1424           ids.map(id => `"VideoModel".id = ${id} DESC`).join(', ')
1425         )
1426       ]
1427     }
1428     const rows = await VideoModel.scope(apiScope).findAll(secondQuery)
1429
1430     return {
1431       data: rows,
1432       total: count
1433     }
1434   }
1435
1436   static getCategoryLabel (id: number) {
1437     return VIDEO_CATEGORIES[ id ] || 'Misc'
1438   }
1439
1440   static getLicenceLabel (id: number) {
1441     return VIDEO_LICENCES[ id ] || 'Unknown'
1442   }
1443
1444   static getLanguageLabel (id: string) {
1445     return VIDEO_LANGUAGES[ id ] || 'Unknown'
1446   }
1447
1448   static getPrivacyLabel (id: number) {
1449     return VIDEO_PRIVACIES[ id ] || 'Unknown'
1450   }
1451
1452   static getStateLabel (id: number) {
1453     return VIDEO_STATES[ id ] || 'Unknown'
1454   }
1455
1456   static buildWhereIdOrUUID (id: number | string) {
1457     return validator.isInt('' + id) ? { id } : { uuid: id }
1458   }
1459
1460   getOriginalFile () {
1461     if (Array.isArray(this.VideoFiles) === false) return undefined
1462
1463     // The original file is the file that have the higher resolution
1464     return maxBy(this.VideoFiles, file => file.resolution)
1465   }
1466
1467   getVideoFilename (videoFile: VideoFileModel) {
1468     return this.uuid + '-' + videoFile.resolution + videoFile.extname
1469   }
1470
1471   getThumbnailName () {
1472     // We always have a copy of the thumbnail
1473     const extension = '.jpg'
1474     return this.uuid + extension
1475   }
1476
1477   getPreviewName () {
1478     const extension = '.jpg'
1479     return this.uuid + extension
1480   }
1481
1482   getTorrentFileName (videoFile: VideoFileModel) {
1483     const extension = '.torrent'
1484     return this.uuid + '-' + videoFile.resolution + extension
1485   }
1486
1487   isOwned () {
1488     return this.remote === false
1489   }
1490
1491   createPreview (videoFile: VideoFileModel) {
1492     return generateImageFromVideoFile(
1493       this.getVideoFilePath(videoFile),
1494       CONFIG.STORAGE.PREVIEWS_DIR,
1495       this.getPreviewName(),
1496       PREVIEWS_SIZE
1497     )
1498   }
1499
1500   createThumbnail (videoFile: VideoFileModel) {
1501     return generateImageFromVideoFile(
1502       this.getVideoFilePath(videoFile),
1503       CONFIG.STORAGE.THUMBNAILS_DIR,
1504       this.getThumbnailName(),
1505       THUMBNAILS_SIZE
1506     )
1507   }
1508
1509   getTorrentFilePath (videoFile: VideoFileModel) {
1510     return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1511   }
1512
1513   getVideoFilePath (videoFile: VideoFileModel) {
1514     return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1515   }
1516
1517   async createTorrentAndSetInfoHash (videoFile: VideoFileModel) {
1518     const options = {
1519       // Keep the extname, it's used by the client to stream the file inside a web browser
1520       name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`,
1521       createdBy: 'PeerTube',
1522       announceList: [
1523         [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
1524         [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
1525       ],
1526       urlList: [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ]
1527     }
1528
1529     const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
1530
1531     const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1532     logger.info('Creating torrent %s.', filePath)
1533
1534     await writeFile(filePath, torrent)
1535
1536     const parsedTorrent = parseTorrent(torrent)
1537     videoFile.infoHash = parsedTorrent.infoHash
1538   }
1539
1540   getWatchStaticPath () {
1541     return '/videos/watch/' + this.uuid
1542   }
1543
1544   getEmbedStaticPath () {
1545     return '/videos/embed/' + this.uuid
1546   }
1547
1548   getThumbnailStaticPath () {
1549     return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
1550   }
1551
1552   getPreviewStaticPath () {
1553     return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
1554   }
1555
1556   toFormattedJSON (options?: VideoFormattingJSONOptions): Video {
1557     return videoModelToFormattedJSON(this, options)
1558   }
1559
1560   toFormattedDetailsJSON (): VideoDetails {
1561     return videoModelToFormattedDetailsJSON(this)
1562   }
1563
1564   getFormattedVideoFilesJSON (): VideoFile[] {
1565     return videoFilesModelToFormattedJSON(this, this.VideoFiles)
1566   }
1567
1568   toActivityPubObject (): VideoTorrentObject {
1569     return videoModelToActivityPubObject(this)
1570   }
1571
1572   getTruncatedDescription () {
1573     if (!this.description) return null
1574
1575     const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
1576     return peertubeTruncate(this.description, maxLength)
1577   }
1578
1579   getOriginalFileResolution () {
1580     const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1581
1582     return getVideoFileResolution(originalFilePath)
1583   }
1584
1585   getDescriptionAPIPath () {
1586     return `/api/${API_VERSION}/videos/${this.uuid}/description`
1587   }
1588
1589   removeThumbnail () {
1590     const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1591     return remove(thumbnailPath)
1592       .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err }))
1593   }
1594
1595   removePreview () {
1596     const previewPath = join(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1597     return remove(previewPath)
1598       .catch(err => logger.warn('Cannot delete preview %s.', previewPath, { err }))
1599   }
1600
1601   removeFile (videoFile: VideoFileModel, isRedundancy = false) {
1602     const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR
1603
1604     const filePath = join(baseDir, this.getVideoFilename(videoFile))
1605     return remove(filePath)
1606       .catch(err => logger.warn('Cannot delete file %s.', filePath, { err }))
1607   }
1608
1609   removeTorrent (videoFile: VideoFileModel) {
1610     const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1611     return remove(torrentPath)
1612       .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))
1613   }
1614
1615   isOutdated () {
1616     if (this.isOwned()) return false
1617
1618     const now = Date.now()
1619     const createdAtTime = this.createdAt.getTime()
1620     const updatedAtTime = this.updatedAt.getTime()
1621
1622     return (now - createdAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL &&
1623       (now - updatedAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL
1624   }
1625
1626   setAsRefreshed () {
1627     this.changed('updatedAt', true)
1628
1629     return this.save()
1630   }
1631
1632   getBaseUrls () {
1633     let baseUrlHttp
1634     let baseUrlWs
1635
1636     if (this.isOwned()) {
1637       baseUrlHttp = CONFIG.WEBSERVER.URL
1638       baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1639     } else {
1640       baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1641       baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1642     }
1643
1644     return { baseUrlHttp, baseUrlWs }
1645   }
1646
1647   generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1648     const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1649     const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1650     let urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1651
1652     const redundancies = videoFile.RedundancyVideos
1653     if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
1654
1655     const magnetHash = {
1656       xs,
1657       announce,
1658       urlList,
1659       infoHash: videoFile.infoHash,
1660       name: this.name
1661     }
1662
1663     return magnetUtil.encode(magnetHash)
1664   }
1665
1666   getThumbnailUrl (baseUrlHttp: string) {
1667     return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1668   }
1669
1670   getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1671     return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1672   }
1673
1674   getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1675     return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1676   }
1677
1678   getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1679     return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1680   }
1681
1682   getVideoRedundancyUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1683     return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getVideoFilename(videoFile)
1684   }
1685
1686   getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1687     return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile)
1688   }
1689 }