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