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