X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fserver%2Fserver-blocklist.ts;h=9752dfbc3f0a0c3330cd17364194decd765146b9;hb=d0800f7661f13fabe7bb6f4aa0ea50764f106405;hp=b3579d5896f41f1bf596436abb4a67e484d4d463;hpb=eb34ec30e0b57286fc6f85160490d2e973a3b0b1;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts index b3579d589..9752dfbc3 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 '../utils' 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 {