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