diff options
Diffstat (limited to 'controllers/api/v1/pods.js')
-rw-r--r-- | controllers/api/v1/pods.js | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/controllers/api/v1/pods.js b/controllers/api/v1/pods.js index b073e85af..82d8d7f08 100644 --- a/controllers/api/v1/pods.js +++ b/controllers/api/v1/pods.js | |||
@@ -2,18 +2,23 @@ | |||
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | var express = require('express') | 4 | var express = require('express') |
5 | var fs = require('fs') | ||
5 | 6 | ||
7 | var logger = require('../../../helpers/logger') | ||
8 | var friends = require('../../../lib/friends') | ||
6 | var middleware = require('../../../middlewares') | 9 | var middleware = require('../../../middlewares') |
7 | var miscMiddleware = middleware.misc | 10 | var miscMiddleware = middleware.misc |
8 | var pods = require('../../../models/pods') | 11 | var Pods = require('../../../models/pods') |
9 | var reqValidator = middleware.reqValidators.pods | 12 | var reqValidator = middleware.reqValidators.pods |
10 | var secureRequest = middleware.reqValidators.remote.secureRequest | 13 | var secureRequest = middleware.reqValidators.remote.secureRequest |
14 | var utils = require('../../../helpers/utils') | ||
15 | var Videos = require('../../../models/videos') | ||
11 | 16 | ||
12 | var router = express.Router() | 17 | var router = express.Router() |
13 | 18 | ||
14 | router.get('/', miscMiddleware.cache(false), listPods) | 19 | router.get('/', miscMiddleware.cache(false), listPods) |
15 | router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods) | 20 | router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods) |
16 | router.get('/makefriends', miscMiddleware.cache(false), makeFriends) | 21 | router.get('/makefriends', reqValidator.makeFriends, miscMiddleware.cache(false), makeFriends) |
17 | router.get('/quitfriends', miscMiddleware.cache(false), quitFriends) | 22 | router.get('/quitfriends', miscMiddleware.cache(false), quitFriends) |
18 | // Post because this is a secured request | 23 | // Post because this is a secured request |
19 | router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods) | 24 | router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods) |
@@ -25,15 +30,32 @@ | |||
25 | // --------------------------------------------------------------------------- | 30 | // --------------------------------------------------------------------------- |
26 | 31 | ||
27 | function addPods (req, res, next) { | 32 | function addPods (req, res, next) { |
28 | pods.add(req.body.data, function (err, json) { | 33 | var informations = req.body.data |
34 | Pods.add(informations, function (err) { | ||
29 | if (err) return next(err) | 35 | if (err) return next(err) |
30 | 36 | ||
31 | res.json(json) | 37 | Videos.addRemotes(informations.videos) |
38 | |||
39 | fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { | ||
40 | if (err) { | ||
41 | logger.error('Cannot read cert file.', { error: err }) | ||
42 | return next(err) | ||
43 | } | ||
44 | |||
45 | Videos.listOwned(function (err, videos_list) { | ||
46 | if (err) { | ||
47 | logger.error('Cannot get the list of owned videos.', { error: err }) | ||
48 | return next(err) | ||
49 | } | ||
50 | |||
51 | res.json({ cert: cert, videos: videos_list }) | ||
52 | }) | ||
53 | }) | ||
32 | }) | 54 | }) |
33 | } | 55 | } |
34 | 56 | ||
35 | function listPods (req, res, next) { | 57 | function listPods (req, res, next) { |
36 | pods.list(function (err, pods_list) { | 58 | Pods.list(function (err, pods_list) { |
37 | if (err) return next(err) | 59 | if (err) return next(err) |
38 | 60 | ||
39 | res.json(pods_list) | 61 | res.json(pods_list) |
@@ -41,32 +63,28 @@ | |||
41 | } | 63 | } |
42 | 64 | ||
43 | function makeFriends (req, res, next) { | 65 | function makeFriends (req, res, next) { |
44 | pods.hasFriends(function (err, has_friends) { | 66 | friends.makeFriends(function (err) { |
45 | if (err) return next(err) | 67 | if (err) return next(err) |
46 | 68 | ||
47 | if (has_friends === true) { | 69 | res.sendStatus(204) |
48 | // We need to quit our friends before make new ones | ||
49 | res.sendStatus(409) | ||
50 | } else { | ||
51 | pods.makeFriends(function (err) { | ||
52 | if (err) return next(err) | ||
53 | |||
54 | res.sendStatus(204) | ||
55 | }) | ||
56 | } | ||
57 | }) | 70 | }) |
58 | } | 71 | } |
59 | 72 | ||
60 | function removePods (req, res, next) { | 73 | function removePods (req, res, next) { |
61 | pods.remove(req.body.signature.url, function (err) { | 74 | var url = req.body.signature.url |
75 | Pods.remove(url, function (err) { | ||
62 | if (err) return next(err) | 76 | if (err) return next(err) |
63 | 77 | ||
64 | res.sendStatus(204) | 78 | Videos.removeAllRemotesOf(url, function (err) { |
79 | if (err) logger.error('Cannot remove all remote videos of %s.', url) | ||
80 | logger.info('%s pod removed.', url) | ||
81 | res.sendStatus(204) | ||
82 | }) | ||
65 | }) | 83 | }) |
66 | } | 84 | } |
67 | 85 | ||
68 | function quitFriends (req, res, next) { | 86 | function quitFriends (req, res, next) { |
69 | pods.quitFriends(function (err) { | 87 | friends.quitFriends(function (err) { |
70 | if (err) return next(err) | 88 | if (err) return next(err) |
71 | 89 | ||
72 | res.sendStatus(204) | 90 | res.sendStatus(204) |