1 import { sequelizeTypescript } from '@server/initializers/database'
2 import { getServerActor } from '@server/models/application/application'
3 import { MAccountBlocklist, MAccountId, MAccountServer, MServerBlocklist } from '@server/types/models'
4 import { AccountBlocklistModel } from '../models/account/account-blocklist'
5 import { ServerBlocklistModel } from '../models/server/server-blocklist'
7 function addAccountInBlocklist (byAccountId: number, targetAccountId: number) {
8 return sequelizeTypescript.transaction(async t => {
9 return AccountBlocklistModel.upsert({
10 accountId: byAccountId,
11 targetAccountId: targetAccountId
12 }, { transaction: t })
16 function addServerInBlocklist (byAccountId: number, targetServerId: number) {
17 return sequelizeTypescript.transaction(async t => {
18 return ServerBlocklistModel.upsert({
19 accountId: byAccountId,
21 }, { transaction: t })
25 function removeAccountFromBlocklist (accountBlock: MAccountBlocklist) {
26 return sequelizeTypescript.transaction(async t => {
27 return accountBlock.destroy({ transaction: t })
31 function removeServerFromBlocklist (serverBlock: MServerBlocklist) {
32 return sequelizeTypescript.transaction(async t => {
33 return serverBlock.destroy({ transaction: t })
37 async function isBlockedByServerOrAccount (targetAccount: MAccountServer, userAccount?: MAccountId) {
38 const serverAccountId = (await getServerActor()).Account.id
39 const sourceAccounts = [ serverAccountId ]
41 if (userAccount) sourceAccounts.push(userAccount.id)
43 const accountMutedHash = await AccountBlocklistModel.isAccountMutedByAccounts(sourceAccounts, targetAccount.id)
44 if (accountMutedHash[serverAccountId] || (userAccount && accountMutedHash[userAccount.id])) {
48 const instanceMutedHash = await ServerBlocklistModel.isServerMutedByAccounts(sourceAccounts, targetAccount.Actor.serverId)
49 if (instanceMutedHash[serverAccountId] || (userAccount && instanceMutedHash[userAccount.id])) {
57 addAccountInBlocklist,
59 removeAccountFromBlocklist,
60 removeServerFromBlocklist,
61 isBlockedByServerOrAccount