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 } 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' ],
84 [Op.in]: accountIds // FIXME: sequelize ANY seems broken
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 (accountId: number, start: number, count: number, sort: string) {
127 order: getSort(sort),
133 return ServerBlocklistModel
134 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
135 .findAndCountAll<MServerBlocklistAccountServer>(query)
136 .then(({ rows, count }) => {
137 return { total: count, data: rows }
141 toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
143 byAccount: this.ByAccount.toFormattedJSON(),
144 blockedServer: this.BlockedServer.toFormattedJSON(),
145 createdAt: this.createdAt