1
0
Fork 0
peertube/server/lib/blocklist.ts

63 lines
2.1 KiB
TypeScript
Raw Normal View History

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