Usernames are case insensitive now
[oweals/peertube.git] / server / helpers / custom-validators / video-authors.ts
1 import * as Promise from 'bluebird'
2 import * as validator from 'validator'
3 import * as express from 'express'
4 import 'express-validator'
5
6 import { database as db } from '../../initializers'
7 import { AuthorInstance } from '../../models'
8 import { logger } from '../logger'
9
10 import { isUserUsernameValid } from './users'
11
12 function isVideoAuthorNameValid (value: string) {
13   return isUserUsernameValid(value)
14 }
15
16 function checkVideoAuthorExists (id: string, res: express.Response, callback: () => void) {
17   let promise: Promise<AuthorInstance>
18   if (validator.isInt(id)) {
19     promise = db.Author.load(+id)
20   } else { // UUID
21     promise = db.Author.loadByUUID(id)
22   }
23
24   promise.then(author => {
25     if (!author) {
26       return res.status(404)
27         .json({ error: 'Video author not found' })
28         .end()
29     }
30
31     res.locals.author = author
32     callback()
33   })
34     .catch(err => {
35       logger.error('Error in video author request validator.', err)
36       return res.sendStatus(500)
37     })
38 }
39
40 // ---------------------------------------------------------------------------
41
42 export {
43   checkVideoAuthorExists,
44   isVideoAuthorNameValid
45 }