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