my-blocklist.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import * as express from 'express'
  2. import 'multer'
  3. import { getFormattedObjects } from '../../../helpers/utils'
  4. import {
  5. asyncMiddleware,
  6. asyncRetryTransactionMiddleware,
  7. authenticate,
  8. paginationValidator,
  9. setDefaultPagination,
  10. setDefaultSort,
  11. unblockAccountByAccountValidator
  12. } from '../../../middlewares'
  13. import {
  14. accountsBlocklistSortValidator,
  15. blockAccountValidator,
  16. blockServerValidator,
  17. serversBlocklistSortValidator,
  18. unblockServerByAccountValidator
  19. } from '../../../middlewares/validators'
  20. import { AccountBlocklistModel } from '../../../models/account/account-blocklist'
  21. import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist'
  22. import { ServerBlocklistModel } from '../../../models/server/server-blocklist'
  23. const myBlocklistRouter = express.Router()
  24. myBlocklistRouter.get('/me/blocklist/accounts',
  25. authenticate,
  26. paginationValidator,
  27. accountsBlocklistSortValidator,
  28. setDefaultSort,
  29. setDefaultPagination,
  30. asyncMiddleware(listBlockedAccounts)
  31. )
  32. myBlocklistRouter.post('/me/blocklist/accounts',
  33. authenticate,
  34. asyncMiddleware(blockAccountValidator),
  35. asyncRetryTransactionMiddleware(blockAccount)
  36. )
  37. myBlocklistRouter.delete('/me/blocklist/accounts/:accountName',
  38. authenticate,
  39. asyncMiddleware(unblockAccountByAccountValidator),
  40. asyncRetryTransactionMiddleware(unblockAccount)
  41. )
  42. myBlocklistRouter.get('/me/blocklist/servers',
  43. authenticate,
  44. paginationValidator,
  45. serversBlocklistSortValidator,
  46. setDefaultSort,
  47. setDefaultPagination,
  48. asyncMiddleware(listBlockedServers)
  49. )
  50. myBlocklistRouter.post('/me/blocklist/servers',
  51. authenticate,
  52. asyncMiddleware(blockServerValidator),
  53. asyncRetryTransactionMiddleware(blockServer)
  54. )
  55. myBlocklistRouter.delete('/me/blocklist/servers/:host',
  56. authenticate,
  57. asyncMiddleware(unblockServerByAccountValidator),
  58. asyncRetryTransactionMiddleware(unblockServer)
  59. )
  60. export {
  61. myBlocklistRouter
  62. }
  63. // ---------------------------------------------------------------------------
  64. async function listBlockedAccounts (req: express.Request, res: express.Response) {
  65. const user = res.locals.oauth.token.User
  66. const resultList = await AccountBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort)
  67. return res.json(getFormattedObjects(resultList.data, resultList.total))
  68. }
  69. async function blockAccount (req: express.Request, res: express.Response) {
  70. const user = res.locals.oauth.token.User
  71. const accountToBlock = res.locals.account
  72. await addAccountInBlocklist(user.Account.id, accountToBlock.id)
  73. return res.status(204).end()
  74. }
  75. async function unblockAccount (req: express.Request, res: express.Response) {
  76. const accountBlock = res.locals.accountBlock
  77. await removeAccountFromBlocklist(accountBlock)
  78. return res.status(204).end()
  79. }
  80. async function listBlockedServers (req: express.Request, res: express.Response) {
  81. const user = res.locals.oauth.token.User
  82. const resultList = await ServerBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort)
  83. return res.json(getFormattedObjects(resultList.data, resultList.total))
  84. }
  85. async function blockServer (req: express.Request, res: express.Response) {
  86. const user = res.locals.oauth.token.User
  87. const serverToBlock = res.locals.server
  88. await addServerInBlocklist(user.Account.id, serverToBlock.id)
  89. return res.status(204).end()
  90. }
  91. async function unblockServer (req: express.Request, res: express.Response) {
  92. const serverBlock = res.locals.serverBlock
  93. await removeServerFromBlocklist(serverBlock)
  94. return res.status(204).end()
  95. }