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'
7 async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
8 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
10 const video = await fetchVideo(id, fetchType, userId)
14 .json({ error: 'Video not found' })
22 res.locals.videoAll = video as MVideoFullLight
26 res.locals.videoId = video
30 res.locals.onlyVideo = video as MVideoThumbnail
33 case 'only-video-with-rights':
34 res.locals.onlyVideoWithRights = video as MVideoWithRights
41 async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
42 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
43 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
44 if (videoChannel === null) {
46 .json({ error: 'Unknown video `video channel` on this instance.' })
52 res.locals.videoChannel = videoChannel
56 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
57 if (videoChannel === null) {
59 .json({ error: 'Unknown video `video channel` for this account.' })
65 res.locals.videoChannel = videoChannel
69 function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response) {
70 // Retrieve the user who did the request
71 if (video.isOwned() === false) {
73 .json({ error: 'Cannot manage a video of another server.' })
78 // Check if the user can delete the video
79 // The user can delete it if he has the right
80 // Or if s/he is the video's account
81 const account = video.VideoChannel.Account
82 if (user.hasRight(right) === false && account.userId !== user.id) {
84 .json({ error: 'Cannot manage a video of another user.' })
92 // ---------------------------------------------------------------------------
95 doesVideoChannelOfAccountExist,
97 checkUserCanManageVideo