Server: fix migration at installation
[oweals/peertube.git] / server / initializers / checker.js
1 'use strict'
2
3 const config = require('config')
4 const mongoose = require('mongoose')
5
6 const Client = mongoose.model('OAuthClient')
7 const User = mongoose.model('User')
8
9 const checker = {
10   checkConfig: checkConfig,
11   clientsExist: clientsExist,
12   usersExist: usersExist
13 }
14
15 // Check the config files
16 function checkConfig () {
17   const required = [ 'listen.port',
18     'webserver.https', 'webserver.host', 'webserver.port',
19     'database.host', 'database.port', 'database.suffix',
20     'storage.certs', 'storage.uploads', 'storage.logs', 'storage.thumbnails',
21     'electron.debug' ]
22   const miss = []
23
24   for (const key of required) {
25     if (!config.has(key)) {
26       miss.push(key)
27     }
28   }
29
30   return miss
31 }
32
33 function clientsExist (callback) {
34   Client.list(function (err, clients) {
35     if (err) return callback(err)
36
37     return callback(null, clients.length !== 0)
38   })
39 }
40
41 function usersExist (callback) {
42   User.countTotal(function (err, totalUsers) {
43     if (err) return callback(err)
44
45     return callback(null, totalUsers !== 0)
46   })
47 }
48
49 // ---------------------------------------------------------------------------
50
51 module.exports = checker