1 import { Model } from 'sequelize-typescript'
2 import { ResultList } from '../../shared'
3 import { VideoResolution } from '../../shared/models/videos'
4 import { CONFIG } from '../initializers'
5 import { UserModel } from '../models/account/user'
6 import { ActorModel } from '../models/activitypub/actor'
7 import { ApplicationModel } from '../models/application/application'
8 import { pseudoRandomBytesPromise } from './core-utils'
9 import { logger } from './logger'
11 async function generateRandomString (size: number) {
12 const raw = await pseudoRandomBytesPromise(size)
14 return raw.toString('hex')
17 interface FormattableToJSON {
21 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
22 const formattedObjects: U[] = []
24 objects.forEach(object => {
25 formattedObjects.push(object.toFormattedJSON())
28 const res: ResultList<U> = {
30 data: formattedObjects
36 async function isSignupAllowed () {
37 if (CONFIG.SIGNUP.ENABLED === false) {
41 // No limit and signup is enabled
42 if (CONFIG.SIGNUP.LIMIT === -1) {
46 const totalUsers = await UserModel.countTotal()
48 return totalUsers < CONFIG.SIGNUP.LIMIT
51 function computeResolutionsToTranscode (videoFileHeight: number) {
52 const resolutionsEnabled: number[] = []
53 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
56 VideoResolution.H_240P,
57 VideoResolution.H_360P,
58 VideoResolution.H_480P,
59 VideoResolution.H_720P,
60 VideoResolution.H_1080P
63 for (const resolution of resolutions) {
64 if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) {
65 resolutionsEnabled.push(resolution)
69 return resolutionsEnabled
72 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
73 Object.keys(savedFields).forEach(key => {
74 const value = savedFields[key]
75 instance.set(key, value)
79 let serverActor: ActorModel
80 async function getServerActor () {
81 if (serverActor === undefined) {
82 const application = await ApplicationModel.load()
83 serverActor = application.Account.Actor
87 logger.error('Cannot load server actor.')
91 return Promise.resolve(serverActor)
94 type SortType = { sortModel: any, sortValue: string }
96 // ---------------------------------------------------------------------------
102 computeResolutionsToTranscode,
103 resetSequelizeInstance,