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