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