Add publishedAt field for video model.
[oweals/peertube.git] / server / models / video / video.ts
1 import * as Bluebird from 'bluebird'
2 import { map, 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   AfterDestroy,
9   AllowNull,
10   BeforeDestroy,
11   BelongsTo,
12   BelongsToMany,
13   Column,
14   CreatedAt,
15   DataType,
16   Default,
17   ForeignKey,
18   HasMany,
19   IFindOptions,
20   Is,
21   IsInt,
22   IsUUID,
23   Min,
24   Model,
25   Scopes,
26   Table,
27   UpdatedAt
28 } from 'sequelize-typescript'
29 import { VideoPrivacy, VideoResolution } from '../../../shared'
30 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
31 import { Video, VideoDetails } from '../../../shared/models/videos'
32 import { VideoFilter } from '../../../shared/models/videos/video-query.type'
33 import { activityPubCollection } from '../../helpers/activitypub'
34 import {
35   createTorrentPromise, peertubeTruncate, renamePromise, statPromise, unlinkPromise,
36   writeFilePromise
37 } from '../../helpers/core-utils'
38 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
39 import { isBooleanValid } from '../../helpers/custom-validators/misc'
40 import {
41   isVideoCategoryValid,
42   isVideoDescriptionValid,
43   isVideoDurationValid,
44   isVideoLanguageValid,
45   isVideoLicenceValid,
46   isVideoNameValid,
47   isVideoPrivacyValid,
48   isVideoSupportValid
49 } from '../../helpers/custom-validators/videos'
50 import { generateImageFromVideoFile, getVideoFileResolution, transcode } from '../../helpers/ffmpeg-utils'
51 import { logger } from '../../helpers/logger'
52 import { getServerActor } from '../../helpers/utils'
53 import {
54   API_VERSION,
55   CONFIG,
56   CONSTRAINTS_FIELDS,
57   PREVIEWS_SIZE,
58   REMOTE_SCHEME,
59   STATIC_PATHS,
60   THUMBNAILS_SIZE,
61   VIDEO_CATEGORIES,
62   VIDEO_LANGUAGES,
63   VIDEO_LICENCES,
64   VIDEO_PRIVACIES
65 } from '../../initializers'
66 import {
67   getVideoCommentsActivityPubUrl,
68   getVideoDislikesActivityPubUrl,
69   getVideoLikesActivityPubUrl,
70   getVideoSharesActivityPubUrl
71 } from '../../lib/activitypub'
72 import { sendDeleteVideo } from '../../lib/activitypub/send'
73 import { AccountModel } from '../account/account'
74 import { AccountVideoRateModel } from '../account/account-video-rate'
75 import { ActorModel } from '../activitypub/actor'
76 import { AvatarModel } from '../avatar/avatar'
77 import { ServerModel } from '../server/server'
78 import { getSort, throwIfNotValid } from '../utils'
79 import { TagModel } from './tag'
80 import { VideoAbuseModel } from './video-abuse'
81 import { VideoChannelModel } from './video-channel'
82 import { VideoCommentModel } from './video-comment'
83 import { VideoFileModel } from './video-file'
84 import { VideoShareModel } from './video-share'
85 import { VideoTagModel } from './video-tag'
86
87 enum ScopeNames {
88   AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
89   WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS',
90   WITH_TAGS = 'WITH_TAGS',
91   WITH_FILES = 'WITH_FILES',
92   WITH_SHARES = 'WITH_SHARES',
93   WITH_RATES = 'WITH_RATES',
94   WITH_COMMENTS = 'WITH_COMMENTS'
95 }
96
97 @Scopes({
98   [ScopeNames.AVAILABLE_FOR_LIST]: (actorId: number, filter?: VideoFilter) => ({
99     where: {
100       id: {
101         [Sequelize.Op.notIn]: Sequelize.literal(
102           '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
103         ),
104         [ Sequelize.Op.in ]: Sequelize.literal(
105           '(' +
106             'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
107             'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
108             'WHERE "actorFollow"."actorId" = ' + parseInt(actorId.toString(), 10) +
109             ' UNION ' +
110             'SELECT "video"."id" AS "id" FROM "video" ' +
111             'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
112             'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
113             'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
114             'LEFT JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
115             'WHERE "actor"."serverId" IS NULL OR "actorFollow"."actorId" = ' + parseInt(actorId.toString(), 10) +
116           ')'
117         )
118       },
119       privacy: VideoPrivacy.PUBLIC
120     },
121     include: [
122       {
123         attributes: [ 'name', 'description' ],
124         model: VideoChannelModel.unscoped(),
125         required: true,
126         include: [
127           {
128             attributes: [ 'name' ],
129             model: AccountModel.unscoped(),
130             required: true,
131             include: [
132               {
133                 attributes: [ 'preferredUsername', 'url', 'serverId' ],
134                 model: ActorModel.unscoped(),
135                 required: true,
136                 where: VideoModel.buildActorWhereWithFilter(filter),
137                 include: [
138                   {
139                     attributes: [ 'host' ],
140                     model: ServerModel.unscoped(),
141                     required: false
142                   },
143                   {
144                     model: AvatarModel.unscoped(),
145                     required: false
146                   }
147                 ]
148               }
149             ]
150           }
151         ]
152       }
153     ]
154   }),
155   [ScopeNames.WITH_ACCOUNT_DETAILS]: {
156     include: [
157       {
158         model: () => VideoChannelModel.unscoped(),
159         required: true,
160         include: [
161           {
162             attributes: {
163               exclude: [ 'privateKey', 'publicKey' ]
164             },
165             model: () => ActorModel.unscoped(),
166             required: true,
167             include: [
168               {
169                 attributes: [ 'host' ],
170                 model: () => ServerModel.unscoped(),
171                 required: false
172               }
173             ]
174           },
175           {
176             model: () => AccountModel.unscoped(),
177             required: true,
178             include: [
179               {
180                 model: () => ActorModel.unscoped(),
181                 attributes: {
182                   exclude: [ 'privateKey', 'publicKey' ]
183                 },
184                 required: true,
185                 include: [
186                   {
187                     attributes: [ 'host' ],
188                     model: () => ServerModel.unscoped(),
189                     required: false
190                   },
191                   {
192                     model: () => AvatarModel.unscoped(),
193                     required: false
194                   }
195                 ]
196               }
197             ]
198           }
199         ]
200       }
201     ]
202   },
203   [ScopeNames.WITH_TAGS]: {
204     include: [ () => TagModel ]
205   },
206   [ScopeNames.WITH_FILES]: {
207     include: [
208       {
209         model: () => VideoFileModel.unscoped(),
210         required: true
211       }
212     ]
213   },
214   [ScopeNames.WITH_SHARES]: {
215     include: [
216       {
217         model: () => VideoShareModel.unscoped()
218       }
219     ]
220   },
221   [ScopeNames.WITH_RATES]: {
222     include: [
223       {
224         model: () => AccountVideoRateModel,
225         include: [
226           {
227             model: () => AccountModel.unscoped(),
228             required: true,
229             include: [
230               {
231                 attributes: [ 'url' ],
232                 model: () => ActorModel.unscoped()
233               }
234             ]
235           }
236         ]
237       }
238     ]
239   },
240   [ScopeNames.WITH_COMMENTS]: {
241     include: [
242       {
243         model: () => VideoCommentModel.unscoped()
244       }
245     ]
246   }
247 })
248 @Table({
249   tableName: 'video',
250   indexes: [
251     {
252       fields: [ 'name' ]
253     },
254     {
255       fields: [ 'createdAt' ]
256     },
257     {
258       fields: [ 'duration' ]
259     },
260     {
261       fields: [ 'views' ]
262     },
263     {
264       fields: [ 'likes' ]
265     },
266     {
267       fields: [ 'uuid' ]
268     },
269     {
270       fields: [ 'channelId' ]
271     },
272     {
273       fields: [ 'id', 'privacy' ]
274     },
275     {
276       fields: [ 'url'],
277       unique: true
278     }
279   ]
280 })
281 export class VideoModel extends Model<VideoModel> {
282
283   @AllowNull(false)
284   @Default(DataType.UUIDV4)
285   @IsUUID(4)
286   @Column(DataType.UUID)
287   uuid: string
288
289   @AllowNull(false)
290   @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
291   @Column
292   name: string
293
294   @AllowNull(true)
295   @Default(null)
296   @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
297   @Column
298   category: number
299
300   @AllowNull(true)
301   @Default(null)
302   @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
303   @Column
304   licence: number
305
306   @AllowNull(true)
307   @Default(null)
308   @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
309   @Column
310   language: number
311
312   @AllowNull(false)
313   @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
314   @Column
315   privacy: number
316
317   @AllowNull(false)
318   @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean'))
319   @Column
320   nsfw: boolean
321
322   @AllowNull(true)
323   @Default(null)
324   @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
325   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
326   description: string
327
328   @AllowNull(true)
329   @Default(null)
330   @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support'))
331   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max))
332   support: string
333
334   @AllowNull(false)
335   @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
336   @Column
337   duration: number
338
339   @AllowNull(false)
340   @Default(0)
341   @IsInt
342   @Min(0)
343   @Column
344   views: number
345
346   @AllowNull(false)
347   @Default(0)
348   @IsInt
349   @Min(0)
350   @Column
351   likes: number
352
353   @AllowNull(false)
354   @Default(0)
355   @IsInt
356   @Min(0)
357   @Column
358   dislikes: number
359
360   @AllowNull(false)
361   @Column
362   remote: boolean
363
364   @AllowNull(false)
365   @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
366   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
367   url: string
368
369   @AllowNull(false)
370   @Column
371   commentsEnabled: boolean
372
373   @CreatedAt
374   createdAt: Date
375
376   @UpdatedAt
377   updatedAt: Date
378
379   @AllowNull(false)
380   @Default(Sequelize.NOW)
381   @Column
382   publishedAt: Date
383
384   @ForeignKey(() => VideoChannelModel)
385   @Column
386   channelId: number
387
388   @BelongsTo(() => VideoChannelModel, {
389     foreignKey: {
390       allowNull: true
391     },
392     onDelete: 'cascade'
393   })
394   VideoChannel: VideoChannelModel
395
396   @BelongsToMany(() => TagModel, {
397     foreignKey: 'videoId',
398     through: () => VideoTagModel,
399     onDelete: 'CASCADE'
400   })
401   Tags: TagModel[]
402
403   @HasMany(() => VideoAbuseModel, {
404     foreignKey: {
405       name: 'videoId',
406       allowNull: false
407     },
408     onDelete: 'cascade'
409   })
410   VideoAbuses: VideoAbuseModel[]
411
412   @HasMany(() => VideoFileModel, {
413     foreignKey: {
414       name: 'videoId',
415       allowNull: false
416     },
417     onDelete: 'cascade'
418   })
419   VideoFiles: VideoFileModel[]
420
421   @HasMany(() => VideoShareModel, {
422     foreignKey: {
423       name: 'videoId',
424       allowNull: false
425     },
426     onDelete: 'cascade'
427   })
428   VideoShares: VideoShareModel[]
429
430   @HasMany(() => AccountVideoRateModel, {
431     foreignKey: {
432       name: 'videoId',
433       allowNull: false
434     },
435     onDelete: 'cascade'
436   })
437   AccountVideoRates: AccountVideoRateModel[]
438
439   @HasMany(() => VideoCommentModel, {
440     foreignKey: {
441       name: 'videoId',
442       allowNull: false
443     },
444     onDelete: 'cascade',
445     hooks: true
446   })
447   VideoComments: VideoCommentModel[]
448
449   @BeforeDestroy
450   static async sendDelete (instance: VideoModel, options) {
451     if (instance.isOwned()) {
452       if (!instance.VideoChannel) {
453         instance.VideoChannel = await instance.$get('VideoChannel', {
454           include: [
455             {
456               model: AccountModel,
457               include: [ ActorModel ]
458             }
459           ],
460           transaction: options.transaction
461         }) as VideoChannelModel
462       }
463
464       logger.debug('Sending delete of video %s.', instance.url)
465
466       return sendDeleteVideo(instance, options.transaction)
467     }
468
469     return undefined
470   }
471
472   @AfterDestroy
473   static async removeFilesAndSendDelete (instance: VideoModel) {
474     const tasks: Promise<any>[] = []
475
476     tasks.push(instance.removeThumbnail())
477
478     if (instance.isOwned()) {
479       if (!Array.isArray(instance.VideoFiles)) {
480         instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[]
481       }
482
483       tasks.push(instance.removePreview())
484
485       // Remove physical files and torrents
486       instance.VideoFiles.forEach(file => {
487         tasks.push(instance.removeFile(file))
488         tasks.push(instance.removeTorrent(file))
489       })
490     }
491
492     return Promise.all(tasks)
493       .catch(err => {
494         logger.error('Some errors when removing files of video %s in after destroy hook.', instance.uuid, { err })
495       })
496   }
497
498   static list () {
499     return VideoModel.scope(ScopeNames.WITH_FILES).findAll()
500   }
501
502   static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
503     function getRawQuery (select: string) {
504       const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
505         'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
506         'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' +
507         'WHERE "Account"."actorId" = ' + actorId
508       const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
509         'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
510         'WHERE "VideoShare"."actorId" = ' + actorId
511
512       return `(${queryVideo}) UNION (${queryVideoShare})`
513     }
514
515     const rawQuery = getRawQuery('"Video"."id"')
516     const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
517
518     const query = {
519       distinct: true,
520       offset: start,
521       limit: count,
522       order: getSort('createdAt', [ 'Tags', 'name', 'ASC' ]),
523       where: {
524         id: {
525           [Sequelize.Op.in]: Sequelize.literal('(' + rawQuery + ')')
526         },
527         [Sequelize.Op.or]: [
528           { privacy: VideoPrivacy.PUBLIC },
529           { privacy: VideoPrivacy.UNLISTED }
530         ]
531       },
532       include: [
533         {
534           attributes: [ 'id', 'url' ],
535           model: VideoShareModel.unscoped(),
536           required: false,
537           where: {
538             [Sequelize.Op.and]: [
539               {
540                 id: {
541                   [Sequelize.Op.not]: null
542                 }
543               },
544               {
545                 actorId
546               }
547             ]
548           },
549           include: [
550             {
551               attributes: [ 'id', 'url' ],
552               model: ActorModel.unscoped()
553             }
554           ]
555         },
556         {
557           model: VideoChannelModel.unscoped(),
558           required: true,
559           include: [
560             {
561               attributes: [ 'name' ],
562               model: AccountModel.unscoped(),
563               required: true,
564               include: [
565                 {
566                   attributes: [ 'id', 'url' ],
567                   model: ActorModel.unscoped(),
568                   required: true
569                 }
570               ]
571             },
572             {
573               attributes: [ 'id', 'url' ],
574               model: ActorModel.unscoped(),
575               required: true
576             }
577           ]
578         },
579         {
580           attributes: [ 'type' ],
581           model: AccountVideoRateModel,
582           required: false,
583           include: [
584             {
585               attributes: [ 'id' ],
586               model: AccountModel.unscoped(),
587               include: [
588                 {
589                   attributes: [ 'url' ],
590                   model: ActorModel.unscoped(),
591                   include: [
592                     {
593                       attributes: [ 'host' ],
594                       model: ServerModel,
595                       required: false
596                     }
597                   ]
598                 }
599               ]
600             }
601           ]
602         },
603         {
604           attributes: [ 'url' ],
605           model: VideoCommentModel,
606           required: false
607         },
608         VideoFileModel,
609         TagModel
610       ]
611     }
612
613     return Bluebird.all([
614       // FIXME: typing issue
615       VideoModel.findAll(query as any),
616       VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
617     ]).then(([ rows, totals ]) => {
618       // totals: totalVideos + totalVideoShares
619       let totalVideos = 0
620       let totalVideoShares = 0
621       if (totals[0]) totalVideos = parseInt(totals[0].total, 10)
622       if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10)
623
624       const total = totalVideos + totalVideoShares
625       return {
626         data: rows,
627         total: total
628       }
629     })
630   }
631
632   static listUserVideosForApi (userId: number, start: number, count: number, sort: string) {
633     const query = {
634       offset: start,
635       limit: count,
636       order: getSort(sort),
637       include: [
638         {
639           model: VideoChannelModel,
640           required: true,
641           include: [
642             {
643               model: AccountModel,
644               where: {
645                 userId
646               },
647               required: true
648             }
649           ]
650         }
651       ]
652     }
653
654     return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
655       return {
656         data: rows,
657         total: count
658       }
659     })
660   }
661
662   static async listForApi (start: number, count: number, sort: string, filter?: VideoFilter) {
663     const query = {
664       offset: start,
665       limit: count,
666       order: getSort(sort)
667     }
668
669     const serverActor = await getServerActor()
670
671     return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, filter ] })
672       .findAndCountAll(query)
673       .then(({ rows, count }) => {
674         return {
675           data: rows,
676           total: count
677         }
678       })
679   }
680
681   static async searchAndPopulateAccountAndServerAndTags (value: string, start: number, count: number, sort: string) {
682     const query: IFindOptions<VideoModel> = {
683       offset: start,
684       limit: count,
685       order: getSort(sort),
686       where: {
687         [Sequelize.Op.or]: [
688           {
689             name: {
690               [ Sequelize.Op.iLike ]: '%' + value + '%'
691             }
692           },
693           {
694             preferredUsername: Sequelize.where(Sequelize.col('preferredUsername'), {
695               [ Sequelize.Op.iLike ]: '%' + value + '%'
696             })
697           },
698           {
699             host: Sequelize.where(Sequelize.col('host'), {
700               [ Sequelize.Op.iLike ]: '%' + value + '%'
701             })
702           }
703         ]
704       }
705     }
706
707     const serverActor = await getServerActor()
708
709     return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id ] })
710       .findAndCountAll(query).then(({ rows, count }) => {
711         return {
712           data: rows,
713           total: count
714         }
715       })
716   }
717
718   static load (id: number) {
719     return VideoModel.findById(id)
720   }
721
722   static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
723     const query: IFindOptions<VideoModel> = {
724       where: {
725         url
726       }
727     }
728
729     if (t !== undefined) query.transaction = t
730
731     return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
732   }
733
734   static loadByUUIDOrURLAndPopulateAccount (uuid: string, url: string, t?: Sequelize.Transaction) {
735     const query: IFindOptions<VideoModel> = {
736       where: {
737         [Sequelize.Op.or]: [
738           { uuid },
739           { url }
740         ]
741       }
742     }
743
744     if (t !== undefined) query.transaction = t
745
746     return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
747   }
748
749   static loadAndPopulateAccountAndServerAndTags (id: number) {
750     const options = {
751       order: [ [ 'Tags', 'name', 'ASC' ] ]
752     }
753
754     return VideoModel
755       .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ])
756       .findById(id, options)
757   }
758
759   static loadByUUID (uuid: string) {
760     const options = {
761       where: {
762         uuid
763       }
764     }
765
766     return VideoModel
767       .scope([ ScopeNames.WITH_FILES ])
768       .findOne(options)
769   }
770
771   static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string) {
772     const options = {
773       order: [ [ 'Tags', 'name', 'ASC' ] ],
774       where: {
775         uuid
776       }
777     }
778
779     return VideoModel
780       .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ])
781       .findOne(options)
782   }
783
784   static loadAndPopulateAll (id: number) {
785     const options = {
786       order: [ [ 'Tags', 'name', 'ASC' ] ],
787       where: {
788         id
789       }
790     }
791
792     return VideoModel
793       .scope([
794         ScopeNames.WITH_RATES,
795         ScopeNames.WITH_SHARES,
796         ScopeNames.WITH_TAGS,
797         ScopeNames.WITH_FILES,
798         ScopeNames.WITH_ACCOUNT_DETAILS,
799         ScopeNames.WITH_COMMENTS
800       ])
801       .findOne(options)
802   }
803
804   static async getStats () {
805     const totalLocalVideos = await VideoModel.count({
806       where: {
807         remote: false
808       }
809     })
810     const totalVideos = await VideoModel.count()
811
812     let totalLocalVideoViews = await VideoModel.sum('views', {
813       where: {
814         remote: false
815       }
816     })
817     // Sequelize could return null...
818     if (!totalLocalVideoViews) totalLocalVideoViews = 0
819
820     return {
821       totalLocalVideos,
822       totalLocalVideoViews,
823       totalVideos
824     }
825   }
826
827   private static buildActorWhereWithFilter (filter?: VideoFilter) {
828     if (filter && filter === 'local') {
829       return {
830         serverId: null
831       }
832     }
833
834     return {}
835   }
836
837   private static getCategoryLabel (id: number) {
838     let categoryLabel = VIDEO_CATEGORIES[id]
839     if (!categoryLabel) categoryLabel = 'Misc'
840
841     return categoryLabel
842   }
843
844   private static getLicenceLabel (id: number) {
845     let licenceLabel = VIDEO_LICENCES[id]
846     if (!licenceLabel) licenceLabel = 'Unknown'
847
848     return licenceLabel
849   }
850
851   private static getLanguageLabel (id: number) {
852     let languageLabel = VIDEO_LANGUAGES[id]
853     if (!languageLabel) languageLabel = 'Unknown'
854
855     return languageLabel
856   }
857
858   getOriginalFile () {
859     if (Array.isArray(this.VideoFiles) === false) return undefined
860
861     // The original file is the file that have the higher resolution
862     return maxBy(this.VideoFiles, file => file.resolution)
863   }
864
865   getVideoFilename (videoFile: VideoFileModel) {
866     return this.uuid + '-' + videoFile.resolution + videoFile.extname
867   }
868
869   getThumbnailName () {
870     // We always have a copy of the thumbnail
871     const extension = '.jpg'
872     return this.uuid + extension
873   }
874
875   getPreviewName () {
876     const extension = '.jpg'
877     return this.uuid + extension
878   }
879
880   getTorrentFileName (videoFile: VideoFileModel) {
881     const extension = '.torrent'
882     return this.uuid + '-' + videoFile.resolution + extension
883   }
884
885   isOwned () {
886     return this.remote === false
887   }
888
889   createPreview (videoFile: VideoFileModel) {
890     return generateImageFromVideoFile(
891       this.getVideoFilePath(videoFile),
892       CONFIG.STORAGE.PREVIEWS_DIR,
893       this.getPreviewName(),
894       PREVIEWS_SIZE
895     )
896   }
897
898   createThumbnail (videoFile: VideoFileModel) {
899     return generateImageFromVideoFile(
900       this.getVideoFilePath(videoFile),
901       CONFIG.STORAGE.THUMBNAILS_DIR,
902       this.getThumbnailName(),
903       THUMBNAILS_SIZE
904     )
905   }
906
907   getVideoFilePath (videoFile: VideoFileModel) {
908     return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
909   }
910
911   createTorrentAndSetInfoHash = async function (videoFile: VideoFileModel) {
912     const options = {
913       announceList: [
914         [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
915         [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
916       ],
917       urlList: [
918         CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
919       ]
920     }
921
922     const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
923
924     const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
925     logger.info('Creating torrent %s.', filePath)
926
927     await writeFilePromise(filePath, torrent)
928
929     const parsedTorrent = parseTorrent(torrent)
930     videoFile.infoHash = parsedTorrent.infoHash
931   }
932
933   getEmbedPath () {
934     return '/videos/embed/' + this.uuid
935   }
936
937   getThumbnailPath () {
938     return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
939   }
940
941   getPreviewPath () {
942     return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
943   }
944
945   toFormattedJSON (): Video {
946     const formattedAccount = this.VideoChannel.Account.toFormattedJSON()
947
948     return {
949       id: this.id,
950       uuid: this.uuid,
951       name: this.name,
952       category: {
953         id: this.category,
954         label: VideoModel.getCategoryLabel(this.category)
955       },
956       licence: {
957         id: this.licence,
958         label: VideoModel.getLicenceLabel(this.licence)
959       },
960       language: {
961         id: this.language,
962         label: VideoModel.getLanguageLabel(this.language)
963       },
964       nsfw: this.nsfw,
965       description: this.getTruncatedDescription(),
966       isLocal: this.isOwned(),
967       duration: this.duration,
968       views: this.views,
969       likes: this.likes,
970       dislikes: this.dislikes,
971       thumbnailPath: this.getThumbnailPath(),
972       previewPath: this.getPreviewPath(),
973       embedPath: this.getEmbedPath(),
974       createdAt: this.createdAt,
975       updatedAt: this.updatedAt,
976       publishedAt: this.publishedAt,
977       account: {
978         name: formattedAccount.name,
979         displayName: formattedAccount.displayName,
980         url: formattedAccount.url,
981         host: formattedAccount.host,
982         avatar: formattedAccount.avatar
983       }
984     }
985   }
986
987   toFormattedDetailsJSON (): VideoDetails {
988     const formattedJson = this.toFormattedJSON()
989
990     // Maybe our server is not up to date and there are new privacy settings since our version
991     let privacyLabel = VIDEO_PRIVACIES[this.privacy]
992     if (!privacyLabel) privacyLabel = 'Unknown'
993
994     const detailsJson = {
995       privacy: {
996         id: this.privacy,
997         label: privacyLabel
998       },
999       support: this.support,
1000       descriptionPath: this.getDescriptionPath(),
1001       channel: this.VideoChannel.toFormattedJSON(),
1002       account: this.VideoChannel.Account.toFormattedJSON(),
1003       tags: map<TagModel, string>(this.Tags, 'name'),
1004       commentsEnabled: this.commentsEnabled,
1005       files: []
1006     }
1007
1008     // Format and sort video files
1009     const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1010     detailsJson.files = this.VideoFiles
1011       .map(videoFile => {
1012         let resolutionLabel = videoFile.resolution + 'p'
1013
1014         return {
1015           resolution: {
1016             id: videoFile.resolution,
1017             label: resolutionLabel
1018           },
1019           magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
1020           size: videoFile.size,
1021           torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
1022           fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp)
1023         }
1024       })
1025       .sort((a, b) => {
1026         if (a.resolution.id < b.resolution.id) return 1
1027         if (a.resolution.id === b.resolution.id) return 0
1028         return -1
1029       })
1030
1031     return Object.assign(formattedJson, detailsJson)
1032   }
1033
1034   toActivityPubObject (): VideoTorrentObject {
1035     const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1036     if (!this.Tags) this.Tags = []
1037
1038     const tag = this.Tags.map(t => ({
1039       type: 'Hashtag' as 'Hashtag',
1040       name: t.name
1041     }))
1042
1043     let language
1044     if (this.language) {
1045       language = {
1046         identifier: this.language + '',
1047         name: VideoModel.getLanguageLabel(this.language)
1048       }
1049     }
1050
1051     let category
1052     if (this.category) {
1053       category = {
1054         identifier: this.category + '',
1055         name: VideoModel.getCategoryLabel(this.category)
1056       }
1057     }
1058
1059     let licence
1060     if (this.licence) {
1061       licence = {
1062         identifier: this.licence + '',
1063         name: VideoModel.getLicenceLabel(this.licence)
1064       }
1065     }
1066
1067     let likesObject
1068     let dislikesObject
1069
1070     if (Array.isArray(this.AccountVideoRates)) {
1071       const res = this.toRatesActivityPubObjects()
1072       likesObject = res.likesObject
1073       dislikesObject = res.dislikesObject
1074     }
1075
1076     let sharesObject
1077     if (Array.isArray(this.VideoShares)) {
1078       sharesObject = this.toAnnouncesActivityPubObject()
1079     }
1080
1081     let commentsObject
1082     if (Array.isArray(this.VideoComments)) {
1083       commentsObject = this.toCommentsActivityPubObject()
1084     }
1085
1086     const url = []
1087     for (const file of this.VideoFiles) {
1088       url.push({
1089         type: 'Link',
1090         mimeType: 'video/' + file.extname.replace('.', ''),
1091         href: this.getVideoFileUrl(file, baseUrlHttp),
1092         width: file.resolution,
1093         size: file.size
1094       })
1095
1096       url.push({
1097         type: 'Link',
1098         mimeType: 'application/x-bittorrent',
1099         href: this.getTorrentUrl(file, baseUrlHttp),
1100         width: file.resolution
1101       })
1102
1103       url.push({
1104         type: 'Link',
1105         mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
1106         href: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
1107         width: file.resolution
1108       })
1109     }
1110
1111     // Add video url too
1112     url.push({
1113       type: 'Link',
1114       mimeType: 'text/html',
1115       href: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
1116     })
1117
1118     return {
1119       type: 'Video' as 'Video',
1120       id: this.url,
1121       name: this.name,
1122       duration: this.getActivityStreamDuration(),
1123       uuid: this.uuid,
1124       tag,
1125       category,
1126       licence,
1127       language,
1128       views: this.views,
1129       sensitive: this.nsfw,
1130       commentsEnabled: this.commentsEnabled,
1131       published: this.publishedAt.toISOString(),
1132       updated: this.updatedAt.toISOString(),
1133       mediaType: 'text/markdown',
1134       content: this.getTruncatedDescription(),
1135       support: this.support,
1136       icon: {
1137         type: 'Image',
1138         url: this.getThumbnailUrl(baseUrlHttp),
1139         mediaType: 'image/jpeg',
1140         width: THUMBNAILS_SIZE.width,
1141         height: THUMBNAILS_SIZE.height
1142       },
1143       url,
1144       likes: likesObject,
1145       dislikes: dislikesObject,
1146       shares: sharesObject,
1147       comments: commentsObject,
1148       attributedTo: [
1149         {
1150           type: 'Person',
1151           id: this.VideoChannel.Account.Actor.url
1152         },
1153         {
1154           type: 'Group',
1155           id: this.VideoChannel.Actor.url
1156         }
1157       ]
1158     }
1159   }
1160
1161   toAnnouncesActivityPubObject () {
1162     const shares: string[] = []
1163
1164     for (const videoShare of this.VideoShares) {
1165       shares.push(videoShare.url)
1166     }
1167
1168     return activityPubCollection(getVideoSharesActivityPubUrl(this), shares)
1169   }
1170
1171   toCommentsActivityPubObject () {
1172     const comments: string[] = []
1173
1174     for (const videoComment of this.VideoComments) {
1175       comments.push(videoComment.url)
1176     }
1177
1178     return activityPubCollection(getVideoCommentsActivityPubUrl(this), comments)
1179   }
1180
1181   toRatesActivityPubObjects () {
1182     const likes: string[] = []
1183     const dislikes: string[] = []
1184
1185     for (const rate of this.AccountVideoRates) {
1186       if (rate.type === 'like') {
1187         likes.push(rate.Account.Actor.url)
1188       } else if (rate.type === 'dislike') {
1189         dislikes.push(rate.Account.Actor.url)
1190       }
1191     }
1192
1193     const likesObject = activityPubCollection(getVideoLikesActivityPubUrl(this), likes)
1194     const dislikesObject = activityPubCollection(getVideoDislikesActivityPubUrl(this), dislikes)
1195
1196     return { likesObject, dislikesObject }
1197   }
1198
1199   getTruncatedDescription () {
1200     if (!this.description) return null
1201
1202     const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
1203     return peertubeTruncate(this.description, maxLength)
1204   }
1205
1206   optimizeOriginalVideofile = async function () {
1207     const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1208     const newExtname = '.mp4'
1209     const inputVideoFile = this.getOriginalFile()
1210     const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
1211     const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
1212
1213     const transcodeOptions = {
1214       inputPath: videoInputPath,
1215       outputPath: videoOutputPath
1216     }
1217
1218     // Could be very long!
1219     await transcode(transcodeOptions)
1220
1221     try {
1222       await unlinkPromise(videoInputPath)
1223
1224       // Important to do this before getVideoFilename() to take in account the new file extension
1225       inputVideoFile.set('extname', newExtname)
1226
1227       await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
1228       const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
1229
1230       inputVideoFile.set('size', stats.size)
1231
1232       await this.createTorrentAndSetInfoHash(inputVideoFile)
1233       await inputVideoFile.save()
1234
1235     } catch (err) {
1236       // Auto destruction...
1237       this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
1238
1239       throw err
1240     }
1241   }
1242
1243   transcodeOriginalVideofile = async function (resolution: VideoResolution, isPortraitMode: boolean) {
1244     const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1245     const extname = '.mp4'
1246
1247     // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
1248     const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
1249
1250     const newVideoFile = new VideoFileModel({
1251       resolution,
1252       extname,
1253       size: 0,
1254       videoId: this.id
1255     })
1256     const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
1257
1258     const transcodeOptions = {
1259       inputPath: videoInputPath,
1260       outputPath: videoOutputPath,
1261       resolution,
1262       isPortraitMode
1263     }
1264
1265     await transcode(transcodeOptions)
1266
1267     const stats = await statPromise(videoOutputPath)
1268
1269     newVideoFile.set('size', stats.size)
1270
1271     await this.createTorrentAndSetInfoHash(newVideoFile)
1272
1273     await newVideoFile.save()
1274
1275     this.VideoFiles.push(newVideoFile)
1276   }
1277
1278   getOriginalFileResolution () {
1279     const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1280
1281     return getVideoFileResolution(originalFilePath)
1282   }
1283
1284   getDescriptionPath () {
1285     return `/api/${API_VERSION}/videos/${this.uuid}/description`
1286   }
1287
1288   removeThumbnail () {
1289     const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1290     return unlinkPromise(thumbnailPath)
1291   }
1292
1293   removePreview () {
1294     // Same name than video thumbnail
1295     return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1296   }
1297
1298   removeFile (videoFile: VideoFileModel) {
1299     const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1300     return unlinkPromise(filePath)
1301   }
1302
1303   removeTorrent (videoFile: VideoFileModel) {
1304     const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1305     return unlinkPromise(torrentPath)
1306   }
1307
1308   getActivityStreamDuration () {
1309     // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
1310     return 'PT' + this.duration + 'S'
1311   }
1312
1313   private getBaseUrls () {
1314     let baseUrlHttp
1315     let baseUrlWs
1316
1317     if (this.isOwned()) {
1318       baseUrlHttp = CONFIG.WEBSERVER.URL
1319       baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1320     } else {
1321       baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1322       baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1323     }
1324
1325     return { baseUrlHttp, baseUrlWs }
1326   }
1327
1328   private getThumbnailUrl (baseUrlHttp: string) {
1329     return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1330   }
1331
1332   private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1333     return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1334   }
1335
1336   private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1337     return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1338   }
1339
1340   private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1341     const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1342     const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1343     const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1344
1345     const magnetHash = {
1346       xs,
1347       announce,
1348       urlList,
1349       infoHash: videoFile.infoHash,
1350       name: this.name
1351     }
1352
1353     return magnetUtil.encode(magnetHash)
1354   }
1355 }