From c173e56520b0fe4206b9ea8049b6add40bfeabcd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 Feb 2016 21:10:33 +0100 Subject: Split models --- middlewares/misc.js | 9 +++++---- middlewares/reqValidators/pods.js | 15 +++++++++++++++ middlewares/reqValidators/videos.js | 32 ++++++++++++++++---------------- 3 files changed, 36 insertions(+), 20 deletions(-) (limited to 'middlewares') diff --git a/middlewares/misc.js b/middlewares/misc.js index dbb604db3..cc4e2e8a4 100644 --- a/middlewares/misc.js +++ b/middlewares/misc.js @@ -5,7 +5,7 @@ var ursa = require('ursa') var logger = require('../helpers/logger') - var PodsDB = require('../initializers/database').PodsDB + var Pods = require('../models/pods') var utils = require('../helpers/utils') var miscMiddleware = { @@ -28,18 +28,19 @@ } function decryptBody (req, res, next) { - PodsDB.findOne({ url: req.body.signature.url }, function (err, pod) { + var url = req.body.signature.url + Pods.findByUrl(url, function (err, pod) { if (err) { logger.error('Cannot get signed url in decryptBody.', { error: err }) return res.sendStatus(500) } if (pod === null) { - logger.error('Unknown pod %s.', req.body.signature.url) + logger.error('Unknown pod %s.', url) return res.sendStatus(403) } - logger.debug('Decrypting body from %s.', req.body.signature.url) + logger.debug('Decrypting body from %s.', url) var crt = ursa.createPublicKey(pod.publicKey) var signature_ok = crt.hashAndVerify('sha256', new Buffer(req.body.signature.url).toString('hex'), req.body.signature.signature, 'hex') diff --git a/middlewares/reqValidators/pods.js b/middlewares/reqValidators/pods.js index 6ccfd7361..499cafd8f 100644 --- a/middlewares/reqValidators/pods.js +++ b/middlewares/reqValidators/pods.js @@ -2,12 +2,27 @@ 'use strict' var checkErrors = require('./utils').checkErrors + var friends = require('../../lib/friends') var logger = require('../../helpers/logger') var reqValidatorsPod = { + makeFriends: makeFriends, podsAdd: podsAdd } + function makeFriends (req, res, next) { + friends.hasFriends(function (err, has_friends) { + if (err) return next(err) + + if (has_friends === true) { + // We need to quit our friends before make new ones + res.sendStatus(409) + } else { + next() + } + }) + } + function podsAdd (req, res, next) { req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true }) req.checkBody('data.publicKey', 'Should have a public key').notEmpty() diff --git a/middlewares/reqValidators/videos.js b/middlewares/reqValidators/videos.js index 3479c47c3..f7bd24658 100644 --- a/middlewares/reqValidators/videos.js +++ b/middlewares/reqValidators/videos.js @@ -3,7 +3,7 @@ var checkErrors = require('./utils').checkErrors var logger = require('../../helpers/logger') - var VideosDB = require('../../initializers/database').VideosDB + var Videos = require('../../models/videos') var reqValidatorsVideos = { videosAdd: videosAdd, @@ -29,8 +29,13 @@ logger.debug('Checking videosGet parameters', { parameters: req.params }) checkErrors(req, res, function () { - findVideoById(req.params.id, function (video) { - if (!video) return res.status(404).send('Video not found') + Videos.getVideoState(req.params.id, function (err, state) { + if (err) { + logger.error('Error in videosGet request validator.', { error: err }) + res.sendStatus(500) + } + + if (state.exist === false) return res.status(404).send('Video not found') next() }) @@ -43,9 +48,14 @@ logger.debug('Checking videosRemove parameters', { parameters: req.params }) checkErrors(req, res, function () { - findVideoById(req.params.id, function (video) { - if (!video) return res.status(404).send('Video not found') - else if (video.namePath === null) return res.status(403).send('Cannot remove video of another pod') + Videos.getVideoState(req.params.id, function (err, state) { + if (err) { + logger.error('Error in videosRemove request validator.', { error: err }) + res.sendStatus(500) + } + + if (state.exist === false) return res.status(404).send('Video not found') + else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod') next() }) @@ -63,14 +73,4 @@ // --------------------------------------------------------------------------- module.exports = reqValidatorsVideos - - // --------------------------------------------------------------------------- - - function findVideoById (id, callback) { - VideosDB.findById(id, { _id: 1, namePath: 1 }).limit(1).exec(function (err, video) { - if (err) throw err - - callback(video) - }) - } })() -- cgit v1.2.3