Server: fix migration at installation
[oweals/peertube.git] / server / initializers / checker.js
index a21e54982fd606b38d33ca806fd69a3a6e2b808f..2a33009b4ed7b65f26529364b24b0c0d0d5bf415 100644 (file)
@@ -1,12 +1,15 @@
 'use strict'
 
 const config = require('config')
-const mkdirp = require('mkdirp')
-const path = require('path')
+const mongoose = require('mongoose')
+
+const Client = mongoose.model('OAuthClient')
+const User = mongoose.model('User')
 
 const checker = {
   checkConfig: checkConfig,
-  createDirectoriesIfNotExist: createDirectoriesIfNotExist
+  clientsExist: clientsExist,
+  usersExist: usersExist
 }
 
 // Check the config files
@@ -14,8 +17,8 @@ function checkConfig () {
   const required = [ 'listen.port',
     'webserver.https', 'webserver.host', 'webserver.port',
     'database.host', 'database.port', 'database.suffix',
-    'storage.certs', 'storage.uploads', 'storage.logs',
-    'network.friends' ]
+    'storage.certs', 'storage.uploads', 'storage.logs', 'storage.thumbnails',
+    'electron.debug' ]
   const miss = []
 
   for (const key of required) {
@@ -27,18 +30,20 @@ function checkConfig () {
   return miss
 }
 
-// Create directories for the storage if it doesn't exist
-function createDirectoriesIfNotExist () {
-  const storages = config.get('storage')
+function clientsExist (callback) {
+  Client.list(function (err, clients) {
+    if (err) return callback(err)
 
-  for (const key of Object.keys(storages)) {
-    const dir = storages[key]
-    try {
-      mkdirp.sync(path.join(__dirname, '..', '..', dir))
-    } catch (error) {
-      throw new Error('Cannot create ' + path + ':' + error)
-    }
-  }
+    return callback(null, clients.length !== 0)
+  })
+}
+
+function usersExist (callback) {
+  User.countTotal(function (err, totalUsers) {
+    if (err) return callback(err)
+
+    return callback(null, totalUsers !== 0)
+  })
 }
 
 // ---------------------------------------------------------------------------