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