video-live.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import * as express from 'express'
  2. import { body, param } from 'express-validator'
  3. import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos'
  4. import { VideoLiveModel } from '@server/models/video/video-live'
  5. import { ServerErrorCode, UserRight, VideoState } from '@shared/models'
  6. import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
  7. import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
  8. import { cleanUpReqFiles } from '../../../helpers/express-utils'
  9. import { logger } from '../../../helpers/logger'
  10. import { CONFIG } from '../../../initializers/config'
  11. import { areValidationErrors } from '../utils'
  12. import { getCommonVideoEditAttributes } from './videos'
  13. import { VideoModel } from '@server/models/video/video'
  14. import { Hooks } from '@server/lib/plugins/hooks'
  15. import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
  16. import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
  17. const videoLiveGetValidator = [
  18. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  19. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  20. logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
  21. if (areValidationErrors(req, res)) return
  22. if (!await doesVideoExist(req.params.videoId, res, 'all')) return
  23. // Check if the user who did the request is able to get the live info
  24. const user = res.locals.oauth.token.User
  25. if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
  26. const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
  27. if (!videoLive) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  28. res.locals.videoLive = videoLive
  29. return next()
  30. }
  31. ]
  32. const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
  33. body('channelId')
  34. .customSanitizer(toIntOrNull)
  35. .custom(isIdValid).withMessage('Should have correct video channel id'),
  36. body('name')
  37. .custom(isVideoNameValid).withMessage('Should have a valid name'),
  38. body('saveReplay')
  39. .optional()
  40. .customSanitizer(toBooleanOrNull)
  41. .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
  42. body('permanentLive')
  43. .optional()
  44. .customSanitizer(toBooleanOrNull)
  45. .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
  46. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  47. logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
  48. if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
  49. if (CONFIG.LIVE.ENABLED !== true) {
  50. cleanUpReqFiles(req)
  51. return res.status(HttpStatusCode.FORBIDDEN_403)
  52. .json({ error: 'Live is not enabled on this instance' })
  53. }
  54. if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
  55. cleanUpReqFiles(req)
  56. return res.status(HttpStatusCode.FORBIDDEN_403)
  57. .json({ error: 'Saving live replay is not allowed instance' })
  58. }
  59. if (req.body.permanentLive && req.body.saveReplay) {
  60. cleanUpReqFiles(req)
  61. return res.status(HttpStatusCode.BAD_REQUEST_400)
  62. .json({ error: 'Cannot set this live as permanent while saving its replay' })
  63. }
  64. const user = res.locals.oauth.token.User
  65. if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
  66. if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
  67. const totalInstanceLives = await VideoModel.countLocalLives()
  68. if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
  69. cleanUpReqFiles(req)
  70. return res.status(HttpStatusCode.FORBIDDEN_403)
  71. .json({
  72. code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
  73. error: 'Cannot create this live because the max instance lives limit is reached.'
  74. })
  75. }
  76. }
  77. if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
  78. const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
  79. if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
  80. cleanUpReqFiles(req)
  81. return res.status(HttpStatusCode.FORBIDDEN_403)
  82. .json({
  83. code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
  84. error: 'Cannot create this live because the max user lives limit is reached.'
  85. })
  86. }
  87. }
  88. if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
  89. return next()
  90. }
  91. ])
  92. const videoLiveUpdateValidator = [
  93. body('saveReplay')
  94. .optional()
  95. .customSanitizer(toBooleanOrNull)
  96. .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
  97. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  98. logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
  99. if (areValidationErrors(req, res)) return
  100. if (req.body.permanentLive && req.body.saveReplay) {
  101. return res.status(HttpStatusCode.BAD_REQUEST_400)
  102. .json({ error: 'Cannot set this live as permanent while saving its replay' })
  103. }
  104. if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
  105. return res.status(HttpStatusCode.FORBIDDEN_403)
  106. .json({ error: 'Saving live replay is not allowed instance' })
  107. }
  108. if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
  109. return res.status(HttpStatusCode.BAD_REQUEST_400)
  110. .json({ error: 'Cannot update a live that has already started' })
  111. }
  112. // Check the user can manage the live
  113. const user = res.locals.oauth.token.User
  114. if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
  115. return next()
  116. }
  117. ]
  118. // ---------------------------------------------------------------------------
  119. export {
  120. videoLiveAddValidator,
  121. videoLiveUpdateValidator,
  122. videoLiveGetValidator
  123. }
  124. // ---------------------------------------------------------------------------
  125. async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
  126. // Check we accept this video
  127. const acceptParameters = {
  128. liveVideoBody: req.body,
  129. user: res.locals.oauth.token.User
  130. }
  131. const acceptedResult = await Hooks.wrapFun(
  132. isLocalLiveVideoAccepted,
  133. acceptParameters,
  134. 'filter:api.live-video.create.accept.result'
  135. )
  136. if (!acceptedResult || acceptedResult.accepted !== true) {
  137. logger.info('Refused local live video.', { acceptedResult, acceptParameters })
  138. res.status(HttpStatusCode.FORBIDDEN_403)
  139. .json({ error: acceptedResult.errorMessage || 'Refused local live video' })
  140. return false
  141. }
  142. return true
  143. }