From 09979f8959425390b879bce22101a9bc061ae9a0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 5 Mar 2019 11:30:43 +0100 Subject: Refactor video playlist middlewares --- server/models/account/account.ts | 6 +++--- server/models/video/video-playlist.ts | 30 ++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) (limited to 'server/models') diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 3fb766c8a..7cc40f631 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -10,7 +10,8 @@ import { ForeignKey, HasMany, Is, - Model, Scopes, + Model, + Scopes, Table, UpdatedAt } from 'sequelize-typescript' @@ -26,7 +27,6 @@ import { VideoCommentModel } from '../video/video-comment' import { UserModel } from './user' import { CONFIG } from '../../initializers' import { AvatarModel } from '../avatar/avatar' -import { WhereOptions } from 'sequelize' import { VideoPlaylistModel } from '../video/video-playlist' export enum ScopeNames { @@ -42,7 +42,7 @@ export enum ScopeNames { ] }) @Scopes({ - [ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions) => { + [ ScopeNames.SUMMARY ]: (whereActor?: Sequelize.WhereOptions) => { return { attributes: [ 'id', 'name' ], include: [ diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index ce49f77ec..4d2ea0a66 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -47,7 +47,8 @@ enum ScopeNames { AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH', WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY', - WITH_ACCOUNT = 'WITH_ACCOUNT' + WITH_ACCOUNT = 'WITH_ACCOUNT', + WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL' } type AvailableForListOptions = { @@ -89,6 +90,18 @@ type AvailableForListOptions = { } ] }, + [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL ]: { + include: [ + { + model: () => AccountModel, + required: true + }, + { + model: () => VideoChannelModel, + required: false + } + ] + }, [ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => { // Only list local playlists OR playlists that are on an instance followed by actorId const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId) @@ -317,7 +330,7 @@ export class VideoPlaylistModel extends Model { .then(e => !!e) } - static loadWithAccountAndChannel (id: number | string, transaction: Sequelize.Transaction) { + static loadWithAccountAndChannelSummary (id: number | string, transaction: Sequelize.Transaction) { const where = buildWhereIdOrUUID(id) const query = { @@ -330,6 +343,19 @@ export class VideoPlaylistModel extends Model { .findOne(query) } + static loadWithAccountAndChannel (id: number | string, transaction: Sequelize.Transaction) { + const where = buildWhereIdOrUUID(id) + + const query = { + where, + transaction + } + + return VideoPlaylistModel + .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH ]) + .findOne(query) + } + static loadByUrlAndPopulateAccount (url: string) { const query = { where: { -- cgit v1.2.3