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