2020-05-07 14:58:24 +02:00
|
|
|
import { sequelizeTypescript } from '@server/initializers/database'
|
2020-05-22 17:06:26 +02:00
|
|
|
import { getServerActor } from '@server/models/application/application'
|
2020-06-18 10:45:25 +02:00
|
|
|
import { MAccountBlocklist, MAccountId, MAccountServer, MServerBlocklist } from '@server/types/models'
|
2018-10-12 15:26:04 +02:00
|
|
|
import { AccountBlocklistModel } from '../models/account/account-blocklist'
|
|
|
|
import { ServerBlocklistModel } from '../models/server/server-blocklist'
|
|
|
|
|
|
|
|
function addAccountInBlocklist (byAccountId: number, targetAccountId: number) {
|
|
|
|
return sequelizeTypescript.transaction(async t => {
|
2018-10-12 17:26:40 +02:00
|
|
|
return AccountBlocklistModel.upsert({
|
2018-10-12 15:26:04 +02:00
|
|
|
accountId: byAccountId,
|
2022-07-13 11:58:01 +02:00
|
|
|
targetAccountId
|
2018-10-12 15:26:04 +02:00
|
|
|
}, { transaction: t })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function addServerInBlocklist (byAccountId: number, targetServerId: number) {
|
|
|
|
return sequelizeTypescript.transaction(async t => {
|
2018-10-12 17:26:40 +02:00
|
|
|
return ServerBlocklistModel.upsert({
|
2018-10-12 15:26:04 +02:00
|
|
|
accountId: byAccountId,
|
|
|
|
targetServerId
|
|
|
|
}, { transaction: t })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
function removeAccountFromBlocklist (accountBlock: MAccountBlocklist) {
|
2018-10-12 15:26:04 +02:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
return accountBlock.destroy({ transaction: t })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
function removeServerFromBlocklist (serverBlock: MServerBlocklist) {
|
2018-10-12 15:26:04 +02:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
return serverBlock.destroy({ transaction: t })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:06:26 +02:00
|
|
|
async function isBlockedByServerOrAccount (targetAccount: MAccountServer, userAccount?: MAccountId) {
|
|
|
|
const serverAccountId = (await getServerActor()).Account.id
|
|
|
|
const sourceAccounts = [ serverAccountId ]
|
|
|
|
|
|
|
|
if (userAccount) sourceAccounts.push(userAccount.id)
|
|
|
|
|
2021-12-06 16:53:00 +01:00
|
|
|
const accountMutedHash = await AccountBlocklistModel.isAccountMutedByAccounts(sourceAccounts, targetAccount.id)
|
2020-05-22 17:06:26 +02:00
|
|
|
if (accountMutedHash[serverAccountId] || (userAccount && accountMutedHash[userAccount.id])) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-12-06 16:53:00 +01:00
|
|
|
const instanceMutedHash = await ServerBlocklistModel.isServerMutedByAccounts(sourceAccounts, targetAccount.Actor.serverId)
|
2020-05-22 17:06:26 +02:00
|
|
|
if (instanceMutedHash[serverAccountId] || (userAccount && instanceMutedHash[userAccount.id])) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-12 15:26:04 +02:00
|
|
|
export {
|
|
|
|
addAccountInBlocklist,
|
|
|
|
addServerInBlocklist,
|
|
|
|
removeAccountFromBlocklist,
|
2020-05-22 17:06:26 +02:00
|
|
|
removeServerFromBlocklist,
|
|
|
|
isBlockedByServerOrAccount
|
2018-10-12 15:26:04 +02:00
|
|
|
}
|