]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const async = require('async') | |
4 | const express = require('express') | |
5 | ||
6 | const logger = require('../../../helpers/logger') | |
7 | const friends = require('../../../lib/friends') | |
8 | const middlewares = require('../../../middlewares') | |
9 | const Pods = require('../../../models/pods') | |
10 | const oAuth2 = middlewares.oauth2 | |
11 | const reqValidator = middlewares.reqValidators.pods | |
12 | const signatureValidator = middlewares.reqValidators.remote.signature | |
13 | const videos = require('../../../lib/videos') | |
14 | const Videos = require('../../../models/videos') | |
15 | ||
16 | const router = express.Router() | |
17 | ||
18 | router.get('/', listPodsUrl) | |
19 | router.post('/', reqValidator.podsAdd, addPods) | |
20 | router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends) | |
21 | router.get('/quitfriends', oAuth2.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 | Pods.add(informations, callback) | |
37 | }, | |
38 | ||
39 | function sendMyVideos (podCreated, callback) { | |
40 | friends.sendOwnedVideosToPod(podCreated._id) | |
41 | ||
42 | callback(null) | |
43 | }, | |
44 | ||
45 | function fetchMyCertificate (callback) { | |
46 | friends.getMyCertificate(function (err, cert) { | |
47 | if (err) { | |
48 | logger.error('Cannot read cert file.') | |
49 | return callback(err) | |
50 | } | |
51 | ||
52 | return callback(null, cert) | |
53 | }) | |
54 | } | |
55 | ], function (err, cert) { | |
56 | if (err) return next(err) | |
57 | ||
58 | return res.json({ cert: cert }) | |
59 | }) | |
60 | } | |
61 | ||
62 | function listPodsUrl (req, res, next) { | |
63 | Pods.listAllUrls(function (err, podsUrlList) { | |
64 | if (err) return next(err) | |
65 | ||
66 | res.json(podsUrlList) | |
67 | }) | |
68 | } | |
69 | ||
70 | function makeFriends (req, res, next) { | |
71 | friends.makeFriends(function (err) { | |
72 | if (err) return next(err) | |
73 | ||
74 | res.type('json').status(204).end() | |
75 | }) | |
76 | } | |
77 | ||
78 | function removePods (req, res, next) { | |
79 | const url = req.body.signature.url | |
80 | ||
81 | async.waterfall([ | |
82 | function (callback) { | |
83 | Pods.remove(url, function (err) { | |
84 | return callback(err) | |
85 | }) | |
86 | }, | |
87 | ||
88 | function (callback) { | |
89 | Videos.listFromUrl(url, function (err, videosList) { | |
90 | if (err) { | |
91 | logger.error('Cannot list videos from url.', { error: err }) | |
92 | return callback(err) | |
93 | } | |
94 | ||
95 | return callback(null, videosList) | |
96 | }) | |
97 | }, | |
98 | ||
99 | function removeTheRemoteVideos (videosList, callback) { | |
100 | videos.removeRemoteVideos(videosList, function (err) { | |
101 | if (err) { | |
102 | logger.error('Cannot remove remote videos.', { error: err }) | |
103 | return callback(err) | |
104 | } | |
105 | ||
106 | return callback(null) | |
107 | }) | |
108 | } | |
109 | ], function (err) { | |
110 | if (err) return next(err) | |
111 | ||
112 | return res.type('json').status(204).end() | |
113 | }) | |
114 | } | |
115 | ||
116 | function quitFriends (req, res, next) { | |
117 | friends.quitFriends(function (err) { | |
118 | if (err) return next(err) | |
119 | ||
120 | res.type('json').status(204).end() | |
121 | }) | |
122 | } |