aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1/pods.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/v1/pods.js')
-rw-r--r--server/controllers/api/v1/pods.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js
new file mode 100644
index 000000000..c93a86ee8
--- /dev/null
+++ b/server/controllers/api/v1/pods.js
@@ -0,0 +1,93 @@
1'use strict'
2
3var express = require('express')
4var fs = require('fs')
5
6var logger = require('../../../helpers/logger')
7var friends = require('../../../lib/friends')
8var middleware = require('../../../middlewares')
9var cacheMiddleware = middleware.cache
10var peertubeCrypto = require('../../../helpers/peertubeCrypto')
11var Pods = require('../../../models/pods')
12var reqValidator = middleware.reqValidators.pods
13var secureMiddleware = middleware.secure
14var secureRequest = middleware.reqValidators.remote.secureRequest
15var Videos = require('../../../models/videos')
16
17var router = express.Router()
18
19router.get('/', cacheMiddleware.cache(false), listPods)
20router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods)
21router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends)
22router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends)
23// Post because this is a secured request
24router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
25
26// ---------------------------------------------------------------------------
27
28module.exports = router
29
30// ---------------------------------------------------------------------------
31
32function addPods (req, res, next) {
33 var informations = req.body.data
34 Pods.add(informations, function (err) {
35 if (err) return next(err)
36
37 Videos.addRemotes(informations.videos)
38
39 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
40 if (err) {
41 logger.error('Cannot read cert file.')
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.')
48 return next(err)
49 }
50
51 res.json({ cert: cert, videos: videos_list })
52 })
53 })
54 })
55}
56
57function listPods (req, res, next) {
58 Pods.list(function (err, pods_list) {
59 if (err) return next(err)
60
61 res.json(pods_list)
62 })
63}
64
65function makeFriends (req, res, next) {
66 friends.makeFriends(function (err) {
67 if (err) return next(err)
68
69 res.sendStatus(204)
70 })
71}
72
73function removePods (req, res, next) {
74 var url = req.body.signature.url
75 Pods.remove(url, function (err) {
76 if (err) return next(err)
77
78 Videos.removeAllRemotesOf(url, function (err) {
79 if (err) logger.error('Cannot remove all remote videos of %s.', url)
80 else logger.info('%s pod removed.', url)
81
82 res.sendStatus(204)
83 })
84 })
85}
86
87function quitFriends (req, res, next) {
88 friends.quitFriends(function (err) {
89 if (err) return next(err)
90
91 res.sendStatus(204)
92 })
93}