1 import { Response } from 'express'
2 import { fetchVideo, VideoFetchType } from '../video'
3 import { UserModel } from '../../models/account/user'
4 import { UserRight } from '../../../shared/models/users'
5 import { VideoChannelModel } from '../../models/video/video-channel'
6 import { VideoModel } from '../../models/video/video'
8 async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
9 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
11 const video = await fetchVideo(id, fetchType, userId)
15 .json({ error: 'Video not found' })
21 if (fetchType !== 'none') res.locals.video = video
25 async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
26 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
27 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
28 if (videoChannel === null) {
30 .json({ error: 'Unknown video `video channel` on this instance.' })
36 res.locals.videoChannel = videoChannel
40 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
41 if (videoChannel === null) {
43 .json({ error: 'Unknown video `video channel` for this account.' })
49 res.locals.videoChannel = videoChannel
53 function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
54 // Retrieve the user who did the request
55 if (video.isOwned() === false) {
57 .json({ error: 'Cannot manage a video of another server.' })
62 // Check if the user can delete the video
63 // The user can delete it if he has the right
64 // Or if s/he is the video's account
65 const account = video.VideoChannel.Account
66 if (user.hasRight(right) === false && account.userId !== user.id) {
68 .json({ error: 'Cannot manage a video of another user.' })
76 // ---------------------------------------------------------------------------
79 doesVideoChannelOfAccountExist,
81 checkUserCanManageVideo