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