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/typings/models'
16 async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
17 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
19 const video = await fetchVideo(id, fetchType, userId)
23 .json({ error: 'Video not found' })
31 res.locals.videoAll = video as MVideoFullLight
34 case 'only-immutable-attributes':
35 res.locals.onlyImmutableVideo = video as MVideoImmutable
39 res.locals.videoId = video as MVideoIdThumbnail
43 res.locals.onlyVideo = video as MVideoThumbnail
46 case 'only-video-with-rights':
47 res.locals.onlyVideoWithRights = video as MVideoWithRights
54 async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
55 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
56 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
57 if (videoChannel === null) {
59 .json({ error: 'Unknown video `video channel` on this instance.' })
65 res.locals.videoChannel = videoChannel
69 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
70 if (videoChannel === null) {
72 .json({ error: 'Unknown video `video channel` for this account.' })
78 res.locals.videoChannel = videoChannel
82 function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response) {
83 // Retrieve the user who did the request
84 if (video.isOwned() === false) {
86 .json({ error: 'Cannot manage a video of another server.' })
91 // Check if the user can delete the video
92 // The user can delete it if he has the right
93 // Or if s/he is the video's account
94 const account = video.VideoChannel.Account
95 if (user.hasRight(right) === false && account.userId !== user.id) {
97 .json({ error: 'Cannot manage a video of another user.' })
105 // ---------------------------------------------------------------------------
108 doesVideoChannelOfAccountExist,
110 checkUserCanManageVideo