]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Handle announces in inbox
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
69818c93 1import * as express from 'express'
eb080476 2import * as Sequelize from 'sequelize'
69818c93 3
6fcd19ba 4import { pseudoRandomBytesPromise } from './core-utils'
291e8d3e 5import { CONFIG, database as db } from '../initializers'
6fcd19ba 6import { ResultList } from '../../shared'
40298b02 7import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
7a7724e6 8import { AccountInstance } from '../models/account/account-interface'
cbe2f7c3 9
69818c93 10function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
f5028693 11 return res.type('json').status(400).end()
a6fd2b30
C
12}
13
f5028693
C
14async function generateRandomString (size: number) {
15 const raw = await pseudoRandomBytesPromise(size)
16
17 return raw.toString('hex')
e4c87ec2
C
18}
19
40298b02 20interface FormattableToJSON {
0aef76c4 21 toFormattedJSON ()
154898b0
C
22}
23
40298b02 24function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
0aef76c4 25 const formattedObjects: U[] = []
55fa55a9 26
075f16ca 27 objects.forEach(object => {
0aef76c4 28 formattedObjects.push(object.toFormattedJSON())
55fa55a9
C
29 })
30
6fcd19ba 31 const res: ResultList<U> = {
55fa55a9 32 total: objectsTotal,
0aef76c4 33 data: formattedObjects
55fa55a9 34 }
6fcd19ba
C
35
36 return res
55fa55a9
C
37}
38
f5028693 39async function isSignupAllowed () {
291e8d3e 40 if (CONFIG.SIGNUP.ENABLED === false) {
f5028693 41 return false
291e8d3e
C
42 }
43
44 // No limit and signup is enabled
45 if (CONFIG.SIGNUP.LIMIT === -1) {
f5028693 46 return true
291e8d3e
C
47 }
48
f5028693
C
49 const totalUsers = await db.User.countTotal()
50
51 return totalUsers < CONFIG.SIGNUP.LIMIT
291e8d3e
C
52}
53
40298b02
C
54function computeResolutionsToTranscode (videoFileHeight: number) {
55 const resolutionsEnabled: number[] = []
56 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
57
58 const resolutions = [
59 VideoResolution.H_240P,
60 VideoResolution.H_360P,
61 VideoResolution.H_480P,
62 VideoResolution.H_720P,
63 VideoResolution.H_1080P
64 ]
65
66 for (const resolution of resolutions) {
14d3270f 67 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
40298b02
C
68 resolutionsEnabled.push(resolution)
69 }
70 }
71
72 return resolutionsEnabled
73}
74
eb080476
C
75function resetSequelizeInstance (instance: Sequelize.Instance<any>, savedFields: object) {
76 Object.keys(savedFields).forEach(key => {
77 const value = savedFields[key]
78 instance.set(key, value)
79 })
80}
81
7a7724e6
C
82let applicationAccount: AccountInstance
83async function getApplicationAccount () {
84 if (applicationAccount === undefined) {
85 applicationAccount = await db.Account.loadApplication()
86 }
87
88 return Promise.resolve(applicationAccount)
89}
90
792dbaf0
GS
91type SortType = { sortModel: any, sortValue: string }
92
9f10b292 93// ---------------------------------------------------------------------------
c45f7f84 94
65fcc311
C
95export {
96 badRequest,
65fcc311 97 generateRandomString,
0aef76c4 98 getFormattedObjects,
792dbaf0 99 isSignupAllowed,
40298b02 100 computeResolutionsToTranscode,
eb080476 101 resetSequelizeInstance,
7a7724e6 102 getApplicationAccount,
792dbaf0 103 SortType
65fcc311 104}