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'
14 } from '@server/types/models'
15 import { VideoFileModel } from '@server/models/video/video-file'
16 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
18 async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
19 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
21 const video = await fetchVideo(id, fetchType, userId)
24 res.status(HttpStatusCode.NOT_FOUND_404)
25 .json({ error: 'Video not found' })
33 res.locals.videoAll = video as MVideoFullLight
36 case 'only-immutable-attributes':
37 res.locals.onlyImmutableVideo = video as MVideoImmutable
41 res.locals.videoId = video as MVideoIdThumbnail
45 res.locals.onlyVideo = video as MVideoThumbnail
48 case 'only-video-with-rights':
49 res.locals.onlyVideoWithRights = video as MVideoWithRights
56 async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
57 if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
58 res.status(HttpStatusCode.NOT_FOUND_404)
59 .json({ error: 'VideoFile matching Video not found' })
68 async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
69 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
71 if (videoChannel === null) {
72 res.status(HttpStatusCode.BAD_REQUEST_400)
73 .json({ error: 'Unknown video "video channel" for this instance.' })
78 // Don't check account id if the user can update any video
79 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
80 res.locals.videoChannel = videoChannel
84 if (videoChannel.Account.id !== user.Account.id) {
85 res.status(HttpStatusCode.BAD_REQUEST_400)
86 .json({ error: 'Unknown video "video channel" for this account.' })
91 res.locals.videoChannel = videoChannel
95 function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) {
96 // Retrieve the user who did the request
97 if (onlyOwned && video.isOwned() === false) {
98 res.status(HttpStatusCode.FORBIDDEN_403)
99 .json({ error: 'Cannot manage a video of another server.' })
104 // Check if the user can delete the video
105 // The user can delete it if he has the right
106 // Or if s/he is the video's account
107 const account = video.VideoChannel.Account
108 if (user.hasRight(right) === false && account.userId !== user.id) {
109 res.status(HttpStatusCode.FORBIDDEN_403)
110 .json({ error: 'Cannot manage a video of another user.' })
118 // ---------------------------------------------------------------------------
121 doesVideoChannelOfAccountExist,
123 doesVideoFileOfVideoExist,
124 checkUserCanManageVideo