2
1

blocklist.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { sequelizeTypescript } from '@server/initializers/database'
  2. import { getServerActor } from '@server/models/application/application'
  3. import { MAccountBlocklist, MAccountId, MAccountServer, MServerBlocklist } from '@server/types/models'
  4. import { AccountBlocklistModel } from '../models/account/account-blocklist'
  5. import { ServerBlocklistModel } from '../models/server/server-blocklist'
  6. function addAccountInBlocklist (byAccountId: number, targetAccountId: number) {
  7. return sequelizeTypescript.transaction(async t => {
  8. return AccountBlocklistModel.upsert({
  9. accountId: byAccountId,
  10. targetAccountId: targetAccountId
  11. }, { transaction: t })
  12. })
  13. }
  14. function addServerInBlocklist (byAccountId: number, targetServerId: number) {
  15. return sequelizeTypescript.transaction(async t => {
  16. return ServerBlocklistModel.upsert({
  17. accountId: byAccountId,
  18. targetServerId
  19. }, { transaction: t })
  20. })
  21. }
  22. function removeAccountFromBlocklist (accountBlock: MAccountBlocklist) {
  23. return sequelizeTypescript.transaction(async t => {
  24. return accountBlock.destroy({ transaction: t })
  25. })
  26. }
  27. function removeServerFromBlocklist (serverBlock: MServerBlocklist) {
  28. return sequelizeTypescript.transaction(async t => {
  29. return serverBlock.destroy({ transaction: t })
  30. })
  31. }
  32. async function isBlockedByServerOrAccount (targetAccount: MAccountServer, userAccount?: MAccountId) {
  33. const serverAccountId = (await getServerActor()).Account.id
  34. const sourceAccounts = [ serverAccountId ]
  35. if (userAccount) sourceAccounts.push(userAccount.id)
  36. const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, targetAccount.id)
  37. if (accountMutedHash[serverAccountId] || (userAccount && accountMutedHash[userAccount.id])) {
  38. return true
  39. }
  40. const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, targetAccount.Actor.serverId)
  41. if (instanceMutedHash[serverAccountId] || (userAccount && instanceMutedHash[userAccount.id])) {
  42. return true
  43. }
  44. return false
  45. }
  46. export {
  47. addAccountInBlocklist,
  48. addServerInBlocklist,
  49. removeAccountFromBlocklist,
  50. removeServerFromBlocklist,
  51. isBlockedByServerOrAccount
  52. }