X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fserver%2Fserver-blocklist.ts;h=092998db33c6682ee1cbe0bdf17c0e18f4ad1b20;hb=636d73c58866ed235c207719e41fdde3c2d6c969;hp=b88df4fd52700d072082f4c1b4037e7d39b1258c;hpb=dddc8b1fe0c875f41c88f395b0d55cfea69add2c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts index b88df4fd5..092998db3 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 { AttributesOnly } from '@shared/core-utils' +import { ServerBlock } from '@shared/models' 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,12 +76,12 @@ 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: { accountId: { - [Op.in]: accountIds // FIXME: sequelize ANY seems broken + [Op.in]: accountIds }, targetServerId }, @@ -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,13 +120,56 @@ 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$') } }