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