1 import * as express from 'express'
2 import * as Promise from 'bluebird'
4 import { pseudoRandomBytesPromise } from './core-utils'
5 import { CONFIG, database as db } from '../initializers'
6 import { ResultList } from '../../shared'
8 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
9 res.type('json').status(400).end()
12 function generateRandomString (size: number) {
13 return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex'))
16 interface FormatableToJSON {
20 function getFormatedObjects<U, T extends FormatableToJSON> (objects: T[], objectsTotal: number) {
21 const formatedObjects: U[] = []
23 objects.forEach(object => {
24 formatedObjects.push(object.toFormatedJSON())
27 const res: ResultList<U> = {
35 function isSignupAllowed () {
36 if (CONFIG.SIGNUP.ENABLED === false) {
37 return Promise.resolve(false)
40 // No limit and signup is enabled
41 if (CONFIG.SIGNUP.LIMIT === -1) {
42 return Promise.resolve(true)
45 return db.User.countTotal().then(totalUsers => {
46 return totalUsers < CONFIG.SIGNUP.LIMIT
50 // ---------------------------------------------------------------------------