1 import * as passwordGenerator from 'password-generator'
2 import { UserRole } from '../../shared'
3 import { logger } from '../helpers/logger'
4 import { createApplicationActor, createUserAccountAndChannel } from '../lib/user'
5 import { UserModel } from '../models/account/user'
6 import { ApplicationModel } from '../models/application/application'
7 import { OAuthClientModel } from '../models/oauth/oauth-client'
8 import { applicationExist, clientsExist, usersExist } from './checker-after-init'
9 import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
10 import { sequelizeTypescript } from './database'
11 import { remove, ensureDir } from 'fs-extra'
13 async function installApplication () {
17 sequelizeTypescript.sync()
20 createApplicationIfNotExist(),
21 createOAuthClientIfNotExist(),
22 createOAuthAdminIfNotExist()
27 removeCacheDirectories()
28 .then(() => createDirectoriesIfNotExist())
31 logger.error('Cannot install application.', { err })
36 // ---------------------------------------------------------------------------
42 // ---------------------------------------------------------------------------
44 function removeCacheDirectories () {
45 const cacheDirectories = Object.keys(CACHE)
46 .map(k => CACHE[k].DIRECTORY)
48 const tasks: Promise<any>[] = []
51 for (const key of Object.keys(cacheDirectories)) {
52 const dir = cacheDirectories[key]
53 tasks.push(remove(dir))
56 return Promise.all(tasks)
59 function createDirectoriesIfNotExist () {
60 const storage = CONFIG.STORAGE
61 const cacheDirectories = Object.keys(CACHE)
62 .map(k => CACHE[k].DIRECTORY)
64 const tasks: Promise<void>[] = []
65 for (const key of Object.keys(storage)) {
66 const dir = storage[key]
67 tasks.push(ensureDir(dir))
71 for (const key of Object.keys(cacheDirectories)) {
72 const dir = cacheDirectories[key]
73 tasks.push(ensureDir(dir))
76 return Promise.all(tasks)
79 async function createOAuthClientIfNotExist () {
80 const exist = await clientsExist()
81 // Nothing to do, clients already exist
82 if (exist === true) return undefined
84 logger.info('Creating a default OAuth Client.')
86 const id = passwordGenerator(32, false, /[a-z0-9]/)
87 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
88 const client = new OAuthClientModel({
91 grants: [ 'password', 'refresh_token' ],
95 const createdClient = await client.save()
96 logger.info('Client id: ' + createdClient.clientId)
97 logger.info('Client secret: ' + createdClient.clientSecret)
102 async function createOAuthAdminIfNotExist () {
103 const exist = await usersExist()
104 // Nothing to do, users already exist
105 if (exist === true) return undefined
107 logger.info('Creating the administrator.')
109 const username = 'root'
110 const role = UserRole.ADMINISTRATOR
111 const email = CONFIG.ADMIN.EMAIL
112 let validatePassword = true
115 // Do not generate a random password for tests
116 if (process.env.NODE_ENV === 'test') {
119 if (process.env.NODE_APP_INSTANCE) {
120 password += process.env.NODE_APP_INSTANCE
123 // Our password is weak so do not validate it
124 validatePassword = false
126 password = passwordGenerator(16, true)
135 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
139 const user = new UserModel(userData)
141 await createUserAccountAndChannel(user, validatePassword)
142 logger.info('Username: ' + username)
143 logger.info('User password: ' + password)
146 async function createApplicationIfNotExist () {
147 const exist = await applicationExist()
148 // Nothing to do, application already exist
149 if (exist === true) return undefined
151 logger.info('Creating application account.')
153 const application = await ApplicationModel.create({
154 migrationVersion: LAST_MIGRATION_VERSION
157 return createApplicationActor(application.id)