aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/middlewares/video-abuses.ts
blob: 97a5724b6dd69afd69ad3c57051896ff1af310a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Response } from 'express'
import { VideoAbuseModel } from '../../models/video/video-abuse'
import { fetchVideo } from '../video'

async function doesVideoAbuseExist (abuseIdArg: number | string, videoUUID: string, res: Response) {
  const abuseId = parseInt(abuseIdArg + '', 10)
  let videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, null, videoUUID)

  if (!videoAbuse) {
    const userId = res.locals.oauth?.token.User.id
    const video = await fetchVideo(videoUUID, 'all', userId)

    if (video) videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, video.id)
  }

  if (videoAbuse === null) {
    res.status(404)
       .json({ error: 'Video abuse not found' })
       .end()

    return false
  }

  res.locals.videoAbuse = videoAbuse
  return true
}

// ---------------------------------------------------------------------------

export {
  doesVideoAbuseExist
}