1 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2 import { AccountModel } from './account'
3 import { getSort } from '../utils'
4 import { AccountBlock } from '../../../shared/models/blocklist'
5 import { Op } from 'sequelize'
6 import * as Bluebird from 'bluebird'
7 import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/typings/models'
10 WITH_ACCOUNTS = 'WITH_ACCOUNTS'
14 [ScopeNames.WITH_ACCOUNTS]: {
31 tableName: 'accountBlocklist',
34 fields: [ 'accountId', 'targetAccountId' ],
38 fields: [ 'targetAccountId' ]
42 export class AccountBlocklistModel extends Model<AccountBlocklistModel> {
50 @ForeignKey(() => AccountModel)
54 @BelongsTo(() => AccountModel, {
62 ByAccount: AccountModel
64 @ForeignKey(() => AccountModel)
66 targetAccountId: number
68 @BelongsTo(() => AccountModel, {
70 name: 'targetAccountId',
76 BlockedAccount: AccountModel
78 static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) {
80 attributes: [ 'accountId', 'id' ],
90 return AccountBlocklistModel.unscoped()
93 const result: { [accountId: number]: boolean } = {}
95 for (const accountId of accountIds) {
96 result[accountId] = !!rows.find(r => r.accountId === accountId)
103 static loadByAccountAndTarget (accountId: number, targetAccountId: number): Bluebird<MAccountBlocklist> {
111 return AccountBlocklistModel.findOne(query)
114 static listForApi (accountId: number, start: number, count: number, sort: string) {
118 order: getSort(sort),
124 return AccountBlocklistModel
125 .scope([ ScopeNames.WITH_ACCOUNTS ])
126 .findAndCountAll<MAccountBlocklistAccounts>(query)
127 .then(({ rows, count }) => {
128 return { total: count, data: rows }
132 toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock {
134 byAccount: this.ByAccount.toFormattedJSON(),
135 blockedAccount: this.BlockedAccount.toFormattedJSON(),
136 createdAt: this.createdAt