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'
8 WITH_ACCOUNTS = 'WITH_ACCOUNTS'
12 [ScopeNames.WITH_ACCOUNTS]: {
29 tableName: 'accountBlocklist',
32 fields: [ 'accountId', 'targetAccountId' ],
36 fields: [ 'targetAccountId' ]
40 export class AccountBlocklistModel extends Model<AccountBlocklistModel> {
48 @ForeignKey(() => AccountModel)
52 @BelongsTo(() => AccountModel, {
60 ByAccount: AccountModel
62 @ForeignKey(() => AccountModel)
64 targetAccountId: number
66 @BelongsTo(() => AccountModel, {
68 name: 'targetAccountId',
74 BlockedAccount: AccountModel
76 static isAccountMutedBy (accountId: number, targetAccountId: number) {
77 return AccountBlocklistModel.isAccountMutedByMulti([ accountId ], targetAccountId)
78 .then(result => result[accountId])
81 static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) {
83 attributes: [ 'accountId', 'id' ],
86 [Op.in]: accountIds // FIXME: sequelize ANY seems broken
93 return AccountBlocklistModel.unscoped()
96 const result: { [accountId: number]: boolean } = {}
98 for (const accountId of accountIds) {
99 result[accountId] = !!rows.find(r => r.accountId === accountId)
106 static loadByAccountAndTarget (accountId: number, targetAccountId: number) {
114 return AccountBlocklistModel.findOne(query)
117 static listForApi (accountId: number, start: number, count: number, sort: string) {
121 order: getSort(sort),
127 return AccountBlocklistModel
128 .scope([ ScopeNames.WITH_ACCOUNTS ])
129 .findAndCountAll(query)
130 .then(({ rows, count }) => {
131 return { total: count, data: rows }
135 toFormattedJSON (): AccountBlock {
137 byAccount: this.ByAccount.toFormattedJSON(),
138 blockedAccount: this.BlockedAccount.toFormattedJSON(),
139 createdAt: this.createdAt