videos.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { Response } from 'express'
  2. import { fetchVideo, VideoFetchType } from '../video'
  3. import { UserRight } from '../../../shared/models/users'
  4. import { VideoChannelModel } from '../../models/video/video-channel'
  5. import { MUser, MUserAccountId, MVideoAccountLight, MVideoFullLight, MVideoThumbnail, MVideoWithRights } from '@server/typings/models'
  6. async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
  7. const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
  8. const video = await fetchVideo(id, fetchType, userId)
  9. if (video === null) {
  10. res.status(404)
  11. .json({ error: 'Video not found' })
  12. .end()
  13. return false
  14. }
  15. switch (fetchType) {
  16. case 'all':
  17. res.locals.videoAll = video as MVideoFullLight
  18. break
  19. case 'id':
  20. res.locals.videoId = video
  21. break
  22. case 'only-video':
  23. res.locals.onlyVideo = video as MVideoThumbnail
  24. break
  25. case 'only-video-with-rights':
  26. res.locals.onlyVideoWithRights = video as MVideoWithRights
  27. break
  28. }
  29. return true
  30. }
  31. async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
  32. if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
  33. const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
  34. if (videoChannel === null) {
  35. res.status(400)
  36. .json({ error: 'Unknown video `video channel` on this instance.' })
  37. .end()
  38. return false
  39. }
  40. res.locals.videoChannel = videoChannel
  41. return true
  42. }
  43. const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
  44. if (videoChannel === null) {
  45. res.status(400)
  46. .json({ error: 'Unknown video `video channel` for this account.' })
  47. .end()
  48. return false
  49. }
  50. res.locals.videoChannel = videoChannel
  51. return true
  52. }
  53. function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response) {
  54. // Retrieve the user who did the request
  55. if (video.isOwned() === false) {
  56. res.status(403)
  57. .json({ error: 'Cannot manage a video of another server.' })
  58. .end()
  59. return false
  60. }
  61. // Check if the user can delete the video
  62. // The user can delete it if he has the right
  63. // Or if s/he is the video's account
  64. const account = video.VideoChannel.Account
  65. if (user.hasRight(right) === false && account.userId !== user.id) {
  66. res.status(403)
  67. .json({ error: 'Cannot manage a video of another user.' })
  68. .end()
  69. return false
  70. }
  71. return true
  72. }
  73. // ---------------------------------------------------------------------------
  74. export {
  75. doesVideoChannelOfAccountExist,
  76. doesVideoExist,
  77. checkUserCanManageVideo
  78. }