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