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,
15 } from '@server/types/models'
16 import { HttpStatusCode } from '@shared/core-utils'
17 import { UserRight } from '@shared/models'
19 async function doesVideoExist (id: number | string, res: Response, fetchType: VideoLoadType = 'all') {
20 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
22 const video = await loadVideo(id, fetchType, userId)
26 status: HttpStatusCode.NOT_FOUND_404,
27 message: 'Video not found'
34 res.locals.videoAPI = video as MVideoFormattableDetails
38 res.locals.videoAll = video as MVideoFullLight
41 case 'only-immutable-attributes':
42 res.locals.onlyImmutableVideo = video as MVideoImmutable
46 res.locals.videoId = video as MVideoIdThumbnail
50 res.locals.onlyVideo = video as MVideoThumbnail
53 case 'only-video-with-rights':
54 res.locals.onlyVideoWithRights = video as MVideoWithRights
61 async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
62 if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
64 status: HttpStatusCode.NOT_FOUND_404,
65 message: 'VideoFile matching Video not found'
73 async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
74 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
76 if (videoChannel === null) {
77 res.fail({ message: 'Unknown video "video channel" for this instance.' })
81 // Don't check account id if the user can update any video
82 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
83 res.locals.videoChannel = videoChannel
87 if (videoChannel.Account.id !== user.Account.id) {
89 message: 'Unknown video "video channel" for this account.'
94 res.locals.videoChannel = videoChannel
98 function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) {
99 // Retrieve the user who did the request
100 if (onlyOwned && video.isOwned() === false) {
102 status: HttpStatusCode.FORBIDDEN_403,
103 message: 'Cannot manage a video of another server.'
108 // Check if the user can delete the video
109 // The user can delete it if he has the right
110 // Or if s/he is the video's account
111 const account = video.VideoChannel.Account
112 if (user.hasRight(right) === false && account.userId !== user.id) {
114 status: HttpStatusCode.FORBIDDEN_403,
115 message: 'Cannot manage a video of another user.'
123 // ---------------------------------------------------------------------------
126 doesVideoChannelOfAccountExist,
128 doesVideoFileOfVideoExist,
129 checkUserCanManageVideo