]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
8c308c2b | 2 | |
1cad0f39 | 3 | const async = require('async') |
f0f5567b | 4 | const express = require('express') |
f0f5567b C |
5 | |
6 | const logger = require('../../../helpers/logger') | |
7 | const friends = require('../../../lib/friends') | |
b3b92647 | 8 | const middlewares = require('../../../middlewares') |
f0f5567b | 9 | const Pods = require('../../../models/pods') |
b3b92647 C |
10 | const oAuth2 = middlewares.oauth2 |
11 | const reqValidator = middlewares.reqValidators.pods | |
12 | const secureMiddleware = middlewares.secure | |
13 | const secureRequest = middlewares.reqValidators.remote.secureRequest | |
cbe2f7c3 | 14 | const videos = require('../../../lib/videos') |
f0f5567b C |
15 | const Videos = require('../../../models/videos') |
16 | ||
17 | const router = express.Router() | |
8c308c2b | 18 | |
5dda52c9 C |
19 | router.get('/', listPods) |
20 | router.post('/', reqValidator.podsAdd, addPods) | |
b3b92647 C |
21 | router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends) |
22 | router.get('/quitfriends', oAuth2.authenticate, quitFriends) | |
9f10b292 C |
23 | // Post because this is a secured request |
24 | router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) | |
c45f7f84 | 25 | |
9f10b292 | 26 | // --------------------------------------------------------------------------- |
c45f7f84 | 27 | |
9f10b292 | 28 | module.exports = router |
c45f7f84 | 29 | |
9f10b292 | 30 | // --------------------------------------------------------------------------- |
8c308c2b | 31 | |
9f10b292 | 32 | function addPods (req, res, next) { |
f0f5567b | 33 | const informations = req.body.data |
8c308c2b | 34 | |
1cad0f39 C |
35 | async.waterfall([ |
36 | function addPod (callback) { | |
37 | Pods.add(informations, function (err) { | |
38 | return callback(err) | |
39 | }) | |
40 | }, | |
41 | ||
42 | function createVideosOfThisPod (callback) { | |
43 | // Create the remote videos from the new pod | |
44 | videos.createRemoteVideos(informations.videos, function (err) { | |
45 | if (err) logger.error('Cannot create remote videos.', { error: err }) | |
46 | ||
47 | return callback(err) | |
48 | }) | |
49 | }, | |
50 | ||
51 | function fetchMyCertificate (callback) { | |
52 | friends.getMyCertificate(function (err, cert) { | |
53 | if (err) { | |
54 | logger.error('Cannot read cert file.') | |
55 | return callback(err) | |
56 | } | |
c173e565 | 57 | |
1cad0f39 C |
58 | return callback(null, cert) |
59 | }) | |
60 | }, | |
9f10b292 | 61 | |
1cad0f39 | 62 | function getListOfMyVideos (cert, callback) { |
bc503c2a | 63 | Videos.listOwned(function (err, videosList) { |
c173e565 | 64 | if (err) { |
9f10b292 | 65 | logger.error('Cannot get the list of owned videos.') |
1cad0f39 | 66 | return callback(err) |
c173e565 C |
67 | } |
68 | ||
1cad0f39 | 69 | return callback(null, cert, videosList) |
c173e565 | 70 | }) |
1cad0f39 C |
71 | } |
72 | ], function (err, cert, videosList) { | |
73 | if (err) return next(err) | |
74 | ||
75 | return res.json({ cert: cert, videos: videosList }) | |
9f10b292 C |
76 | }) |
77 | } | |
8c308c2b | 78 | |
9f10b292 | 79 | function listPods (req, res, next) { |
bc503c2a | 80 | Pods.list(function (err, podsList) { |
9f10b292 | 81 | if (err) return next(err) |
45239549 | 82 | |
bc503c2a | 83 | res.json(podsList) |
9f10b292 C |
84 | }) |
85 | } | |
45239549 | 86 | |
9f10b292 C |
87 | function makeFriends (req, res, next) { |
88 | friends.makeFriends(function (err) { | |
89 | if (err) return next(err) | |
45239549 | 90 | |
dc8bc31b | 91 | res.type('json').status(204).end() |
9f10b292 C |
92 | }) |
93 | } | |
45239549 | 94 | |
9f10b292 | 95 | function removePods (req, res, next) { |
f0f5567b | 96 | const url = req.body.signature.url |
8c308c2b | 97 | |
1cad0f39 C |
98 | async.waterfall([ |
99 | function (callback) { | |
100 | Pods.remove(url, function (err) { | |
101 | return callback(err) | |
102 | }) | |
103 | }, | |
104 | ||
105 | function (callback) { | |
106 | Videos.listFromUrl(url, function (err, videosList) { | |
107 | if (err) { | |
108 | logger.error('Cannot list videos from url.', { error: err }) | |
109 | return callback(err) | |
110 | } | |
111 | ||
112 | return callback(null, videosList) | |
113 | }) | |
114 | }, | |
8425cb89 | 115 | |
1cad0f39 | 116 | function removeTheRemoteVideos (videosList, callback) { |
bc503c2a | 117 | videos.removeRemoteVideos(videosList, function (err) { |
cbe2f7c3 C |
118 | if (err) { |
119 | logger.error('Cannot remove remote videos.', { error: err }) | |
1cad0f39 | 120 | callback(err) |
cbe2f7c3 C |
121 | } |
122 | ||
1cad0f39 | 123 | return callback(null) |
cbe2f7c3 | 124 | }) |
1cad0f39 C |
125 | } |
126 | ], function (err) { | |
127 | if (err) return next(err) | |
128 | ||
129 | return res.type('json').status(204).end() | |
9f10b292 C |
130 | }) |
131 | } | |
8c308c2b | 132 | |
9f10b292 C |
133 | function quitFriends (req, res, next) { |
134 | friends.quitFriends(function (err) { | |
135 | if (err) return next(err) | |
8c308c2b | 136 | |
dc8bc31b | 137 | res.type('json').status(204).end() |
9f10b292 C |
138 | }) |
139 | } |