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'
8 WITH_ACCOUNT = 'WITH_ACCOUNT',
9 WITH_SERVER = 'WITH_SERVER'
13 [ScopeNames.WITH_ACCOUNT]: {
21 [ScopeNames.WITH_SERVER]: {
32 tableName: 'serverBlocklist',
35 fields: [ 'accountId', 'targetServerId' ],
39 fields: [ 'targetServerId' ]
43 export class ServerBlocklistModel extends Model<ServerBlocklistModel> {
51 @ForeignKey(() => AccountModel)
55 @BelongsTo(() => AccountModel, {
62 ByAccount: AccountModel
64 @ForeignKey(() => ServerModel)
66 targetServerId: number
68 @BelongsTo(() => ServerModel, {
70 name: 'targetServerId',
75 BlockedServer: ServerModel
77 static loadByAccountAndHost (accountId: number, host: string) {
93 return ServerBlocklistModel.findOne(query)
96 static listForApi (accountId: number, start: number, count: number, sort: string) {
100 order: getSort(sort),
106 return ServerBlocklistModel
107 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
108 .findAndCountAll(query)
109 .then(({ rows, count }) => {
110 return { total: count, data: rows }
114 toFormattedJSON (): ServerBlock {
116 byAccount: this.ByAccount.toFormattedJSON(),
117 blockedServer: this.BlockedServer.toFormattedJSON(),
118 createdAt: this.createdAt