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'
17 async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
18 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
20 const video = await fetchVideo(id, fetchType, userId)
24 .json({ error: 'Video not found' })
32 res.locals.videoAll = video as MVideoFullLight
35 case 'only-immutable-attributes':
36 res.locals.onlyImmutableVideo = video as MVideoImmutable
40 res.locals.videoId = video as MVideoIdThumbnail
44 res.locals.onlyVideo = video as MVideoThumbnail
47 case 'only-video-with-rights':
48 res.locals.onlyVideoWithRights = video as MVideoWithRights
55 async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
56 if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
58 .json({ error: 'VideoFile matching Video not found' })
67 async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
68 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
69 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
70 if (videoChannel === null) {
72 .json({ error: 'Unknown video `video channel` on this instance.' })
78 res.locals.videoChannel = videoChannel
82 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
83 if (videoChannel === null) {
85 .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) {
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) {
110 .json({ error: 'Cannot manage a video of another user.' })
118 // ---------------------------------------------------------------------------
121 doesVideoChannelOfAccountExist,
123 doesVideoFileOfVideoExist,
124 checkUserCanManageVideo