Add downloadingEnabled property to video model
[oweals/peertube.git] / server / helpers / custom-validators / videos.ts
index f4c1c8b0720d5f1944ed8f59f0c3ddf16a5515de..714f7ac956c25b8dcc35f02b716830430a131e10 100644 (file)
@@ -18,9 +18,9 @@ import { exists, isArray, isFileValid } from './misc'
 import { VideoChannelModel } from '../../models/video/video-channel'
 import { UserModel } from '../../models/account/user'
 import * as magnetUtil from 'magnet-uri'
+import { fetchVideo, VideoFetchType } from '../video'
 
 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
-const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
 
 function isVideoCategoryValid (value: any) {
   return value === null || VIDEO_CATEGORIES[ value ] !== undefined
@@ -71,10 +71,6 @@ function isVideoTagsValid (tags: string[]) {
   )
 }
 
-function isVideoAbuseReasonValid (value: string) {
-  return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
-}
-
 function isVideoViewsValid (value: string) {
   return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
 }
@@ -157,14 +153,10 @@ function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: Use
   return true
 }
 
-async function isVideoExist (id: string, res: Response) {
-  let video: VideoModel | null
+async function isVideoExist (id: string, res: Response, fetchType: VideoFetchType = 'all') {
+  const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
 
-  if (validator.isInt(id)) {
-    video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
-  } else { // UUID
-    video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
-  }
+  const video = await fetchVideo(id, fetchType, userId)
 
   if (video === null) {
     res.status(404)
@@ -174,7 +166,7 @@ async function isVideoExist (id: string, res: Response) {
     return false
   }
 
-  res.locals.video = video
+  if (fetchType !== 'none') res.locals.video = video
   return true
 }
 
@@ -183,7 +175,7 @@ async function isVideoChannelOfAccountExist (channelId: number, user: UserModel,
     const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
     if (videoChannel === null) {
       res.status(400)
-         .json({ error: 'Unknown video video channel on this instance.' })
+         .json({ error: 'Unknown video `video channel` on this instance.' })
          .end()
 
       return false
@@ -196,7 +188,7 @@ async function isVideoChannelOfAccountExist (channelId: number, user: UserModel,
   const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
   if (videoChannel === null) {
     res.status(400)
-       .json({ error: 'Unknown video video channel for this account.' })
+       .json({ error: 'Unknown video `video channel` for this account.' })
        .end()
 
     return false
@@ -220,7 +212,6 @@ export {
   isVideoTagsValid,
   isVideoFPSResolutionValid,
   isScheduleVideoUpdatePrivacyValid,
-  isVideoAbuseReasonValid,
   isVideoFile,
   isVideoMagnetUriValid,
   isVideoStateValid,