aboutsummaryrefslogblamecommitdiffhomepage
path: root/controllers/api/v1/pods.js
blob: b073e85af97dcc9d7d0a0d6724bbac9e2046b686 (plain) (tree)
1
2
3
4
5
6
7
8
9



                                  
 
                                                  
                                      
                                            
                                                  
                                                                   
 
                               
 











                                                                                


                                                  
                               




                    

                                         

                               
                         


      
                                         















                                                           

                                                        
                               




                         


                                         
 


                         
    
;(function () {
  'use strict'

  var express = require('express')

  var middleware = require('../../../middlewares')
  var miscMiddleware = middleware.misc
  var pods = require('../../../models/pods')
  var reqValidator = middleware.reqValidators.pods
  var secureRequest = middleware.reqValidators.remote.secureRequest

  var router = express.Router()

  router.get('/', miscMiddleware.cache(false), listPods)
  router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods)
  router.get('/makefriends', miscMiddleware.cache(false), makeFriends)
  router.get('/quitfriends', miscMiddleware.cache(false), quitFriends)
  // Post because this is a secured request
  router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods)

  // ---------------------------------------------------------------------------

  module.exports = router

  // ---------------------------------------------------------------------------

  function addPods (req, res, next) {
    pods.add(req.body.data, function (err, json) {
      if (err) return next(err)

      res.json(json)
    })
  }

  function listPods (req, res, next) {
    pods.list(function (err, pods_list) {
      if (err) return next(err)

      res.json(pods_list)
    })
  }

  function makeFriends (req, res, next) {
    pods.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 {
        pods.makeFriends(function (err) {
          if (err) return next(err)

          res.sendStatus(204)
        })
      }
    })
  }

  function removePods (req, res, next) {
    pods.remove(req.body.signature.url, function (err) {
      if (err) return next(err)

      res.sendStatus(204)
    })
  }

  function quitFriends (req, res, next) {
    pods.quitFriends(function (err) {
      if (err) return next(err)

      res.sendStatus(204)
    })
  }
})()