1 import { Response } from 'express'
2 import { loadVideo, VideoLoadType } from '@server/lib/model-loaders'
3 import { VideoChannelModel } from '@server/models/video/video-channel'
4 import { VideoFileModel } from '@server/models/video/video-file'
9 MVideoFormattableDetails,
14 } from '@server/types/models'
15 import { HttpStatusCode, UserRight } from '@shared/models'
17 async function doesVideoExist (id: number | string, res: Response, fetchType: VideoLoadType = 'all') {
18 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
20 const video = await loadVideo(id, fetchType, userId)
24 status: HttpStatusCode.NOT_FOUND_404,
25 message: 'Video not found'
32 res.locals.videoAPI = video as MVideoFormattableDetails
36 res.locals.videoAll = video as MVideoFullLight
39 case 'only-immutable-attributes':
40 res.locals.onlyImmutableVideo = video as MVideoImmutable
44 res.locals.videoId = video as MVideoId
48 res.locals.onlyVideo = video as MVideoThumbnail
55 async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
56 if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
58 status: HttpStatusCode.NOT_FOUND_404,
59 message: 'VideoFile matching Video not found'
67 async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
68 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
70 if (videoChannel === null) {
71 res.fail({ message: 'Unknown video "video channel" for this instance.' })
75 // Don't check account id if the user can update any video
76 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
77 res.locals.videoChannel = videoChannel
81 if (videoChannel.Account.id !== user.Account.id) {
83 message: 'Unknown video "video channel" for this account.'
88 res.locals.videoChannel = videoChannel
92 function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) {
93 // Retrieve the user who did the request
94 if (onlyOwned && video.isOwned() === false) {
96 status: HttpStatusCode.FORBIDDEN_403,
97 message: 'Cannot manage a video of another server.'
102 // Check if the user can delete the video
103 // The user can delete it if he has the right
104 // Or if s/he is the video's account
105 const account = video.VideoChannel.Account
106 if (user.hasRight(right) === false && account.userId !== user.id) {
108 status: HttpStatusCode.FORBIDDEN_403,
109 message: 'Cannot manage a video of another user.'
117 // ---------------------------------------------------------------------------
120 doesVideoChannelOfAccountExist,
122 doesVideoFileOfVideoExist,
123 checkUserCanManageVideo