aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/middlewares/abuses.ts
blob: be8c8b449b574f166db2c3926ca64b1f2ec8d01d (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Response } from 'express'
import { AbuseModel } from '../../models/abuse/abuse'
import { fetchVideo } from '../video'

// FIXME: deprecated in 2.3. Remove this function
async function doesVideoAbuseExist (abuseIdArg: number | string, videoUUID: string, res: Response) {
  const abuseId = parseInt(abuseIdArg + '', 10)
  let abuse = await AbuseModel.loadByIdAndVideoId(abuseId, null, videoUUID)

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

    if (video) abuse = await AbuseModel.loadByIdAndVideoId(abuseId, video.id)
  }

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

    return false
  }

  res.locals.abuse = abuse
  return true
}

async function doesAbuseExist (abuseId: number | string, res: Response) {
  const abuse = await AbuseModel.loadById(parseInt(abuseId + '', 10))

  if (!abuse) {
    res.status(404)
       .json({ error: 'Abuse not found' })

    return false
  }

  res.locals.abuse = abuse
  return true
}

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

export {
  doesAbuseExist,
  doesVideoAbuseExist
}