Make sure a report doesn't get deleted upon the deletion of its video
[oweals/peertube.git] / server / helpers / middlewares / video-abuses.ts
index 8a1d3d618296e38dece030bcf18282e2a9e21c1e..7553a5eb3355e445606ad8bcbc2cc16a76c1494f 100644 (file)
@@ -1,9 +1,17 @@
 import { Response } from 'express'
 import { VideoAbuseModel } from '../../models/video/video-abuse'
+import { fetchVideo } from '../video'
 
-async function doesVideoAbuseExist (abuseIdArg: number | string, videoId: number, res: Response) {
+async function doesVideoAbuseExist (abuseIdArg: number | string, videoUUID: string, res: Response) {
   const abuseId = parseInt(abuseIdArg + '', 10)
-  const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId)
+  let videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, null, videoUUID)
+
+  if (!videoAbuse) {
+    const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
+    const video = await fetchVideo(videoUUID, 'all', userId)
+
+    if (video) videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, video.id)
+  }
 
   if (videoAbuse === null) {
     res.status(404)