]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/pods.js
Implement user API (create, update, remove, list)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
1 'use strict'
2
3 const each = require('async/each')
4 const express = require('express')
5 const mongoose = require('mongoose')
6 const waterfall = require('async/waterfall')
7
8 const logger = require('../../../helpers/logger')
9 const friends = require('../../../lib/friends')
10 const middlewares = require('../../../middlewares')
11 const admin = middlewares.admin
12 const oAuth = middlewares.oauth
13 const validators = middlewares.validators.pods
14 const signatureValidator = middlewares.validators.remote.signature
15
16 const router = express.Router()
17 const Pod = mongoose.model('Pod')
18 const Video = mongoose.model('Video')
19
20 router.get('/', listPodsUrl)
21 router.post('/', validators.podsAdd, addPods)
22 router.get('/makefriends',
23 oAuth.authenticate,
24 admin.ensureIsAdmin,
25 validators.makeFriends,
26 makeFriends
27 )
28 router.get('/quitfriends',
29 oAuth.authenticate,
30 admin.ensureIsAdmin,
31 quitFriends
32 )
33 // Post because this is a secured request
34 router.post('/remove', signatureValidator, removePods)
35
36 // ---------------------------------------------------------------------------
37
38 module.exports = router
39
40 // ---------------------------------------------------------------------------
41
42 function addPods (req, res, next) {
43 const informations = req.body
44
45 waterfall([
46 function addPod (callback) {
47 const pod = new Pod(informations)
48 pod.save(function (err, podCreated) {
49 // Be sure about the number of parameters for the callback
50 return callback(err, podCreated)
51 })
52 },
53
54 function sendMyVideos (podCreated, callback) {
55 friends.sendOwnedVideosToPod(podCreated._id)
56
57 callback(null)
58 },
59
60 function fetchMyCertificate (callback) {
61 friends.getMyCertificate(function (err, cert) {
62 if (err) {
63 logger.error('Cannot read cert file.')
64 return callback(err)
65 }
66
67 return callback(null, cert)
68 })
69 }
70 ], function (err, cert) {
71 if (err) return next(err)
72
73 return res.json({ cert: cert })
74 })
75 }
76
77 function listPodsUrl (req, res, next) {
78 Pod.listOnlyUrls(function (err, podsUrlList) {
79 if (err) return next(err)
80
81 res.json(podsUrlList)
82 })
83 }
84
85 function makeFriends (req, res, next) {
86 friends.makeFriends(function (err) {
87 if (err) return next(err)
88
89 res.type('json').status(204).end()
90 })
91 }
92
93 function removePods (req, res, next) {
94 const url = req.body.signature.url
95
96 waterfall([
97 function loadPod (callback) {
98 Pod.loadByUrl(url, callback)
99 },
100
101 function removePod (pod, callback) {
102 pod.remove(function (err) {
103 // Be sure we only return one argument in the callback
104 return callback(err)
105 })
106 },
107
108 function (callback) {
109 Video.listByUrls([ url ], function (err, videosList) {
110 if (err) {
111 logger.error('Cannot list videos from url.', { error: err })
112 return callback(err)
113 }
114
115 return callback(null, videosList)
116 })
117 },
118
119 function removeTheRemoteVideos (videosList, callback) {
120 each(videosList, function (video, callbackEach) {
121 video.remove(callbackEach)
122 }, callback)
123 }
124 ], function (err) {
125 if (err) return next(err)
126
127 return res.type('json').status(204).end()
128 })
129 }
130
131 function quitFriends (req, res, next) {
132 friends.quitFriends(function (err) {
133 if (err) return next(err)
134
135 res.type('json').status(204).end()
136 })
137 }