]>
Commit | Line | Data |
---|---|---|
80fdaf06 | 1 | import { sequelizeTypescript } from '@server/initializers/database' |
696d83fd | 2 | import { getServerActor } from '@server/models/application/application' |
cb0eda56 | 3 | import { MAccountBlocklist, MAccountId, MAccountHost, MServerBlocklist } from '@server/types/models' |
7ad9b984 C |
4 | import { AccountBlocklistModel } from '../models/account/account-blocklist' |
5 | import { ServerBlocklistModel } from '../models/server/server-blocklist' | |
6 | ||
7 | function addAccountInBlocklist (byAccountId: number, targetAccountId: number) { | |
8 | return sequelizeTypescript.transaction(async t => { | |
af5767ff | 9 | return AccountBlocklistModel.upsert({ |
7ad9b984 | 10 | accountId: byAccountId, |
ba2684ce | 11 | targetAccountId |
7ad9b984 C |
12 | }, { transaction: t }) |
13 | }) | |
14 | } | |
15 | ||
16 | function addServerInBlocklist (byAccountId: number, targetServerId: number) { | |
17 | return sequelizeTypescript.transaction(async t => { | |
af5767ff | 18 | return ServerBlocklistModel.upsert({ |
7ad9b984 C |
19 | accountId: byAccountId, |
20 | targetServerId | |
21 | }, { transaction: t }) | |
22 | }) | |
23 | } | |
24 | ||
453e83ea | 25 | function removeAccountFromBlocklist (accountBlock: MAccountBlocklist) { |
7ad9b984 C |
26 | return sequelizeTypescript.transaction(async t => { |
27 | return accountBlock.destroy({ transaction: t }) | |
28 | }) | |
29 | } | |
30 | ||
453e83ea | 31 | function removeServerFromBlocklist (serverBlock: MServerBlocklist) { |
7ad9b984 C |
32 | return sequelizeTypescript.transaction(async t => { |
33 | return serverBlock.destroy({ transaction: t }) | |
34 | }) | |
35 | } | |
36 | ||
cb0eda56 | 37 | async function isBlockedByServerOrAccount (targetAccount: MAccountHost, userAccount?: MAccountId) { |
696d83fd C |
38 | const serverAccountId = (await getServerActor()).Account.id |
39 | const sourceAccounts = [ serverAccountId ] | |
40 | ||
41 | if (userAccount) sourceAccounts.push(userAccount.id) | |
42 | ||
80badf49 | 43 | const accountMutedHash = await AccountBlocklistModel.isAccountMutedByAccounts(sourceAccounts, targetAccount.id) |
696d83fd C |
44 | if (accountMutedHash[serverAccountId] || (userAccount && accountMutedHash[userAccount.id])) { |
45 | return true | |
46 | } | |
47 | ||
80badf49 | 48 | const instanceMutedHash = await ServerBlocklistModel.isServerMutedByAccounts(sourceAccounts, targetAccount.Actor.serverId) |
696d83fd C |
49 | if (instanceMutedHash[serverAccountId] || (userAccount && instanceMutedHash[userAccount.id])) { |
50 | return true | |
51 | } | |
52 | ||
53 | return false | |
54 | } | |
55 | ||
7ad9b984 C |
56 | export { |
57 | addAccountInBlocklist, | |
58 | addServerInBlocklist, | |
59 | removeAccountFromBlocklist, | |
696d83fd C |
60 | removeServerFromBlocklist, |
61 | isBlockedByServerOrAccount | |
7ad9b984 | 62 | } |