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