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