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 { ServerBlock } from '@shared/models'
5 import { AccountModel } from '../account/account'
6 import { getSort, searchAttribute } from '../utils'
7 import { ServerModel } from './server'
10 WITH_ACCOUNT = 'WITH_ACCOUNT',
11 WITH_SERVER = 'WITH_SERVER'
15 [ScopeNames.WITH_ACCOUNT]: {
23 [ScopeNames.WITH_SERVER]: {
34 tableName: 'serverBlocklist',
37 fields: [ 'accountId', 'targetServerId' ],
41 fields: [ 'targetServerId' ]
45 export class ServerBlocklistModel extends Model {
53 @ForeignKey(() => AccountModel)
57 @BelongsTo(() => AccountModel, {
64 ByAccount: AccountModel
66 @ForeignKey(() => ServerModel)
68 targetServerId: number
70 @BelongsTo(() => ServerModel, {
76 BlockedServer: ServerModel
78 static isServerMutedByMulti (accountIds: number[], targetServerId: number) {
80 attributes: [ 'accountId', 'id' ],
90 return ServerBlocklistModel.unscoped()
93 const result: { [accountId: number]: boolean } = {}
95 for (const accountId of accountIds) {
96 result[accountId] = !!rows.find(r => r.accountId === accountId)
103 static loadByAccountAndHost (accountId: number, host: string): Promise<MServerBlocklist> {
119 return ServerBlocklistModel.findOne(query)
122 static listHostsBlockedBy (accountIds: number[]): Promise<string[]> {
132 attributes: [ 'host' ],
133 model: ServerModel.unscoped(),
139 return ServerBlocklistModel.findAll(query)
140 .then(entries => entries.map(e => e.BlockedServer.host))
143 static listForApi (parameters: {
150 const { start, count, sort, search, accountId } = parameters
155 order: getSort(sort),
158 ...searchAttribute(search, '$BlockedServer.host$')
162 return ServerBlocklistModel
163 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
164 .findAndCountAll<MServerBlocklistAccountServer>(query)
165 .then(({ rows, count }) => {
166 return { total: count, data: rows }
170 toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
172 byAccount: this.ByAccount.toFormattedJSON(),
173 blockedServer: this.BlockedServer.toFormattedJSON(),
174 createdAt: this.createdAt