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 { AccountBlock } from '../../../shared/models'
5 import { ActorModel } from '../activitypub/actor'
6 import { ServerModel } from '../server/server'
7 import { getSort, searchAttribute } from '../utils'
8 import { AccountModel } from './account'
11 WITH_ACCOUNTS = 'WITH_ACCOUNTS'
15 [ScopeNames.WITH_ACCOUNTS]: {
32 tableName: 'accountBlocklist',
35 fields: [ 'accountId', 'targetAccountId' ],
39 fields: [ 'targetAccountId' ]
43 export class AccountBlocklistModel extends Model {
51 @ForeignKey(() => AccountModel)
55 @BelongsTo(() => AccountModel, {
63 ByAccount: AccountModel
65 @ForeignKey(() => AccountModel)
67 targetAccountId: number
69 @BelongsTo(() => AccountModel, {
71 name: 'targetAccountId',
77 BlockedAccount: AccountModel
79 static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) {
81 attributes: [ 'accountId', 'id' ],
91 return AccountBlocklistModel.unscoped()
94 const result: { [accountId: number]: boolean } = {}
96 for (const accountId of accountIds) {
97 result[accountId] = !!rows.find(r => r.accountId === accountId)
104 static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<MAccountBlocklist> {
112 return AccountBlocklistModel.findOne(query)
115 static listForApi (parameters: {
122 const { start, count, sort, search, accountId } = parameters
135 Object.assign(where, {
137 searchAttribute(search, '$BlockedAccount.name$'),
138 searchAttribute(search, '$BlockedAccount.Actor.url$')
143 Object.assign(query, { where })
145 return AccountBlocklistModel
146 .scope([ ScopeNames.WITH_ACCOUNTS ])
147 .findAndCountAll<MAccountBlocklistAccounts>(query)
148 .then(({ rows, count }) => {
149 return { total: count, data: rows }
153 static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> {
155 attributes: [ 'id' ],
163 attributes: [ 'id' ],
164 model: AccountModel.unscoped(),
166 as: 'BlockedAccount',
169 attributes: [ 'preferredUsername' ],
170 model: ActorModel.unscoped(),
174 attributes: [ 'host' ],
175 model: ServerModel.unscoped(),
185 return AccountBlocklistModel.findAll(query)
186 .then(entries => entries.map(e => `${e.BlockedAccount.Actor.preferredUsername}@${e.BlockedAccount.Actor.Server.host}`))
189 toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock {
191 byAccount: this.ByAccount.toFormattedJSON(),
192 blockedAccount: this.BlockedAccount.toFormattedJSON(),
193 createdAt: this.createdAt