]>
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 => { | |
765 | tasks.push(instance.removeFile(file)) | |
90a8bd30 | 766 | tasks.push(file.removeTorrent()) |
3fd3ab2d | 767 | }) |
09209296 C |
768 | |
769 | // Remove playlists file | |
ffc65cbd | 770 | if (!Array.isArray(instance.VideoStreamingPlaylists)) { |
9f7657b6 | 771 | instance.VideoStreamingPlaylists = await instance.$get('VideoStreamingPlaylists', { transaction: options.transaction }) |
ffc65cbd C |
772 | } |
773 | ||
774 | for (const p of instance.VideoStreamingPlaylists) { | |
775 | tasks.push(instance.removeStreamingPlaylistFiles(p)) | |
776 | } | |
3fd3ab2d | 777 | } |
40298b02 | 778 | |
6b738c7a C |
779 | // Do not wait video deletion because we could be in a transaction |
780 | Promise.all(tasks) | |
8ea6f49a C |
781 | .catch(err => { |
782 | logger.error('Some errors when removing files of video %s in before destroy hook.', instance.uuid, { err }) | |
783 | }) | |
6b738c7a C |
784 | |
785 | return undefined | |
3fd3ab2d | 786 | } |
f285faa0 | 787 | |
a5cf76af C |
788 | @BeforeDestroy |
789 | static stopLiveIfNeeded (instance: VideoModel) { | |
790 | if (!instance.isLive) return | |
791 | ||
68e70a74 C |
792 | logger.info('Stopping live of video %s after video deletion.', instance.uuid) |
793 | ||
9f7657b6 | 794 | LiveManager.Instance.stopSessionOf(instance.id) |
a5cf76af C |
795 | } |
796 | ||
7eba5e1f C |
797 | @BeforeDestroy |
798 | static invalidateCache (instance: VideoModel) { | |
799 | ModelCache.Instance.invalidateCache('video', instance.id) | |
800 | } | |
801 | ||
68d19a0a RK |
802 | @BeforeDestroy |
803 | static async saveEssentialDataToAbuses (instance: VideoModel, options) { | |
804 | const tasks: Promise<any>[] = [] | |
805 | ||
68d19a0a | 806 | if (!Array.isArray(instance.VideoAbuses)) { |
9f7657b6 | 807 | instance.VideoAbuses = await instance.$get('VideoAbuses', { transaction: options.transaction }) |
68d19a0a RK |
808 | |
809 | if (instance.VideoAbuses.length === 0) return undefined | |
810 | } | |
811 | ||
57f6896f C |
812 | logger.info('Saving video abuses details of video %s.', instance.url) |
813 | ||
42ec411b | 814 | if (!instance.Trackers) instance.Trackers = await instance.$get('Trackers', { transaction: options.transaction }) |
86521a67 | 815 | const details = instance.toFormattedDetailsJSON() |
68d19a0a RK |
816 | |
817 | for (const abuse of instance.VideoAbuses) { | |
0251197e RK |
818 | abuse.deletedVideo = details |
819 | tasks.push(abuse.save({ transaction: options.transaction })) | |
68d19a0a RK |
820 | } |
821 | ||
9f7657b6 | 822 | await Promise.all(tasks) |
68d19a0a RK |
823 | } |
824 | ||
90a8bd30 | 825 | static listLocal (): Promise<MVideo[]> { |
9f1ddd24 C |
826 | const query = { |
827 | where: { | |
828 | remote: false | |
829 | } | |
830 | } | |
831 | ||
90a8bd30 | 832 | return VideoModel.findAll(query) |
9f1ddd24 C |
833 | } |
834 | ||
50d6de9c | 835 | static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) { |
3fd3ab2d C |
836 | function getRawQuery (select: string) { |
837 | const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' + | |
838 | 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + | |
50d6de9c C |
839 | 'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' + |
840 | 'WHERE "Account"."actorId" = ' + actorId | |
3fd3ab2d C |
841 | const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' + |
842 | 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' + | |
50d6de9c | 843 | 'WHERE "VideoShare"."actorId" = ' + actorId |
558d7c23 | 844 | |
3fd3ab2d C |
845 | return `(${queryVideo}) UNION (${queryVideoShare})` |
846 | } | |
aaf61f38 | 847 | |
3fd3ab2d C |
848 | const rawQuery = getRawQuery('"Video"."id"') |
849 | const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"') | |
850 | ||
851 | const query = { | |
852 | distinct: true, | |
853 | offset: start, | |
854 | limit: count, | |
71398458 | 855 | order: getVideoSort('-createdAt', [ 'Tags', 'name', 'ASC' ] as any), // FIXME: sequelize typings |
3fd3ab2d C |
856 | where: { |
857 | id: { | |
a1587156 | 858 | [Op.in]: Sequelize.literal('(' + rawQuery + ')') |
3c75ce12 | 859 | }, |
3092e9bb | 860 | [Op.or]: getPrivaciesForFederation() |
3fd3ab2d C |
861 | }, |
862 | include: [ | |
40e87e9e | 863 | { |
1afb3c47 | 864 | attributes: [ 'filename', 'language', 'fileUrl' ], |
40e87e9e C |
865 | model: VideoCaptionModel.unscoped(), |
866 | required: false | |
867 | }, | |
3fd3ab2d | 868 | { |
1d230c44 | 869 | attributes: [ 'id', 'url' ], |
2c897999 | 870 | model: VideoShareModel.unscoped(), |
3fd3ab2d | 871 | required: false, |
e3d5ea4f C |
872 | // We only want videos shared by this actor |
873 | where: { | |
a1587156 | 874 | [Op.and]: [ |
e3d5ea4f C |
875 | { |
876 | id: { | |
a1587156 | 877 | [Op.not]: null |
e3d5ea4f C |
878 | } |
879 | }, | |
880 | { | |
881 | actorId | |
882 | } | |
883 | ] | |
884 | }, | |
50d6de9c C |
885 | include: [ |
886 | { | |
2c897999 C |
887 | attributes: [ 'id', 'url' ], |
888 | model: ActorModel.unscoped() | |
50d6de9c C |
889 | } |
890 | ] | |
3fd3ab2d C |
891 | }, |
892 | { | |
2c897999 | 893 | model: VideoChannelModel.unscoped(), |
3fd3ab2d C |
894 | required: true, |
895 | include: [ | |
896 | { | |
2c897999 C |
897 | attributes: [ 'name' ], |
898 | model: AccountModel.unscoped(), | |
899 | required: true, | |
900 | include: [ | |
901 | { | |
e3d5ea4f | 902 | attributes: [ 'id', 'url', 'followersUrl' ], |
2c897999 C |
903 | model: ActorModel.unscoped(), |
904 | required: true | |
905 | } | |
906 | ] | |
907 | }, | |
908 | { | |
e3d5ea4f | 909 | attributes: [ 'id', 'url', 'followersUrl' ], |
2c897999 | 910 | model: ActorModel.unscoped(), |
3fd3ab2d C |
911 | required: true |
912 | } | |
913 | ] | |
914 | }, | |
af4ae64f C |
915 | { |
916 | model: VideoStreamingPlaylistModel.unscoped(), | |
917 | required: false, | |
918 | include: [ | |
919 | { | |
920 | model: VideoFileModel, | |
921 | required: false | |
922 | } | |
923 | ] | |
924 | }, | |
c8f3cfeb | 925 | VideoLiveModel.unscoped(), |
3fd3ab2d | 926 | VideoFileModel, |
2c897999 | 927 | TagModel |
3fd3ab2d C |
928 | ] |
929 | } | |
164174a6 | 930 | |
3fd3ab2d | 931 | return Bluebird.all([ |
3acc5084 C |
932 | VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findAll(query), |
933 | VideoModel.sequelize.query<{ total: string }>(rawCountQuery, { type: QueryTypes.SELECT }) | |
3fd3ab2d C |
934 | ]).then(([ rows, totals ]) => { |
935 | // totals: totalVideos + totalVideoShares | |
936 | let totalVideos = 0 | |
937 | let totalVideoShares = 0 | |
a1587156 C |
938 | if (totals[0]) totalVideos = parseInt(totals[0].total, 10) |
939 | if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10) | |
3fd3ab2d C |
940 | |
941 | const total = totalVideos + totalVideoShares | |
942 | return { | |
943 | data: rows, | |
944 | total: total | |
945 | } | |
946 | }) | |
947 | } | |
93e1258c | 948 | |
9e2e51dc | 949 | static async listPublishedLiveUUIDs () { |
5c0904fc | 950 | const options = { |
9e2e51dc | 951 | attributes: [ 'uuid' ], |
5c0904fc C |
952 | where: { |
953 | isLive: true, | |
f49b3231 | 954 | remote: false, |
5c0904fc C |
955 | state: VideoState.PUBLISHED |
956 | } | |
957 | } | |
958 | ||
b49f22d8 C |
959 | const result = await VideoModel.findAll(options) |
960 | ||
9e2e51dc | 961 | return result.map(v => v.uuid) |
5c0904fc C |
962 | } |
963 | ||
a4d2ca07 C |
964 | static listUserVideosForApi (options: { |
965 | accountId: number | |
966 | start: number | |
967 | count: number | |
968 | sort: string | |
1fd61899 | 969 | isLive?: boolean |
bf64ed41 | 970 | search?: string |
a4d2ca07 | 971 | }) { |
1fd61899 | 972 | const { accountId, start, count, sort, search, isLive } = options |
a4d2ca07 | 973 | |
3acc5084 | 974 | function buildBaseQuery (): FindOptions { |
1fd61899 C |
975 | const where: WhereOptions = {} |
976 | ||
977 | if (search) { | |
978 | where.name = { | |
979 | [Op.iLike]: '%' + search + '%' | |
980 | } | |
981 | } | |
982 | ||
983 | if (isLive) { | |
984 | where.isLive = isLive | |
985 | } | |
986 | ||
987 | const baseQuery = { | |
3acc5084 C |
988 | offset: start, |
989 | limit: count, | |
1fd61899 | 990 | where, |
3acc5084 C |
991 | order: getVideoSort(sort), |
992 | include: [ | |
993 | { | |
994 | model: VideoChannelModel, | |
995 | required: true, | |
996 | include: [ | |
997 | { | |
998 | model: AccountModel, | |
999 | where: { | |
1000 | id: accountId | |
1001 | }, | |
1002 | required: true | |
1003 | } | |
1004 | ] | |
1005 | } | |
1006 | ] | |
1007 | } | |
bf64ed41 | 1008 | |
bf64ed41 | 1009 | return baseQuery |
3fd3ab2d | 1010 | } |
d8755eed | 1011 | |
3acc5084 C |
1012 | const countQuery = buildBaseQuery() |
1013 | const findQuery = buildBaseQuery() | |
1014 | ||
bf64ed41 | 1015 | const findScopes: (string | ScopeOptions)[] = [ |
a18f275d C |
1016 | ScopeNames.WITH_SCHEDULED_UPDATE, |
1017 | ScopeNames.WITH_BLACKLISTED, | |
1018 | ScopeNames.WITH_THUMBNAILS | |
1019 | ] | |
3acc5084 | 1020 | |
3acc5084 C |
1021 | return Promise.all([ |
1022 | VideoModel.count(countQuery), | |
0283eaac | 1023 | VideoModel.scope(findScopes).findAll<MVideoForUser>(findQuery) |
3acc5084 C |
1024 | ]).then(([ count, rows ]) => { |
1025 | return { | |
0283eaac | 1026 | data: rows, |
3acc5084 C |
1027 | total: count |
1028 | } | |
1029 | }) | |
3fd3ab2d | 1030 | } |
93e1258c | 1031 | |
48dce1c9 | 1032 | static async listForApi (options: { |
a1587156 C |
1033 | start: number |
1034 | count: number | |
1035 | sort: string | |
1fd61899 | 1036 | |
a1587156 | 1037 | nsfw: boolean |
1fd61899 C |
1038 | filter?: VideoFilter |
1039 | isLive?: boolean | |
1040 | ||
a1587156 C |
1041 | includeLocalVideos: boolean |
1042 | withFiles: boolean | |
1fd61899 | 1043 | |
a1587156 C |
1044 | categoryOneOf?: number[] |
1045 | licenceOneOf?: number[] | |
1046 | languageOneOf?: string[] | |
1047 | tagsOneOf?: string[] | |
1048 | tagsAllOf?: string[] | |
1fd61899 | 1049 | |
a1587156 C |
1050 | accountId?: number |
1051 | videoChannelId?: number | |
1fd61899 | 1052 | |
4e74e803 | 1053 | followerActorId?: number |
1fd61899 | 1054 | |
a1587156 | 1055 | videoPlaylistId?: number |
1fd61899 | 1056 | |
a1587156 | 1057 | trendingDays?: number |
1fd61899 | 1058 | |
a1587156 C |
1059 | user?: MUserAccountId |
1060 | historyOfUser?: MUserId | |
1fd61899 | 1061 | |
fe987656 | 1062 | countVideos?: boolean |
1fd61899 | 1063 | |
d8b34ee5 | 1064 | search?: string |
fe987656 | 1065 | }) { |
0aa52e17 | 1066 | if ((options.filter === 'all-local' || options.filter === 'all') && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) { |
7ad9b984 | 1067 | throw new Error('Try to filter all-local but no user has not the see all videos right') |
1cd3facc C |
1068 | } |
1069 | ||
5f3e2425 C |
1070 | const trendingDays = options.sort.endsWith('trending') |
1071 | ? CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS | |
1072 | : undefined | |
3d4e112d RK |
1073 | let trendingAlgorithm |
1074 | if (options.sort.endsWith('hot')) trendingAlgorithm = 'hot' | |
1075 | if (options.sort.endsWith('best')) trendingAlgorithm = 'best' | |
93e1258c | 1076 | |
7ad9b984 C |
1077 | const serverActor = await getServerActor() |
1078 | ||
4e74e803 | 1079 | // followerActorId === null has a meaning, so just check undefined |
5f3e2425 C |
1080 | const followerActorId = options.followerActorId !== undefined |
1081 | ? options.followerActorId | |
1082 | : serverActor.id | |
06a05d5f | 1083 | |
afd2cba5 | 1084 | const queryOptions = { |
5f3e2425 C |
1085 | start: options.start, |
1086 | count: options.count, | |
1087 | sort: options.sort, | |
4e74e803 | 1088 | followerActorId, |
7ad9b984 | 1089 | serverAccountId: serverActor.Account.id, |
afd2cba5 | 1090 | nsfw: options.nsfw, |
1fd61899 | 1091 | isLive: options.isLive, |
afd2cba5 C |
1092 | categoryOneOf: options.categoryOneOf, |
1093 | licenceOneOf: options.licenceOneOf, | |
1094 | languageOneOf: options.languageOneOf, | |
1095 | tagsOneOf: options.tagsOneOf, | |
1096 | tagsAllOf: options.tagsAllOf, | |
1097 | filter: options.filter, | |
1098 | withFiles: options.withFiles, | |
1099 | accountId: options.accountId, | |
1100 | videoChannelId: options.videoChannelId, | |
418d092a | 1101 | videoPlaylistId: options.videoPlaylistId, |
9a629c6e | 1102 | includeLocalVideos: options.includeLocalVideos, |
7ad9b984 | 1103 | user: options.user, |
8b9a525a | 1104 | historyOfUser: options.historyOfUser, |
d8b34ee5 | 1105 | trendingDays, |
3d4e112d | 1106 | trendingAlgorithm, |
d8b34ee5 | 1107 | search: options.search |
48dce1c9 C |
1108 | } |
1109 | ||
5f3e2425 | 1110 | return VideoModel.getAvailableForApi(queryOptions, options.countVideos) |
93e1258c C |
1111 | } |
1112 | ||
0b18f4aa | 1113 | static async searchAndPopulateAccountAndServer (options: { |
06a05d5f | 1114 | includeLocalVideos: boolean |
d4112450 | 1115 | search?: string |
0b18f4aa C |
1116 | start?: number |
1117 | count?: number | |
1118 | sort?: string | |
1119 | startDate?: string // ISO 8601 | |
1120 | endDate?: string // ISO 8601 | |
31d065cc AM |
1121 | originallyPublishedStartDate?: string |
1122 | originallyPublishedEndDate?: string | |
0b18f4aa | 1123 | nsfw?: boolean |
1fd61899 | 1124 | isLive?: boolean |
0b18f4aa C |
1125 | categoryOneOf?: number[] |
1126 | licenceOneOf?: number[] | |
1127 | languageOneOf?: string[] | |
1128 | tagsOneOf?: string[] | |
1129 | tagsAllOf?: string[] | |
1130 | durationMin?: number // seconds | |
1131 | durationMax?: number // seconds | |
a1587156 | 1132 | user?: MUserAccountId |
1cd3facc | 1133 | filter?: VideoFilter |
0b18f4aa | 1134 | }) { |
f05a1c30 | 1135 | const serverActor = await getServerActor() |
1fd61899 | 1136 | |
afd2cba5 | 1137 | const queryOptions = { |
4e74e803 | 1138 | followerActorId: serverActor.id, |
7ad9b984 | 1139 | serverAccountId: serverActor.Account.id, |
1fd61899 | 1140 | |
afd2cba5 C |
1141 | includeLocalVideos: options.includeLocalVideos, |
1142 | nsfw: options.nsfw, | |
1fd61899 C |
1143 | isLive: options.isLive, |
1144 | ||
afd2cba5 C |
1145 | categoryOneOf: options.categoryOneOf, |
1146 | licenceOneOf: options.licenceOneOf, | |
1147 | languageOneOf: options.languageOneOf, | |
1fd61899 | 1148 | |
afd2cba5 | 1149 | tagsOneOf: options.tagsOneOf, |
6e46de09 | 1150 | tagsAllOf: options.tagsAllOf, |
1fd61899 | 1151 | |
7ad9b984 | 1152 | user: options.user, |
3caf77d3 | 1153 | filter: options.filter, |
1fd61899 | 1154 | |
5f3e2425 C |
1155 | start: options.start, |
1156 | count: options.count, | |
1157 | sort: options.sort, | |
1fd61899 | 1158 | |
5f3e2425 C |
1159 | startDate: options.startDate, |
1160 | endDate: options.endDate, | |
1fd61899 | 1161 | |
5f3e2425 C |
1162 | originallyPublishedStartDate: options.originallyPublishedStartDate, |
1163 | originallyPublishedEndDate: options.originallyPublishedEndDate, | |
1164 | ||
1165 | durationMin: options.durationMin, | |
1166 | durationMax: options.durationMax, | |
1167 | ||
1168 | search: options.search | |
48dce1c9 | 1169 | } |
f05a1c30 | 1170 | |
5f3e2425 | 1171 | return VideoModel.getAvailableForApi(queryOptions) |
f05a1c30 C |
1172 | } |
1173 | ||
a056ca48 C |
1174 | static countLocalLives () { |
1175 | const options = { | |
1176 | where: { | |
1177 | remote: false, | |
875f0610 C |
1178 | isLive: true, |
1179 | state: { | |
1180 | [Op.ne]: VideoState.LIVE_ENDED | |
1181 | } | |
a056ca48 C |
1182 | } |
1183 | } | |
1184 | ||
1185 | return VideoModel.count(options) | |
1186 | } | |
1187 | ||
77d7e851 C |
1188 | static countVideosUploadedByUserSince (userId: number, since: Date) { |
1189 | const options = { | |
1190 | include: [ | |
1191 | { | |
1192 | model: VideoChannelModel.unscoped(), | |
1193 | required: true, | |
1194 | include: [ | |
1195 | { | |
1196 | model: AccountModel.unscoped(), | |
1197 | required: true, | |
1198 | include: [ | |
1199 | { | |
1200 | model: UserModel.unscoped(), | |
1201 | required: true, | |
1202 | where: { | |
1203 | id: userId | |
1204 | } | |
1205 | } | |
1206 | ] | |
1207 | } | |
1208 | ] | |
1209 | } | |
1210 | ], | |
1211 | where: { | |
1212 | createdAt: { | |
1213 | [Op.gte]: since | |
1214 | } | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | return VideoModel.unscoped().count(options) | |
1219 | } | |
1220 | ||
a056ca48 C |
1221 | static countLivesOfAccount (accountId: number) { |
1222 | const options = { | |
1223 | where: { | |
1224 | remote: false, | |
fb4b3f91 C |
1225 | isLive: true, |
1226 | state: { | |
1227 | [Op.ne]: VideoState.LIVE_ENDED | |
1228 | } | |
a056ca48 C |
1229 | }, |
1230 | include: [ | |
1231 | { | |
1232 | required: true, | |
1233 | model: VideoChannelModel.unscoped(), | |
1234 | where: { | |
1235 | accountId | |
1236 | } | |
1237 | } | |
1238 | ] | |
1239 | } | |
1240 | ||
1241 | return VideoModel.count(options) | |
1242 | } | |
1243 | ||
71d4af1e C |
1244 | static load (id: number | string, transaction?: Transaction): Promise<MVideoThumbnail> { |
1245 | const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize) | |
d8755eed | 1246 | |
71d4af1e | 1247 | return queryBuilder.queryVideo({ id, transaction, type: 'thumbnails' }) |
3fd3ab2d | 1248 | } |
d8755eed | 1249 | |
71d4af1e C |
1250 | static loadWithBlacklist (id: number | string, transaction?: Transaction): Promise<MVideoThumbnailBlacklist> { |
1251 | const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize) | |
d636ab58 | 1252 | |
71d4af1e | 1253 | return queryBuilder.queryVideo({ id, transaction, type: 'thumbnails-blacklist' }) |
d636ab58 C |
1254 | } |
1255 | ||
b49f22d8 | 1256 | static loadImmutableAttributes (id: number | string, t?: Transaction): Promise<MVideoImmutable> { |
7eba5e1f | 1257 | const fun = () => { |
943e5193 C |
1258 | const query = { |
1259 | where: buildWhereIdOrUUID(id), | |
7eba5e1f C |
1260 | transaction: t |
1261 | } | |
1262 | ||
943e5193 | 1263 | return VideoModel.scope(ScopeNames.WITH_IMMUTABLE_ATTRIBUTES).findOne(query) |
7eba5e1f C |
1264 | } |
1265 | ||
1266 | return ModelCache.Instance.doCache({ | |
943e5193 | 1267 | cacheType: 'load-video-immutable-id', |
7eba5e1f C |
1268 | key: '' + id, |
1269 | deleteKey: 'video', | |
1270 | fun | |
1271 | }) | |
1272 | } | |
1273 | ||
b49f22d8 | 1274 | static loadByUrlImmutableAttributes (url: string, transaction?: Transaction): Promise<MVideoImmutable> { |
943e5193 C |
1275 | const fun = () => { |
1276 | const query: FindOptions = { | |
1277 | where: { | |
1278 | url | |
1279 | }, | |
1280 | transaction | |
1281 | } | |
1282 | ||
1283 | return VideoModel.scope(ScopeNames.WITH_IMMUTABLE_ATTRIBUTES).findOne(query) | |
1284 | } | |
1285 | ||
1286 | return ModelCache.Instance.doCache({ | |
1287 | cacheType: 'load-video-immutable-url', | |
1288 | key: url, | |
1289 | deleteKey: 'video', | |
1290 | fun | |
1291 | }) | |
1292 | } | |
1293 | ||
71d4af1e C |
1294 | static loadOnlyId (id: number | string, transaction?: Transaction): Promise<MVideoId> { |
1295 | const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize) | |
627621c1 | 1296 | |
71d4af1e | 1297 | return queryBuilder.queryVideo({ id, transaction, type: 'id' }) |
627621c1 C |
1298 | } |
1299 | ||
71d4af1e C |
1300 | static loadWithFiles (id: number | string, transaction?: Transaction, logging?: boolean): Promise<MVideoWithAllFiles> { |
1301 | const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize) | |
627621c1 | 1302 | |
71d4af1e C |
1303 | return queryBuilder.queryVideo({ id, transaction, type: 'all-files', logging }) |
1304 | } | |
fd45e8f4 | 1305 | |
71d4af1e C |
1306 | static loadByUrl (url: string, transaction?: Transaction): Promise<MVideoThumbnail> { |
1307 | const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize) | |
09209296 | 1308 | |
71d4af1e C |
1309 | return queryBuilder.queryVideo({ url, transaction, type: 'thumbnails' }) |
1310 | } | |
1311 | ||
1312 | static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Promise<MVideoAccountLightBlacklistAllFiles> { | |
1313 | const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize) | |
1314 | ||
1315 | return queryBuilder.queryVideo({ url, transaction, type: 'account-blacklist-files' }) | |
1316 | } | |
1317 | ||
1318 | static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number): Promise<MVideoFullLight> { | |
1319 | const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize) | |
09209296 | 1320 | |
71d4af1e | 1321 | return queryBuilder.queryVideo({ id, transaction: t, type: 'full-light', userId }) |
09209296 C |
1322 | } |
1323 | ||
89cd1275 | 1324 | static loadForGetAPI (parameters: { |
a1587156 | 1325 | id: number | string |
ca4b4b2e | 1326 | transaction?: Transaction |
89cd1275 | 1327 | userId?: number |
b49f22d8 | 1328 | }): Promise<MVideoDetails> { |
ca4b4b2e | 1329 | const { id, transaction, userId } = parameters |
d9bf974f | 1330 | const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize) |
09209296 | 1331 | |
71d4af1e | 1332 | return queryBuilder.queryVideo({ id, transaction, type: 'api', userId }) |
da854ddd C |
1333 | } |
1334 | ||
09cababd C |
1335 | static async getStats () { |
1336 | const totalLocalVideos = await VideoModel.count({ | |
1337 | where: { | |
1338 | remote: false | |
1339 | } | |
1340 | }) | |
09cababd C |
1341 | |
1342 | let totalLocalVideoViews = await VideoModel.sum('views', { | |
1343 | where: { | |
1344 | remote: false | |
1345 | } | |
1346 | }) | |
baab47ca | 1347 | |
09cababd C |
1348 | // Sequelize could return null... |
1349 | if (!totalLocalVideoViews) totalLocalVideoViews = 0 | |
1350 | ||
baab47ca C |
1351 | const { total: totalVideos } = await VideoModel.listForApi({ |
1352 | start: 0, | |
1353 | count: 0, | |
1354 | sort: '-publishedAt', | |
1355 | nsfw: buildNSFWFilter(), | |
1356 | includeLocalVideos: true, | |
1357 | withFiles: false | |
1358 | }) | |
1359 | ||
09cababd C |
1360 | return { |
1361 | totalLocalVideos, | |
1362 | totalLocalVideoViews, | |
1363 | totalVideos | |
1364 | } | |
1365 | } | |
1366 | ||
6b616860 C |
1367 | static incrementViews (id: number, views: number) { |
1368 | return VideoModel.increment('views', { | |
1369 | by: views, | |
1370 | where: { | |
1371 | id | |
1372 | } | |
1373 | }) | |
1374 | } | |
1375 | ||
74d249bc C |
1376 | static updateRatesOf (videoId: number, type: VideoRateType, t: Transaction) { |
1377 | const field = type === 'like' | |
1378 | ? 'likes' | |
1379 | : 'dislikes' | |
1380 | ||
1381 | const rawQuery = `UPDATE "video" SET "${field}" = ` + | |
1382 | '(' + | |
69322042 | 1383 | 'SELECT COUNT(id) FROM "accountVideoRate" WHERE "accountVideoRate"."videoId" = "video"."id" AND type = :rateType' + |
74d249bc C |
1384 | ') ' + |
1385 | 'WHERE "video"."id" = :videoId' | |
1386 | ||
1387 | return AccountVideoRateModel.sequelize.query(rawQuery, { | |
1388 | transaction: t, | |
1389 | replacements: { videoId, rateType: type }, | |
1390 | type: QueryTypes.UPDATE | |
1391 | }) | |
1392 | } | |
1393 | ||
8d427346 C |
1394 | static checkVideoHasInstanceFollow (videoId: number, followerActorId: number) { |
1395 | // Instances only share videos | |
1396 | const query = 'SELECT 1 FROM "videoShare" ' + | |
a1587156 | 1397 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + |
f046e2fa | 1398 | 'WHERE "actorFollow"."actorId" = $followerActorId AND "actorFollow"."state" = \'accepted\' AND "videoShare"."videoId" = $videoId ' + |
a1587156 | 1399 | 'LIMIT 1' |
8d427346 C |
1400 | |
1401 | const options = { | |
d5d9b6d7 | 1402 | type: QueryTypes.SELECT as QueryTypes.SELECT, |
8d427346 C |
1403 | bind: { followerActorId, videoId }, |
1404 | raw: true | |
1405 | } | |
1406 | ||
1407 | return VideoModel.sequelize.query(query, options) | |
1408 | .then(results => results.length === 1) | |
1409 | } | |
1410 | ||
69322042 | 1411 | static bulkUpdateSupportField (ofChannel: MChannel, t: Transaction) { |
7d14d4d2 C |
1412 | const options = { |
1413 | where: { | |
69322042 | 1414 | channelId: ofChannel.id |
7d14d4d2 C |
1415 | }, |
1416 | transaction: t | |
1417 | } | |
1418 | ||
69322042 | 1419 | return VideoModel.update({ support: ofChannel.support }, options) |
7d14d4d2 C |
1420 | } |
1421 | ||
b49f22d8 | 1422 | static getAllIdsFromChannel (videoChannel: MChannelId): Promise<number[]> { |
7d14d4d2 C |
1423 | const query = { |
1424 | attributes: [ 'id' ], | |
1425 | where: { | |
1426 | channelId: videoChannel.id | |
1427 | } | |
1428 | } | |
1429 | ||
1430 | return VideoModel.findAll(query) | |
a1587156 | 1431 | .then(videos => videos.map(v => v.id)) |
7d14d4d2 C |
1432 | } |
1433 | ||
2d3741d6 | 1434 | // threshold corresponds to how many video the field should have to be returned |
7348b1fd | 1435 | static async getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) { |
65b21c96 | 1436 | const serverActor = await getServerActor() |
4e74e803 | 1437 | const followerActorId = serverActor.id |
7348b1fd | 1438 | |
e5dbd508 | 1439 | const queryOptions: BuildVideosListQueryOptions = { |
5f3e2425 C |
1440 | attributes: [ `"${field}"` ], |
1441 | group: `GROUP BY "${field}"`, | |
1442 | having: `HAVING COUNT("${field}") >= ${threshold}`, | |
1443 | start: 0, | |
1444 | sort: 'random', | |
1445 | count, | |
65b21c96 | 1446 | serverAccountId: serverActor.Account.id, |
4e74e803 | 1447 | followerActorId, |
5f3e2425 | 1448 | includeLocalVideos: true |
7348b1fd C |
1449 | } |
1450 | ||
e5dbd508 | 1451 | const queryBuilder = new VideosIdListQueryBuilder(VideoModel.sequelize) |
2d3741d6 | 1452 | |
e5dbd508 C |
1453 | return queryBuilder.queryVideoIds(queryOptions) |
1454 | .then(rows => rows.map(r => r[field])) | |
2d3741d6 C |
1455 | } |
1456 | ||
b36f41ca C |
1457 | static buildTrendingQuery (trendingDays: number) { |
1458 | return { | |
1459 | attributes: [], | |
1460 | subQuery: false, | |
1461 | model: VideoViewModel, | |
1462 | required: false, | |
1463 | where: { | |
1464 | startDate: { | |
a1587156 | 1465 | [Op.gte]: new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays) |
b36f41ca C |
1466 | } |
1467 | } | |
1468 | } | |
1469 | } | |
1470 | ||
6e46de09 | 1471 | private static async getAvailableForApi ( |
e5dbd508 | 1472 | options: BuildVideosListQueryOptions, |
6e46de09 | 1473 | countVideos = true |
b84d4c80 | 1474 | ): Promise<ResultList<VideoModel>> { |
6b842050 C |
1475 | function getCount () { |
1476 | if (countVideos !== true) return Promise.resolve(undefined) | |
8ea6f49a | 1477 | |
6b842050 | 1478 | const countOptions = Object.assign({}, options, { isCount: true }) |
e5dbd508 | 1479 | const queryBuilder = new VideosIdListQueryBuilder(VideoModel.sequelize) |
3caf77d3 | 1480 | |
e5dbd508 | 1481 | return queryBuilder.countVideoIds(countOptions) |
6b842050 C |
1482 | } |
1483 | ||
1484 | function getModels () { | |
baab47ca C |
1485 | if (options.count === 0) return Promise.resolve([]) |
1486 | ||
e5dbd508 | 1487 | const queryBuilder = new VideosModelListQueryBuilder(VideoModel.sequelize) |
6b842050 | 1488 | |
e5dbd508 | 1489 | return queryBuilder.queryVideos(options) |
6b842050 C |
1490 | } |
1491 | ||
1492 | const [ count, rows ] = await Promise.all([ getCount(), getModels() ]) | |
afd2cba5 | 1493 | |
ddc07312 C |
1494 | return { |
1495 | data: rows, | |
1496 | total: count | |
1497 | } | |
1498 | } | |
1499 | ||
5b77537c C |
1500 | isBlacklisted () { |
1501 | return !!this.VideoBlacklist | |
1502 | } | |
1503 | ||
bfbd9128 | 1504 | isBlocked () { |
faa9d434 | 1505 | return this.VideoChannel.Account.Actor.Server?.isBlocked() || this.VideoChannel.Account.isBlocked() |
bfbd9128 C |
1506 | } |
1507 | ||
a1587156 | 1508 | getQualityFileBy<T extends MVideoWithFile> (this: T, fun: (files: MVideoFile[], it: (file: MVideoFile) => number) => MVideoFile) { |
24516aa2 | 1509 | // We first transcode to WebTorrent format, so try this array first |
d7a25329 | 1510 | if (Array.isArray(this.VideoFiles) && this.VideoFiles.length !== 0) { |
92e0f42e | 1511 | const file = fun(this.VideoFiles, file => file.resolution) |
d7a25329 C |
1512 | |
1513 | return Object.assign(file, { Video: this }) | |
1514 | } | |
1515 | ||
1516 | // No webtorrent files, try with streaming playlist files | |
1517 | if (Array.isArray(this.VideoStreamingPlaylists) && this.VideoStreamingPlaylists.length !== 0) { | |
1518 | const streamingPlaylistWithVideo = Object.assign(this.VideoStreamingPlaylists[0], { Video: this }) | |
1519 | ||
92e0f42e | 1520 | const file = fun(streamingPlaylistWithVideo.VideoFiles, file => file.resolution) |
d7a25329 C |
1521 | return Object.assign(file, { VideoStreamingPlaylist: streamingPlaylistWithVideo }) |
1522 | } | |
aaf61f38 | 1523 | |
d7a25329 | 1524 | return undefined |
e4f97bab | 1525 | } |
aaf61f38 | 1526 | |
a1587156 | 1527 | getMaxQualityFile<T extends MVideoWithFile> (this: T): MVideoFileVideo | MVideoFileStreamingPlaylistVideo { |
92e0f42e C |
1528 | return this.getQualityFileBy(maxBy) |
1529 | } | |
1530 | ||
a1587156 | 1531 | getMinQualityFile<T extends MVideoWithFile> (this: T): MVideoFileVideo | MVideoFileStreamingPlaylistVideo { |
92e0f42e C |
1532 | return this.getQualityFileBy(minBy) |
1533 | } | |
1534 | ||
a1587156 | 1535 | getWebTorrentFile<T extends MVideoWithFile> (this: T, resolution: number): MVideoFileVideo { |
29d4e137 C |
1536 | if (Array.isArray(this.VideoFiles) === false) return undefined |
1537 | ||
d7a25329 C |
1538 | const file = this.VideoFiles.find(f => f.resolution === resolution) |
1539 | if (!file) return undefined | |
1540 | ||
1541 | return Object.assign(file, { Video: this }) | |
29d4e137 C |
1542 | } |
1543 | ||
6939cbac C |
1544 | hasWebTorrentFiles () { |
1545 | return Array.isArray(this.VideoFiles) === true && this.VideoFiles.length !== 0 | |
1546 | } | |
1547 | ||
28dfb44b | 1548 | async addAndSaveThumbnail (thumbnail: MThumbnail, transaction?: Transaction) { |
3acc5084 C |
1549 | thumbnail.videoId = this.id |
1550 | ||
1551 | const savedThumbnail = await thumbnail.save({ transaction }) | |
1552 | ||
e8bafea3 C |
1553 | if (Array.isArray(this.Thumbnails) === false) this.Thumbnails = [] |
1554 | ||
1555 | // Already have this thumbnail, skip | |
3acc5084 | 1556 | if (this.Thumbnails.find(t => t.id === savedThumbnail.id)) return |
e8bafea3 | 1557 | |
3acc5084 | 1558 | this.Thumbnails.push(savedThumbnail) |
e8bafea3 C |
1559 | } |
1560 | ||
3acc5084 | 1561 | getMiniature () { |
e8bafea3 C |
1562 | if (Array.isArray(this.Thumbnails) === false) return undefined |
1563 | ||
3acc5084 | 1564 | return this.Thumbnails.find(t => t.type === ThumbnailType.MINIATURE) |
e8bafea3 C |
1565 | } |
1566 | ||
6872996d C |
1567 | hasPreview () { |
1568 | return !!this.getPreview() | |
1569 | } | |
1570 | ||
e8bafea3 C |
1571 | getPreview () { |
1572 | if (Array.isArray(this.Thumbnails) === false) return undefined | |
1573 | ||
1574 | return this.Thumbnails.find(t => t.type === ThumbnailType.PREVIEW) | |
3fd3ab2d | 1575 | } |
7b1f49de | 1576 | |
3fd3ab2d C |
1577 | isOwned () { |
1578 | return this.remote === false | |
9567011b C |
1579 | } |
1580 | ||
cef534ed | 1581 | getWatchStaticPath () { |
a1eda903 | 1582 | return '/w/' + this.uuid |
cef534ed C |
1583 | } |
1584 | ||
40e87e9e | 1585 | getEmbedStaticPath () { |
3fd3ab2d C |
1586 | return '/videos/embed/' + this.uuid |
1587 | } | |
e4f97bab | 1588 | |
3acc5084 C |
1589 | getMiniatureStaticPath () { |
1590 | const thumbnail = this.getMiniature() | |
e8bafea3 C |
1591 | if (!thumbnail) return null |
1592 | ||
1593 | return join(STATIC_PATHS.THUMBNAILS, thumbnail.filename) | |
e4f97bab | 1594 | } |
227d02fe | 1595 | |
40e87e9e | 1596 | getPreviewStaticPath () { |
e8bafea3 C |
1597 | const preview = this.getPreview() |
1598 | if (!preview) return null | |
1599 | ||
1600 | // We use a local cache, so specify our cache endpoint instead of potential remote URL | |
557b13ae | 1601 | return join(LAZY_STATIC_PATHS.PREVIEWS, preview.filename) |
3fd3ab2d | 1602 | } |
40298b02 | 1603 | |
b5fecbf4 | 1604 | toFormattedJSON (this: MVideoFormattable, options?: VideoFormattingJSONOptions): Video { |
098eb377 | 1605 | return videoModelToFormattedJSON(this, options) |
14d3270f | 1606 | } |
14d3270f | 1607 | |
b5fecbf4 | 1608 | toFormattedDetailsJSON (this: MVideoFormattableDetails): VideoDetails { |
098eb377 | 1609 | return videoModelToFormattedDetailsJSON(this) |
244e76a5 RK |
1610 | } |
1611 | ||
f66db4d5 | 1612 | getFormattedVideoFilesJSON (includeMagnet = true): VideoFile[] { |
7a499487 | 1613 | let files: VideoFile[] = [] |
97816649 | 1614 | |
97816649 | 1615 | if (Array.isArray(this.VideoFiles)) { |
f66db4d5 | 1616 | const result = videoFilesModelToFormattedJSON(this, this.VideoFiles, includeMagnet) |
7a499487 | 1617 | files = files.concat(result) |
97816649 C |
1618 | } |
1619 | ||
1620 | for (const p of (this.VideoStreamingPlaylists || [])) { | |
f66db4d5 | 1621 | const result = videoFilesModelToFormattedJSON(this, p.VideoFiles, includeMagnet) |
7a499487 | 1622 | files = files.concat(result) |
97816649 C |
1623 | } |
1624 | ||
7a499487 | 1625 | return files |
3fd3ab2d | 1626 | } |
e4f97bab | 1627 | |
de6310b2 | 1628 | toActivityPubObject (this: MVideoAP): VideoObject { |
098eb377 | 1629 | return videoModelToActivityPubObject(this) |
3fd3ab2d C |
1630 | } |
1631 | ||
1632 | getTruncatedDescription () { | |
1633 | if (!this.description) return null | |
93e1258c | 1634 | |
bffbebbe | 1635 | const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max |
687c6180 | 1636 | return peertubeTruncate(this.description, { length: maxLength }) |
93e1258c C |
1637 | } |
1638 | ||
d7a25329 C |
1639 | getMaxQualityResolution () { |
1640 | const file = this.getMaxQualityFile() | |
1641 | const videoOrPlaylist = file.getVideoOrStreamingPlaylist() | |
1642 | const originalFilePath = getVideoFilePath(videoOrPlaylist, file) | |
0d0e8dd0 | 1643 | |
056aa7f2 | 1644 | return getVideoFileResolution(originalFilePath) |
3fd3ab2d | 1645 | } |
0d0e8dd0 | 1646 | |
96f29c0f | 1647 | getDescriptionAPIPath () { |
3fd3ab2d | 1648 | return `/api/${API_VERSION}/videos/${this.uuid}/description` |
feb4bdfd C |
1649 | } |
1650 | ||
d7a25329 | 1651 | getHLSPlaylist (): MStreamingPlaylistFilesVideo { |
e2600d8b C |
1652 | if (!this.VideoStreamingPlaylists) return undefined |
1653 | ||
d7a25329 C |
1654 | const playlist = this.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) |
1655 | playlist.Video = this | |
1656 | ||
1657 | return playlist | |
e2600d8b C |
1658 | } |
1659 | ||
d7a25329 C |
1660 | setHLSPlaylist (playlist: MStreamingPlaylist) { |
1661 | const toAdd = [ playlist ] as [ VideoStreamingPlaylistModel ] | |
b9fffa29 | 1662 | |
d7a25329 C |
1663 | if (Array.isArray(this.VideoStreamingPlaylists) === false || this.VideoStreamingPlaylists.length === 0) { |
1664 | this.VideoStreamingPlaylists = toAdd | |
1665 | return | |
1666 | } | |
1667 | ||
1668 | this.VideoStreamingPlaylists = this.VideoStreamingPlaylists | |
a1587156 C |
1669 | .filter(s => s.type !== VideoStreamingPlaylistType.HLS) |
1670 | .concat(toAdd) | |
d7a25329 C |
1671 | } |
1672 | ||
1673 | removeFile (videoFile: MVideoFile, isRedundancy = false) { | |
1674 | const filePath = getVideoFilePath(this, videoFile, isRedundancy) | |
62689b94 | 1675 | return remove(filePath) |
ed31c059 | 1676 | .catch(err => logger.warn('Cannot delete file %s.', filePath, { err })) |
feb4bdfd C |
1677 | } |
1678 | ||
ffc65cbd | 1679 | async removeStreamingPlaylistFiles (streamingPlaylist: MStreamingPlaylist, isRedundancy = false) { |
66fb2aa3 | 1680 | const directoryPath = getHLSDirectory(this, isRedundancy) |
09209296 | 1681 | |
ffc65cbd C |
1682 | await remove(directoryPath) |
1683 | ||
1684 | if (isRedundancy !== true) { | |
a1587156 | 1685 | const streamingPlaylistWithFiles = streamingPlaylist as MStreamingPlaylistFilesVideo |
ffc65cbd C |
1686 | streamingPlaylistWithFiles.Video = this |
1687 | ||
1688 | if (!Array.isArray(streamingPlaylistWithFiles.VideoFiles)) { | |
1689 | streamingPlaylistWithFiles.VideoFiles = await streamingPlaylistWithFiles.$get('VideoFiles') | |
1690 | } | |
1691 | ||
1692 | // Remove physical files and torrents | |
1693 | await Promise.all( | |
90a8bd30 | 1694 | streamingPlaylistWithFiles.VideoFiles.map(file => file.removeTorrent()) |
ffc65cbd C |
1695 | ) |
1696 | } | |
09209296 C |
1697 | } |
1698 | ||
1297eb5d C |
1699 | isOutdated () { |
1700 | if (this.isOwned()) return false | |
1701 | ||
9f79ade6 | 1702 | return isOutdated(this, ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL) |
1297eb5d C |
1703 | } |
1704 | ||
22a73cb8 | 1705 | hasPrivacyForFederation () { |
3092e9bb | 1706 | return isPrivacyForFederation(this.privacy) |
22a73cb8 C |
1707 | } |
1708 | ||
68e70a74 C |
1709 | hasStateForFederation () { |
1710 | return isStateForFederation(this.state) | |
1711 | } | |
1712 | ||
22a73cb8 | 1713 | isNewVideo (newPrivacy: VideoPrivacy) { |
3092e9bb | 1714 | return this.hasPrivacyForFederation() === false && isPrivacyForFederation(newPrivacy) === true |
22a73cb8 C |
1715 | } |
1716 | ||
04b8c3fb | 1717 | setAsRefreshed () { |
e024fd6a | 1718 | return setAsUpdated('video', this.id) |
04b8c3fb C |
1719 | } |
1720 | ||
22a73cb8 C |
1721 | requiresAuth () { |
1722 | return this.privacy === VideoPrivacy.PRIVATE || this.privacy === VideoPrivacy.INTERNAL || !!this.VideoBlacklist | |
1723 | } | |
1724 | ||
1725 | setPrivacy (newPrivacy: VideoPrivacy) { | |
1726 | if (this.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) { | |
1727 | this.publishedAt = new Date() | |
1728 | } | |
1729 | ||
1730 | this.privacy = newPrivacy | |
1731 | } | |
1732 | ||
1733 | isConfidential () { | |
1734 | return this.privacy === VideoPrivacy.PRIVATE || | |
1735 | this.privacy === VideoPrivacy.UNLISTED || | |
1736 | this.privacy === VideoPrivacy.INTERNAL | |
1737 | } | |
1738 | ||
d7a25329 C |
1739 | async publishIfNeededAndSave (t: Transaction) { |
1740 | if (this.state !== VideoState.PUBLISHED) { | |
1741 | this.state = VideoState.PUBLISHED | |
1742 | this.publishedAt = new Date() | |
1743 | await this.save({ transaction: t }) | |
7920c273 | 1744 | |
d7a25329 | 1745 | return true |
6fcd19ba | 1746 | } |
aaf61f38 | 1747 | |
d7a25329 | 1748 | return false |
15d4ee04 | 1749 | } |
a96aed15 | 1750 | |
d9a2a031 C |
1751 | getBandwidthBits (videoFile: MVideoFile) { |
1752 | return Math.ceil((videoFile.size * 8) / this.duration) | |
c48e82b5 C |
1753 | } |
1754 | ||
d9a2a031 C |
1755 | getTrackerUrls () { |
1756 | if (this.isOwned()) { | |
1757 | return [ | |
1758 | WEBSERVER.URL + '/tracker/announce', | |
1759 | WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' | |
1760 | ] | |
1761 | } | |
09209296 | 1762 | |
d9a2a031 | 1763 | return this.Trackers.map(t => t.url) |
09209296 | 1764 | } |
a96aed15 | 1765 | } |