1 import { Op } from 'sequelize'
2 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/types/models'
4 import { AttributesOnly } from '@shared/core-utils'
5 import { AccountBlock } from '../../../shared/models'
6 import { ActorModel } from '../actor/actor'
7 import { ServerModel } from '../server/server'
8 import { getSort, searchAttribute } from '../utils'
9 import { AccountModel } from './account'
12 WITH_ACCOUNTS = 'WITH_ACCOUNTS'
16 [ScopeNames.WITH_ACCOUNTS]: {
33 tableName: 'accountBlocklist',
36 fields: [ 'accountId', 'targetAccountId' ],
40 fields: [ 'targetAccountId' ]
44 export class AccountBlocklistModel extends Model<Partial<AttributesOnly<AccountBlocklistModel>>> {
52 @ForeignKey(() => AccountModel)
56 @BelongsTo(() => AccountModel, {
64 ByAccount: AccountModel
66 @ForeignKey(() => AccountModel)
68 targetAccountId: number
70 @BelongsTo(() => AccountModel, {
72 name: 'targetAccountId',
78 BlockedAccount: AccountModel
80 static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) {
82 attributes: [ 'accountId', 'id' ],
92 return AccountBlocklistModel.unscoped()
95 const result: { [accountId: number]: boolean } = {}
97 for (const accountId of accountIds) {
98 result[accountId] = !!rows.find(r => r.accountId === accountId)
105 static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<MAccountBlocklist> {
113 return AccountBlocklistModel.findOne(query)
116 static listForApi (parameters: {
123 const { start, count, sort, search, accountId } = parameters
136 Object.assign(where, {
138 searchAttribute(search, '$BlockedAccount.name$'),
139 searchAttribute(search, '$BlockedAccount.Actor.url$')
144 Object.assign(query, { where })
146 return AccountBlocklistModel
147 .scope([ ScopeNames.WITH_ACCOUNTS ])
148 .findAndCountAll<MAccountBlocklistAccounts>(query)
149 .then(({ rows, count }) => {
150 return { total: count, data: rows }
154 static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> {
156 attributes: [ 'id' ],
164 attributes: [ 'id' ],
165 model: AccountModel.unscoped(),
167 as: 'BlockedAccount',
170 attributes: [ 'preferredUsername' ],
171 model: ActorModel.unscoped(),
175 attributes: [ 'host' ],
176 model: ServerModel.unscoped(),
186 return AccountBlocklistModel.findAll(query)
187 .then(entries => entries.map(e => `${e.BlockedAccount.Actor.preferredUsername}@${e.BlockedAccount.Actor.Server.host}`))
190 toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock {
192 byAccount: this.ByAccount.toFormattedJSON(),
193 blockedAccount: this.BlockedAccount.toFormattedJSON(),
194 createdAt: this.createdAt