X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fblocklist.ts;h=a11b717b548414daa8478fcd3ed6d73b5a4b2176;hb=bd911b54b555b11df7e9849cf92d358bccfecf6e;hp=1633e500cb34964b9d9252adfc43c6f41ce6c9b3;hpb=af5767ffae41b2d5604e41ba9a7225c623dd6735;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/blocklist.ts b/server/lib/blocklist.ts index 1633e500c..a11b717b5 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' @@ -6,7 +8,7 @@ function addAccountInBlocklist (byAccountId: number, targetAccountId: number) { return sequelizeTypescript.transaction(async t => { return AccountBlocklistModel.upsert({ accountId: byAccountId, - targetAccountId: targetAccountId + targetAccountId }, { transaction: t }) }) } @@ -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.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 + removeServerFromBlocklist, + isBlockedByServerOrAccount }