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