diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-29 14:17:03 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-29 14:17:03 +0200 |
commit | 9c9a236b541a286e165d67341e4ddd6ea2fabdf1 (patch) | |
tree | d7e397a3e160a64162f7ea1943529b2a06fb830a /server/models/video | |
parent | 56d07460b5fe55cc86501742e759d9711d7eb386 (diff) | |
download | PeerTube-9c9a236b541a286e165d67341e4ddd6ea2fabdf1.tar.gz PeerTube-9c9a236b541a286e165d67341e4ddd6ea2fabdf1.tar.zst PeerTube-9c9a236b541a286e165d67341e4ddd6ea2fabdf1.zip |
Refactor options in models
Diffstat (limited to 'server/models/video')
-rw-r--r-- | server/models/video/video-channel.ts | 52 | ||||
-rw-r--r-- | server/models/video/video-playlist.ts | 30 | ||||
-rw-r--r-- | server/models/video/video.ts | 110 |
3 files changed, 82 insertions, 110 deletions
diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index e4b12c517..9f04a57c6 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts | |||
@@ -438,30 +438,6 @@ ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"` | |||
438 | } | 438 | } |
439 | } | 439 | } |
440 | 440 | ||
441 | static listForApi (parameters: { | ||
442 | actorId: number | ||
443 | start: number | ||
444 | count: number | ||
445 | sort: string | ||
446 | }) { | ||
447 | const { actorId } = parameters | ||
448 | |||
449 | const query = { | ||
450 | offset: parameters.start, | ||
451 | limit: parameters.count, | ||
452 | order: getSort(parameters.sort) | ||
453 | } | ||
454 | |||
455 | return VideoChannelModel | ||
456 | .scope({ | ||
457 | method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ] | ||
458 | }) | ||
459 | .findAndCountAll(query) | ||
460 | .then(({ rows, count }) => { | ||
461 | return { total: count, data: rows } | ||
462 | }) | ||
463 | } | ||
464 | |||
465 | static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> { | 441 | static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> { |
466 | const query = { | 442 | const query = { |
467 | attributes: [ ], | 443 | attributes: [ ], |
@@ -483,15 +459,33 @@ ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"` | |||
483 | .findAll(query) | 459 | .findAll(query) |
484 | } | 460 | } |
485 | 461 | ||
486 | static searchForApi (options: { | 462 | static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & { |
487 | actorId: number | ||
488 | search?: string | ||
489 | start: number | 463 | start: number |
490 | count: number | 464 | count: number |
491 | sort: string | 465 | sort: string |
466 | }) { | ||
467 | const { actorId } = parameters | ||
468 | |||
469 | const query = { | ||
470 | offset: parameters.start, | ||
471 | limit: parameters.count, | ||
472 | order: getSort(parameters.sort) | ||
473 | } | ||
474 | |||
475 | return VideoChannelModel | ||
476 | .scope({ | ||
477 | method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ] | ||
478 | }) | ||
479 | .findAndCountAll(query) | ||
480 | .then(({ rows, count }) => { | ||
481 | return { total: count, data: rows } | ||
482 | }) | ||
483 | } | ||
492 | 484 | ||
493 | host?: string | 485 | static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & { |
494 | handles?: string[] | 486 | start: number |
487 | count: number | ||
488 | sort: string | ||
495 | }) { | 489 | }) { |
496 | let attributesInclude: any[] = [ literal('0 as similarity') ] | 490 | let attributesInclude: any[] = [ literal('0 as similarity') ] |
497 | let where: WhereOptions | 491 | let where: WhereOptions |
diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index caa79952d..630684a88 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts | |||
@@ -19,7 +19,7 @@ import { | |||
19 | } from 'sequelize-typescript' | 19 | } from 'sequelize-typescript' |
20 | import { buildUUID, uuidToShort } from '@server/helpers/uuid' | 20 | import { buildUUID, uuidToShort } from '@server/helpers/uuid' |
21 | import { MAccountId, MChannelId } from '@server/types/models' | 21 | import { MAccountId, MChannelId } from '@server/types/models' |
22 | import { AttributesOnly, buildPlaylistEmbedPath, buildPlaylistWatchPath } from '@shared/core-utils' | 22 | import { AttributesOnly, buildPlaylistEmbedPath, buildPlaylistWatchPath, pick } from '@shared/core-utils' |
23 | import { ActivityIconObject } from '../../../shared/models/activitypub/objects' | 23 | import { ActivityIconObject } from '../../../shared/models/activitypub/objects' |
24 | import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' | 24 | import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' |
25 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' | 25 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' |
@@ -357,19 +357,10 @@ export class VideoPlaylistModel extends Model<Partial<AttributesOnly<VideoPlayli | |||
357 | }) | 357 | }) |
358 | Thumbnail: ThumbnailModel | 358 | Thumbnail: ThumbnailModel |
359 | 359 | ||
360 | static listForApi (options: { | 360 | static listForApi (options: AvailableForListOptions & { |
361 | followerActorId: number | ||
362 | start: number | 361 | start: number |
363 | count: number | 362 | count: number |
364 | sort: string | 363 | sort: string |
365 | type?: VideoPlaylistType | ||
366 | accountId?: number | ||
367 | videoChannelId?: number | ||
368 | listMyPlaylists?: boolean | ||
369 | search?: string | ||
370 | host?: string | ||
371 | uuids?: string[] | ||
372 | withVideos?: boolean // false by default | ||
373 | }) { | 364 | }) { |
374 | const query = { | 365 | const query = { |
375 | offset: options.start, | 366 | offset: options.start, |
@@ -382,14 +373,8 @@ export class VideoPlaylistModel extends Model<Partial<AttributesOnly<VideoPlayli | |||
382 | method: [ | 373 | method: [ |
383 | ScopeNames.AVAILABLE_FOR_LIST, | 374 | ScopeNames.AVAILABLE_FOR_LIST, |
384 | { | 375 | { |
385 | type: options.type, | 376 | ...pick(options, [ 'type', 'followerActorId', 'accountId', 'videoChannelId', 'listMyPlaylists', 'search', 'host', 'uuids' ]), |
386 | followerActorId: options.followerActorId, | 377 | |
387 | accountId: options.accountId, | ||
388 | videoChannelId: options.videoChannelId, | ||
389 | listMyPlaylists: options.listMyPlaylists, | ||
390 | search: options.search, | ||
391 | host: options.host, | ||
392 | uuids: options.uuids, | ||
393 | withVideos: options.withVideos || false | 378 | withVideos: options.withVideos || false |
394 | } as AvailableForListOptions | 379 | } as AvailableForListOptions |
395 | ] | 380 | ] |
@@ -406,17 +391,14 @@ export class VideoPlaylistModel extends Model<Partial<AttributesOnly<VideoPlayli | |||
406 | }) | 391 | }) |
407 | } | 392 | } |
408 | 393 | ||
409 | static searchForApi (options: { | 394 | static searchForApi (options: Pick<AvailableForListOptions, 'followerActorId' | 'search'| 'host'| 'uuids'> & { |
410 | followerActorId: number | ||
411 | start: number | 395 | start: number |
412 | count: number | 396 | count: number |
413 | sort: string | 397 | sort: string |
414 | search?: string | ||
415 | host?: string | ||
416 | uuids?: string[] | ||
417 | }) { | 398 | }) { |
418 | return VideoPlaylistModel.listForApi({ | 399 | return VideoPlaylistModel.listForApi({ |
419 | ...options, | 400 | ...options, |
401 | |||
420 | type: VideoPlaylistType.REGULAR, | 402 | type: VideoPlaylistType.REGULAR, |
421 | listMyPlaylists: false, | 403 | listMyPlaylists: false, |
422 | withVideos: true | 404 | withVideos: true |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index d5efe2eac..56a5b0e18 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -31,7 +31,7 @@ import { LiveManager } from '@server/lib/live/live-manager' | |||
31 | import { getHLSDirectory, getVideoFilePath } from '@server/lib/video-paths' | 31 | import { getHLSDirectory, getVideoFilePath } from '@server/lib/video-paths' |
32 | import { getServerActor } from '@server/models/application/application' | 32 | import { getServerActor } from '@server/models/application/application' |
33 | import { ModelCache } from '@server/models/model-cache' | 33 | import { ModelCache } from '@server/models/model-cache' |
34 | import { AttributesOnly, buildVideoEmbedPath, buildVideoWatchPath } from '@shared/core-utils' | 34 | import { AttributesOnly, buildVideoEmbedPath, buildVideoWatchPath, pick } from '@shared/core-utils' |
35 | import { VideoFile } from '@shared/models/videos/video-file.model' | 35 | import { VideoFile } from '@shared/models/videos/video-file.model' |
36 | import { ResultList, UserRight, VideoPrivacy, VideoState } from '../../../shared' | 36 | import { ResultList, UserRight, VideoPrivacy, VideoState } from '../../../shared' |
37 | import { VideoObject } from '../../../shared/models/activitypub/objects' | 37 | import { VideoObject } from '../../../shared/models/activitypub/objects' |
@@ -1083,41 +1083,44 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> { | |||
1083 | : serverActor.id | 1083 | : serverActor.id |
1084 | 1084 | ||
1085 | const queryOptions = { | 1085 | const queryOptions = { |
1086 | start: options.start, | 1086 | ...pick(options, [ |
1087 | count: options.count, | 1087 | 'start', |
1088 | sort: options.sort, | 1088 | 'count', |
1089 | 'sort', | ||
1090 | 'nsfw', | ||
1091 | 'isLive', | ||
1092 | 'categoryOneOf', | ||
1093 | 'licenceOneOf', | ||
1094 | 'languageOneOf', | ||
1095 | 'tagsOneOf', | ||
1096 | 'tagsAllOf', | ||
1097 | 'filter', | ||
1098 | 'withFiles', | ||
1099 | 'accountId', | ||
1100 | 'videoChannelId', | ||
1101 | 'videoPlaylistId', | ||
1102 | 'includeLocalVideos', | ||
1103 | 'user', | ||
1104 | 'historyOfUser', | ||
1105 | 'search' | ||
1106 | ]), | ||
1107 | |||
1089 | followerActorId, | 1108 | followerActorId, |
1090 | serverAccountId: serverActor.Account.id, | 1109 | serverAccountId: serverActor.Account.id, |
1091 | nsfw: options.nsfw, | ||
1092 | isLive: options.isLive, | ||
1093 | categoryOneOf: options.categoryOneOf, | ||
1094 | licenceOneOf: options.licenceOneOf, | ||
1095 | languageOneOf: options.languageOneOf, | ||
1096 | tagsOneOf: options.tagsOneOf, | ||
1097 | tagsAllOf: options.tagsAllOf, | ||
1098 | filter: options.filter, | ||
1099 | withFiles: options.withFiles, | ||
1100 | accountId: options.accountId, | ||
1101 | videoChannelId: options.videoChannelId, | ||
1102 | videoPlaylistId: options.videoPlaylistId, | ||
1103 | includeLocalVideos: options.includeLocalVideos, | ||
1104 | user: options.user, | ||
1105 | historyOfUser: options.historyOfUser, | ||
1106 | trendingDays, | 1110 | trendingDays, |
1107 | trendingAlgorithm, | 1111 | trendingAlgorithm |
1108 | search: options.search | ||
1109 | } | 1112 | } |
1110 | 1113 | ||
1111 | return VideoModel.getAvailableForApi(queryOptions, options.countVideos) | 1114 | return VideoModel.getAvailableForApi(queryOptions, options.countVideos) |
1112 | } | 1115 | } |
1113 | 1116 | ||
1114 | static async searchAndPopulateAccountAndServer (options: { | 1117 | static async searchAndPopulateAccountAndServer (options: { |
1118 | start: number | ||
1119 | count: number | ||
1120 | sort: string | ||
1115 | includeLocalVideos: boolean | 1121 | includeLocalVideos: boolean |
1116 | search?: string | 1122 | search?: string |
1117 | host?: string | 1123 | host?: string |
1118 | start?: number | ||
1119 | count?: number | ||
1120 | sort?: string | ||
1121 | startDate?: string // ISO 8601 | 1124 | startDate?: string // ISO 8601 |
1122 | endDate?: string // ISO 8601 | 1125 | endDate?: string // ISO 8601 |
1123 | originallyPublishedStartDate?: string | 1126 | originallyPublishedStartDate?: string |
@@ -1138,40 +1141,33 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> { | |||
1138 | const serverActor = await getServerActor() | 1141 | const serverActor = await getServerActor() |
1139 | 1142 | ||
1140 | const queryOptions = { | 1143 | const queryOptions = { |
1141 | followerActorId: serverActor.id, | 1144 | ...pick(options, [ |
1142 | serverAccountId: serverActor.Account.id, | 1145 | 'includeLocalVideos', |
1143 | 1146 | 'nsfw', | |
1144 | includeLocalVideos: options.includeLocalVideos, | 1147 | 'isLive', |
1145 | nsfw: options.nsfw, | 1148 | 'categoryOneOf', |
1146 | isLive: options.isLive, | 1149 | 'licenceOneOf', |
1147 | 1150 | 'languageOneOf', | |
1148 | categoryOneOf: options.categoryOneOf, | 1151 | 'tagsOneOf', |
1149 | licenceOneOf: options.licenceOneOf, | 1152 | 'tagsAllOf', |
1150 | languageOneOf: options.languageOneOf, | 1153 | 'user', |
1154 | 'filter', | ||
1155 | 'host', | ||
1156 | 'start', | ||
1157 | 'count', | ||
1158 | 'sort', | ||
1159 | 'startDate', | ||
1160 | 'endDate', | ||
1161 | 'originallyPublishedStartDate', | ||
1162 | 'originallyPublishedEndDate', | ||
1163 | 'durationMin', | ||
1164 | 'durationMax', | ||
1165 | 'uuids', | ||
1166 | 'search' | ||
1167 | ]), | ||
1151 | 1168 | ||
1152 | tagsOneOf: options.tagsOneOf, | 1169 | followerActorId: serverActor.id, |
1153 | tagsAllOf: options.tagsAllOf, | 1170 | serverAccountId: serverActor.Account.id |
1154 | |||
1155 | user: options.user, | ||
1156 | filter: options.filter, | ||
1157 | host: options.host, | ||
1158 | |||
1159 | start: options.start, | ||
1160 | count: options.count, | ||
1161 | sort: options.sort, | ||
1162 | |||
1163 | startDate: options.startDate, | ||
1164 | endDate: options.endDate, | ||
1165 | |||
1166 | originallyPublishedStartDate: options.originallyPublishedStartDate, | ||
1167 | originallyPublishedEndDate: options.originallyPublishedEndDate, | ||
1168 | |||
1169 | durationMin: options.durationMin, | ||
1170 | durationMax: options.durationMax, | ||
1171 | |||
1172 | uuids: options.uuids, | ||
1173 | |||
1174 | search: options.search | ||
1175 | } | 1171 | } |
1176 | 1172 | ||
1177 | return VideoModel.getAvailableForApi(queryOptions) | 1173 | return VideoModel.getAvailableForApi(queryOptions) |