]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Fix login when there is an error
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
69818c93 1import * as express from 'express'
291e8d3e 2import * as Promise from 'bluebird'
69818c93 3
6fcd19ba 4import { pseudoRandomBytesPromise } from './core-utils'
291e8d3e 5import { CONFIG, database as db } from '../initializers'
6fcd19ba 6import { ResultList } from '../../shared'
cbe2f7c3 7
69818c93 8function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
a6fd2b30
C
9 res.type('json').status(400).end()
10}
11
6fcd19ba
C
12function generateRandomString (size: number) {
13 return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex'))
e4c87ec2
C
14}
15
154898b0 16interface FormatableToJSON {
0aef76c4 17 toFormattedJSON ()
154898b0
C
18}
19
0aef76c4
C
20function getFormattedObjects<U, T extends FormatableToJSON> (objects: T[], objectsTotal: number) {
21 const formattedObjects: U[] = []
55fa55a9 22
075f16ca 23 objects.forEach(object => {
0aef76c4 24 formattedObjects.push(object.toFormattedJSON())
55fa55a9
C
25 })
26
6fcd19ba 27 const res: ResultList<U> = {
55fa55a9 28 total: objectsTotal,
0aef76c4 29 data: formattedObjects
55fa55a9 30 }
6fcd19ba
C
31
32 return res
55fa55a9
C
33}
34
291e8d3e
C
35function isSignupAllowed () {
36 if (CONFIG.SIGNUP.ENABLED === false) {
37 return Promise.resolve(false)
38 }
39
40 // No limit and signup is enabled
41 if (CONFIG.SIGNUP.LIMIT === -1) {
42 return Promise.resolve(true)
43 }
44
45 return db.User.countTotal().then(totalUsers => {
46 return totalUsers < CONFIG.SIGNUP.LIMIT
47 })
48}
49
9f10b292 50// ---------------------------------------------------------------------------
c45f7f84 51
65fcc311
C
52export {
53 badRequest,
65fcc311 54 generateRandomString,
0aef76c4 55 getFormattedObjects,
291e8d3e 56 isSignupAllowed
65fcc311 57}