From c173e56520b0fe4206b9ea8049b6add40bfeabcd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 Feb 2016 21:10:33 +0100 Subject: Split models --- middlewares/reqValidators/pods.js | 15 +++++++++++++++ middlewares/reqValidators/videos.js | 32 ++++++++++++++++---------------- 2 files changed, 31 insertions(+), 16 deletions(-) (limited to 'middlewares/reqValidators') 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