From bfbd912886eba17b4aa9a40dcef2fddc685d85bf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 31 Jul 2019 15:57:32 +0200 Subject: Fix broken playlist api --- server/models/account/account-video-rate.ts | 4 +- server/models/account/account.ts | 71 +++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 10 deletions(-) (limited to 'server/models/account') diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index 59f586b54..85af9e378 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -9,7 +9,7 @@ import { ActorModel } from '../activitypub/actor' import { getSort, throwIfNotValid } from '../utils' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { AccountVideoRate } from '../../../shared' -import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from '../video/video-channel' +import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' /* Account rates per video. @@ -109,7 +109,7 @@ export class AccountVideoRateModel extends Model { required: true, include: [ { - model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, true] }), + model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), required: true } ] diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 09cada096..28014946f 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -27,12 +27,19 @@ import { UserModel } from './user' import { AvatarModel } from '../avatar/avatar' import { VideoPlaylistModel } from '../video/video-playlist' import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' -import { Op, Transaction, WhereOptions } from 'sequelize' +import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' +import { AccountBlocklistModel } from './account-blocklist' +import { ServerBlocklistModel } from '../server/server-blocklist' export enum ScopeNames { SUMMARY = 'SUMMARY' } +export type SummaryOptions = { + whereActor?: WhereOptions + withAccountBlockerIds?: number[] +} + @DefaultScope(() => ({ include: [ { @@ -42,8 +49,16 @@ export enum ScopeNames { ] })) @Scopes(() => ({ - [ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions) => { - return { + [ ScopeNames.SUMMARY ]: (options: SummaryOptions = {}) => { + const whereActor = options.whereActor || undefined + + const serverInclude: IncludeOptions = { + attributes: [ 'host' ], + model: ServerModel.unscoped(), + required: false + } + + const query: FindOptions = { attributes: [ 'id', 'name' ], include: [ { @@ -52,11 +67,8 @@ export enum ScopeNames { required: true, where: whereActor, include: [ - { - attributes: [ 'host' ], - model: ServerModel.unscoped(), - required: false - }, + serverInclude, + { model: AvatarModel.unscoped(), required: false @@ -65,6 +77,35 @@ export enum ScopeNames { } ] } + + if (options.withAccountBlockerIds) { + query.include.push({ + attributes: [ 'id' ], + model: AccountBlocklistModel.unscoped(), + as: 'BlockedAccounts', + required: false, + where: { + accountId: { + [Op.in]: options.withAccountBlockerIds + } + } + }) + + serverInclude.include = [ + { + attributes: [ 'id' ], + model: ServerBlocklistModel.unscoped(), + required: false, + where: { + accountId: { + [Op.in]: options.withAccountBlockerIds + } + } + } + ] + } + + return query } })) @Table({ @@ -163,6 +204,16 @@ export class AccountModel extends Model { }) VideoComments: VideoCommentModel[] + @HasMany(() => AccountBlocklistModel, { + foreignKey: { + name: 'targetAccountId', + allowNull: false + }, + as: 'BlockedAccounts', + onDelete: 'CASCADE' + }) + BlockedAccounts: AccountBlocklistModel[] + @BeforeDestroy static async sendDeleteIfOwned (instance: AccountModel, options) { if (!instance.Actor) { @@ -343,4 +394,8 @@ export class AccountModel extends Model { getDisplayName () { return this.name } + + isBlocked () { + return this.BlockedAccounts && this.BlockedAccounts.length !== 0 + } } -- cgit v1.2.3