]>
Commit | Line | Data |
---|---|---|
39445ead | 1 | import * as Bluebird from 'bluebird' |
098eb377 | 2 | import { maxBy } from 'lodash' |
571389d4 | 3 | import * as magnetUtil from 'magnet-uri' |
4d4e5cd4 | 4 | import * as parseTorrent from 'parse-torrent' |
098eb377 | 5 | import { join } from 'path' |
e02643f3 | 6 | import * as Sequelize from 'sequelize' |
3fd3ab2d | 7 | import { |
4ba3b8ea C |
8 | AllowNull, |
9 | BeforeDestroy, | |
10 | BelongsTo, | |
11 | BelongsToMany, | |
12 | Column, | |
13 | CreatedAt, | |
14 | DataType, | |
15 | Default, | |
16 | ForeignKey, | |
17 | HasMany, | |
2baea0c7 | 18 | HasOne, |
4ba3b8ea | 19 | IFindOptions, |
9a629c6e | 20 | IIncludeOptions, |
4ba3b8ea C |
21 | Is, |
22 | IsInt, | |
23 | IsUUID, | |
24 | Min, | |
25 | Model, | |
26 | Scopes, | |
27 | Table, | |
9a629c6e | 28 | UpdatedAt |
3fd3ab2d | 29 | } from 'sequelize-typescript' |
7ad9b984 | 30 | import { UserRight, VideoPrivacy, VideoState } from '../../../shared' |
3fd3ab2d | 31 | import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' |
a8462c8e | 32 | import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos' |
066e94c5 | 33 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' |
62689b94 | 34 | import { createTorrentPromise, peertubeTruncate } from '../../helpers/core-utils' |
da854ddd | 35 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
c48e82b5 | 36 | import { isArray, isBooleanValid } from '../../helpers/custom-validators/misc' |
3fd3ab2d | 37 | import { |
4ba3b8ea C |
38 | isVideoCategoryValid, |
39 | isVideoDescriptionValid, | |
40 | isVideoDurationValid, | |
41 | isVideoLanguageValid, | |
42 | isVideoLicenceValid, | |
418d092a | 43 | isVideoNameValid, |
2baea0c7 C |
44 | isVideoPrivacyValid, |
45 | isVideoStateValid, | |
b64c950a | 46 | isVideoSupportValid |
3fd3ab2d | 47 | } from '../../helpers/custom-validators/videos' |
098eb377 | 48 | import { generateImageFromVideoFile, getVideoFileResolution } from '../../helpers/ffmpeg-utils' |
da854ddd | 49 | import { logger } from '../../helpers/logger' |
f05a1c30 | 50 | import { getServerActor } from '../../helpers/utils' |
65fcc311 | 51 | import { |
1297eb5d | 52 | ACTIVITY_PUB, |
4ba3b8ea C |
53 | API_VERSION, |
54 | CONFIG, | |
418d092a | 55 | CONSTRAINTS_FIELDS, |
9c6ca37f | 56 | HLS_STREAMING_PLAYLIST_DIRECTORY, |
418d092a | 57 | HLS_REDUNDANCY_DIRECTORY, |
4ba3b8ea C |
58 | PREVIEWS_SIZE, |
59 | REMOTE_SCHEME, | |
02756fbd | 60 | STATIC_DOWNLOAD_PATHS, |
4ba3b8ea C |
61 | STATIC_PATHS, |
62 | THUMBNAILS_SIZE, | |
63 | VIDEO_CATEGORIES, | |
64 | VIDEO_LANGUAGES, | |
65 | VIDEO_LICENCES, | |
2baea0c7 C |
66 | VIDEO_PRIVACIES, |
67 | VIDEO_STATES | |
3fd3ab2d | 68 | } from '../../initializers' |
50d6de9c | 69 | import { sendDeleteVideo } from '../../lib/activitypub/send' |
3fd3ab2d C |
70 | import { AccountModel } from '../account/account' |
71 | import { AccountVideoRateModel } from '../account/account-video-rate' | |
50d6de9c | 72 | import { ActorModel } from '../activitypub/actor' |
b6a4fd6b | 73 | import { AvatarModel } from '../avatar/avatar' |
3fd3ab2d | 74 | import { ServerModel } from '../server/server' |
418d092a C |
75 | import { |
76 | buildBlockedAccountSQL, | |
77 | buildTrigramSearchIndex, | |
78 | buildWhereIdOrUUID, | |
79 | createSimilarityAttribute, | |
9f79ade6 | 80 | getVideoSort, isOutdated, |
418d092a C |
81 | throwIfNotValid |
82 | } from '../utils' | |
3fd3ab2d C |
83 | import { TagModel } from './tag' |
84 | import { VideoAbuseModel } from './video-abuse' | |
418d092a | 85 | import { VideoChannelModel, ScopeNames as VideoChannelScopeNames } from './video-channel' |
da854ddd | 86 | import { VideoCommentModel } from './video-comment' |
3fd3ab2d C |
87 | import { VideoFileModel } from './video-file' |
88 | import { VideoShareModel } from './video-share' | |
89 | import { VideoTagModel } from './video-tag' | |
2baea0c7 | 90 | import { ScheduleVideoUpdateModel } from './schedule-video-update' |
40e87e9e | 91 | import { VideoCaptionModel } from './video-caption' |
26b7305a | 92 | import { VideoBlacklistModel } from './video-blacklist' |
098eb377 | 93 | import { remove, writeFile } from 'fs-extra' |
9a629c6e | 94 | import { VideoViewModel } from './video-views' |
c48e82b5 | 95 | import { VideoRedundancyModel } from '../redundancy/video-redundancy' |
098eb377 C |
96 | import { |
97 | videoFilesModelToFormattedJSON, | |
98 | VideoFormattingJSONOptions, | |
99 | videoModelToActivityPubObject, | |
100 | videoModelToFormattedDetailsJSON, | |
101 | videoModelToFormattedJSON | |
102 | } from './video-format-utils' | |
6e46de09 | 103 | import { UserVideoHistoryModel } from '../account/user-video-history' |
7ad9b984 | 104 | import { UserModel } from '../account/user' |
dc133480 | 105 | import { VideoImportModel } from './video-import' |
09209296 | 106 | import { VideoStreamingPlaylistModel } from './video-streaming-playlist' |
418d092a | 107 | import { VideoPlaylistElementModel } from './video-playlist-element' |
6e46de09 | 108 | |
57c36b27 C |
109 | // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation |
110 | const indexes: Sequelize.DefineIndexesOptions[] = [ | |
111 | buildTrigramSearchIndex('video_name_trigram', 'name'), | |
112 | ||
8cd72bd3 C |
113 | { fields: [ 'createdAt' ] }, |
114 | { fields: [ 'publishedAt' ] }, | |
115 | { fields: [ 'duration' ] }, | |
8cd72bd3 | 116 | { fields: [ 'views' ] }, |
8cd72bd3 | 117 | { fields: [ 'channelId' ] }, |
7519127b C |
118 | { |
119 | fields: [ 'originallyPublishedAt' ], | |
120 | where: { | |
121 | originallyPublishedAt: { | |
122 | [Sequelize.Op.ne]: null | |
123 | } | |
124 | } | |
125 | }, | |
439b1744 C |
126 | { |
127 | fields: [ 'category' ], // We don't care videos with an unknown category | |
128 | where: { | |
129 | category: { | |
130 | [Sequelize.Op.ne]: null | |
131 | } | |
132 | } | |
133 | }, | |
134 | { | |
135 | fields: [ 'licence' ], // We don't care videos with an unknown licence | |
136 | where: { | |
137 | licence: { | |
138 | [Sequelize.Op.ne]: null | |
139 | } | |
140 | } | |
141 | }, | |
142 | { | |
143 | fields: [ 'language' ], // We don't care videos with an unknown language | |
144 | where: { | |
145 | language: { | |
146 | [Sequelize.Op.ne]: null | |
147 | } | |
148 | } | |
149 | }, | |
150 | { | |
151 | fields: [ 'nsfw' ], // Most of the videos are not NSFW | |
152 | where: { | |
153 | nsfw: true | |
154 | } | |
155 | }, | |
156 | { | |
157 | fields: [ 'remote' ], // Only index local videos | |
158 | where: { | |
159 | remote: false | |
160 | } | |
161 | }, | |
57c36b27 | 162 | { |
8cd72bd3 C |
163 | fields: [ 'uuid' ], |
164 | unique: true | |
57c36b27 C |
165 | }, |
166 | { | |
8ea6f49a | 167 | fields: [ 'url' ], |
57c36b27 C |
168 | unique: true |
169 | } | |
170 | ] | |
171 | ||
2baea0c7 | 172 | export enum ScopeNames { |
afd2cba5 C |
173 | AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS', |
174 | FOR_API = 'FOR_API', | |
4cb6d457 | 175 | WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS', |
d48ff09d | 176 | WITH_TAGS = 'WITH_TAGS', |
bbe0f064 | 177 | WITH_FILES = 'WITH_FILES', |
191764f3 | 178 | WITH_SCHEDULED_UPDATE = 'WITH_SCHEDULED_UPDATE', |
6e46de09 | 179 | WITH_BLACKLISTED = 'WITH_BLACKLISTED', |
09209296 C |
180 | WITH_USER_HISTORY = 'WITH_USER_HISTORY', |
181 | WITH_STREAMING_PLAYLISTS = 'WITH_STREAMING_PLAYLISTS', | |
182 | WITH_USER_ID = 'WITH_USER_ID' | |
d48ff09d C |
183 | } |
184 | ||
afd2cba5 C |
185 | type ForAPIOptions = { |
186 | ids: number[] | |
418d092a C |
187 | |
188 | videoPlaylistId?: number | |
189 | ||
afd2cba5 C |
190 | withFiles?: boolean |
191 | } | |
192 | ||
193 | type AvailableForListIDsOptions = { | |
7ad9b984 | 194 | serverAccountId: number |
4e74e803 | 195 | followerActorId: number |
afd2cba5 | 196 | includeLocalVideos: boolean |
418d092a | 197 | |
afd2cba5 C |
198 | filter?: VideoFilter |
199 | categoryOneOf?: number[] | |
200 | nsfw?: boolean | |
201 | licenceOneOf?: number[] | |
202 | languageOneOf?: string[] | |
203 | tagsOneOf?: string[] | |
204 | tagsAllOf?: string[] | |
418d092a | 205 | |
afd2cba5 | 206 | withFiles?: boolean |
418d092a | 207 | |
afd2cba5 | 208 | accountId?: number |
d525fc39 | 209 | videoChannelId?: number |
418d092a C |
210 | |
211 | videoPlaylistId?: number | |
212 | ||
9a629c6e | 213 | trendingDays?: number |
8b9a525a C |
214 | user?: UserModel, |
215 | historyOfUser?: UserModel | |
d525fc39 C |
216 | } |
217 | ||
d48ff09d | 218 | @Scopes({ |
8ea6f49a | 219 | [ ScopeNames.FOR_API ]: (options: ForAPIOptions) => { |
244e76a5 | 220 | const query: IFindOptions<VideoModel> = { |
afd2cba5 C |
221 | where: { |
222 | id: { | |
8ea6f49a | 223 | [ Sequelize.Op.any ]: options.ids |
afd2cba5 C |
224 | } |
225 | }, | |
418d092a C |
226 | include: [ |
227 | { | |
76564702 C |
228 | model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }), |
229 | required: true | |
418d092a C |
230 | } |
231 | ] | |
afd2cba5 C |
232 | } |
233 | ||
234 | if (options.withFiles === true) { | |
235 | query.include.push({ | |
236 | model: VideoFileModel.unscoped(), | |
237 | required: true | |
238 | }) | |
239 | } | |
240 | ||
418d092a C |
241 | if (options.videoPlaylistId) { |
242 | query.include.push({ | |
243 | model: VideoPlaylistElementModel.unscoped(), | |
15e9d5ca C |
244 | required: true, |
245 | where: { | |
246 | videoPlaylistId: options.videoPlaylistId | |
247 | } | |
418d092a C |
248 | }) |
249 | } | |
250 | ||
afd2cba5 C |
251 | return query |
252 | }, | |
8ea6f49a | 253 | [ ScopeNames.AVAILABLE_FOR_LIST_IDS ]: (options: AvailableForListIDsOptions) => { |
afd2cba5 | 254 | const query: IFindOptions<VideoModel> = { |
2b62cccd | 255 | raw: true, |
afd2cba5 | 256 | attributes: [ 'id' ], |
244e76a5 RK |
257 | where: { |
258 | id: { | |
8ea6f49a | 259 | [ Sequelize.Op.and ]: [ |
b6314e3c C |
260 | { |
261 | [ Sequelize.Op.notIn ]: Sequelize.literal( | |
262 | '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")' | |
263 | ) | |
264 | } | |
265 | ] | |
7ad9b984 C |
266 | }, |
267 | channelId: { | |
268 | [ Sequelize.Op.notIn ]: Sequelize.literal( | |
269 | '(' + | |
270 | 'SELECT id FROM "videoChannel" WHERE "accountId" IN (' + | |
271 | buildBlockedAccountSQL(options.serverAccountId, options.user ? options.user.Account.id : undefined) + | |
272 | ')' + | |
273 | ')' | |
274 | ) | |
1cd3facc C |
275 | } |
276 | }, | |
277 | include: [] | |
278 | } | |
279 | ||
280 | // Only list public/published videos | |
281 | if (!options.filter || options.filter !== 'all-local') { | |
282 | const privacyWhere = { | |
2186386c C |
283 | // Always list public videos |
284 | privacy: VideoPrivacy.PUBLIC, | |
285 | // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding | |
286 | [ Sequelize.Op.or ]: [ | |
287 | { | |
288 | state: VideoState.PUBLISHED | |
289 | }, | |
290 | { | |
291 | [ Sequelize.Op.and ]: { | |
292 | state: VideoState.TO_TRANSCODE, | |
293 | waitTranscoding: false | |
294 | } | |
295 | } | |
296 | ] | |
1cd3facc C |
297 | } |
298 | ||
299 | Object.assign(query.where, privacyWhere) | |
afd2cba5 C |
300 | } |
301 | ||
418d092a C |
302 | if (options.videoPlaylistId) { |
303 | query.include.push({ | |
304 | attributes: [], | |
305 | model: VideoPlaylistElementModel.unscoped(), | |
306 | required: true, | |
307 | where: { | |
308 | videoPlaylistId: options.videoPlaylistId | |
309 | } | |
310 | }) | |
15e9d5ca C |
311 | |
312 | query.subQuery = false | |
418d092a C |
313 | } |
314 | ||
afd2cba5 C |
315 | if (options.filter || options.accountId || options.videoChannelId) { |
316 | const videoChannelInclude: IIncludeOptions = { | |
317 | attributes: [], | |
318 | model: VideoChannelModel.unscoped(), | |
319 | required: true | |
320 | } | |
321 | ||
322 | if (options.videoChannelId) { | |
323 | videoChannelInclude.where = { | |
324 | id: options.videoChannelId | |
325 | } | |
326 | } | |
327 | ||
328 | if (options.filter || options.accountId) { | |
329 | const accountInclude: IIncludeOptions = { | |
330 | attributes: [], | |
331 | model: AccountModel.unscoped(), | |
332 | required: true | |
333 | } | |
334 | ||
335 | if (options.filter) { | |
336 | accountInclude.include = [ | |
337 | { | |
338 | attributes: [], | |
339 | model: ActorModel.unscoped(), | |
340 | required: true, | |
341 | where: VideoModel.buildActorWhereWithFilter(options.filter) | |
342 | } | |
343 | ] | |
344 | } | |
345 | ||
346 | if (options.accountId) { | |
347 | accountInclude.where = { id: options.accountId } | |
348 | } | |
349 | ||
350 | videoChannelInclude.include = [ accountInclude ] | |
351 | } | |
352 | ||
353 | query.include.push(videoChannelInclude) | |
244e76a5 RK |
354 | } |
355 | ||
4e74e803 | 356 | if (options.followerActorId) { |
687d638c C |
357 | let localVideosReq = '' |
358 | if (options.includeLocalVideos === true) { | |
359 | localVideosReq = ' UNION ALL ' + | |
360 | 'SELECT "video"."id" AS "id" FROM "video" ' + | |
361 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
362 | 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + | |
363 | 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' + | |
364 | 'WHERE "actor"."serverId" IS NULL' | |
365 | } | |
366 | ||
367 | // Force actorId to be a number to avoid SQL injections | |
4e74e803 | 368 | const actorIdNumber = parseInt(options.followerActorId.toString(), 10) |
8ea6f49a | 369 | query.where[ 'id' ][ Sequelize.Op.and ].push({ |
b6314e3c C |
370 | [ Sequelize.Op.in ]: Sequelize.literal( |
371 | '(' + | |
8ea6f49a C |
372 | 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' + |
373 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + | |
374 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + | |
375 | ' UNION ALL ' + | |
376 | 'SELECT "video"."id" AS "id" FROM "video" ' + | |
377 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
378 | 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + | |
379 | 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' + | |
380 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' + | |
381 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + | |
382 | localVideosReq + | |
b6314e3c C |
383 | ')' |
384 | ) | |
385 | }) | |
687d638c C |
386 | } |
387 | ||
48dce1c9 | 388 | if (options.withFiles === true) { |
8ea6f49a | 389 | query.where[ 'id' ][ Sequelize.Op.and ].push({ |
b6314e3c C |
390 | [ Sequelize.Op.in ]: Sequelize.literal( |
391 | '(SELECT "videoId" FROM "videoFile")' | |
392 | ) | |
244e76a5 RK |
393 | }) |
394 | } | |
395 | ||
d525fc39 C |
396 | // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN() |
397 | if (options.tagsAllOf || options.tagsOneOf) { | |
398 | const createTagsIn = (tags: string[]) => { | |
399 | return tags.map(t => VideoModel.sequelize.escape(t)) | |
400 | .join(', ') | |
401 | } | |
402 | ||
403 | if (options.tagsOneOf) { | |
8ea6f49a C |
404 | query.where[ 'id' ][ Sequelize.Op.and ].push({ |
405 | [ Sequelize.Op.in ]: Sequelize.literal( | |
b6314e3c | 406 | '(' + |
d525fc39 C |
407 | 'SELECT "videoId" FROM "videoTag" ' + |
408 | 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + | |
409 | 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsOneOf) + ')' + | |
b6314e3c C |
410 | ')' |
411 | ) | |
412 | }) | |
d525fc39 C |
413 | } |
414 | ||
415 | if (options.tagsAllOf) { | |
8ea6f49a C |
416 | query.where[ 'id' ][ Sequelize.Op.and ].push({ |
417 | [ Sequelize.Op.in ]: Sequelize.literal( | |
d525fc39 | 418 | '(' + |
b6314e3c C |
419 | 'SELECT "videoId" FROM "videoTag" ' + |
420 | 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + | |
421 | 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsAllOf) + ')' + | |
422 | 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length + | |
d525fc39 | 423 | ')' |
b6314e3c C |
424 | ) |
425 | }) | |
d525fc39 C |
426 | } |
427 | } | |
428 | ||
429 | if (options.nsfw === true || options.nsfw === false) { | |
8ea6f49a | 430 | query.where[ 'nsfw' ] = options.nsfw |
d525fc39 C |
431 | } |
432 | ||
433 | if (options.categoryOneOf) { | |
8ea6f49a C |
434 | query.where[ 'category' ] = { |
435 | [ Sequelize.Op.or ]: options.categoryOneOf | |
d525fc39 C |
436 | } |
437 | } | |
438 | ||
439 | if (options.licenceOneOf) { | |
8ea6f49a C |
440 | query.where[ 'licence' ] = { |
441 | [ Sequelize.Op.or ]: options.licenceOneOf | |
d525fc39 | 442 | } |
0883b324 C |
443 | } |
444 | ||
d525fc39 | 445 | if (options.languageOneOf) { |
8ea6f49a C |
446 | query.where[ 'language' ] = { |
447 | [ Sequelize.Op.or ]: options.languageOneOf | |
d525fc39 | 448 | } |
61b909b9 P |
449 | } |
450 | ||
9a629c6e | 451 | if (options.trendingDays) { |
b36f41ca | 452 | query.include.push(VideoModel.buildTrendingQuery(options.trendingDays)) |
9a629c6e C |
453 | |
454 | query.subQuery = false | |
455 | } | |
456 | ||
8b9a525a C |
457 | if (options.historyOfUser) { |
458 | query.include.push({ | |
459 | model: UserVideoHistoryModel, | |
460 | required: true, | |
461 | where: { | |
462 | userId: options.historyOfUser.id | |
463 | } | |
464 | }) | |
80bfd33c C |
465 | |
466 | // Even if the relation is n:m, we know that a user only have 0..1 video history | |
467 | // So we won't have multiple rows for the same video | |
468 | // Without this, we would not be able to sort on "updatedAt" column of UserVideoHistoryModel | |
469 | query.subQuery = false | |
8b9a525a C |
470 | } |
471 | ||
244e76a5 RK |
472 | return query |
473 | }, | |
09209296 C |
474 | [ ScopeNames.WITH_USER_ID ]: { |
475 | include: [ | |
476 | { | |
477 | attributes: [ 'accountId' ], | |
478 | model: () => VideoChannelModel.unscoped(), | |
479 | required: true, | |
480 | include: [ | |
481 | { | |
482 | attributes: [ 'userId' ], | |
483 | model: () => AccountModel.unscoped(), | |
484 | required: true | |
485 | } | |
486 | ] | |
487 | } | |
488 | ] | |
489 | }, | |
8ea6f49a | 490 | [ ScopeNames.WITH_ACCOUNT_DETAILS ]: { |
d48ff09d C |
491 | include: [ |
492 | { | |
6120941f | 493 | model: () => VideoChannelModel.unscoped(), |
d48ff09d C |
494 | required: true, |
495 | include: [ | |
6120941f C |
496 | { |
497 | attributes: { | |
498 | exclude: [ 'privateKey', 'publicKey' ] | |
499 | }, | |
3e500247 C |
500 | model: () => ActorModel.unscoped(), |
501 | required: true, | |
502 | include: [ | |
503 | { | |
504 | attributes: [ 'host' ], | |
505 | model: () => ServerModel.unscoped(), | |
506 | required: false | |
52d9f792 C |
507 | }, |
508 | { | |
509 | model: () => AvatarModel.unscoped(), | |
510 | required: false | |
3e500247 C |
511 | } |
512 | ] | |
6120941f | 513 | }, |
d48ff09d | 514 | { |
3e500247 | 515 | model: () => AccountModel.unscoped(), |
d48ff09d C |
516 | required: true, |
517 | include: [ | |
518 | { | |
3e500247 | 519 | model: () => ActorModel.unscoped(), |
6120941f C |
520 | attributes: { |
521 | exclude: [ 'privateKey', 'publicKey' ] | |
522 | }, | |
50d6de9c C |
523 | required: true, |
524 | include: [ | |
525 | { | |
3e500247 C |
526 | attributes: [ 'host' ], |
527 | model: () => ServerModel.unscoped(), | |
50d6de9c | 528 | required: false |
b6a4fd6b C |
529 | }, |
530 | { | |
531 | model: () => AvatarModel.unscoped(), | |
532 | required: false | |
50d6de9c C |
533 | } |
534 | ] | |
d48ff09d C |
535 | } |
536 | ] | |
537 | } | |
538 | ] | |
539 | } | |
540 | ] | |
541 | }, | |
8ea6f49a | 542 | [ ScopeNames.WITH_TAGS ]: { |
d48ff09d C |
543 | include: [ () => TagModel ] |
544 | }, | |
8ea6f49a | 545 | [ ScopeNames.WITH_BLACKLISTED ]: { |
191764f3 C |
546 | include: [ |
547 | { | |
548 | attributes: [ 'id', 'reason' ], | |
549 | model: () => VideoBlacklistModel, | |
550 | required: false | |
551 | } | |
552 | ] | |
553 | }, | |
09209296 C |
554 | [ ScopeNames.WITH_FILES ]: (withRedundancies = false) => { |
555 | let subInclude: any[] = [] | |
556 | ||
557 | if (withRedundancies === true) { | |
558 | subInclude = [ | |
559 | { | |
560 | attributes: [ 'fileUrl' ], | |
561 | model: VideoRedundancyModel.unscoped(), | |
562 | required: false | |
563 | } | |
564 | ] | |
565 | } | |
566 | ||
567 | return { | |
568 | include: [ | |
569 | { | |
570 | model: VideoFileModel.unscoped(), | |
571 | // FIXME: typings | |
572 | [ 'separate' as any ]: true, // We may have multiple files, having multiple redundancies so let's separate this join | |
573 | required: false, | |
574 | include: subInclude | |
575 | } | |
576 | ] | |
577 | } | |
578 | }, | |
579 | [ ScopeNames.WITH_STREAMING_PLAYLISTS ]: (withRedundancies = false) => { | |
580 | let subInclude: any[] = [] | |
581 | ||
582 | if (withRedundancies === true) { | |
583 | subInclude = [ | |
584 | { | |
585 | attributes: [ 'fileUrl' ], | |
586 | model: VideoRedundancyModel.unscoped(), | |
587 | required: false | |
588 | } | |
589 | ] | |
590 | } | |
591 | ||
592 | return { | |
593 | include: [ | |
594 | { | |
595 | model: VideoStreamingPlaylistModel.unscoped(), | |
596 | // FIXME: typings | |
597 | [ 'separate' as any ]: true, // We may have multiple streaming playlists, having multiple redundancies so let's separate this join | |
598 | required: false, | |
599 | include: subInclude | |
600 | } | |
601 | ] | |
602 | } | |
bbe0f064 | 603 | }, |
8ea6f49a | 604 | [ ScopeNames.WITH_SCHEDULED_UPDATE ]: { |
bbe0f064 C |
605 | include: [ |
606 | { | |
607 | model: () => ScheduleVideoUpdateModel.unscoped(), | |
608 | required: false | |
609 | } | |
610 | ] | |
6e46de09 C |
611 | }, |
612 | [ ScopeNames.WITH_USER_HISTORY ]: (userId: number) => { | |
613 | return { | |
614 | include: [ | |
615 | { | |
616 | attributes: [ 'currentTime' ], | |
617 | model: UserVideoHistoryModel.unscoped(), | |
618 | required: false, | |
619 | where: { | |
620 | userId | |
621 | } | |
622 | } | |
623 | ] | |
624 | } | |
d48ff09d C |
625 | } |
626 | }) | |
3fd3ab2d C |
627 | @Table({ |
628 | tableName: 'video', | |
57c36b27 | 629 | indexes |
3fd3ab2d C |
630 | }) |
631 | export class VideoModel extends Model<VideoModel> { | |
632 | ||
633 | @AllowNull(false) | |
634 | @Default(DataType.UUIDV4) | |
635 | @IsUUID(4) | |
636 | @Column(DataType.UUID) | |
637 | uuid: string | |
638 | ||
639 | @AllowNull(false) | |
640 | @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name')) | |
641 | @Column | |
642 | name: string | |
643 | ||
644 | @AllowNull(true) | |
645 | @Default(null) | |
646 | @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category')) | |
647 | @Column | |
648 | category: number | |
649 | ||
650 | @AllowNull(true) | |
651 | @Default(null) | |
652 | @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence')) | |
653 | @Column | |
654 | licence: number | |
655 | ||
656 | @AllowNull(true) | |
657 | @Default(null) | |
658 | @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language')) | |
9d3ef9fe C |
659 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max)) |
660 | language: string | |
3fd3ab2d C |
661 | |
662 | @AllowNull(false) | |
663 | @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy')) | |
664 | @Column | |
665 | privacy: number | |
666 | ||
667 | @AllowNull(false) | |
47564bbe | 668 | @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean')) |
3fd3ab2d C |
669 | @Column |
670 | nsfw: boolean | |
671 | ||
672 | @AllowNull(true) | |
673 | @Default(null) | |
674 | @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description')) | |
675 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max)) | |
676 | description: string | |
677 | ||
2422c46b C |
678 | @AllowNull(true) |
679 | @Default(null) | |
680 | @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support')) | |
681 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max)) | |
682 | support: string | |
683 | ||
3fd3ab2d C |
684 | @AllowNull(false) |
685 | @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration')) | |
686 | @Column | |
687 | duration: number | |
688 | ||
689 | @AllowNull(false) | |
690 | @Default(0) | |
691 | @IsInt | |
692 | @Min(0) | |
693 | @Column | |
694 | views: number | |
695 | ||
696 | @AllowNull(false) | |
697 | @Default(0) | |
698 | @IsInt | |
699 | @Min(0) | |
700 | @Column | |
701 | likes: number | |
702 | ||
703 | @AllowNull(false) | |
704 | @Default(0) | |
705 | @IsInt | |
706 | @Min(0) | |
707 | @Column | |
708 | dislikes: number | |
709 | ||
710 | @AllowNull(false) | |
711 | @Column | |
712 | remote: boolean | |
713 | ||
714 | @AllowNull(false) | |
715 | @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
716 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max)) | |
717 | url: string | |
718 | ||
47564bbe C |
719 | @AllowNull(false) |
720 | @Column | |
721 | commentsEnabled: boolean | |
722 | ||
156c50af LD |
723 | @AllowNull(false) |
724 | @Column | |
7f2cfe3a | 725 | downloadEnabled: boolean |
156c50af | 726 | |
2186386c C |
727 | @AllowNull(false) |
728 | @Column | |
729 | waitTranscoding: boolean | |
730 | ||
731 | @AllowNull(false) | |
732 | @Default(null) | |
733 | @Is('VideoState', value => throwIfNotValid(value, isVideoStateValid, 'state')) | |
734 | @Column | |
735 | state: VideoState | |
736 | ||
3fd3ab2d C |
737 | @CreatedAt |
738 | createdAt: Date | |
739 | ||
740 | @UpdatedAt | |
741 | updatedAt: Date | |
742 | ||
2922e048 JLB |
743 | @AllowNull(false) |
744 | @Default(Sequelize.NOW) | |
745 | @Column | |
746 | publishedAt: Date | |
747 | ||
7519127b C |
748 | @AllowNull(true) |
749 | @Default(null) | |
c8034165 | 750 | @Column |
751 | originallyPublishedAt: Date | |
752 | ||
3fd3ab2d C |
753 | @ForeignKey(() => VideoChannelModel) |
754 | @Column | |
755 | channelId: number | |
756 | ||
757 | @BelongsTo(() => VideoChannelModel, { | |
feb4bdfd | 758 | foreignKey: { |
50d6de9c | 759 | allowNull: true |
feb4bdfd | 760 | }, |
6b738c7a | 761 | hooks: true |
feb4bdfd | 762 | }) |
3fd3ab2d | 763 | VideoChannel: VideoChannelModel |
7920c273 | 764 | |
3fd3ab2d | 765 | @BelongsToMany(() => TagModel, { |
7920c273 | 766 | foreignKey: 'videoId', |
3fd3ab2d C |
767 | through: () => VideoTagModel, |
768 | onDelete: 'CASCADE' | |
7920c273 | 769 | }) |
3fd3ab2d | 770 | Tags: TagModel[] |
55fa55a9 | 771 | |
418d092a C |
772 | @HasMany(() => VideoPlaylistElementModel, { |
773 | foreignKey: { | |
774 | name: 'videoId', | |
775 | allowNull: false | |
776 | }, | |
777 | onDelete: 'cascade' | |
778 | }) | |
779 | VideoPlaylistElements: VideoPlaylistElementModel[] | |
780 | ||
3fd3ab2d | 781 | @HasMany(() => VideoAbuseModel, { |
55fa55a9 C |
782 | foreignKey: { |
783 | name: 'videoId', | |
784 | allowNull: false | |
785 | }, | |
786 | onDelete: 'cascade' | |
787 | }) | |
3fd3ab2d | 788 | VideoAbuses: VideoAbuseModel[] |
93e1258c | 789 | |
3fd3ab2d | 790 | @HasMany(() => VideoFileModel, { |
93e1258c C |
791 | foreignKey: { |
792 | name: 'videoId', | |
793 | allowNull: false | |
794 | }, | |
c48e82b5 | 795 | hooks: true, |
93e1258c C |
796 | onDelete: 'cascade' |
797 | }) | |
3fd3ab2d | 798 | VideoFiles: VideoFileModel[] |
e71bcc0f | 799 | |
09209296 C |
800 | @HasMany(() => VideoStreamingPlaylistModel, { |
801 | foreignKey: { | |
802 | name: 'videoId', | |
803 | allowNull: false | |
804 | }, | |
805 | hooks: true, | |
806 | onDelete: 'cascade' | |
807 | }) | |
808 | VideoStreamingPlaylists: VideoStreamingPlaylistModel[] | |
809 | ||
3fd3ab2d | 810 | @HasMany(() => VideoShareModel, { |
e71bcc0f C |
811 | foreignKey: { |
812 | name: 'videoId', | |
813 | allowNull: false | |
814 | }, | |
815 | onDelete: 'cascade' | |
816 | }) | |
3fd3ab2d | 817 | VideoShares: VideoShareModel[] |
16b90975 | 818 | |
3fd3ab2d | 819 | @HasMany(() => AccountVideoRateModel, { |
16b90975 C |
820 | foreignKey: { |
821 | name: 'videoId', | |
822 | allowNull: false | |
823 | }, | |
824 | onDelete: 'cascade' | |
825 | }) | |
3fd3ab2d | 826 | AccountVideoRates: AccountVideoRateModel[] |
f285faa0 | 827 | |
da854ddd C |
828 | @HasMany(() => VideoCommentModel, { |
829 | foreignKey: { | |
830 | name: 'videoId', | |
831 | allowNull: false | |
832 | }, | |
f05a1c30 C |
833 | onDelete: 'cascade', |
834 | hooks: true | |
da854ddd C |
835 | }) |
836 | VideoComments: VideoCommentModel[] | |
837 | ||
9a629c6e C |
838 | @HasMany(() => VideoViewModel, { |
839 | foreignKey: { | |
840 | name: 'videoId', | |
841 | allowNull: false | |
842 | }, | |
6e46de09 | 843 | onDelete: 'cascade' |
9a629c6e C |
844 | }) |
845 | VideoViews: VideoViewModel[] | |
846 | ||
6e46de09 C |
847 | @HasMany(() => UserVideoHistoryModel, { |
848 | foreignKey: { | |
849 | name: 'videoId', | |
850 | allowNull: false | |
851 | }, | |
852 | onDelete: 'cascade' | |
853 | }) | |
854 | UserVideoHistories: UserVideoHistoryModel[] | |
855 | ||
2baea0c7 C |
856 | @HasOne(() => ScheduleVideoUpdateModel, { |
857 | foreignKey: { | |
858 | name: 'videoId', | |
859 | allowNull: false | |
860 | }, | |
861 | onDelete: 'cascade' | |
862 | }) | |
863 | ScheduleVideoUpdate: ScheduleVideoUpdateModel | |
864 | ||
26b7305a C |
865 | @HasOne(() => VideoBlacklistModel, { |
866 | foreignKey: { | |
867 | name: 'videoId', | |
868 | allowNull: false | |
869 | }, | |
870 | onDelete: 'cascade' | |
871 | }) | |
872 | VideoBlacklist: VideoBlacklistModel | |
873 | ||
dc133480 C |
874 | @HasOne(() => VideoImportModel, { |
875 | foreignKey: { | |
876 | name: 'videoId', | |
877 | allowNull: true | |
878 | }, | |
879 | onDelete: 'set null' | |
880 | }) | |
881 | VideoImport: VideoImportModel | |
882 | ||
40e87e9e C |
883 | @HasMany(() => VideoCaptionModel, { |
884 | foreignKey: { | |
885 | name: 'videoId', | |
886 | allowNull: false | |
887 | }, | |
888 | onDelete: 'cascade', | |
889 | hooks: true, | |
8ea6f49a | 890 | [ 'separate' as any ]: true |
40e87e9e C |
891 | }) |
892 | VideoCaptions: VideoCaptionModel[] | |
893 | ||
f05a1c30 C |
894 | @BeforeDestroy |
895 | static async sendDelete (instance: VideoModel, options) { | |
896 | if (instance.isOwned()) { | |
897 | if (!instance.VideoChannel) { | |
898 | instance.VideoChannel = await instance.$get('VideoChannel', { | |
899 | include: [ | |
900 | { | |
901 | model: AccountModel, | |
902 | include: [ ActorModel ] | |
903 | } | |
904 | ], | |
905 | transaction: options.transaction | |
906 | }) as VideoChannelModel | |
907 | } | |
908 | ||
f05a1c30 C |
909 | return sendDeleteVideo(instance, options.transaction) |
910 | } | |
911 | ||
912 | return undefined | |
913 | } | |
914 | ||
6b738c7a | 915 | @BeforeDestroy |
40e87e9e | 916 | static async removeFiles (instance: VideoModel) { |
f05a1c30 | 917 | const tasks: Promise<any>[] = [] |
f285faa0 | 918 | |
8e0fd45e | 919 | logger.info('Removing files of video %s.', instance.url) |
6b738c7a | 920 | |
f05a1c30 | 921 | tasks.push(instance.removeThumbnail()) |
93e1258c | 922 | |
3fd3ab2d | 923 | if (instance.isOwned()) { |
f05a1c30 C |
924 | if (!Array.isArray(instance.VideoFiles)) { |
925 | instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[] | |
926 | } | |
927 | ||
928 | tasks.push(instance.removePreview()) | |
40298b02 | 929 | |
3fd3ab2d C |
930 | // Remove physical files and torrents |
931 | instance.VideoFiles.forEach(file => { | |
932 | tasks.push(instance.removeFile(file)) | |
933 | tasks.push(instance.removeTorrent(file)) | |
934 | }) | |
09209296 C |
935 | |
936 | // Remove playlists file | |
937 | tasks.push(instance.removeStreamingPlaylist()) | |
3fd3ab2d | 938 | } |
40298b02 | 939 | |
6b738c7a C |
940 | // Do not wait video deletion because we could be in a transaction |
941 | Promise.all(tasks) | |
8ea6f49a C |
942 | .catch(err => { |
943 | logger.error('Some errors when removing files of video %s in before destroy hook.', instance.uuid, { err }) | |
944 | }) | |
6b738c7a C |
945 | |
946 | return undefined | |
3fd3ab2d | 947 | } |
f285faa0 | 948 | |
9f1ddd24 C |
949 | static listLocal () { |
950 | const query = { | |
951 | where: { | |
952 | remote: false | |
953 | } | |
954 | } | |
955 | ||
09209296 | 956 | return VideoModel.scope([ ScopeNames.WITH_FILES, ScopeNames.WITH_STREAMING_PLAYLISTS ]).findAll(query) |
9f1ddd24 C |
957 | } |
958 | ||
50d6de9c | 959 | static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) { |
3fd3ab2d C |
960 | function getRawQuery (select: string) { |
961 | const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' + | |
962 | 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + | |
50d6de9c C |
963 | 'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' + |
964 | 'WHERE "Account"."actorId" = ' + actorId | |
3fd3ab2d C |
965 | const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' + |
966 | 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' + | |
50d6de9c | 967 | 'WHERE "VideoShare"."actorId" = ' + actorId |
558d7c23 | 968 | |
3fd3ab2d C |
969 | return `(${queryVideo}) UNION (${queryVideoShare})` |
970 | } | |
aaf61f38 | 971 | |
3fd3ab2d C |
972 | const rawQuery = getRawQuery('"Video"."id"') |
973 | const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"') | |
974 | ||
975 | const query = { | |
976 | distinct: true, | |
977 | offset: start, | |
978 | limit: count, | |
9a629c6e | 979 | order: getVideoSort('createdAt', [ 'Tags', 'name', 'ASC' ]), |
3fd3ab2d C |
980 | where: { |
981 | id: { | |
8ea6f49a | 982 | [ Sequelize.Op.in ]: Sequelize.literal('(' + rawQuery + ')') |
3c75ce12 | 983 | }, |
8ea6f49a | 984 | [ Sequelize.Op.or ]: [ |
3c75ce12 C |
985 | { privacy: VideoPrivacy.PUBLIC }, |
986 | { privacy: VideoPrivacy.UNLISTED } | |
987 | ] | |
3fd3ab2d C |
988 | }, |
989 | include: [ | |
40e87e9e C |
990 | { |
991 | attributes: [ 'language' ], | |
992 | model: VideoCaptionModel.unscoped(), | |
993 | required: false | |
994 | }, | |
3fd3ab2d | 995 | { |
1d230c44 | 996 | attributes: [ 'id', 'url' ], |
2c897999 | 997 | model: VideoShareModel.unscoped(), |
3fd3ab2d | 998 | required: false, |
e3d5ea4f C |
999 | // We only want videos shared by this actor |
1000 | where: { | |
8ea6f49a | 1001 | [ Sequelize.Op.and ]: [ |
e3d5ea4f C |
1002 | { |
1003 | id: { | |
8ea6f49a | 1004 | [ Sequelize.Op.not ]: null |
e3d5ea4f C |
1005 | } |
1006 | }, | |
1007 | { | |
1008 | actorId | |
1009 | } | |
1010 | ] | |
1011 | }, | |
50d6de9c C |
1012 | include: [ |
1013 | { | |
2c897999 C |
1014 | attributes: [ 'id', 'url' ], |
1015 | model: ActorModel.unscoped() | |
50d6de9c C |
1016 | } |
1017 | ] | |
3fd3ab2d C |
1018 | }, |
1019 | { | |
2c897999 | 1020 | model: VideoChannelModel.unscoped(), |
3fd3ab2d C |
1021 | required: true, |
1022 | include: [ | |
1023 | { | |
2c897999 C |
1024 | attributes: [ 'name' ], |
1025 | model: AccountModel.unscoped(), | |
1026 | required: true, | |
1027 | include: [ | |
1028 | { | |
e3d5ea4f | 1029 | attributes: [ 'id', 'url', 'followersUrl' ], |
2c897999 C |
1030 | model: ActorModel.unscoped(), |
1031 | required: true | |
1032 | } | |
1033 | ] | |
1034 | }, | |
1035 | { | |
e3d5ea4f | 1036 | attributes: [ 'id', 'url', 'followersUrl' ], |
2c897999 | 1037 | model: ActorModel.unscoped(), |
3fd3ab2d C |
1038 | required: true |
1039 | } | |
1040 | ] | |
1041 | }, | |
3fd3ab2d | 1042 | VideoFileModel, |
2c897999 | 1043 | TagModel |
3fd3ab2d C |
1044 | ] |
1045 | } | |
164174a6 | 1046 | |
3fd3ab2d C |
1047 | return Bluebird.all([ |
1048 | // FIXME: typing issue | |
1049 | VideoModel.findAll(query as any), | |
1050 | VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT }) | |
1051 | ]).then(([ rows, totals ]) => { | |
1052 | // totals: totalVideos + totalVideoShares | |
1053 | let totalVideos = 0 | |
1054 | let totalVideoShares = 0 | |
8ea6f49a C |
1055 | if (totals[ 0 ]) totalVideos = parseInt(totals[ 0 ].total, 10) |
1056 | if (totals[ 1 ]) totalVideoShares = parseInt(totals[ 1 ].total, 10) | |
3fd3ab2d C |
1057 | |
1058 | const total = totalVideos + totalVideoShares | |
1059 | return { | |
1060 | data: rows, | |
1061 | total: total | |
1062 | } | |
1063 | }) | |
1064 | } | |
93e1258c | 1065 | |
26b7305a | 1066 | static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) { |
244e76a5 | 1067 | const query: IFindOptions<VideoModel> = { |
3fd3ab2d C |
1068 | offset: start, |
1069 | limit: count, | |
9a629c6e | 1070 | order: getVideoSort(sort), |
3fd3ab2d C |
1071 | include: [ |
1072 | { | |
1073 | model: VideoChannelModel, | |
1074 | required: true, | |
1075 | include: [ | |
1076 | { | |
1077 | model: AccountModel, | |
1078 | where: { | |
7b87d2d5 | 1079 | id: accountId |
3fd3ab2d C |
1080 | }, |
1081 | required: true | |
1082 | } | |
1083 | ] | |
2baea0c7 C |
1084 | }, |
1085 | { | |
1086 | model: ScheduleVideoUpdateModel, | |
1087 | required: false | |
26b7305a C |
1088 | }, |
1089 | { | |
1090 | model: VideoBlacklistModel, | |
1091 | required: false | |
d48ff09d | 1092 | } |
3fd3ab2d C |
1093 | ] |
1094 | } | |
d8755eed | 1095 | |
244e76a5 RK |
1096 | if (withFiles === true) { |
1097 | query.include.push({ | |
1098 | model: VideoFileModel.unscoped(), | |
1099 | required: true | |
1100 | }) | |
1101 | } | |
1102 | ||
3fd3ab2d C |
1103 | return VideoModel.findAndCountAll(query).then(({ rows, count }) => { |
1104 | return { | |
1105 | data: rows, | |
1106 | total: count | |
1107 | } | |
1108 | }) | |
1109 | } | |
93e1258c | 1110 | |
48dce1c9 | 1111 | static async listForApi (options: { |
0626e7af C |
1112 | start: number, |
1113 | count: number, | |
1114 | sort: string, | |
d525fc39 | 1115 | nsfw: boolean, |
06a05d5f | 1116 | includeLocalVideos: boolean, |
48dce1c9 | 1117 | withFiles: boolean, |
d525fc39 C |
1118 | categoryOneOf?: number[], |
1119 | licenceOneOf?: number[], | |
1120 | languageOneOf?: string[], | |
1121 | tagsOneOf?: string[], | |
1122 | tagsAllOf?: string[], | |
0626e7af | 1123 | filter?: VideoFilter, |
48dce1c9 | 1124 | accountId?: number, |
06a05d5f | 1125 | videoChannelId?: number, |
4e74e803 | 1126 | followerActorId?: number |
418d092a | 1127 | videoPlaylistId?: number, |
6e46de09 | 1128 | trendingDays?: number, |
8b9a525a C |
1129 | user?: UserModel, |
1130 | historyOfUser?: UserModel | |
7348b1fd | 1131 | }, countVideos = true) { |
7ad9b984 C |
1132 | if (options.filter && options.filter === 'all-local' && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) { |
1133 | throw new Error('Try to filter all-local but no user has not the see all videos right') | |
1cd3facc C |
1134 | } |
1135 | ||
9a629c6e | 1136 | const query: IFindOptions<VideoModel> = { |
48dce1c9 C |
1137 | offset: options.start, |
1138 | limit: options.count, | |
9a629c6e C |
1139 | order: getVideoSort(options.sort) |
1140 | } | |
1141 | ||
1142 | let trendingDays: number | |
1143 | if (options.sort.endsWith('trending')) { | |
1144 | trendingDays = CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS | |
1145 | ||
1146 | query.group = 'VideoModel.id' | |
3fd3ab2d | 1147 | } |
93e1258c | 1148 | |
7ad9b984 C |
1149 | const serverActor = await getServerActor() |
1150 | ||
4e74e803 C |
1151 | // followerActorId === null has a meaning, so just check undefined |
1152 | const followerActorId = options.followerActorId !== undefined ? options.followerActorId : serverActor.id | |
06a05d5f | 1153 | |
afd2cba5 | 1154 | const queryOptions = { |
4e74e803 | 1155 | followerActorId, |
7ad9b984 | 1156 | serverAccountId: serverActor.Account.id, |
afd2cba5 C |
1157 | nsfw: options.nsfw, |
1158 | categoryOneOf: options.categoryOneOf, | |
1159 | licenceOneOf: options.licenceOneOf, | |
1160 | languageOneOf: options.languageOneOf, | |
1161 | tagsOneOf: options.tagsOneOf, | |
1162 | tagsAllOf: options.tagsAllOf, | |
1163 | filter: options.filter, | |
1164 | withFiles: options.withFiles, | |
1165 | accountId: options.accountId, | |
1166 | videoChannelId: options.videoChannelId, | |
418d092a | 1167 | videoPlaylistId: options.videoPlaylistId, |
9a629c6e | 1168 | includeLocalVideos: options.includeLocalVideos, |
7ad9b984 | 1169 | user: options.user, |
8b9a525a | 1170 | historyOfUser: options.historyOfUser, |
9a629c6e | 1171 | trendingDays |
48dce1c9 C |
1172 | } |
1173 | ||
7348b1fd | 1174 | return VideoModel.getAvailableForApi(query, queryOptions, countVideos) |
93e1258c C |
1175 | } |
1176 | ||
0b18f4aa | 1177 | static async searchAndPopulateAccountAndServer (options: { |
06a05d5f | 1178 | includeLocalVideos: boolean |
d4112450 | 1179 | search?: string |
0b18f4aa C |
1180 | start?: number |
1181 | count?: number | |
1182 | sort?: string | |
1183 | startDate?: string // ISO 8601 | |
1184 | endDate?: string // ISO 8601 | |
31d065cc AM |
1185 | originallyPublishedStartDate?: string |
1186 | originallyPublishedEndDate?: string | |
0b18f4aa C |
1187 | nsfw?: boolean |
1188 | categoryOneOf?: number[] | |
1189 | licenceOneOf?: number[] | |
1190 | languageOneOf?: string[] | |
1191 | tagsOneOf?: string[] | |
1192 | tagsAllOf?: string[] | |
1193 | durationMin?: number // seconds | |
1194 | durationMax?: number // seconds | |
7ad9b984 | 1195 | user?: UserModel, |
1cd3facc | 1196 | filter?: VideoFilter |
0b18f4aa | 1197 | }) { |
8ea6f49a | 1198 | const whereAnd = [] |
d525fc39 C |
1199 | |
1200 | if (options.startDate || options.endDate) { | |
8ea6f49a | 1201 | const publishedAtRange = {} |
d525fc39 | 1202 | |
8ea6f49a C |
1203 | if (options.startDate) publishedAtRange[ Sequelize.Op.gte ] = options.startDate |
1204 | if (options.endDate) publishedAtRange[ Sequelize.Op.lte ] = options.endDate | |
d525fc39 C |
1205 | |
1206 | whereAnd.push({ publishedAt: publishedAtRange }) | |
1207 | } | |
1208 | ||
31d065cc AM |
1209 | if (options.originallyPublishedStartDate || options.originallyPublishedEndDate) { |
1210 | const originallyPublishedAtRange = {} | |
1211 | ||
1212 | if (options.originallyPublishedStartDate) originallyPublishedAtRange[ Sequelize.Op.gte ] = options.originallyPublishedStartDate | |
1213 | if (options.originallyPublishedEndDate) originallyPublishedAtRange[ Sequelize.Op.lte ] = options.originallyPublishedEndDate | |
1214 | ||
1215 | whereAnd.push({ originallyPublishedAt: originallyPublishedAtRange }) | |
1216 | } | |
1217 | ||
d525fc39 | 1218 | if (options.durationMin || options.durationMax) { |
8ea6f49a | 1219 | const durationRange = {} |
d525fc39 | 1220 | |
8ea6f49a C |
1221 | if (options.durationMin) durationRange[ Sequelize.Op.gte ] = options.durationMin |
1222 | if (options.durationMax) durationRange[ Sequelize.Op.lte ] = options.durationMax | |
d525fc39 C |
1223 | |
1224 | whereAnd.push({ duration: durationRange }) | |
1225 | } | |
1226 | ||
d4112450 | 1227 | const attributesInclude = [] |
dbfd3e9b C |
1228 | const escapedSearch = VideoModel.sequelize.escape(options.search) |
1229 | const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%') | |
d4112450 C |
1230 | if (options.search) { |
1231 | whereAnd.push( | |
1232 | { | |
dbfd3e9b C |
1233 | id: { |
1234 | [ Sequelize.Op.in ]: Sequelize.literal( | |
1235 | '(' + | |
8ea6f49a C |
1236 | 'SELECT "video"."id" FROM "video" ' + |
1237 | 'WHERE ' + | |
1238 | 'lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' + | |
1239 | 'lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' + | |
1240 | 'UNION ALL ' + | |
1241 | 'SELECT "video"."id" FROM "video" LEFT JOIN "videoTag" ON "videoTag"."videoId" = "video"."id" ' + | |
1242 | 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + | |
1243 | 'WHERE "tag"."name" = ' + escapedSearch + | |
dbfd3e9b C |
1244 | ')' |
1245 | ) | |
1246 | } | |
d4112450 C |
1247 | } |
1248 | ) | |
1249 | ||
1250 | attributesInclude.push(createSimilarityAttribute('VideoModel.name', options.search)) | |
1251 | } | |
1252 | ||
1253 | // Cannot search on similarity if we don't have a search | |
1254 | if (!options.search) { | |
1255 | attributesInclude.push( | |
1256 | Sequelize.literal('0 as similarity') | |
1257 | ) | |
1258 | } | |
d525fc39 | 1259 | |
f05a1c30 | 1260 | const query: IFindOptions<VideoModel> = { |
57c36b27 | 1261 | attributes: { |
d4112450 | 1262 | include: attributesInclude |
57c36b27 | 1263 | }, |
d525fc39 C |
1264 | offset: options.start, |
1265 | limit: options.count, | |
9a629c6e | 1266 | order: getVideoSort(options.sort), |
d525fc39 C |
1267 | where: { |
1268 | [ Sequelize.Op.and ]: whereAnd | |
1269 | } | |
f05a1c30 C |
1270 | } |
1271 | ||
1272 | const serverActor = await getServerActor() | |
afd2cba5 | 1273 | const queryOptions = { |
4e74e803 | 1274 | followerActorId: serverActor.id, |
7ad9b984 | 1275 | serverAccountId: serverActor.Account.id, |
afd2cba5 C |
1276 | includeLocalVideos: options.includeLocalVideos, |
1277 | nsfw: options.nsfw, | |
1278 | categoryOneOf: options.categoryOneOf, | |
1279 | licenceOneOf: options.licenceOneOf, | |
1280 | languageOneOf: options.languageOneOf, | |
1281 | tagsOneOf: options.tagsOneOf, | |
6e46de09 | 1282 | tagsAllOf: options.tagsAllOf, |
7ad9b984 | 1283 | user: options.user, |
1cd3facc | 1284 | filter: options.filter |
48dce1c9 | 1285 | } |
f05a1c30 | 1286 | |
afd2cba5 | 1287 | return VideoModel.getAvailableForApi(query, queryOptions) |
f05a1c30 C |
1288 | } |
1289 | ||
627621c1 | 1290 | static load (id: number | string, t?: Sequelize.Transaction) { |
418d092a | 1291 | const where = buildWhereIdOrUUID(id) |
627621c1 C |
1292 | const options = { |
1293 | where, | |
1294 | transaction: t | |
3fd3ab2d | 1295 | } |
d8755eed | 1296 | |
627621c1 | 1297 | return VideoModel.findOne(options) |
3fd3ab2d | 1298 | } |
d8755eed | 1299 | |
09209296 | 1300 | static loadWithRights (id: number | string, t?: Sequelize.Transaction) { |
418d092a | 1301 | const where = buildWhereIdOrUUID(id) |
09209296 C |
1302 | const options = { |
1303 | where, | |
1304 | transaction: t | |
1305 | } | |
1306 | ||
1307 | return VideoModel.scope([ ScopeNames.WITH_BLACKLISTED, ScopeNames.WITH_USER_ID ]).findOne(options) | |
1308 | } | |
1309 | ||
627621c1 | 1310 | static loadOnlyId (id: number | string, t?: Sequelize.Transaction) { |
418d092a | 1311 | const where = buildWhereIdOrUUID(id) |
627621c1 | 1312 | |
3fd3ab2d | 1313 | const options = { |
627621c1 C |
1314 | attributes: [ 'id' ], |
1315 | where, | |
1316 | transaction: t | |
3fd3ab2d | 1317 | } |
72c7248b | 1318 | |
627621c1 C |
1319 | return VideoModel.findOne(options) |
1320 | } | |
1321 | ||
09209296 C |
1322 | static loadWithFiles (id: number, t?: Sequelize.Transaction, logging?: boolean) { |
1323 | return VideoModel.scope([ ScopeNames.WITH_FILES, ScopeNames.WITH_STREAMING_PLAYLISTS ]) | |
9b39106d | 1324 | .findByPk(id, { transaction: t, logging }) |
3fd3ab2d | 1325 | } |
72c7248b | 1326 | |
627621c1 | 1327 | static loadByUUIDWithFile (uuid: string) { |
8fa5653a C |
1328 | const options = { |
1329 | where: { | |
1330 | uuid | |
1331 | } | |
1332 | } | |
1333 | ||
09209296 | 1334 | return VideoModel.findOne(options) |
8fa5653a C |
1335 | } |
1336 | ||
4157cdb1 | 1337 | static loadByUrl (url: string, transaction?: Sequelize.Transaction) { |
627621c1 C |
1338 | const query: IFindOptions<VideoModel> = { |
1339 | where: { | |
1340 | url | |
4157cdb1 C |
1341 | }, |
1342 | transaction | |
627621c1 C |
1343 | } |
1344 | ||
4157cdb1 C |
1345 | return VideoModel.findOne(query) |
1346 | } | |
1347 | ||
1348 | static loadByUrlAndPopulateAccount (url: string, transaction?: Sequelize.Transaction) { | |
1349 | const query: IFindOptions<VideoModel> = { | |
1350 | where: { | |
1351 | url | |
1352 | }, | |
1353 | transaction | |
1354 | } | |
627621c1 | 1355 | |
09209296 C |
1356 | return VideoModel.scope([ |
1357 | ScopeNames.WITH_ACCOUNT_DETAILS, | |
1358 | ScopeNames.WITH_FILES, | |
1359 | ScopeNames.WITH_STREAMING_PLAYLISTS | |
1360 | ]).findOne(query) | |
627621c1 C |
1361 | } |
1362 | ||
6e46de09 | 1363 | static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Sequelize.Transaction, userId?: number) { |
418d092a | 1364 | const where = buildWhereIdOrUUID(id) |
627621c1 | 1365 | |
3fd3ab2d C |
1366 | const options = { |
1367 | order: [ [ 'Tags', 'name', 'ASC' ] ], | |
627621c1 | 1368 | where, |
2186386c | 1369 | transaction: t |
3fd3ab2d | 1370 | } |
fd45e8f4 | 1371 | |
6e46de09 C |
1372 | const scopes = [ |
1373 | ScopeNames.WITH_TAGS, | |
1374 | ScopeNames.WITH_BLACKLISTED, | |
09209296 C |
1375 | ScopeNames.WITH_ACCOUNT_DETAILS, |
1376 | ScopeNames.WITH_SCHEDULED_UPDATE, | |
6e46de09 | 1377 | ScopeNames.WITH_FILES, |
09209296 C |
1378 | ScopeNames.WITH_STREAMING_PLAYLISTS |
1379 | ] | |
1380 | ||
1381 | if (userId) { | |
1382 | scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] } as any) // FIXME: typings | |
1383 | } | |
1384 | ||
1385 | return VideoModel | |
1386 | .scope(scopes) | |
1387 | .findOne(options) | |
1388 | } | |
1389 | ||
1390 | static loadForGetAPI (id: number | string, t?: Sequelize.Transaction, userId?: number) { | |
418d092a | 1391 | const where = buildWhereIdOrUUID(id) |
09209296 C |
1392 | |
1393 | const options = { | |
1394 | order: [ [ 'Tags', 'name', 'ASC' ] ], | |
1395 | where, | |
1396 | transaction: t | |
1397 | } | |
1398 | ||
1399 | const scopes = [ | |
1400 | ScopeNames.WITH_TAGS, | |
1401 | ScopeNames.WITH_BLACKLISTED, | |
6e46de09 | 1402 | ScopeNames.WITH_ACCOUNT_DETAILS, |
09209296 C |
1403 | ScopeNames.WITH_SCHEDULED_UPDATE, |
1404 | { method: [ ScopeNames.WITH_FILES, true ] } as any, // FIXME: typings | |
1405 | { method: [ ScopeNames.WITH_STREAMING_PLAYLISTS, true ] } as any // FIXME: typings | |
6e46de09 C |
1406 | ] |
1407 | ||
1408 | if (userId) { | |
1409 | scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] } as any) // FIXME: typings | |
1410 | } | |
1411 | ||
d48ff09d | 1412 | return VideoModel |
6e46de09 | 1413 | .scope(scopes) |
da854ddd C |
1414 | .findOne(options) |
1415 | } | |
1416 | ||
09cababd C |
1417 | static async getStats () { |
1418 | const totalLocalVideos = await VideoModel.count({ | |
1419 | where: { | |
1420 | remote: false | |
1421 | } | |
1422 | }) | |
1423 | const totalVideos = await VideoModel.count() | |
1424 | ||
1425 | let totalLocalVideoViews = await VideoModel.sum('views', { | |
1426 | where: { | |
1427 | remote: false | |
1428 | } | |
1429 | }) | |
1430 | // Sequelize could return null... | |
1431 | if (!totalLocalVideoViews) totalLocalVideoViews = 0 | |
1432 | ||
1433 | return { | |
1434 | totalLocalVideos, | |
1435 | totalLocalVideoViews, | |
1436 | totalVideos | |
1437 | } | |
1438 | } | |
1439 | ||
6b616860 C |
1440 | static incrementViews (id: number, views: number) { |
1441 | return VideoModel.increment('views', { | |
1442 | by: views, | |
1443 | where: { | |
1444 | id | |
1445 | } | |
1446 | }) | |
1447 | } | |
1448 | ||
8d427346 C |
1449 | static checkVideoHasInstanceFollow (videoId: number, followerActorId: number) { |
1450 | // Instances only share videos | |
1451 | const query = 'SELECT 1 FROM "videoShare" ' + | |
1452 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + | |
1453 | 'WHERE "actorFollow"."actorId" = $followerActorId AND "videoShare"."videoId" = $videoId ' + | |
1454 | 'LIMIT 1' | |
1455 | ||
1456 | const options = { | |
1457 | type: Sequelize.QueryTypes.SELECT, | |
1458 | bind: { followerActorId, videoId }, | |
1459 | raw: true | |
1460 | } | |
1461 | ||
1462 | return VideoModel.sequelize.query(query, options) | |
1463 | .then(results => results.length === 1) | |
1464 | } | |
1465 | ||
2d3741d6 | 1466 | // threshold corresponds to how many video the field should have to be returned |
7348b1fd | 1467 | static async getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) { |
65b21c96 | 1468 | const serverActor = await getServerActor() |
4e74e803 | 1469 | const followerActorId = serverActor.id |
7348b1fd | 1470 | |
65b21c96 C |
1471 | const scopeOptions: AvailableForListIDsOptions = { |
1472 | serverAccountId: serverActor.Account.id, | |
4e74e803 | 1473 | followerActorId, |
7348b1fd C |
1474 | includeLocalVideos: true |
1475 | } | |
1476 | ||
2d3741d6 C |
1477 | const query: IFindOptions<VideoModel> = { |
1478 | attributes: [ field ], | |
1479 | limit: count, | |
1480 | group: field, | |
1481 | having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col(field)), { | |
8ea6f49a | 1482 | [ Sequelize.Op.gte ]: threshold |
2d3741d6 | 1483 | }) as any, // FIXME: typings |
2d3741d6 C |
1484 | order: [ this.sequelize.random() ] |
1485 | } | |
1486 | ||
7348b1fd C |
1487 | return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST_IDS, scopeOptions ] }) |
1488 | .findAll(query) | |
8ea6f49a | 1489 | .then(rows => rows.map(r => r[ field ])) |
2d3741d6 C |
1490 | } |
1491 | ||
b36f41ca C |
1492 | static buildTrendingQuery (trendingDays: number) { |
1493 | return { | |
1494 | attributes: [], | |
1495 | subQuery: false, | |
1496 | model: VideoViewModel, | |
1497 | required: false, | |
1498 | where: { | |
1499 | startDate: { | |
1500 | [ Sequelize.Op.gte ]: new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays) | |
1501 | } | |
1502 | } | |
1503 | } | |
1504 | } | |
1505 | ||
066e94c5 | 1506 | private static buildActorWhereWithFilter (filter?: VideoFilter) { |
1cd3facc | 1507 | if (filter && (filter === 'local' || filter === 'all-local')) { |
066e94c5 C |
1508 | return { |
1509 | serverId: null | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | return {} | |
1514 | } | |
afd2cba5 | 1515 | |
6e46de09 C |
1516 | private static async getAvailableForApi ( |
1517 | query: IFindOptions<VideoModel>, | |
7ad9b984 | 1518 | options: AvailableForListIDsOptions, |
6e46de09 C |
1519 | countVideos = true |
1520 | ) { | |
afd2cba5 C |
1521 | const idsScope = { |
1522 | method: [ | |
1523 | ScopeNames.AVAILABLE_FOR_LIST_IDS, options | |
1524 | ] | |
1525 | } | |
1526 | ||
8ea6f49a C |
1527 | // Remove trending sort on count, because it uses a group by |
1528 | const countOptions = Object.assign({}, options, { trendingDays: undefined }) | |
1529 | const countQuery = Object.assign({}, query, { attributes: undefined, group: undefined }) | |
1530 | const countScope = { | |
1531 | method: [ | |
1532 | ScopeNames.AVAILABLE_FOR_LIST_IDS, countOptions | |
1533 | ] | |
1534 | } | |
1535 | ||
1536 | const [ count, rowsId ] = await Promise.all([ | |
8b9a525a | 1537 | countVideos ? VideoModel.scope(countScope).count(countQuery) : Promise.resolve<number>(undefined), |
8ea6f49a C |
1538 | VideoModel.scope(idsScope).findAll(query) |
1539 | ]) | |
afd2cba5 C |
1540 | const ids = rowsId.map(r => r.id) |
1541 | ||
1542 | if (ids.length === 0) return { data: [], total: count } | |
1543 | ||
df0b219d | 1544 | const secondQuery: IFindOptions<VideoModel> = { |
b6314e3c C |
1545 | offset: 0, |
1546 | limit: query.limit, | |
9a629c6e C |
1547 | attributes: query.attributes, |
1548 | order: [ // Keep original order | |
1549 | Sequelize.literal( | |
2ba92871 | 1550 | ids.map(id => `"VideoModel".id = ${id} DESC`).join(', ') |
9a629c6e C |
1551 | ) |
1552 | ] | |
b6314e3c | 1553 | } |
df0b219d C |
1554 | |
1555 | // FIXME: typing | |
1556 | const apiScope: any[] = [] | |
1557 | ||
1558 | if (options.user) { | |
1559 | apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] }) | |
1560 | ||
1561 | // Even if the relation is n:m, we know that a user only have 0..1 video history | |
1562 | // So we won't have multiple rows for the same video | |
1563 | // A subquery adds some bugs in our query so disable it | |
1564 | secondQuery.subQuery = false | |
1565 | } | |
1566 | ||
1567 | apiScope.push({ | |
1568 | method: [ | |
1569 | ScopeNames.FOR_API, { | |
76564702 C |
1570 | ids, |
1571 | withFiles: options.withFiles, | |
df0b219d C |
1572 | videoPlaylistId: options.videoPlaylistId |
1573 | } as ForAPIOptions | |
1574 | ] | |
1575 | }) | |
1576 | ||
b6314e3c | 1577 | const rows = await VideoModel.scope(apiScope).findAll(secondQuery) |
afd2cba5 C |
1578 | |
1579 | return { | |
1580 | data: rows, | |
1581 | total: count | |
1582 | } | |
1583 | } | |
066e94c5 | 1584 | |
098eb377 | 1585 | static getCategoryLabel (id: number) { |
8ea6f49a | 1586 | return VIDEO_CATEGORIES[ id ] || 'Misc' |
ae5a3dd6 C |
1587 | } |
1588 | ||
098eb377 | 1589 | static getLicenceLabel (id: number) { |
8ea6f49a | 1590 | return VIDEO_LICENCES[ id ] || 'Unknown' |
ae5a3dd6 C |
1591 | } |
1592 | ||
098eb377 | 1593 | static getLanguageLabel (id: string) { |
8ea6f49a | 1594 | return VIDEO_LANGUAGES[ id ] || 'Unknown' |
ae5a3dd6 C |
1595 | } |
1596 | ||
098eb377 | 1597 | static getPrivacyLabel (id: number) { |
8ea6f49a | 1598 | return VIDEO_PRIVACIES[ id ] || 'Unknown' |
2186386c | 1599 | } |
2243730c | 1600 | |
098eb377 | 1601 | static getStateLabel (id: number) { |
8ea6f49a | 1602 | return VIDEO_STATES[ id ] || 'Unknown' |
2243730c C |
1603 | } |
1604 | ||
3fd3ab2d C |
1605 | getOriginalFile () { |
1606 | if (Array.isArray(this.VideoFiles) === false) return undefined | |
aaf61f38 | 1607 | |
3fd3ab2d C |
1608 | // The original file is the file that have the higher resolution |
1609 | return maxBy(this.VideoFiles, file => file.resolution) | |
e4f97bab | 1610 | } |
aaf61f38 | 1611 | |
3fd3ab2d C |
1612 | getVideoFilename (videoFile: VideoFileModel) { |
1613 | return this.uuid + '-' + videoFile.resolution + videoFile.extname | |
1614 | } | |
165cdc75 | 1615 | |
3fd3ab2d | 1616 | getThumbnailName () { |
3fd3ab2d C |
1617 | const extension = '.jpg' |
1618 | return this.uuid + extension | |
7b1f49de C |
1619 | } |
1620 | ||
3fd3ab2d C |
1621 | getPreviewName () { |
1622 | const extension = '.jpg' | |
1623 | return this.uuid + extension | |
1624 | } | |
7b1f49de | 1625 | |
3fd3ab2d C |
1626 | getTorrentFileName (videoFile: VideoFileModel) { |
1627 | const extension = '.torrent' | |
1628 | return this.uuid + '-' + videoFile.resolution + extension | |
1629 | } | |
8e7f08b5 | 1630 | |
3fd3ab2d C |
1631 | isOwned () { |
1632 | return this.remote === false | |
9567011b C |
1633 | } |
1634 | ||
3fd3ab2d | 1635 | createPreview (videoFile: VideoFileModel) { |
3fd3ab2d C |
1636 | return generateImageFromVideoFile( |
1637 | this.getVideoFilePath(videoFile), | |
1638 | CONFIG.STORAGE.PREVIEWS_DIR, | |
1639 | this.getPreviewName(), | |
26670720 | 1640 | PREVIEWS_SIZE |
3fd3ab2d C |
1641 | ) |
1642 | } | |
9567011b | 1643 | |
3fd3ab2d | 1644 | createThumbnail (videoFile: VideoFileModel) { |
3fd3ab2d C |
1645 | return generateImageFromVideoFile( |
1646 | this.getVideoFilePath(videoFile), | |
1647 | CONFIG.STORAGE.THUMBNAILS_DIR, | |
1648 | this.getThumbnailName(), | |
26670720 | 1649 | THUMBNAILS_SIZE |
3fd3ab2d | 1650 | ) |
14d3270f C |
1651 | } |
1652 | ||
02756fbd C |
1653 | getTorrentFilePath (videoFile: VideoFileModel) { |
1654 | return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) | |
1655 | } | |
1656 | ||
3fd3ab2d C |
1657 | getVideoFilePath (videoFile: VideoFileModel) { |
1658 | return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) | |
1659 | } | |
14d3270f | 1660 | |
81e504b3 | 1661 | async createTorrentAndSetInfoHash (videoFile: VideoFileModel) { |
3fd3ab2d | 1662 | const options = { |
6cced8f9 C |
1663 | // Keep the extname, it's used by the client to stream the file inside a web browser |
1664 | name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`, | |
81e504b3 | 1665 | createdBy: 'PeerTube', |
3fd3ab2d | 1666 | announceList: [ |
0edf0581 C |
1667 | [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ], |
1668 | [ CONFIG.WEBSERVER.URL + '/tracker/announce' ] | |
3fd3ab2d | 1669 | ], |
c48e82b5 | 1670 | urlList: [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ] |
3fd3ab2d | 1671 | } |
14d3270f | 1672 | |
3fd3ab2d | 1673 | const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options) |
e4f97bab | 1674 | |
3fd3ab2d C |
1675 | const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) |
1676 | logger.info('Creating torrent %s.', filePath) | |
e4f97bab | 1677 | |
62689b94 | 1678 | await writeFile(filePath, torrent) |
e4f97bab | 1679 | |
3fd3ab2d C |
1680 | const parsedTorrent = parseTorrent(torrent) |
1681 | videoFile.infoHash = parsedTorrent.infoHash | |
1682 | } | |
e4f97bab | 1683 | |
cef534ed C |
1684 | getWatchStaticPath () { |
1685 | return '/videos/watch/' + this.uuid | |
1686 | } | |
1687 | ||
40e87e9e | 1688 | getEmbedStaticPath () { |
3fd3ab2d C |
1689 | return '/videos/embed/' + this.uuid |
1690 | } | |
e4f97bab | 1691 | |
40e87e9e | 1692 | getThumbnailStaticPath () { |
3fd3ab2d | 1693 | return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName()) |
e4f97bab | 1694 | } |
227d02fe | 1695 | |
40e87e9e | 1696 | getPreviewStaticPath () { |
3fd3ab2d C |
1697 | return join(STATIC_PATHS.PREVIEWS, this.getPreviewName()) |
1698 | } | |
40298b02 | 1699 | |
098eb377 C |
1700 | toFormattedJSON (options?: VideoFormattingJSONOptions): Video { |
1701 | return videoModelToFormattedJSON(this, options) | |
14d3270f | 1702 | } |
14d3270f | 1703 | |
2422c46b | 1704 | toFormattedDetailsJSON (): VideoDetails { |
098eb377 | 1705 | return videoModelToFormattedDetailsJSON(this) |
244e76a5 RK |
1706 | } |
1707 | ||
1708 | getFormattedVideoFilesJSON (): VideoFile[] { | |
098eb377 | 1709 | return videoFilesModelToFormattedJSON(this, this.VideoFiles) |
3fd3ab2d | 1710 | } |
e4f97bab | 1711 | |
3fd3ab2d | 1712 | toActivityPubObject (): VideoTorrentObject { |
098eb377 | 1713 | return videoModelToActivityPubObject(this) |
3fd3ab2d C |
1714 | } |
1715 | ||
1716 | getTruncatedDescription () { | |
1717 | if (!this.description) return null | |
93e1258c | 1718 | |
bffbebbe | 1719 | const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max |
c73e83da | 1720 | return peertubeTruncate(this.description, maxLength) |
93e1258c C |
1721 | } |
1722 | ||
056aa7f2 | 1723 | getOriginalFileResolution () { |
3fd3ab2d | 1724 | const originalFilePath = this.getVideoFilePath(this.getOriginalFile()) |
0d0e8dd0 | 1725 | |
056aa7f2 | 1726 | return getVideoFileResolution(originalFilePath) |
3fd3ab2d | 1727 | } |
0d0e8dd0 | 1728 | |
96f29c0f | 1729 | getDescriptionAPIPath () { |
3fd3ab2d | 1730 | return `/api/${API_VERSION}/videos/${this.uuid}/description` |
feb4bdfd C |
1731 | } |
1732 | ||
3fd3ab2d C |
1733 | removeThumbnail () { |
1734 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) | |
62689b94 | 1735 | return remove(thumbnailPath) |
ed31c059 | 1736 | .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err })) |
feb4bdfd C |
1737 | } |
1738 | ||
3fd3ab2d | 1739 | removePreview () { |
ed31c059 | 1740 | const previewPath = join(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName()) |
62689b94 | 1741 | return remove(previewPath) |
ed31c059 | 1742 | .catch(err => logger.warn('Cannot delete preview %s.', previewPath, { err })) |
7920c273 C |
1743 | } |
1744 | ||
b9fffa29 C |
1745 | removeFile (videoFile: VideoFileModel, isRedundancy = false) { |
1746 | const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR | |
1747 | ||
1748 | const filePath = join(baseDir, this.getVideoFilename(videoFile)) | |
62689b94 | 1749 | return remove(filePath) |
ed31c059 | 1750 | .catch(err => logger.warn('Cannot delete file %s.', filePath, { err })) |
feb4bdfd C |
1751 | } |
1752 | ||
3fd3ab2d C |
1753 | removeTorrent (videoFile: VideoFileModel) { |
1754 | const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) | |
62689b94 | 1755 | return remove(torrentPath) |
ed31c059 | 1756 | .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err })) |
aaf61f38 C |
1757 | } |
1758 | ||
09209296 | 1759 | removeStreamingPlaylist (isRedundancy = false) { |
9c6ca37f | 1760 | const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_STREAMING_PLAYLIST_DIRECTORY |
09209296 C |
1761 | |
1762 | const filePath = join(baseDir, this.uuid) | |
1763 | return remove(filePath) | |
1764 | .catch(err => logger.warn('Cannot delete playlist directory %s.', filePath, { err })) | |
1765 | } | |
1766 | ||
1297eb5d C |
1767 | isOutdated () { |
1768 | if (this.isOwned()) return false | |
1769 | ||
9f79ade6 | 1770 | return isOutdated(this, ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL) |
1297eb5d C |
1771 | } |
1772 | ||
04b8c3fb C |
1773 | setAsRefreshed () { |
1774 | this.changed('updatedAt', true) | |
1775 | ||
1776 | return this.save() | |
1777 | } | |
1778 | ||
c48e82b5 | 1779 | getBaseUrls () { |
3fd3ab2d C |
1780 | let baseUrlHttp |
1781 | let baseUrlWs | |
7920c273 | 1782 | |
3fd3ab2d C |
1783 | if (this.isOwned()) { |
1784 | baseUrlHttp = CONFIG.WEBSERVER.URL | |
1785 | baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | |
1786 | } else { | |
50d6de9c C |
1787 | baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host |
1788 | baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host | |
6fcd19ba | 1789 | } |
aaf61f38 | 1790 | |
3fd3ab2d | 1791 | return { baseUrlHttp, baseUrlWs } |
15d4ee04 | 1792 | } |
a96aed15 | 1793 | |
c48e82b5 C |
1794 | generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) { |
1795 | const xs = this.getTorrentUrl(videoFile, baseUrlHttp) | |
09209296 | 1796 | const announce = this.getTrackerUrls(baseUrlHttp, baseUrlWs) |
c48e82b5 C |
1797 | let urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ] |
1798 | ||
1799 | const redundancies = videoFile.RedundancyVideos | |
1800 | if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl)) | |
1801 | ||
1802 | const magnetHash = { | |
1803 | xs, | |
1804 | announce, | |
1805 | urlList, | |
1806 | infoHash: videoFile.infoHash, | |
1807 | name: this.name | |
1808 | } | |
1809 | ||
1810 | return magnetUtil.encode(magnetHash) | |
1811 | } | |
1812 | ||
09209296 C |
1813 | getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) { |
1814 | return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ] | |
1815 | } | |
1816 | ||
c48e82b5 | 1817 | getThumbnailUrl (baseUrlHttp: string) { |
3fd3ab2d | 1818 | return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName() |
a96aed15 C |
1819 | } |
1820 | ||
c48e82b5 | 1821 | getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) { |
3fd3ab2d C |
1822 | return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile) |
1823 | } | |
e4f97bab | 1824 | |
c48e82b5 | 1825 | getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) { |
02756fbd C |
1826 | return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile) |
1827 | } | |
1828 | ||
c48e82b5 | 1829 | getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) { |
3fd3ab2d C |
1830 | return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) |
1831 | } | |
a96aed15 | 1832 | |
b9fffa29 C |
1833 | getVideoRedundancyUrl (videoFile: VideoFileModel, baseUrlHttp: string) { |
1834 | return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getVideoFilename(videoFile) | |
1835 | } | |
1836 | ||
c48e82b5 | 1837 | getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) { |
02756fbd C |
1838 | return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile) |
1839 | } | |
09209296 C |
1840 | |
1841 | getBandwidthBits (videoFile: VideoFileModel) { | |
1842 | return Math.ceil((videoFile.size * 8) / this.duration) | |
1843 | } | |
a96aed15 | 1844 | } |