]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
8c308c2b | 2 | |
1cad0f39 | 3 | const async = require('async') |
f0f5567b | 4 | const express = require('express') |
aaf61f38 | 5 | const mongoose = require('mongoose') |
f0f5567b C |
6 | |
7 | const logger = require('../../../helpers/logger') | |
8 | const friends = require('../../../lib/friends') | |
b3b92647 | 9 | const middlewares = require('../../../middlewares') |
69b0a27c | 10 | const oAuth = middlewares.oauth |
fc51fde0 C |
11 | const validators = middlewares.validators.pods |
12 | const signatureValidator = middlewares.validators.remote.signature | |
f0f5567b C |
13 | |
14 | const router = express.Router() | |
a3ee6fa2 | 15 | const Pod = mongoose.model('Pod') |
aaf61f38 | 16 | const Video = mongoose.model('Video') |
8c308c2b | 17 | |
528a9efa | 18 | router.get('/', listPodsUrl) |
fc51fde0 C |
19 | router.post('/', validators.podsAdd, addPods) |
20 | router.get('/makefriends', oAuth.authenticate, validators.makeFriends, makeFriends) | |
69b0a27c | 21 | router.get('/quitfriends', oAuth.authenticate, quitFriends) |
9f10b292 | 22 | // Post because this is a secured request |
528a9efa | 23 | router.post('/remove', signatureValidator, removePods) |
c45f7f84 | 24 | |
9f10b292 | 25 | // --------------------------------------------------------------------------- |
c45f7f84 | 26 | |
9f10b292 | 27 | module.exports = router |
c45f7f84 | 28 | |
9f10b292 | 29 | // --------------------------------------------------------------------------- |
8c308c2b | 30 | |
9f10b292 | 31 | function 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 | 66 | function 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 |
74 | function 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 | 82 | function 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 |
120 | function 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 | } |