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'
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<ServerBlocklistModel> {
53 @ForeignKey(() => AccountModel)
57 @BelongsTo(() => AccountModel, {
64 ByAccount: AccountModel
66 @ForeignKey(() => ServerModel)
68 targetServerId: number
70 @BelongsTo(() => ServerModel, {
76 BlockedServer: ServerModel
78 static loadByAccountAndHost (accountId: number, host: string): Bluebird<MServerBlocklist> {
94 return ServerBlocklistModel.findOne(query)
97 static listForApi (accountId: number, start: number, count: number, sort: string) {
101 order: getSort(sort),
107 return ServerBlocklistModel
108 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
109 .findAndCountAll<MServerBlocklistAccountServer>(query)
110 .then(({ rows, count }) => {
111 return { total: count, data: rows }
115 toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
117 byAccount: this.ByAccount.toFormattedJSON(),
118 blockedServer: this.BlockedServer.toFormattedJSON(),
119 createdAt: this.createdAt