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