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