X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fserver%2Fserver-blocklist.ts;h=9752dfbc3f0a0c3330cd17364194decd765146b9;hb=d0800f7661f13fabe7bb6f4aa0ea50764f106405;hp=883ae47ab0c84e61d7d65299b3e1f386ba79d358;hpb=cf59a2a0c367683ba35758419499bf6087c192ec;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts index 883ae47ab..9752dfbc3 100644 --- a/server/models/server/server-blocklist.ts +++ b/server/models/server/server-blocklist.ts @@ -1,11 +1,11 @@ +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 { ServerBlock } from '@shared/models' +import { AttributesOnly } from '@shared/typescript-utils' import { AccountModel } from '../account/account' +import { createSafeIn, getSort, searchAttribute } from '../utils' import { ServerModel } from './server' -import { ServerBlock } from '../../../shared/models/blocklist' -import { getSort } from '../utils' -import * as Bluebird from 'bluebird' -import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/typings/models' -import { Op } from 'sequelize' enum ScopeNames { WITH_ACCOUNT = 'WITH_ACCOUNT', @@ -43,7 +43,7 @@ enum ScopeNames { } ] }) -export class ServerBlocklistModel extends Model { +export class ServerBlocklistModel extends Model>> { @CreatedAt createdAt: Date @@ -76,7 +76,7 @@ export class ServerBlocklistModel extends Model { }) BlockedServer: ServerModel - static isServerMutedByMulti (accountIds: number[], targetServerId: number) { + static isServerMutedByAccounts (accountIds: number[], targetServerId: number) { const query = { attributes: [ 'accountId', 'id' ], where: { @@ -101,7 +101,7 @@ export class ServerBlocklistModel extends Model { }) } - static loadByAccountAndHost (accountId: number, host: string): Bluebird { + static loadByAccountAndHost (accountId: number, host: string): Promise { const query = { where: { accountId @@ -120,22 +120,64 @@ export class ServerBlocklistModel extends Model { return ServerBlocklistModel.findOne(query) } - static listForApi (accountId: number, start: number, count: number, sort: string) { + static listHostsBlockedBy (accountIds: number[]): Promise { + const query = { + attributes: [ ], + where: { + accountId: { + [Op.in]: accountIds + } + }, + include: [ + { + attributes: [ 'host' ], + model: ServerModel.unscoped(), + required: true + } + ] + } + + return ServerBlocklistModel.findAll(query) + .then(entries => 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 + sort: string + search?: string + accountId: number + }) { + const { start, count, sort, search, accountId } = parameters + const query = { offset: start, limit: count, order: getSort(sort), where: { - accountId + accountId, + + ...searchAttribute(search, '$BlockedServer.host$') } } - return ServerBlocklistModel - .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ]) - .findAndCountAll(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 {