blocklist.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { sequelizeTypescript } from '@server/initializers/database.js'
  2. import { getServerActor } from '@server/models/application/application.js'
  3. import { MAccountBlocklist, MAccountId, MAccountHost, MServerBlocklist } from '@server/types/models/index.js'
  4. import { AccountBlocklistModel } from '../models/account/account-blocklist.js'
  5. import { ServerBlocklistModel } from '../models/server/server-blocklist.js'
  6. import { UserNotificationModel } from '@server/models/user/user-notification.js'
  7. import { logger } from '@server/helpers/logger.js'
  8. async function addAccountInBlocklist (options: {
  9. byAccountId: number
  10. targetAccountId: number
  11. removeNotificationOfUserId: number | null // If blocked by a user
  12. }) {
  13. const { byAccountId, targetAccountId, removeNotificationOfUserId } = options
  14. await sequelizeTypescript.transaction(async t => {
  15. return AccountBlocklistModel.upsert({
  16. accountId: byAccountId,
  17. targetAccountId
  18. }, { transaction: t })
  19. })
  20. UserNotificationModel.removeNotificationsOf({
  21. id: targetAccountId,
  22. type: 'account',
  23. forUserId: removeNotificationOfUserId
  24. }).catch(err => logger.error('Cannot remove notifications after an account mute.', { err }))
  25. }
  26. async function addServerInBlocklist (options: {
  27. byAccountId: number
  28. targetServerId: number
  29. removeNotificationOfUserId: number | null
  30. }) {
  31. const { byAccountId, targetServerId, removeNotificationOfUserId } = options
  32. await sequelizeTypescript.transaction(async t => {
  33. return ServerBlocklistModel.upsert({
  34. accountId: byAccountId,
  35. targetServerId
  36. }, { transaction: t })
  37. })
  38. UserNotificationModel.removeNotificationsOf({
  39. id: targetServerId,
  40. type: 'server',
  41. forUserId: removeNotificationOfUserId
  42. }).catch(err => logger.error('Cannot remove notifications after a server mute.', { err }))
  43. }
  44. function removeAccountFromBlocklist (accountBlock: MAccountBlocklist) {
  45. return sequelizeTypescript.transaction(async t => {
  46. return accountBlock.destroy({ transaction: t })
  47. })
  48. }
  49. function removeServerFromBlocklist (serverBlock: MServerBlocklist) {
  50. return sequelizeTypescript.transaction(async t => {
  51. return serverBlock.destroy({ transaction: t })
  52. })
  53. }
  54. async function isBlockedByServerOrAccount (targetAccount: MAccountHost, userAccount?: MAccountId) {
  55. const serverAccountId = (await getServerActor()).Account.id
  56. const sourceAccounts = [ serverAccountId ]
  57. if (userAccount) sourceAccounts.push(userAccount.id)
  58. const accountMutedHash = await AccountBlocklistModel.isAccountMutedByAccounts(sourceAccounts, targetAccount.id)
  59. if (accountMutedHash[serverAccountId] || (userAccount && accountMutedHash[userAccount.id])) {
  60. return true
  61. }
  62. const instanceMutedHash = await ServerBlocklistModel.isServerMutedByAccounts(sourceAccounts, targetAccount.Actor.serverId)
  63. if (instanceMutedHash[serverAccountId] || (userAccount && instanceMutedHash[userAccount.id])) {
  64. return true
  65. }
  66. return false
  67. }
  68. export {
  69. addAccountInBlocklist,
  70. addServerInBlocklist,
  71. removeAccountFromBlocklist,
  72. removeServerFromBlocklist,
  73. isBlockedByServerOrAccount
  74. }