aboutsummaryrefslogtreecommitdiffhomepage
path: root/controllers/api/v1/pods.js
blob: b073e85af97dcc9d7d0a0d6724bbac9e2046b686 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
;(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)
    })
  }
})()