X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fblocklist.ts;h=d6b684015cb6e3f0fcf3563ae7c28b3ffb5be408;hb=26d6bf6533023326fa017812cf31bbe20c752d36;hp=1633e500cb34964b9d9252adfc43c6f41ce6c9b3;hpb=88108880bbdba473cfe36ecbebc1c3c4f972e102;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/blocklist.ts b/server/lib/blocklist.ts index 1633e500c..d6b684015 100644 --- a/server/lib/blocklist.ts +++ b/server/lib/blocklist.ts @@ -1,4 +1,6 @@ -import { sequelizeTypescript } from '../initializers' +import { sequelizeTypescript } from '@server/initializers/database' +import { getServerActor } from '@server/models/application/application' +import { MAccountBlocklist, MAccountId, MAccountServer, MServerBlocklist } from '@server/types/models' import { AccountBlocklistModel } from '../models/account/account-blocklist' import { ServerBlocklistModel } from '../models/server/server-blocklist' @@ -20,21 +22,41 @@ function addServerInBlocklist (byAccountId: number, targetServerId: number) { }) } -function removeAccountFromBlocklist (accountBlock: AccountBlocklistModel) { +function removeAccountFromBlocklist (accountBlock: MAccountBlocklist) { return sequelizeTypescript.transaction(async t => { return accountBlock.destroy({ transaction: t }) }) } -function removeServerFromBlocklist (serverBlock: ServerBlocklistModel) { +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.isAccountMutedByMulti(sourceAccounts, targetAccount.id) + if (accountMutedHash[serverAccountId] || (userAccount && accountMutedHash[userAccount.id])) { + return true + } + + const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, targetAccount.Actor.serverId) + if (instanceMutedHash[serverAccountId] || (userAccount && instanceMutedHash[userAccount.id])) { + return true + } + + return false +} + export { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, - removeServerFromBlocklist + removeServerFromBlocklist, + isBlockedByServerOrAccount }