]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/pods.js
Server: add pod created date and score to the list controller
[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('/', listPods)
21 router.post('/', validators.podsAdd, addPods)
22 router.post('/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 listPods (req, res, next) {
78 Pod.list(function (err, podsUrlList) {
79 if (err) return next(err)
80
81 res.json(getFormatedPods(podsUrlList))
82 })
83 }
84
85 function makeFriends (req, res, next) {
86 const urls = req.body.urls
87
88 friends.makeFriends(urls, function (err) {
89 if (err) {
90 logger.error('Could not make friends.', { error: err })
91 return
92 }
93
94 logger.info('Made friends!')
95 })
96
97 res.type('json').status(204).end()
98 }
99
100 function removePods (req, res, next) {
101 const url = req.body.signature.url
102
103 waterfall([
104 function loadPod (callback) {
105 Pod.loadByUrl(url, callback)
106 },
107
108 function removePod (pod, callback) {
109 pod.remove(function (err) {
110 // Be sure we only return one argument in the callback
111 return callback(err)
112 })
113 },
114
115 function (callback) {
116 Video.listByUrls([ url ], function (err, videosList) {
117 if (err) {
118 logger.error('Cannot list videos from url.', { error: err })
119 return callback(err)
120 }
121
122 return callback(null, videosList)
123 })
124 },
125
126 function removeTheRemoteVideos (videosList, callback) {
127 each(videosList, function (video, callbackEach) {
128 video.remove(callbackEach)
129 }, callback)
130 }
131 ], function (err) {
132 if (err) return next(err)
133
134 return res.type('json').status(204).end()
135 })
136 }
137
138 function quitFriends (req, res, next) {
139 friends.quitFriends(function (err) {
140 if (err) return next(err)
141
142 res.type('json').status(204).end()
143 })
144 }
145
146 // ---------------------------------------------------------------------------
147
148 function getFormatedPods (pods) {
149 const formatedPods = []
150
151 pods.forEach(function (pod) {
152 formatedPods.push(pod.toFormatedJSON())
153 })
154
155 return formatedPods
156 }