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