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/controllers/api/v1/pods.js | 14 +++- server/controllers/api/v1/users.js | 132 ++++++++++++++++++++++++++++++++++++- 2 files changed, 141 insertions(+), 5 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index 2bc761fef..f61f2a483 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js @@ -8,6 +8,7 @@ const waterfall = require('async/waterfall') const logger = require('../../../helpers/logger') const friends = require('../../../lib/friends') const middlewares = require('../../../middlewares') +const admin = middlewares.admin const oAuth = middlewares.oauth const validators = middlewares.validators.pods const signatureValidator = middlewares.validators.remote.signature @@ -18,8 +19,17 @@ const Video = mongoose.model('Video') router.get('/', listPodsUrl) router.post('/', validators.podsAdd, addPods) -router.get('/makefriends', oAuth.authenticate, validators.makeFriends, makeFriends) -router.get('/quitfriends', oAuth.authenticate, quitFriends) +router.get('/makefriends', + oAuth.authenticate, + admin.ensureIsAdmin, + validators.makeFriends, + makeFriends +) +router.get('/quitfriends', + oAuth.authenticate, + admin.ensureIsAdmin, + quitFriends +) // Post because this is a secured request router.post('/remove', signatureValidator, removePods) diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index fbbe6e472..e084974ce 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js @@ -1,18 +1,49 @@ 'use strict' +const each = require('async/each') const config = require('config') -const mongoose = require('mongoose') const express = require('express') +const mongoose = require('mongoose') +const waterfall = require('async/waterfall') -const oAuth = require('../../../middlewares').oauth +const constants = require('../../../initializers/constants') +const friends = require('../../../lib/friends') +const logger = require('../../../helpers/logger') +const middlewares = require('../../../middlewares') +const admin = middlewares.admin +const oAuth = middlewares.oauth +const validatorsUsers = middlewares.validators.users const Client = mongoose.model('OAuthClient') +const User = mongoose.model('User') +const Video = mongoose.model('Video') const router = express.Router() +router.get('/', listUsers) + +router.post('/', + oAuth.authenticate, + admin.ensureIsAdmin, + validatorsUsers.usersAdd, + createUser +) + +router.put('/:id', + oAuth.authenticate, + validatorsUsers.usersUpdate, + updateUser +) + +router.delete('/:username', + oAuth.authenticate, + admin.ensureIsAdmin, + validatorsUsers.usersRemove, + removeUser +) router.get('/client', getAngularClient) router.post('/token', oAuth.token, success) -// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged,, implement revoke token route +// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route // --------------------------------------------------------------------------- @@ -20,6 +51,20 @@ module.exports = router // --------------------------------------------------------------------------- +function createUser (req, res, next) { + const user = new User({ + username: req.body.username, + password: req.body.password, + role: constants.USER_ROLES.USER + }) + + user.save(function (err, createdUser) { + if (err) return next(err) + + return res.type('json').status(204).end() + }) +} + function getAngularClient (req, res, next) { const serverHost = config.get('webserver.host') const serverPort = config.get('webserver.port') @@ -44,6 +89,87 @@ function getAngularClient (req, res, next) { }) } +function listUsers (req, res, next) { + User.list(function (err, usersList) { + if (err) return next(err) + + res.json(getFormatedUsers(usersList)) + }) +} + +function removeUser (req, res, next) { + waterfall([ + function getUser (callback) { + User.loadByUsername(req.params.username, callback) + }, + + function getVideos (user, callback) { + Video.listOwnedByAuthor(user.username, function (err, videos) { + return callback(err, user, videos) + }) + }, + + function removeVideosFromDB (user, videos, callback) { + each(videos, function (video, callbackEach) { + video.remove(callbackEach) + }, function (err) { + return callback(err, user, videos) + }) + }, + + function sendInformationToFriends (user, videos, callback) { + videos.forEach(function (video) { + const params = { + name: video.name, + magnetUri: video.magnetUri + } + + friends.removeVideoToFriends(params) + }) + + return callback(null, user) + }, + + function removeUserFromDB (user, callback) { + user.remove(callback) + } + ], function andFinally (err) { + if (err) { + logger.error('Errors when removed the user.', { error: err }) + return next(err) + } + + return res.type('json').status(204).end() + }) +} + +function updateUser (req, res, next) { + User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { + if (err) return next(err) + + user.password = req.body.password + user.save(function (err) { + if (err) return next(err) + + return res.json('json').status(204).end() + }) + }) +} + function success (req, res, next) { res.end() } + +// --------------------------------------------------------------------------- + +function getFormatedUsers (users) { + const formatedUsers = [] + + users.forEach(function (user) { + formatedUsers.push(user.toFormatedJSON()) + }) + + return { + data: formatedUsers + } +} -- cgit v1.2.3 From 6606150c49f587bc7eb0ecec4263ce7fbb18bf15 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Aug 2016 16:09:39 +0200 Subject: Server: move clients in its own file --- server/controllers/api/v1/clients.js | 40 ++++++++++++++++++++++++++++++++++++ server/controllers/api/v1/index.js | 2 ++ server/controllers/api/v1/users.js | 28 +------------------------ 3 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 server/controllers/api/v1/clients.js (limited to 'server/controllers') diff --git a/server/controllers/api/v1/clients.js b/server/controllers/api/v1/clients.js new file mode 100644 index 000000000..0d222634b --- /dev/null +++ b/server/controllers/api/v1/clients.js @@ -0,0 +1,40 @@ +'use strict' + +const config = require('config') +const express = require('express') +const mongoose = require('mongoose') + +const Client = mongoose.model('OAuthClient') + +const router = express.Router() + +router.get('/local', getLocalClient) + +// Get the client credentials for the PeerTube front end +function getLocalClient (req, res, next) { + const serverHost = config.get('webserver.host') + const serverPort = config.get('webserver.port') + let headerHostShouldBe = serverHost + if (serverPort !== 80 && serverPort !== 443) { + headerHostShouldBe += ':' + serverPort + } + + // Don't make this check if this is a test instance + if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) { + return res.type('json').status(403).end() + } + + Client.loadFirstClient(function (err, client) { + if (err) return next(err) + if (!client) return next(new Error('No client available.')) + + res.json({ + client_id: client._id, + client_secret: client.clientSecret + }) + }) +} + +// --------------------------------------------------------------------------- + +module.exports = router diff --git a/server/controllers/api/v1/index.js b/server/controllers/api/v1/index.js index e0c29a8a2..af41bc280 100644 --- a/server/controllers/api/v1/index.js +++ b/server/controllers/api/v1/index.js @@ -4,11 +4,13 @@ const express = require('express') const router = express.Router() +const clientsController = require('./clients') const podsController = require('./pods') const remoteController = require('./remote') const usersController = require('./users') const videosController = require('./videos') +router.use('/clients', clientsController) router.use('/pods', podsController) router.use('/remote', remoteController) router.use('/users', usersController) diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index e084974ce..fdbcc3ff5 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js @@ -1,7 +1,6 @@ 'use strict' const each = require('async/each') -const config = require('config') const express = require('express') const mongoose = require('mongoose') const waterfall = require('async/waterfall') @@ -14,7 +13,6 @@ const admin = middlewares.admin const oAuth = middlewares.oauth const validatorsUsers = middlewares.validators.users -const Client = mongoose.model('OAuthClient') const User = mongoose.model('User') const Video = mongoose.model('Video') @@ -41,7 +39,7 @@ router.delete('/:username', validatorsUsers.usersRemove, removeUser ) -router.get('/client', getAngularClient) + router.post('/token', oAuth.token, success) // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route @@ -65,30 +63,6 @@ function createUser (req, res, next) { }) } -function getAngularClient (req, res, next) { - const serverHost = config.get('webserver.host') - const serverPort = config.get('webserver.port') - let headerHostShouldBe = serverHost - if (serverPort !== 80 && serverPort !== 443) { - headerHostShouldBe += ':' + serverPort - } - - // Don't make this check if this is a test instance - if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) { - return res.type('json').status(403).end() - } - - Client.loadFirstClient(function (err, client) { - if (err) return next(err) - if (!client) return next(new Error('No client available.')) - - res.json({ - client_id: client._id, - client_secret: client.clientSecret - }) - }) -} - function listUsers (req, res, next) { User.list(function (err, usersList) { if (err) return next(err) -- cgit v1.2.3 From 99a64bfed25e45547df3045cf249bc895e6f220b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Aug 2016 17:19:08 +0200 Subject: Server: allow user to get its informations (/users/me) --- server/controllers/api/v1/users.js | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index fdbcc3ff5..d831a0de6 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js @@ -19,6 +19,7 @@ const Video = mongoose.model('Video') const router = express.Router() router.get('/', listUsers) +router.get('/me', oAuth.authenticate, getUserInformation) router.post('/', oAuth.authenticate, @@ -63,6 +64,14 @@ function createUser (req, res, next) { }) } +function getUserInformation (req, res, next) { + User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { + if (err) return next(err) + + return res.json(user.toFormatedJSON()) + }) +} + function listUsers (req, res, next) { User.list(function (err, usersList) { if (err) return next(err) -- cgit v1.2.3 From 10431358b27ec1bb4de7ccd662465cf544f1cdcd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Aug 2016 18:08:55 +0200 Subject: Server: fix status code when updating/removing a user --- server/controllers/api/v1/users.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index d831a0de6..057dcaf8d 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js @@ -122,7 +122,7 @@ function removeUser (req, res, next) { return next(err) } - return res.type('json').status(204).end() + return res.sendStatus(204) }) } @@ -134,7 +134,7 @@ function updateUser (req, res, next) { user.save(function (err) { if (err) return next(err) - return res.json('json').status(204).end() + return res.sendStatus(204) }) }) } -- cgit v1.2.3 From 68a3b9f2aacb0225ae8b883b561b144bac339cbd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Aug 2016 21:44:45 +0200 Subject: Server: delete user with the id and not the username --- server/controllers/api/v1/users.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index 057dcaf8d..704df770c 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js @@ -34,7 +34,7 @@ router.put('/:id', updateUser ) -router.delete('/:username', +router.delete('/:id', oAuth.authenticate, admin.ensureIsAdmin, validatorsUsers.usersRemove, @@ -83,7 +83,7 @@ function listUsers (req, res, next) { function removeUser (req, res, next) { waterfall([ function getUser (callback) { - User.loadByUsername(req.params.username, callback) + User.loadById(req.params.id, callback) }, function getVideos (user, callback) { -- cgit v1.2.3 From 0ff21c1c086f907612e34880f76f08bc74eeb78c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Aug 2016 21:51:04 +0200 Subject: Server: video.list -> video.listForApi (with pagination, sort...) --- server/controllers/api/v1/videos.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 1f939b077..0a441f146 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js @@ -142,7 +142,7 @@ function getVideo (req, res, next) { } function listVideos (req, res, next) { - Video.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { + Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { if (err) return next(err) res.json(getFormatedVideos(videosList, videosTotal)) -- 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/controllers/api/v1/users.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index 704df770c..975e25e68 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js @@ -11,6 +11,10 @@ const logger = require('../../../helpers/logger') const middlewares = require('../../../middlewares') const admin = middlewares.admin const oAuth = middlewares.oauth +const pagination = middlewares.pagination +const sort = middlewares.sort +const validatorsPagination = middlewares.validators.pagination +const validatorsSort = middlewares.validators.sort const validatorsUsers = middlewares.validators.users const User = mongoose.model('User') @@ -18,9 +22,16 @@ const Video = mongoose.model('Video') const router = express.Router() -router.get('/', listUsers) router.get('/me', oAuth.authenticate, getUserInformation) +router.get('/', + validatorsPagination.pagination, + validatorsSort.usersSort, + sort.setUsersSort, + pagination.setPagination, + listUsers +) + router.post('/', oAuth.authenticate, admin.ensureIsAdmin, @@ -73,10 +84,10 @@ function getUserInformation (req, res, next) { } function listUsers (req, res, next) { - User.list(function (err, usersList) { + User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { if (err) return next(err) - res.json(getFormatedUsers(usersList)) + res.json(getFormatedUsers(usersList, usersTotal)) }) } @@ -145,7 +156,7 @@ function success (req, res, next) { // --------------------------------------------------------------------------- -function getFormatedUsers (users) { +function getFormatedUsers (users, usersTotal) { const formatedUsers = [] users.forEach(function (user) { @@ -153,6 +164,7 @@ function getFormatedUsers (users) { }) return { + total: usersTotal, data: formatedUsers } } -- 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/controllers/api/v1/clients.js | 7 ++++--- server/controllers/api/v1/videos.js | 5 ++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/clients.js b/server/controllers/api/v1/clients.js index 0d222634b..5b460db2e 100644 --- a/server/controllers/api/v1/clients.js +++ b/server/controllers/api/v1/clients.js @@ -1,9 +1,10 @@ 'use strict' -const config = require('config') const express = require('express') const mongoose = require('mongoose') +const constants = require('../../../initializers/constants') + const Client = mongoose.model('OAuthClient') const router = express.Router() @@ -12,8 +13,8 @@ router.get('/local', getLocalClient) // Get the client credentials for the PeerTube front end function getLocalClient (req, res, next) { - const serverHost = config.get('webserver.host') - const serverPort = config.get('webserver.port') + const serverHost = constants.CONFIG.WEBSERVER.HOST + const serverPort = constants.CONFIG.WEBSERVER.PORT let headerHostShouldBe = serverHost if (serverPort !== 80 && serverPort !== 443) { headerHostShouldBe += ':' + serverPort diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 0a441f146..70d22f139 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js @@ -1,11 +1,11 @@ 'use strict' -const config = require('config') const express = require('express') const mongoose = require('mongoose') const multer = require('multer') const waterfall = require('async/waterfall') +const constants = require('../../../initializers/constants') const logger = require('../../../helpers/logger') const friends = require('../../../lib/friends') const middlewares = require('../../../middlewares') @@ -20,13 +20,12 @@ const sort = middlewares.sort const utils = require('../../../helpers/utils') const router = express.Router() -const uploads = config.get('storage.uploads') const Video = mongoose.model('Video') // multer configuration const storage = multer.diskStorage({ destination: function (req, file, cb) { - cb(null, uploads) + cb(null, constants.CONFIG.STORAGE.UPLOAD_DIR) }, filename: function (req, file, cb) { -- 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/controllers/api/v1/pods.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index f61f2a483..982a1e364 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js @@ -19,7 +19,7 @@ const Video = mongoose.model('Video') router.get('/', listPodsUrl) router.post('/', validators.podsAdd, addPods) -router.get('/makefriends', +router.post('/makefriends', oAuth.authenticate, admin.ensureIsAdmin, validators.makeFriends, @@ -83,7 +83,9 @@ function listPodsUrl (req, res, next) { } function makeFriends (req, res, next) { - friends.makeFriends(function (err) { + const urls = req.body.urls + + friends.makeFriends(urls, function (err) { if (err) return next(err) res.type('json').status(204).end() -- cgit v1.2.3 From 9ab1071c8d90d112f1031cf006ed621011553e84 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Aug 2016 14:48:59 +0200 Subject: Do not wait the make friends process ends to send a response to the request --- server/controllers/api/v1/pods.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index 982a1e364..360575a0d 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js @@ -86,10 +86,15 @@ function makeFriends (req, res, next) { const urls = req.body.urls friends.makeFriends(urls, function (err) { - if (err) return next(err) + if (err) { + logger.error('Could not make friends.', { error: err }) + return + } - res.type('json').status(204).end() + logger.info('Made friends!') }) + + res.type('json').status(204).end() } function removePods (req, res, next) { -- cgit v1.2.3 From 535724234aafd90c9eac17d9998f3f1c6c6c7615 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 26 Aug 2016 18:55:10 +0200 Subject: Server: add pod created date and score to the list controller --- server/controllers/api/v1/pods.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index 360575a0d..2bdfe0c92 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js @@ -17,7 +17,7 @@ const router = express.Router() const Pod = mongoose.model('Pod') const Video = mongoose.model('Video') -router.get('/', listPodsUrl) +router.get('/', listPods) router.post('/', validators.podsAdd, addPods) router.post('/makefriends', oAuth.authenticate, @@ -74,11 +74,11 @@ function addPods (req, res, next) { }) } -function listPodsUrl (req, res, next) { - Pod.listOnlyUrls(function (err, podsUrlList) { +function listPods (req, res, next) { + Pod.list(function (err, podsUrlList) { if (err) return next(err) - res.json(podsUrlList) + res.json(getFormatedPods(podsUrlList)) }) } @@ -142,3 +142,15 @@ function quitFriends (req, res, next) { res.type('json').status(204).end() }) } + +// --------------------------------------------------------------------------- + +function getFormatedPods (pods) { + const formatedPods = [] + + pods.forEach(function (pod) { + formatedPods.push(pod.toFormatedJSON()) + }) + + return formatedPods +} -- 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/controllers/api/v1/index.js | 2 ++ server/controllers/api/v1/requests.js | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 server/controllers/api/v1/requests.js (limited to 'server/controllers') diff --git a/server/controllers/api/v1/index.js b/server/controllers/api/v1/index.js index af41bc280..2e4fb2dab 100644 --- a/server/controllers/api/v1/index.js +++ b/server/controllers/api/v1/index.js @@ -7,12 +7,14 @@ const router = express.Router() const clientsController = require('./clients') const podsController = require('./pods') const remoteController = require('./remote') +const requestsController = require('./requests') const usersController = require('./users') const videosController = require('./videos') router.use('/clients', clientsController) router.use('/pods', podsController) router.use('/remote', remoteController) +router.use('/requests', requestsController) router.use('/users', usersController) router.use('/videos', videosController) router.use('/*', badRequest) diff --git a/server/controllers/api/v1/requests.js b/server/controllers/api/v1/requests.js new file mode 100644 index 000000000..6018bb40c --- /dev/null +++ b/server/controllers/api/v1/requests.js @@ -0,0 +1,38 @@ +'use strict' + +const express = require('express') +const mongoose = require('mongoose') + +const constants = require('../../../initializers/constants') +const middlewares = require('../../../middlewares') +const admin = middlewares.admin +const oAuth = middlewares.oauth + +const Request = mongoose.model('Request') + +const router = express.Router() + +router.get('/stats', + oAuth.authenticate, + admin.ensureIsAdmin, + getStatsRequests +) + +// --------------------------------------------------------------------------- + +module.exports = router + +// --------------------------------------------------------------------------- + +function getStatsRequests (req, res, next) { + Request.list(function (err, requests) { + if (err) return next(err) + + const remainingMilliSeconds = constants.REQUESTS_INTERVAL - (Date.now() % constants.REQUESTS_INTERVAL) + + return res.json({ + requests: requests, + remainingMilliSeconds: remainingMilliSeconds + }) + }) +} -- cgit v1.2.3 From eb4f957eca2a045ad0d5cf0c1e79b9c08494b088 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 23 Sep 2016 17:09:38 +0200 Subject: Client: add requests stats page --- server/controllers/api/v1/requests.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/requests.js b/server/controllers/api/v1/requests.js index 6018bb40c..17aca8af0 100644 --- a/server/controllers/api/v1/requests.js +++ b/server/controllers/api/v1/requests.js @@ -32,7 +32,8 @@ function getStatsRequests (req, res, next) { return res.json({ requests: requests, - remainingMilliSeconds: remainingMilliSeconds + remainingMilliSeconds: remainingMilliSeconds, + milliSecondsInterval: constants.REQUESTS_INTERVAL }) }) } -- cgit v1.2.3 From 0eb78d530376c43d228e3e071e032fe9849149ed Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 1 Oct 2016 09:09:07 +0200 Subject: Server: do not forget to check the signature when another pod wants to quit us --- server/controllers/api/v1/pods.js | 7 ++++++- server/controllers/api/v1/remote.js | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index 2bdfe0c92..d509db964 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js @@ -10,6 +10,7 @@ const friends = require('../../../lib/friends') const middlewares = require('../../../middlewares') const admin = middlewares.admin const oAuth = middlewares.oauth +const checkSignature = middlewares.secure.checkSignature const validators = middlewares.validators.pods const signatureValidator = middlewares.validators.remote.signature @@ -31,7 +32,11 @@ router.get('/quitfriends', quitFriends ) // Post because this is a secured request -router.post('/remove', signatureValidator, removePods) +router.post('/remove', + signatureValidator, + checkSignature, + removePods +) // --------------------------------------------------------------------------- diff --git a/server/controllers/api/v1/remote.js b/server/controllers/api/v1/remote.js index f452986b8..a22c5d151 100644 --- a/server/controllers/api/v1/remote.js +++ b/server/controllers/api/v1/remote.js @@ -16,6 +16,7 @@ const Video = mongoose.model('Video') router.post('/videos', validators.signature, validators.dataToDecrypt, + secureMiddleware.checkSignature, secureMiddleware.decryptBody, validators.remoteVideos, remoteVideos -- cgit v1.2.3 From 1ab844d8593fa84dfe65cad6b5bbf0e45efd7886 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 1 Oct 2016 14:23:50 +0200 Subject: Server: add port when making friends if it is not specified --- server/controllers/api/v1/pods.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index d509db964..8ffade578 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js @@ -10,6 +10,7 @@ const friends = require('../../../lib/friends') const middlewares = require('../../../middlewares') const admin = middlewares.admin const oAuth = middlewares.oauth +const podsMiddleware = middlewares.pods const checkSignature = middlewares.secure.checkSignature const validators = middlewares.validators.pods const signatureValidator = middlewares.validators.remote.signature @@ -19,11 +20,16 @@ const Pod = mongoose.model('Pod') const Video = mongoose.model('Video') router.get('/', listPods) -router.post('/', validators.podsAdd, addPods) +router.post('/', + validators.podsAdd, + podsMiddleware.setBodyUrlPort, + addPods +) router.post('/makefriends', oAuth.authenticate, admin.ensureIsAdmin, validators.makeFriends, + podsMiddleware.setBodyUrlsPort, makeFriends ) router.get('/quitfriends', -- cgit v1.2.3 From 5abeec313f897bbb1f1f39c3849675ec9a77d506 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 1 Oct 2016 15:33:27 +0200 Subject: Server: fix remaining milli seconds before the next requests feature --- server/controllers/api/v1/requests.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/requests.js b/server/controllers/api/v1/requests.js index 17aca8af0..9610e5cd6 100644 --- a/server/controllers/api/v1/requests.js +++ b/server/controllers/api/v1/requests.js @@ -28,11 +28,9 @@ function getStatsRequests (req, res, next) { Request.list(function (err, requests) { if (err) return next(err) - const remainingMilliSeconds = constants.REQUESTS_INTERVAL - (Date.now() % constants.REQUESTS_INTERVAL) - return res.json({ requests: requests, - remainingMilliSeconds: remainingMilliSeconds, + remainingMilliSeconds: Request.remainingMilliSeconds(), milliSecondsInterval: constants.REQUESTS_INTERVAL }) }) -- cgit v1.2.3 From 32e574750a7b06699073ac8e98aeebaf2fa8a82d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 1 Oct 2016 15:43:42 +0200 Subject: Add "max requests number" information for requests iterations --- server/controllers/api/v1/requests.js | 1 + 1 file changed, 1 insertion(+) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/requests.js b/server/controllers/api/v1/requests.js index 9610e5cd6..97616424d 100644 --- a/server/controllers/api/v1/requests.js +++ b/server/controllers/api/v1/requests.js @@ -30,6 +30,7 @@ function getStatsRequests (req, res, next) { return res.json({ requests: requests, + maxRequestsInParallel: constants.REQUESTS_IN_PARALLEL, remainingMilliSeconds: Request.remainingMilliSeconds(), milliSecondsInterval: constants.REQUESTS_INTERVAL }) -- cgit v1.2.3