passwords.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import express from 'express'
  2. import { Transaction } from 'sequelize'
  3. import { HttpStatusCode } from '@peertube/peertube-models'
  4. import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
  5. import { getVideoWithAttributes } from '@server/helpers/video.js'
  6. import { VideoPasswordModel } from '@server/models/video/video-password.js'
  7. import { getFormattedObjects } from '../../../helpers/utils.js'
  8. import {
  9. asyncMiddleware,
  10. asyncRetryTransactionMiddleware,
  11. authenticate,
  12. setDefaultPagination,
  13. setDefaultSort
  14. } from '../../../middlewares/index.js'
  15. import {
  16. listVideoPasswordValidator,
  17. paginationValidator,
  18. removeVideoPasswordValidator,
  19. updateVideoPasswordListValidator,
  20. videoPasswordsSortValidator
  21. } from '../../../middlewares/validators/index.js'
  22. const lTags = loggerTagsFactory('api', 'video')
  23. const videoPasswordRouter = express.Router()
  24. videoPasswordRouter.get('/:videoId/passwords',
  25. authenticate,
  26. paginationValidator,
  27. videoPasswordsSortValidator,
  28. setDefaultSort,
  29. setDefaultPagination,
  30. asyncMiddleware(listVideoPasswordValidator),
  31. asyncMiddleware(listVideoPasswords)
  32. )
  33. videoPasswordRouter.put('/:videoId/passwords',
  34. authenticate,
  35. asyncMiddleware(updateVideoPasswordListValidator),
  36. asyncMiddleware(updateVideoPasswordList)
  37. )
  38. videoPasswordRouter.delete('/:videoId/passwords/:passwordId',
  39. authenticate,
  40. asyncMiddleware(removeVideoPasswordValidator),
  41. asyncRetryTransactionMiddleware(removeVideoPassword)
  42. )
  43. // ---------------------------------------------------------------------------
  44. export {
  45. videoPasswordRouter
  46. }
  47. // ---------------------------------------------------------------------------
  48. async function listVideoPasswords (req: express.Request, res: express.Response) {
  49. const options = {
  50. videoId: res.locals.videoAll.id,
  51. start: req.query.start,
  52. count: req.query.count,
  53. sort: req.query.sort
  54. }
  55. const resultList = await VideoPasswordModel.listPasswords(options)
  56. return res.json(getFormattedObjects(resultList.data, resultList.total))
  57. }
  58. async function updateVideoPasswordList (req: express.Request, res: express.Response) {
  59. const videoInstance = getVideoWithAttributes(res)
  60. const videoId = videoInstance.id
  61. const passwordArray = req.body.passwords as string[]
  62. await VideoPasswordModel.sequelize.transaction(async (t: Transaction) => {
  63. await VideoPasswordModel.deleteAllPasswords(videoId, t)
  64. await VideoPasswordModel.addPasswords(passwordArray, videoId, t)
  65. })
  66. logger.info(
  67. `Video passwords for video with name %s and uuid %s have been updated`,
  68. videoInstance.name,
  69. videoInstance.uuid,
  70. lTags(videoInstance.uuid)
  71. )
  72. return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
  73. }
  74. async function removeVideoPassword (req: express.Request, res: express.Response) {
  75. const videoInstance = getVideoWithAttributes(res)
  76. const password = res.locals.videoPassword
  77. await VideoPasswordModel.deletePassword(password.id)
  78. logger.info(
  79. 'Password with id %d of video named %s and uuid %s has been deleted.',
  80. password.id,
  81. videoInstance.name,
  82. videoInstance.uuid,
  83. lTags(videoInstance.uuid)
  84. )
  85. return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
  86. }