1 import { FindOptions, Op, QueryTypes } from 'sequelize'
2 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { handlesToNameAndHost } from '@server/helpers/actors'
4 import { MAccountBlocklist, MAccountBlocklistFormattable } from '@server/types/models'
5 import { AttributesOnly } from '@shared/typescript-utils'
6 import { AccountBlock } from '../../../shared/models'
7 import { ActorModel } from '../actor/actor'
8 import { ServerModel } from '../server/server'
9 import { createSafeIn, getSort, searchAttribute } from '../shared'
10 import { AccountModel } from './account'
13 tableName: 'accountBlocklist',
16 fields: [ 'accountId', 'targetAccountId' ],
20 fields: [ 'targetAccountId' ]
24 export class AccountBlocklistModel extends Model<Partial<AttributesOnly<AccountBlocklistModel>>> {
32 @ForeignKey(() => AccountModel)
36 @BelongsTo(() => AccountModel, {
44 ByAccount: AccountModel
46 @ForeignKey(() => AccountModel)
48 targetAccountId: number
50 @BelongsTo(() => AccountModel, {
52 name: 'targetAccountId',
58 BlockedAccount: AccountModel
60 static isAccountMutedByAccounts (accountIds: number[], targetAccountId: number) {
62 attributes: [ 'accountId', 'id' ],
72 return AccountBlocklistModel.unscoped()
75 const result: { [accountId: number]: boolean } = {}
77 for (const accountId of accountIds) {
78 result[accountId] = !!rows.find(r => r.accountId === accountId)
85 static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<MAccountBlocklist> {
93 return AccountBlocklistModel.findOne(query)
96 static listForApi (parameters: {
103 const { start, count, sort, search, accountId } = parameters
105 const getQuery = (forCount: boolean) => {
106 const query: FindOptions = {
109 order: getSort(sort),
114 Object.assign(query.where, {
116 searchAttribute(search, '$BlockedAccount.name$'),
117 searchAttribute(search, '$BlockedAccount.Actor.url$')
122 if (forCount !== true) {
135 } else if (search) { // We need some joins when counting with search
138 model: AccountModel.unscoped(),
140 as: 'BlockedAccount',
143 model: ActorModel.unscoped(),
155 AccountBlocklistModel.count(getQuery(true)),
156 AccountBlocklistModel.findAll(getQuery(false))
157 ]).then(([ total, data ]) => ({ total, data }))
160 static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> {
162 attributes: [ 'id' ],
170 attributes: [ 'id' ],
171 model: AccountModel.unscoped(),
173 as: 'BlockedAccount',
176 attributes: [ 'preferredUsername' ],
177 model: ActorModel.unscoped(),
181 attributes: [ 'host' ],
182 model: ServerModel.unscoped(),
192 return AccountBlocklistModel.findAll(query)
193 .then(entries => entries.map(e => `${e.BlockedAccount.Actor.preferredUsername}@${e.BlockedAccount.Actor.Server.host}`))
196 static getBlockStatus (byAccountIds: number[], handles: string[]): Promise<{ name: string, host: string, accountId: number }[]> {
197 const sanitizedHandles = handlesToNameAndHost(handles)
199 const localHandles = sanitizedHandles.filter(h => !h.host)
202 const remoteHandles = sanitizedHandles.filter(h => !!h.host)
203 .map(h => ([ h.name, h.host ]))
205 const handlesWhere: string[] = []
207 if (localHandles.length !== 0) {
208 handlesWhere.push(`("actor"."preferredUsername" IN (:localHandles) AND "server"."id" IS NULL)`)
211 if (remoteHandles.length !== 0) {
212 handlesWhere.push(`(("actor"."preferredUsername", "server"."host") IN (:remoteHandles))`)
215 const rawQuery = `SELECT "accountBlocklist"."accountId", "actor"."preferredUsername" AS "name", "server"."host" ` +
216 `FROM "accountBlocklist" ` +
217 `INNER JOIN "account" ON "account"."id" = "accountBlocklist"."targetAccountId" ` +
218 `INNER JOIN "actor" ON "actor"."id" = "account"."actorId" ` +
219 `LEFT JOIN "server" ON "server"."id" = "actor"."serverId" ` +
220 `WHERE "accountBlocklist"."accountId" IN (${createSafeIn(AccountBlocklistModel.sequelize, byAccountIds)}) ` +
221 `AND (${handlesWhere.join(' OR ')})`
223 return AccountBlocklistModel.sequelize.query(rawQuery, {
224 type: QueryTypes.SELECT as QueryTypes.SELECT,
225 replacements: { byAccountIds, localHandles, remoteHandles }
229 toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock {
231 byAccount: this.ByAccount.toFormattedJSON(),
232 blockedAccount: this.BlockedAccount.toFormattedJSON(),
233 createdAt: this.createdAt