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