1 import { Op, QueryTypes } from 'sequelize'
2 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3 import { handlesToNameAndHost } from '@server/helpers/actors'
4 import { MAccountBlocklist, MAccountBlocklistAccounts, 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 '../utils'
10 import { AccountModel } from './account'
13 WITH_ACCOUNTS = 'WITH_ACCOUNTS'
17 [ScopeNames.WITH_ACCOUNTS]: {
34 tableName: 'accountBlocklist',
37 fields: [ 'accountId', 'targetAccountId' ],
41 fields: [ 'targetAccountId' ]
45 export class AccountBlocklistModel extends Model<Partial<AttributesOnly<AccountBlocklistModel>>> {
53 @ForeignKey(() => AccountModel)
57 @BelongsTo(() => AccountModel, {
65 ByAccount: AccountModel
67 @ForeignKey(() => AccountModel)
69 targetAccountId: number
71 @BelongsTo(() => AccountModel, {
73 name: 'targetAccountId',
79 BlockedAccount: AccountModel
81 static isAccountMutedByAccounts (accountIds: number[], targetAccountId: number) {
83 attributes: [ 'accountId', 'id' ],
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): Promise<MAccountBlocklist> {
114 return AccountBlocklistModel.findOne(query)
117 static listForApi (parameters: {
124 const { start, count, sort, search, accountId } = parameters
137 Object.assign(where, {
139 searchAttribute(search, '$BlockedAccount.name$'),
140 searchAttribute(search, '$BlockedAccount.Actor.url$')
145 Object.assign(query, { where })
147 return AccountBlocklistModel
148 .scope([ ScopeNames.WITH_ACCOUNTS ])
149 .findAndCountAll<MAccountBlocklistAccounts>(query)
150 .then(({ rows, count }) => {
151 return { total: count, data: rows }
155 static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> {
157 attributes: [ 'id' ],
165 attributes: [ 'id' ],
166 model: AccountModel.unscoped(),
168 as: 'BlockedAccount',
171 attributes: [ 'preferredUsername' ],
172 model: ActorModel.unscoped(),
176 attributes: [ 'host' ],
177 model: ServerModel.unscoped(),
187 return AccountBlocklistModel.findAll(query)
188 .then(entries => entries.map(e => `${e.BlockedAccount.Actor.preferredUsername}@${e.BlockedAccount.Actor.Server.host}`))
191 static getBlockStatus (byAccountIds: number[], handles: string[]): Promise<{ name: string, host: string, accountId: number }[]> {
192 const sanitizedHandles = handlesToNameAndHost(handles)
194 const localHandles = sanitizedHandles.filter(h => !h.host)
197 const remoteHandles = sanitizedHandles.filter(h => !!h.host)
198 .map(h => ([ h.name, h.host ]))
200 const handlesWhere: string[] = []
202 if (localHandles.length !== 0) {
203 handlesWhere.push(`("actor"."preferredUsername" IN (:localHandles) AND "server"."id" IS NULL)`)
206 if (remoteHandles.length !== 0) {
207 handlesWhere.push(`(("actor"."preferredUsername", "server"."host") IN (:remoteHandles))`)
210 const rawQuery = `SELECT "accountBlocklist"."accountId", "actor"."preferredUsername" AS "name", "server"."host" ` +
211 `FROM "accountBlocklist" ` +
212 `INNER JOIN "account" ON "account"."id" = "accountBlocklist"."targetAccountId" ` +
213 `INNER JOIN "actor" ON "actor"."id" = "account"."actorId" ` +
214 `LEFT JOIN "server" ON "server"."id" = "actor"."serverId" ` +
215 `WHERE "accountBlocklist"."accountId" IN (${createSafeIn(AccountBlocklistModel.sequelize, byAccountIds)}) ` +
216 `AND (${handlesWhere.join(' OR ')})`
218 return AccountBlocklistModel.sequelize.query(rawQuery, {
219 type: QueryTypes.SELECT as QueryTypes.SELECT,
220 replacements: { byAccountIds, localHandles, remoteHandles }
224 toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock {
226 byAccount: this.ByAccount.toFormattedJSON(),
227 blockedAccount: this.BlockedAccount.toFormattedJSON(),
228 createdAt: this.createdAt