X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fserver%2Fserver-blocklist.ts;h=3d755fe4a1e40f71864945ac9f568dbc1150e293;hb=2f63f629add5d24f8c01f309c7cae43b667b0c2a;hp=b3579d5896f41f1bf596436abb4a67e484d4d463;hpb=20213fbd2a366dffc35aa7dddad71323893f8d62;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts index b3579d589..3d755fe4a 100644 --- a/server/models/server/server-blocklist.ts +++ b/server/models/server/server-blocklist.ts @@ -1,10 +1,10 @@ -import { Op } from 'sequelize' +import { Op, QueryTypes } from 'sequelize' import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/types/models' -import { AttributesOnly } from '@shared/core-utils' import { ServerBlock } from '@shared/models' +import { AttributesOnly } from '@shared/typescript-utils' import { AccountModel } from '../account/account' -import { getSort, searchAttribute } from '../utils' +import { createSafeIn, getSort, searchAttribute } from '../shared' import { ServerModel } from './server' enum ScopeNames { @@ -76,7 +76,7 @@ export class ServerBlocklistModel extends Model entries.map(e => e.BlockedServer.host)) } + static getBlockStatus (byAccountIds: number[], hosts: string[]): Promise<{ host: string, accountId: number }[]> { + const rawQuery = `SELECT "server"."host", "serverBlocklist"."accountId" ` + + `FROM "serverBlocklist" ` + + `INNER JOIN "server" ON "server"."id" = "serverBlocklist"."targetServerId" ` + + `WHERE "server"."host" IN (:hosts) ` + + `AND "serverBlocklist"."accountId" IN (${createSafeIn(ServerBlocklistModel.sequelize, byAccountIds)})` + + return ServerBlocklistModel.sequelize.query(rawQuery, { + type: QueryTypes.SELECT as QueryTypes.SELECT, + replacements: { hosts } + }) + } + static listForApi (parameters: { start: number count: number @@ -156,16 +169,15 @@ export class ServerBlocklistModel extends Model(query) - .then(({ rows, count }) => { - return { total: count, data: rows } - }) + return Promise.all([ + ServerBlocklistModel.scope(ScopeNames.WITH_SERVER).count(query), + ServerBlocklistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ]).findAll(query) + ]).then(([ total, data ]) => ({ total, data })) } toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {