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