1 import { Op } from 'sequelize'
2 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/types/models'
4 import { AttributesOnly } from '@shared/core-utils'
5 import { ServerBlock } from '@shared/models'
6 import { AccountModel } from '../account/account'
7 import { getSort, searchAttribute } from '../utils'
8 import { ServerModel } from './server'
11 WITH_ACCOUNT = 'WITH_ACCOUNT',
12 WITH_SERVER = 'WITH_SERVER'
16 [ScopeNames.WITH_ACCOUNT]: {
24 [ScopeNames.WITH_SERVER]: {
35 tableName: 'serverBlocklist',
38 fields: [ 'accountId', 'targetServerId' ],
42 fields: [ 'targetServerId' ]
46 export class ServerBlocklistModel extends Model<Partial<AttributesOnly<ServerBlocklistModel>>> {
54 @ForeignKey(() => AccountModel)
58 @BelongsTo(() => AccountModel, {
65 ByAccount: AccountModel
67 @ForeignKey(() => ServerModel)
69 targetServerId: number
71 @BelongsTo(() => ServerModel, {
77 BlockedServer: ServerModel
79 static isServerMutedByMulti (accountIds: number[], targetServerId: number) {
81 attributes: [ 'accountId', 'id' ],
91 return ServerBlocklistModel.unscoped()
94 const result: { [accountId: number]: boolean } = {}
96 for (const accountId of accountIds) {
97 result[accountId] = !!rows.find(r => r.accountId === accountId)
104 static loadByAccountAndHost (accountId: number, host: string): Promise<MServerBlocklist> {
120 return ServerBlocklistModel.findOne(query)
123 static listHostsBlockedBy (accountIds: number[]): Promise<string[]> {
133 attributes: [ 'host' ],
134 model: ServerModel.unscoped(),
140 return ServerBlocklistModel.findAll(query)
141 .then(entries => entries.map(e => e.BlockedServer.host))
144 static listForApi (parameters: {
151 const { start, count, sort, search, accountId } = parameters
156 order: getSort(sort),
159 ...searchAttribute(search, '$BlockedServer.host$')
163 return ServerBlocklistModel
164 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
165 .findAndCountAll<MServerBlocklistAccountServer>(query)
166 .then(({ rows, count }) => {
167 return { total: count, data: rows }
171 toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
173 byAccount: this.ByAccount.toFormattedJSON(),
174 blockedServer: this.BlockedServer.toFormattedJSON(),
175 createdAt: this.createdAt