From e4c556196d7b31111f17596840d2e1d60caa7dcb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 31 Jul 2016 20:58:43 +0200 Subject: Server: reorganize express validators --- server/initializers/constants.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 467816f2c..5f4aeccc6 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -3,6 +3,23 @@ // API version of our pod const API_VERSION = 'v1' +const CONSTRAINTS_FIELDS = { + USERS: { + USERNAME: { min: 3, max: 20 }, // Length + PASSWORD: { min: 6, max: 255 } // Length + }, + VIDEOS: { + NAME: { min: 3, max: 50 }, // Length + DESCRIPTION: { min: 3, max: 250 }, // Length + MAGNET_URI: { min: 10 }, // Length + DURATION: { min: 1, max: 7200 }, // Number + TAGS: { min: 1, max: 3 }, // Number of total tags + TAG: { min: 2, max: 10 }, // Length + THUMBNAIL: { min: 2, max: 30 }, + THUMBNAIL64: { min: 0, max: 20000 } // Bytes + } +} + // Score a pod has when we create it as a friend const FRIEND_SCORE = { BASE: 100, @@ -55,29 +72,18 @@ const THUMBNAILS_SIZE = '200x110' // Path for access to thumbnails with express router const THUMBNAILS_STATIC_PATH = '/static/thumbnails' -const VIDEOS_CONSTRAINTS_FIELDS = { - NAME: { min: 3, max: 50 }, // Length - DESCRIPTION: { min: 3, max: 250 }, // Length - MAGNET_URI: { min: 10 }, // Length - DURATION: { min: 1, max: 7200 }, // Number - AUTHOR: { min: 3, max: 20 }, // Length - TAGS: { min: 1, max: 3 }, // Number of total tags - TAG: { min: 2, max: 10 }, // Length - THUMBNAIL: { min: 2, max: 30 }, - THUMBNAIL64: { min: 0, max: 20000 } // Bytes -} - // Special constants for a test instance if (isTestInstance() === true) { FRIEND_SCORE.BASE = 20 INTERVAL = 10000 - VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14 + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14 } // --------------------------------------------------------------------------- module.exports = { API_VERSION: API_VERSION, + CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, FRIEND_SCORE: FRIEND_SCORE, INTERVAL: INTERVAL, OAUTH_LIFETIME: OAUTH_LIFETIME, @@ -90,8 +96,7 @@ module.exports = { SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL, SORTABLE_COLUMNS: SORTABLE_COLUMNS, THUMBNAILS_SIZE: THUMBNAILS_SIZE, - THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH, - VIDEOS_CONSTRAINTS_FIELDS: VIDEOS_CONSTRAINTS_FIELDS + THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH } // --------------------------------------------------------------------------- -- cgit v1.2.3 From 9bd2662976a75d3b03364cdbe6419e57c80f99a6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 Aug 2016 22:32:36 +0200 Subject: Implement user API (create, update, remove, list) --- server/initializers/constants.js | 8 +++++++- server/initializers/installer.js | 9 ++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 5f4aeccc6..416356400 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -72,6 +72,11 @@ const THUMBNAILS_SIZE = '200x110' // Path for access to thumbnails with express router const THUMBNAILS_STATIC_PATH = '/static/thumbnails' +const USER_ROLES = { + ADMIN: 'admin', + USER: 'user' +} + // Special constants for a test instance if (isTestInstance() === true) { FRIEND_SCORE.BASE = 20 @@ -96,7 +101,8 @@ module.exports = { SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL, SORTABLE_COLUMNS: SORTABLE_COLUMNS, THUMBNAILS_SIZE: THUMBNAILS_SIZE, - THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH + THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH, + USER_ROLES: USER_ROLES } // --------------------------------------------------------------------------- diff --git a/server/initializers/installer.js b/server/initializers/installer.js index 32830d4da..c12187871 100644 --- a/server/initializers/installer.js +++ b/server/initializers/installer.js @@ -9,6 +9,7 @@ const path = require('path') const series = require('async/series') const checker = require('./checker') +const constants = require('./constants') const logger = require('../helpers/logger') const peertubeCrypto = require('../helpers/peertube-crypto') @@ -34,7 +35,7 @@ function installApplication (callback) { }, function createOAuthUser (callbackAsync) { - createOAuthUserIfNotExist(callbackAsync) + createOAuthAdminIfNotExist(callbackAsync) } ], callback) } @@ -80,7 +81,7 @@ function createOAuthClientIfNotExist (callback) { }) } -function createOAuthUserIfNotExist (callback) { +function createOAuthAdminIfNotExist (callback) { checker.usersExist(function (err, exist) { if (err) return callback(err) @@ -90,6 +91,7 @@ function createOAuthUserIfNotExist (callback) { logger.info('Creating the administrator.') const username = 'root' + const role = constants.USER_ROLES.ADMIN let password = '' // Do not generate a random password for tests @@ -105,7 +107,8 @@ function createOAuthUserIfNotExist (callback) { const user = new User({ username: username, - password: password + password: password, + role: role }) user.save(function (err, createdUser) { -- cgit v1.2.3 From 089ff2f2046fdbaf9531726eea1f8c6726ebf0c0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Aug 2016 21:51:35 +0200 Subject: Server: optimize function to see if there are users or not --- server/initializers/checker.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/checker.js b/server/initializers/checker.js index 3831efb8d..871d3cac2 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js @@ -39,10 +39,10 @@ function clientsExist (callback) { } function usersExist (callback) { - User.list(function (err, users) { + User.count(function (err, totalUsers) { if (err) return callback(err) - return callback(null, users.length !== 0) + return callback(null, totalUsers !== 0) }) } -- cgit v1.2.3 From 5c39adb7313e0696aabb4b71196ab7b0b378c359 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Aug 2016 22:31:45 +0200 Subject: Server: add user list sort/pagination --- server/initializers/checker.js | 2 +- server/initializers/constants.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'server/initializers') diff --git a/server/initializers/checker.js b/server/initializers/checker.js index 871d3cac2..4b6997547 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js @@ -39,7 +39,7 @@ function clientsExist (callback) { } function usersExist (callback) { - User.count(function (err, totalUsers) { + User.countTotal(function (err, totalUsers) { if (err) return callback(err) return callback(null, totalUsers !== 0) diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 416356400..cd2e0cfb9 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -63,6 +63,7 @@ const SEEDS_IN_PARALLEL = 3 // Sortable columns per schema const SORTABLE_COLUMNS = { + USERS: [ 'username', '-username', 'createdDate', '-createdDate' ], VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ] } -- cgit v1.2.3 From e861452fb26553177ad4e32bfb18b4fd8a5b1816 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Aug 2016 21:34:51 +0200 Subject: Server: put config in constants --- server/initializers/constants.js | 27 +++++++++++++++++++++++++++ server/initializers/database.js | 8 ++------ 2 files changed, 29 insertions(+), 6 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/constants.js b/server/initializers/constants.js index cd2e0cfb9..ce9f8ad6c 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -1,8 +1,34 @@ 'use strict' +const config = require('config') +const path = require('path') + // API version of our pod const API_VERSION = 'v1' +const CONFIG = { + DATABASE: { + DBNAME: 'peertube' + config.get('database.suffix'), + HOST: config.get('database.host'), + PORT: config.get('database.port') + }, + ELECTRON: { + DEBUG: config.get('electron.debug') + }, + STORAGE: { + CERT_DIR: path.join(__dirname, '..', '..', config.get('storage.certs')), + LOG_DIR: path.join(__dirname, '..', '..', config.get('storage.logs')), + UPLOAD_DIR: path.join(__dirname, '..', '..', config.get('storage.uploads')), + THUMBNAILS_DIR: path.join(__dirname, '..', '..', config.get('storage.thumbnails')) + }, + WEBSERVER: { + SCHEME: config.get('webserver.https') === true ? 'https' : 'http', + HOST: config.get('webserver.host'), + PORT: config.get('webserver.port') + } +} +CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOST + ':' + CONFIG.WEBSERVER.PORT + const CONSTRAINTS_FIELDS = { USERS: { USERNAME: { min: 3, max: 20 }, // Length @@ -89,6 +115,7 @@ if (isTestInstance() === true) { module.exports = { API_VERSION: API_VERSION, + CONFIG: CONFIG, CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, FRIEND_SCORE: FRIEND_SCORE, INTERVAL: INTERVAL, diff --git a/server/initializers/database.js b/server/initializers/database.js index 8626895ee..20dcc056e 100644 --- a/server/initializers/database.js +++ b/server/initializers/database.js @@ -1,8 +1,8 @@ 'use strict' -const config = require('config') const mongoose = require('mongoose') +const constants = require('../initializers/constants') const logger = require('../helpers/logger') // Bootstrap models @@ -14,17 +14,13 @@ require('../models/video') // Request model needs Video model require('../models/request') -const dbname = 'peertube' + config.get('database.suffix') -const host = config.get('database.host') -const port = config.get('database.port') - const database = { connect: connect } function connect () { mongoose.Promise = global.Promise - mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname) + mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOST + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME) mongoose.connection.on('error', function () { throw new Error('Mongodb connection error.') }) -- cgit v1.2.3 From 1e2564d3927ce4ca4ca9a09930da6da7ebb4e9a1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 20 Aug 2016 16:59:25 +0200 Subject: Server: make friends urls come from the request instead of the configuration file --- server/initializers/checker.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/checker.js b/server/initializers/checker.js index 4b6997547..2a33009b4 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js @@ -17,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', 'electron.debug' ] + 'storage.certs', 'storage.uploads', 'storage.logs', 'storage.thumbnails', + 'electron.debug' ] const miss = [] for (const key of required) { -- cgit v1.2.3 From 26d7d31ba3b1d26ea9a51e8626e4a4537867db94 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 25 Aug 2016 17:57:37 +0200 Subject: Server: encrypt password in database --- server/initializers/constants.js | 3 +++ server/initializers/installer.js | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/constants.js b/server/initializers/constants.js index ce9f8ad6c..dd4eff493 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -6,6 +6,8 @@ const path = require('path') // API version of our pod const API_VERSION = 'v1' +const BCRYPT_SALT_SIZE = 10 + const CONFIG = { DATABASE: { DBNAME: 'peertube' + config.get('database.suffix'), @@ -115,6 +117,7 @@ if (isTestInstance() === true) { module.exports = { API_VERSION: API_VERSION, + BCRYPT_SALT_SIZE: BCRYPT_SALT_SIZE, CONFIG: CONFIG, CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, FRIEND_SCORE: FRIEND_SCORE, diff --git a/server/initializers/installer.js b/server/initializers/installer.js index c12187871..974402094 100644 --- a/server/initializers/installer.js +++ b/server/initializers/installer.js @@ -114,8 +114,8 @@ function createOAuthAdminIfNotExist (callback) { user.save(function (err, createdUser) { if (err) return callback(err) - logger.info('Username: ' + createdUser.username) - logger.info('User password: ' + createdUser.password) + logger.info('Username: ' + username) + logger.info('User password: ' + password) return callback(null) }) -- cgit v1.2.3 From d3cd34be2fd03ea79145dfa8eb16e82d2e23191b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 19 Sep 2016 21:33:46 +0200 Subject: Server: add requests stats endpoint --- server/initializers/constants.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/constants.js b/server/initializers/constants.js index dd4eff493..76ebb8681 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -55,7 +55,7 @@ const FRIEND_SCORE = { } // Time to wait between requests to the friends (10 min) -let INTERVAL = 600000 +let REQUESTS_INTERVAL = 600000 const OAUTH_LIFETIME = { ACCESS_TOKEN: 3600 * 4, // 4 hours @@ -109,7 +109,7 @@ const USER_ROLES = { // Special constants for a test instance if (isTestInstance() === true) { FRIEND_SCORE.BASE = 20 - INTERVAL = 10000 + REQUESTS_INTERVAL = 10000 CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14 } @@ -121,7 +121,7 @@ module.exports = { CONFIG: CONFIG, CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, FRIEND_SCORE: FRIEND_SCORE, - INTERVAL: INTERVAL, + REQUESTS_INTERVAL: REQUESTS_INTERVAL, OAUTH_LIFETIME: OAUTH_LIFETIME, PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, PODS_SCORE: PODS_SCORE, -- cgit v1.2.3 From 00d6b0dda4b1964ab11127851c0fc7106cc0f912 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 26 Sep 2016 22:36:36 +0200 Subject: Add migration (for db, folders...) mechanism --- server/initializers/constants.js | 16 ++++++- server/initializers/database.js | 1 + .../migrations/0005-create-application.js | 17 +++++++ .../initializers/migrations/0010-users-password.js | 23 +++++++++ server/initializers/migrator.js | 56 ++++++++++++++++++++++ 5 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 server/initializers/migrations/0005-create-application.js create mode 100644 server/initializers/migrations/0010-users-password.js create mode 100644 server/initializers/migrator.js (limited to 'server/initializers') diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 76ebb8681..10ae48e95 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -54,6 +54,18 @@ const FRIEND_SCORE = { MAX: 1000 } +const MONGO_MIGRATION_SCRIPTS = [ + { + script: '0005-create-application', + version: 5 + }, + { + script: '0010-users-password', + version: 10 + } +] +const LAST_MONGO_SCHEMA_VERSION = 10 + // Time to wait between requests to the friends (10 min) let REQUESTS_INTERVAL = 600000 @@ -121,11 +133,13 @@ module.exports = { CONFIG: CONFIG, CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, FRIEND_SCORE: FRIEND_SCORE, - REQUESTS_INTERVAL: REQUESTS_INTERVAL, + LAST_MONGO_SCHEMA_VERSION: LAST_MONGO_SCHEMA_VERSION, + MONGO_MIGRATION_SCRIPTS: MONGO_MIGRATION_SCRIPTS, OAUTH_LIFETIME: OAUTH_LIFETIME, PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, PODS_SCORE: PODS_SCORE, REQUESTS_IN_PARALLEL: REQUESTS_IN_PARALLEL, + REQUESTS_INTERVAL: REQUESTS_INTERVAL, REQUESTS_LIMIT: REQUESTS_LIMIT, RETRY_REQUESTS: RETRY_REQUESTS, SEARCHABLE_COLUMNS: SEARCHABLE_COLUMNS, diff --git a/server/initializers/database.js b/server/initializers/database.js index 20dcc056e..1da574631 100644 --- a/server/initializers/database.js +++ b/server/initializers/database.js @@ -6,6 +6,7 @@ const constants = require('../initializers/constants') const logger = require('../helpers/logger') // Bootstrap models +require('../models/application') require('../models/user') require('../models/oauth-client') require('../models/oauth-token') diff --git a/server/initializers/migrations/0005-create-application.js b/server/initializers/migrations/0005-create-application.js new file mode 100644 index 000000000..e99dec019 --- /dev/null +++ b/server/initializers/migrations/0005-create-application.js @@ -0,0 +1,17 @@ +/* + Create the application collection in MongoDB. + Used to store the actual MongoDB scheme version. +*/ + +const mongoose = require('mongoose') + +const Application = mongoose.model('Application') + +exports.up = function (callback) { + const application = new Application() + application.save(callback) +} + +exports.down = function (callback) { + throw new Error('Not implemented.') +} diff --git a/server/initializers/migrations/0010-users-password.js b/server/initializers/migrations/0010-users-password.js new file mode 100644 index 000000000..e031fa142 --- /dev/null +++ b/server/initializers/migrations/0010-users-password.js @@ -0,0 +1,23 @@ +/* + Convert plain user password to encrypted user password. +*/ + +const mongoose = require('mongoose') + +const User = mongoose.model('User') + +exports.up = function (callback) { + User.list(function (err, users) { + if (err) return callback(err) + + users.forEach(function (user) { + user.save() + }) + + return callback(null) + }) +} + +exports.down = function (callback) { + throw new Error('Not implemented.') +} diff --git a/server/initializers/migrator.js b/server/initializers/migrator.js new file mode 100644 index 000000000..6b31d994f --- /dev/null +++ b/server/initializers/migrator.js @@ -0,0 +1,56 @@ +'use strict' + +const eachSeries = require('async/eachSeries') +const mongoose = require('mongoose') +const path = require('path') + +const constants = require('./constants') +const logger = require('../helpers/logger') + +const Application = mongoose.model('Application') + +const migrator = { + migrate: migrate +} + +function migrate (callback) { + Application.loadMongoSchemaVersion(function (err, actualVersion) { + if (err) return callback(err) + + // If there are a new mongo schemas + if (!actualVersion || actualVersion < constants.LAST_MONGO_SCHEMA_VERSION) { + logger.info('Begin migrations.') + + eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) { + const versionScript = entity.version + + // Do not execute old migration scripts + if (versionScript <= actualVersion) return callbackEach(null) + + // Load the migration module and run it + const migrationScriptName = entity.script + logger.info('Executing %s migration script.', migrationScriptName) + + const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) + migrationScript.up(function (err) { + if (err) return callbackEach(err) + + // Update the new mongo version schema + Application.updateMongoSchemaVersion(versionScript, callbackEach) + }) + }, function (err) { + if (err) return callback(err) + + logger.info('Migrations finished. New mongo version schema: %s', constants.LAST_MONGO_SCHEMA_VERSION) + return callback(null) + }) + } else { + return callback(null) + } + }) +} + +// --------------------------------------------------------------------------- + +module.exports = migrator + -- cgit v1.2.3 From f6a0754fdacf9b890292f1efc62a9035bceb454a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 26 Sep 2016 23:10:32 +0200 Subject: Server: fix migration at installation --- server/initializers/database.js | 2 +- server/initializers/installer.js | 5 ++++- server/initializers/migrations/0010-users-password.js | 9 ++++----- 3 files changed, 9 insertions(+), 7 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/database.js b/server/initializers/database.js index 1da574631..45c8a240d 100644 --- a/server/initializers/database.js +++ b/server/initializers/database.js @@ -7,9 +7,9 @@ const logger = require('../helpers/logger') // Bootstrap models require('../models/application') +require('../models/oauth-token') require('../models/user') require('../models/oauth-client') -require('../models/oauth-token') require('../models/pods') require('../models/video') // Request model needs Video model diff --git a/server/initializers/installer.js b/server/initializers/installer.js index 974402094..8c3148e79 100644 --- a/server/initializers/installer.js +++ b/server/initializers/installer.js @@ -13,6 +13,7 @@ const constants = require('./constants') const logger = require('../helpers/logger') const peertubeCrypto = require('../helpers/peertube-crypto') +const Application = mongoose.model('Application') const Client = mongoose.model('OAuthClient') const User = mongoose.model('User') @@ -117,7 +118,9 @@ function createOAuthAdminIfNotExist (callback) { logger.info('Username: ' + username) logger.info('User password: ' + password) - return callback(null) + logger.info('Creating Application collection.') + const application = new Application({ mongoSchemaVersion: constants.LAST_MONGO_SCHEMA_VERSION }) + application.save(callback) }) }) } diff --git a/server/initializers/migrations/0010-users-password.js b/server/initializers/migrations/0010-users-password.js index e031fa142..a0616a269 100644 --- a/server/initializers/migrations/0010-users-password.js +++ b/server/initializers/migrations/0010-users-password.js @@ -2,6 +2,7 @@ Convert plain user password to encrypted user password. */ +const eachSeries = require('async/eachSeries') const mongoose = require('mongoose') const User = mongoose.model('User') @@ -10,11 +11,9 @@ exports.up = function (callback) { User.list(function (err, users) { if (err) return callback(err) - users.forEach(function (user) { - user.save() - }) - - return callback(null) + eachSeries(users, function (user, callbackEach) { + user.save(callbackEach) + }, callback) }) } -- cgit v1.2.3 From c60f2212fd326c323dc2d145ba64080612b655d3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 27 Sep 2016 22:41:38 +0200 Subject: Server: migration script that add admin role to root user --- server/initializers/constants.js | 6 +++++- server/initializers/migrations/0015-admin-role.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 server/initializers/migrations/0015-admin-role.js (limited to 'server/initializers') diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 10ae48e95..02043bd45 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -62,9 +62,13 @@ const MONGO_MIGRATION_SCRIPTS = [ { script: '0010-users-password', version: 10 + }, + { + script: '0015-admin-role', + version: 15 } ] -const LAST_MONGO_SCHEMA_VERSION = 10 +const LAST_MONGO_SCHEMA_VERSION = 15 // Time to wait between requests to the friends (10 min) let REQUESTS_INTERVAL = 600000 diff --git a/server/initializers/migrations/0015-admin-role.js b/server/initializers/migrations/0015-admin-role.js new file mode 100644 index 000000000..af06dca9e --- /dev/null +++ b/server/initializers/migrations/0015-admin-role.js @@ -0,0 +1,16 @@ +/* + Set the admin role to the root user. +*/ + +const constants = require('../constants') +const mongoose = require('mongoose') + +const User = mongoose.model('User') + +exports.up = function (callback) { + User.update({ username: 'root' }, { role: constants.USER_ROLES.ADMIN }, callback) +} + +exports.down = function (callback) { + throw new Error('Not implemented.') +} -- cgit v1.2.3 From 9f6bae3a9db13bf827f8aaff903aac06ec430903 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 2 Oct 2016 11:14:08 +0200 Subject: Server: reorganize constant file --- server/initializers/constants.js | 111 ++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 47 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 02043bd45..b1d033377 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -3,10 +3,31 @@ const config = require('config') const path = require('path') -// API version of our pod +// --------------------------------------------------------------------------- + +// API version const API_VERSION = 'v1' -const BCRYPT_SALT_SIZE = 10 +// Number of results by default for the pagination +const PAGINATION_COUNT_DEFAULT = 15 + +// Sortable columns per schema +const SEARCHABLE_COLUMNS = { + VIDEOS: [ 'name', 'magnetUri', 'podUrl', 'author', 'tags' ] +} + +// Sortable columns per schema +const SORTABLE_COLUMNS = { + USERS: [ 'username', '-username', 'createdDate', '-createdDate' ], + VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ] +} + +const OAUTH_LIFETIME = { + ACCESS_TOKEN: 3600 * 4, // 4 hours + REFRESH_TOKEN: 1209600 // 2 weeks +} + +// --------------------------------------------------------------------------- const CONFIG = { DATABASE: { @@ -31,6 +52,8 @@ const CONFIG = { } CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOST + ':' + CONFIG.WEBSERVER.PORT +// --------------------------------------------------------------------------- + const CONSTRAINTS_FIELDS = { USERS: { USERNAME: { min: 3, max: 20 }, // Length @@ -48,12 +71,16 @@ const CONSTRAINTS_FIELDS = { } } +// --------------------------------------------------------------------------- + // Score a pod has when we create it as a friend const FRIEND_SCORE = { BASE: 100, MAX: 1000 } +// --------------------------------------------------------------------------- + const MONGO_MIGRATION_SCRIPTS = [ { script: '0005-create-application', @@ -70,16 +97,7 @@ const MONGO_MIGRATION_SCRIPTS = [ ] const LAST_MONGO_SCHEMA_VERSION = 15 -// Time to wait between requests to the friends (10 min) -let REQUESTS_INTERVAL = 600000 - -const OAUTH_LIFETIME = { - ACCESS_TOKEN: 3600 * 4, // 4 hours - REFRESH_TOKEN: 1209600 // 2 weeks -} - -// Number of results by default for the pagination -const PAGINATION_COUNT_DEFAULT = 15 +// --------------------------------------------------------------------------- // Number of points we add/remove from a friend after a successful/bad request const PODS_SCORE = { @@ -87,29 +105,22 @@ const PODS_SCORE = { BONUS: 10 } +// Time to wait between requests to the friends (10 min) +let REQUESTS_INTERVAL = 600000 + // Number of requests in parallel we can make const REQUESTS_IN_PARALLEL = 10 -// How many requests we put in request (request scheduler) +// How many requests we put in request const REQUESTS_LIMIT = 10 // Number of requests to retry for replay requests module const RETRY_REQUESTS = 5 -// Sortable columns per schema -const SEARCHABLE_COLUMNS = { - VIDEOS: [ 'name', 'magnetUri', 'podUrl', 'author', 'tags' ] -} - -// Seeds in parallel we send to electron when "seed all" -// Once a video is in seeding state we seed another video etc -const SEEDS_IN_PARALLEL = 3 +// --------------------------------------------------------------------------- -// Sortable columns per schema -const SORTABLE_COLUMNS = { - USERS: [ 'username', '-username', 'createdDate', '-createdDate' ], - VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ] -} +// Password encryption +const BCRYPT_SALT_SIZE = 10 // Videos thumbnail size const THUMBNAILS_SIZE = '200x110' @@ -122,36 +133,42 @@ const USER_ROLES = { USER: 'user' } +// Seeds in parallel we send to electron when "seed all" +// Once a video is in seeding state we seed another video etc +const SEEDS_IN_PARALLEL = 3 + +// --------------------------------------------------------------------------- + // Special constants for a test instance if (isTestInstance() === true) { + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14 FRIEND_SCORE.BASE = 20 REQUESTS_INTERVAL = 10000 - CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14 } // --------------------------------------------------------------------------- module.exports = { - API_VERSION: API_VERSION, - BCRYPT_SALT_SIZE: BCRYPT_SALT_SIZE, - CONFIG: CONFIG, - CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, - FRIEND_SCORE: FRIEND_SCORE, - LAST_MONGO_SCHEMA_VERSION: LAST_MONGO_SCHEMA_VERSION, - MONGO_MIGRATION_SCRIPTS: MONGO_MIGRATION_SCRIPTS, - OAUTH_LIFETIME: OAUTH_LIFETIME, - PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, - PODS_SCORE: PODS_SCORE, - REQUESTS_IN_PARALLEL: REQUESTS_IN_PARALLEL, - REQUESTS_INTERVAL: REQUESTS_INTERVAL, - REQUESTS_LIMIT: REQUESTS_LIMIT, - RETRY_REQUESTS: RETRY_REQUESTS, - SEARCHABLE_COLUMNS: SEARCHABLE_COLUMNS, - SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL, - SORTABLE_COLUMNS: SORTABLE_COLUMNS, - THUMBNAILS_SIZE: THUMBNAILS_SIZE, - THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH, - USER_ROLES: USER_ROLES + API_VERSION, + BCRYPT_SALT_SIZE, + CONFIG, + CONSTRAINTS_FIELDS, + FRIEND_SCORE, + LAST_MONGO_SCHEMA_VERSION, + MONGO_MIGRATION_SCRIPTS, + OAUTH_LIFETIME, + PAGINATION_COUNT_DEFAULT, + PODS_SCORE, + REQUESTS_IN_PARALLEL, + REQUESTS_INTERVAL, + REQUESTS_LIMIT, + RETRY_REQUESTS, + SEARCHABLE_COLUMNS, + SEEDS_IN_PARALLEL, + SORTABLE_COLUMNS, + THUMBNAILS_SIZE, + THUMBNAILS_STATIC_PATH, + USER_ROLES } // --------------------------------------------------------------------------- -- cgit v1.2.3 From c4403b29ad4db097af528a7f04eea07e0ed320d0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 2 Oct 2016 12:19:02 +0200 Subject: Server: remove useless hash affectations --- server/initializers/checker.js | 6 +++--- server/initializers/installer.js | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'server/initializers') diff --git a/server/initializers/checker.js b/server/initializers/checker.js index 2a33009b4..91fbcfaf9 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js @@ -7,9 +7,9 @@ const Client = mongoose.model('OAuthClient') const User = mongoose.model('User') const checker = { - checkConfig: checkConfig, - clientsExist: clientsExist, - usersExist: usersExist + checkConfig, + clientsExist, + usersExist } // Check the config files diff --git a/server/initializers/installer.js b/server/initializers/installer.js index 8c3148e79..1df300ba8 100644 --- a/server/initializers/installer.js +++ b/server/initializers/installer.js @@ -18,7 +18,7 @@ const Client = mongoose.model('OAuthClient') const User = mongoose.model('User') const installer = { - installApplication: installApplication + installApplication } function installApplication (callback) { @@ -107,9 +107,9 @@ function createOAuthAdminIfNotExist (callback) { } const user = new User({ - username: username, - password: password, - role: role + username, + password, + role }) user.save(function (err, createdUser) { -- cgit v1.2.3