video-channels.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import * as express from 'express'
  2. import { body, param } from 'express-validator'
  3. import { UserRight } from '../../../../shared'
  4. import {
  5. isVideoChannelDescriptionValid,
  6. isVideoChannelNameValid,
  7. isVideoChannelSupportValid
  8. } from '../../../helpers/custom-validators/video-channels'
  9. import { logger } from '../../../helpers/logger'
  10. import { VideoChannelModel } from '../../../models/video/video-channel'
  11. import { areValidationErrors } from '../utils'
  12. import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
  13. import { ActorModel } from '../../../models/activitypub/actor'
  14. import { isBooleanValid } from '../../../helpers/custom-validators/misc'
  15. import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares'
  16. import { MChannelActorAccountDefault } from '../../../typings/models/video'
  17. import { MUser } from '@server/typings/models'
  18. const videoChannelsAddValidator = [
  19. body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
  20. body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
  21. body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
  22. body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
  23. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  24. logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
  25. if (areValidationErrors(req, res)) return
  26. const actor = await ActorModel.loadLocalByName(req.body.name)
  27. if (actor) {
  28. res.status(409)
  29. .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
  30. .end()
  31. return false
  32. }
  33. return next()
  34. }
  35. ]
  36. const videoChannelsUpdateValidator = [
  37. param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
  38. body('displayName')
  39. .optional()
  40. .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
  41. body('description')
  42. .optional()
  43. .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
  44. body('support')
  45. .optional()
  46. .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
  47. body('bulkVideosSupportUpdate')
  48. .optional()
  49. .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
  50. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  51. logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
  52. if (areValidationErrors(req, res)) return
  53. if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
  54. // We need to make additional checks
  55. if (res.locals.videoChannel.Actor.isOwned() === false) {
  56. return res.status(403)
  57. .json({ error: 'Cannot update video channel of another server' })
  58. .end()
  59. }
  60. if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
  61. return res.status(403)
  62. .json({ error: 'Cannot update video channel of another user' })
  63. .end()
  64. }
  65. return next()
  66. }
  67. ]
  68. const videoChannelsRemoveValidator = [
  69. param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
  70. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  71. logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
  72. if (areValidationErrors(req, res)) return
  73. if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
  74. if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
  75. if (!await checkVideoChannelIsNotTheLastOne(res)) return
  76. return next()
  77. }
  78. ]
  79. const videoChannelsNameWithHostValidator = [
  80. param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
  81. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  82. logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
  83. if (areValidationErrors(req, res)) return
  84. if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
  85. return next()
  86. }
  87. ]
  88. const localVideoChannelValidator = [
  89. param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
  90. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  91. logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
  92. if (areValidationErrors(req, res)) return
  93. if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
  94. return next()
  95. }
  96. ]
  97. // ---------------------------------------------------------------------------
  98. export {
  99. videoChannelsAddValidator,
  100. videoChannelsUpdateValidator,
  101. videoChannelsRemoveValidator,
  102. videoChannelsNameWithHostValidator,
  103. localVideoChannelValidator
  104. }
  105. // ---------------------------------------------------------------------------
  106. function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelActorAccountDefault, res: express.Response) {
  107. if (videoChannel.Actor.isOwned() === false) {
  108. res.status(403)
  109. .json({ error: 'Cannot remove video channel of another server.' })
  110. .end()
  111. return false
  112. }
  113. // Check if the user can delete the video channel
  114. // The user can delete it if s/he is an admin
  115. // Or if s/he is the video channel's account
  116. if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
  117. res.status(403)
  118. .json({ error: 'Cannot remove video channel of another user' })
  119. .end()
  120. return false
  121. }
  122. return true
  123. }
  124. async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
  125. const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
  126. if (count <= 1) {
  127. res.status(409)
  128. .json({ error: 'Cannot remove the last channel of this user' })
  129. .end()
  130. return false
  131. }
  132. return true
  133. }