aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/video-ownership.ts
blob: a7771e07b2e0bf6b1266e42407e27588f90e1b75 (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
import { Response } from 'express'
import * as validator from 'validator'
import { VideoChangeOwnershipModel } from '../../models/video/video-change-ownership'
import { UserModel } from '../../models/account/user'

export async function doesChangeVideoOwnershipExist (id: string, res: Response): Promise<boolean> {
  const videoChangeOwnership = await loadVideoChangeOwnership(id)

  if (!videoChangeOwnership) {
    res.status(404)
      .json({ error: 'Video change ownership not found' })
      .end()

    return false
  }

  res.locals.videoChangeOwnership = videoChangeOwnership
  return true
}

async function loadVideoChangeOwnership (id: string): Promise<VideoChangeOwnershipModel | undefined> {
  if (validator.isInt(id)) {
    return VideoChangeOwnershipModel.load(parseInt(id, 10))
  }

  return undefined
}

export function checkUserCanTerminateOwnershipChange (
  user: UserModel,
  videoChangeOwnership: VideoChangeOwnershipModel,
  res: Response
): boolean {
  if (videoChangeOwnership.NextOwner.userId === user.id) {
    return true
  }

  res.status(403)
    .json({ error: 'Cannot terminate an ownership change of another user' })
    .end()
  return false
}