1 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2 import { AccountModel } from '../account/account'
3 import { ServerModel } from './server'
4 import { ServerBlock } from '../../../shared/models/blocklist'
5 import { getSort, searchAttribute } from '../utils'
6 import * as Bluebird from 'bluebird'
7 import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/typings/models'
8 import { Op } from 'sequelize'
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<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): Bluebird<MServerBlocklist> {
120 return ServerBlocklistModel.findOne(query)
123 static listForApi (parameters: {
130 const { start, count, sort, search, accountId } = parameters
135 order: getSort(sort),
138 ...searchAttribute(search, '$BlockedServer.host$')
142 return ServerBlocklistModel
143 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
144 .findAndCountAll<MServerBlocklistAccountServer>(query)
145 .then(({ rows, count }) => {
146 return { total: count, data: rows }
150 toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
152 byAccount: this.ByAccount.toFormattedJSON(),
153 blockedServer: this.BlockedServer.toFormattedJSON(),
154 createdAt: this.createdAt