1 import * as passwordGenerator from 'password-generator'
2 import { UserRole } from '../../shared'
3 import { mkdirpPromise, rimrafPromise } from '../helpers/core-utils'
4 import { logger } from '../helpers/logger'
5 import { createApplicationActor, createUserAccountAndChannel } from '../lib/user'
6 import { UserModel } from '../models/account/user'
7 import { ApplicationModel } from '../models/application/application'
8 import { OAuthClientModel } from '../models/oauth/oauth-client'
9 import { applicationExist, clientsExist, usersExist } from './checker'
10 import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
11 import { sequelizeTypescript } from './database'
13 async function installApplication () {
15 await sequelizeTypescript.sync()
16 await removeCacheDirectories()
17 await createDirectoriesIfNotExist()
18 await createApplicationIfNotExist()
19 await createOAuthClientIfNotExist()
20 await createOAuthAdminIfNotExist()
22 logger.error('Cannot install application.', { err })
27 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
35 function removeCacheDirectories () {
36 const cacheDirectories = CACHE.DIRECTORIES
38 const tasks: Promise<any>[] = []
41 for (const key of Object.keys(cacheDirectories)) {
42 const dir = cacheDirectories[key]
43 tasks.push(rimrafPromise(dir))
46 return Promise.all(tasks)
49 function createDirectoriesIfNotExist () {
50 const storage = CONFIG.STORAGE
51 const cacheDirectories = CACHE.DIRECTORIES
54 for (const key of Object.keys(storage)) {
55 const dir = storage[key]
56 tasks.push(mkdirpPromise(dir))
60 for (const key of Object.keys(cacheDirectories)) {
61 const dir = cacheDirectories[key]
62 tasks.push(mkdirpPromise(dir))
65 return Promise.all(tasks)
68 async function createOAuthClientIfNotExist () {
69 const exist = await clientsExist()
70 // Nothing to do, clients already exist
71 if (exist === true) return undefined
73 logger.info('Creating a default OAuth Client.')
75 const id = passwordGenerator(32, false, /[a-z0-9]/)
76 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
77 const client = new OAuthClientModel({
80 grants: [ 'password', 'refresh_token' ],
84 const createdClient = await client.save()
85 logger.info('Client id: ' + createdClient.clientId)
86 logger.info('Client secret: ' + createdClient.clientSecret)
91 async function createOAuthAdminIfNotExist () {
92 const exist = await usersExist()
93 // Nothing to do, users already exist
94 if (exist === true) return undefined
96 logger.info('Creating the administrator.')
98 const username = 'root'
99 const role = UserRole.ADMINISTRATOR
100 const email = CONFIG.ADMIN.EMAIL
101 let validatePassword = true
104 // Do not generate a random password for tests
105 if (process.env.NODE_ENV === 'test') {
108 if (process.env.NODE_APP_INSTANCE) {
109 password += process.env.NODE_APP_INSTANCE
112 // Our password is weak so do not validate it
113 validatePassword = false
115 password = passwordGenerator(16, true)
123 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
126 const user = new UserModel(userData)
128 await createUserAccountAndChannel(user, validatePassword)
129 logger.info('Username: ' + username)
130 logger.info('User password: ' + password)
133 async function createApplicationIfNotExist () {
134 const exist = await applicationExist()
135 // Nothing to do, application already exist
136 if (exist === true) return undefined
138 logger.info('Creating application account.')
140 const application = await ApplicationModel.create({
141 migrationVersion: LAST_MIGRATION_VERSION
144 return createApplicationActor(application.id)