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