]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/v1/pods.js
Pod model refractoring -> use mongoose api
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
CommitLineData
9f10b292 1'use strict'
8c308c2b 2
1cad0f39 3const async = require('async')
f0f5567b 4const express = require('express')
aaf61f38 5const mongoose = require('mongoose')
f0f5567b
C
6
7const logger = require('../../../helpers/logger')
8const friends = require('../../../lib/friends')
b3b92647 9const middlewares = require('../../../middlewares')
b3b92647
C
10const oAuth2 = middlewares.oauth2
11const reqValidator = middlewares.reqValidators.pods
528a9efa 12const signatureValidator = middlewares.reqValidators.remote.signature
f0f5567b
C
13
14const router = express.Router()
a3ee6fa2 15const Pod = mongoose.model('Pod')
aaf61f38 16const Video = mongoose.model('Video')
8c308c2b 17
528a9efa 18router.get('/', listPodsUrl)
5dda52c9 19router.post('/', reqValidator.podsAdd, addPods)
b3b92647
C
20router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends)
21router.get('/quitfriends', oAuth2.authenticate, quitFriends)
9f10b292 22// Post because this is a secured request
528a9efa 23router.post('/remove', signatureValidator, removePods)
c45f7f84 24
9f10b292 25// ---------------------------------------------------------------------------
c45f7f84 26
9f10b292 27module.exports = router
c45f7f84 28
9f10b292 29// ---------------------------------------------------------------------------
8c308c2b 30
9f10b292 31function addPods (req, res, next) {
528a9efa 32 const informations = req.body
8c308c2b 33
1cad0f39
C
34 async.waterfall([
35 function addPod (callback) {
a3ee6fa2
C
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 })
1cad0f39
C
41 },
42
528a9efa
C
43 function sendMyVideos (podCreated, callback) {
44 friends.sendOwnedVideosToPod(podCreated._id)
1cad0f39 45
528a9efa 46 callback(null)
1cad0f39
C
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 }
c173e565 55
1cad0f39
C
56 return callback(null, cert)
57 })
1cad0f39 58 }
528a9efa 59 ], function (err, cert) {
1cad0f39
C
60 if (err) return next(err)
61
528a9efa 62 return res.json({ cert: cert })
9f10b292
C
63 })
64}
8c308c2b 65
528a9efa 66function listPodsUrl (req, res, next) {
a3ee6fa2 67 Pod.listOnlyUrls(function (err, podsUrlList) {
9f10b292 68 if (err) return next(err)
45239549 69
528a9efa 70 res.json(podsUrlList)
9f10b292
C
71 })
72}
45239549 73
9f10b292
C
74function makeFriends (req, res, next) {
75 friends.makeFriends(function (err) {
76 if (err) return next(err)
45239549 77
dc8bc31b 78 res.type('json').status(204).end()
9f10b292
C
79 })
80}
45239549 81
9f10b292 82function removePods (req, res, next) {
f0f5567b 83 const url = req.body.signature.url
8c308c2b 84
1cad0f39 85 async.waterfall([
a3ee6fa2
C
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
1cad0f39
C
93 return callback(err)
94 })
95 },
96
97 function (callback) {
aaf61f38 98 Video.listByUrls([ url ], function (err, videosList) {
1cad0f39
C
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 },
8425cb89 107
1cad0f39 108 function removeTheRemoteVideos (videosList, callback) {
aaf61f38
C
109 async.each(videosList, function (video, callbackEach) {
110 video.remove(callbackEach)
111 }, callback)
1cad0f39
C
112 }
113 ], function (err) {
114 if (err) return next(err)
115
116 return res.type('json').status(204).end()
9f10b292
C
117 })
118}
8c308c2b 119
9f10b292
C
120function quitFriends (req, res, next) {
121 friends.quitFriends(function (err) {
122 if (err) return next(err)
8c308c2b 123
dc8bc31b 124 res.type('json').status(204).end()
9f10b292
C
125 })
126}