aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/video-accounts.ts
blob: 31808ae1ecb63ae19927912894eb02a36f5a13fb (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
48
49
50
51
52
53
import * as Promise from 'bluebird'
import * as validator from 'validator'
import * as express from 'express'
import 'express-validator'

import { database as db } from '../../initializers'
import { AccountInstance } from '../../models'
import { logger } from '../logger'

import { isUserUsernameValid } from './users'
import { isHostValid } from './pods'

function isVideoAccountNameValid (value: string) {
  return isUserUsernameValid(value)
}

function isAccountNameWithHostValid (value: string) {
  const [ name, host ] = value.split('@')

  return isVideoAccountNameValid(name) && isHostValid(host)
}

function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) {
  let promise: Promise<AccountInstance>
  if (validator.isInt(id)) {
    promise = db.Account.load(+id)
  } else { // UUID
    promise = db.Account.loadByUUID(id)
  }

  promise.then(account => {
    if (!account) {
      return res.status(404)
        .json({ error: 'Video account not found' })
        .end()
    }

    res.locals.account = account
    callback()
  })
    .catch(err => {
      logger.error('Error in video account request validator.', err)
      return res.sendStatus(500)
    })
}

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

export {
  checkVideoAccountExists,
  isAccountNameWithHostValid,
  isVideoAccountNameValid
}