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, {
74 BlockedServer: ServerModel
76 static loadByAccountAndHost (accountId: number, host: string) {
92 return ServerBlocklistModel.findOne(query)
95 static listForApi (accountId: number, start: number, count: number, sort: string) {
105 return ServerBlocklistModel
106 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
107 .findAndCountAll(query)
108 .then(({ rows, count }) => {
109 return { total: count, data: rows }
113 toFormattedJSON (): ServerBlock {
115 byAccount: this.ByAccount.toFormattedJSON(),
116 blockedServer: this.BlockedServer.toFormattedJSON(),
117 createdAt: this.createdAt