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