blocklist.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { body, param } from 'express-validator'
  2. import * as express from 'express'
  3. import { logger } from '../../helpers/logger'
  4. import { areValidationErrors } from './utils'
  5. import { AccountBlocklistModel } from '../../models/account/account-blocklist'
  6. import { isHostValid } from '../../helpers/custom-validators/servers'
  7. import { ServerBlocklistModel } from '../../models/server/server-blocklist'
  8. import { ServerModel } from '../../models/server/server'
  9. import { getServerActor } from '../../helpers/utils'
  10. import { WEBSERVER } from '../../initializers/constants'
  11. import { doesAccountNameWithHostExist } from '../../helpers/middlewares'
  12. const blockAccountValidator = [
  13. body('accountName').exists().withMessage('Should have an account name with host'),
  14. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  15. logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body })
  16. if (areValidationErrors(req, res)) return
  17. if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return
  18. const user = res.locals.oauth.token.User
  19. const accountToBlock = res.locals.account
  20. if (user.Account.id === accountToBlock.id) {
  21. res.status(409)
  22. .send({ error: 'You cannot block yourself.' })
  23. .end()
  24. return
  25. }
  26. return next()
  27. }
  28. ]
  29. const unblockAccountByAccountValidator = [
  30. param('accountName').exists().withMessage('Should have an account name with host'),
  31. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  32. logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params })
  33. if (areValidationErrors(req, res)) return
  34. if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
  35. const user = res.locals.oauth.token.User
  36. const targetAccount = res.locals.account
  37. if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return
  38. return next()
  39. }
  40. ]
  41. const unblockAccountByServerValidator = [
  42. param('accountName').exists().withMessage('Should have an account name with host'),
  43. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  44. logger.debug('Checking unblockAccountByServerValidator parameters', { parameters: req.params })
  45. if (areValidationErrors(req, res)) return
  46. if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
  47. const serverActor = await getServerActor()
  48. const targetAccount = res.locals.account
  49. if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return
  50. return next()
  51. }
  52. ]
  53. const blockServerValidator = [
  54. body('host').custom(isHostValid).withMessage('Should have a valid host'),
  55. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  56. logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
  57. if (areValidationErrors(req, res)) return
  58. const host: string = req.body.host
  59. if (host === WEBSERVER.HOST) {
  60. return res.status(409)
  61. .send({ error: 'You cannot block your own server.' })
  62. .end()
  63. }
  64. const server = await ServerModel.loadByHost(host)
  65. if (!server) {
  66. return res.status(404)
  67. .send({ error: 'Server host not found.' })
  68. .end()
  69. }
  70. res.locals.server = server
  71. return next()
  72. }
  73. ]
  74. const unblockServerByAccountValidator = [
  75. param('host').custom(isHostValid).withMessage('Should have an account name with host'),
  76. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  77. logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params })
  78. if (areValidationErrors(req, res)) return
  79. const user = res.locals.oauth.token.User
  80. if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return
  81. return next()
  82. }
  83. ]
  84. const unblockServerByServerValidator = [
  85. param('host').custom(isHostValid).withMessage('Should have an account name with host'),
  86. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  87. logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params })
  88. if (areValidationErrors(req, res)) return
  89. const serverActor = await getServerActor()
  90. if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return
  91. return next()
  92. }
  93. ]
  94. // ---------------------------------------------------------------------------
  95. export {
  96. blockServerValidator,
  97. blockAccountValidator,
  98. unblockAccountByAccountValidator,
  99. unblockServerByAccountValidator,
  100. unblockAccountByServerValidator,
  101. unblockServerByServerValidator
  102. }
  103. // ---------------------------------------------------------------------------
  104. async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) {
  105. const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId)
  106. if (!accountBlock) {
  107. res.status(404)
  108. .send({ error: 'Account block entry not found.' })
  109. .end()
  110. return false
  111. }
  112. res.locals.accountBlock = accountBlock
  113. return true
  114. }
  115. async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) {
  116. const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host)
  117. if (!serverBlock) {
  118. res.status(404)
  119. .send({ error: 'Server block entry not found.' })
  120. .end()
  121. return false
  122. }
  123. res.locals.serverBlock = serverBlock
  124. return true
  125. }