]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
8c308c2b | 2 | |
1a42c9e2 | 3 | const each = require('async/each') |
f0f5567b | 4 | const express = require('express') |
aaf61f38 | 5 | const mongoose = require('mongoose') |
1a42c9e2 | 6 | const waterfall = require('async/waterfall') |
f0f5567b C |
7 | |
8 | const logger = require('../../../helpers/logger') | |
9 | const friends = require('../../../lib/friends') | |
b3b92647 | 10 | const middlewares = require('../../../middlewares') |
69b0a27c | 11 | const oAuth = middlewares.oauth |
fc51fde0 C |
12 | const validators = middlewares.validators.pods |
13 | const signatureValidator = middlewares.validators.remote.signature | |
f0f5567b C |
14 | |
15 | const router = express.Router() | |
a3ee6fa2 | 16 | const Pod = mongoose.model('Pod') |
aaf61f38 | 17 | const Video = mongoose.model('Video') |
8c308c2b | 18 | |
528a9efa | 19 | router.get('/', listPodsUrl) |
fc51fde0 C |
20 | router.post('/', validators.podsAdd, addPods) |
21 | router.get('/makefriends', oAuth.authenticate, validators.makeFriends, makeFriends) | |
69b0a27c | 22 | router.get('/quitfriends', oAuth.authenticate, quitFriends) |
9f10b292 | 23 | // Post because this is a secured request |
528a9efa | 24 | router.post('/remove', signatureValidator, 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) { |
528a9efa | 33 | const informations = req.body |
8c308c2b | 34 | |
1a42c9e2 | 35 | waterfall([ |
1cad0f39 | 36 | function addPod (callback) { |
a3ee6fa2 C |
37 | const pod = new Pod(informations) |
38 | pod.save(function (err, podCreated) { | |
39 | // Be sure about the number of parameters for the callback | |
40 | return callback(err, podCreated) | |
41 | }) | |
1cad0f39 C |
42 | }, |
43 | ||
528a9efa C |
44 | function sendMyVideos (podCreated, callback) { |
45 | friends.sendOwnedVideosToPod(podCreated._id) | |
1cad0f39 | 46 | |
528a9efa | 47 | callback(null) |
1cad0f39 C |
48 | }, |
49 | ||
50 | function fetchMyCertificate (callback) { | |
51 | friends.getMyCertificate(function (err, cert) { | |
52 | if (err) { | |
53 | logger.error('Cannot read cert file.') | |
54 | return callback(err) | |
55 | } | |
c173e565 | 56 | |
1cad0f39 C |
57 | return callback(null, cert) |
58 | }) | |
1cad0f39 | 59 | } |
528a9efa | 60 | ], function (err, cert) { |
1cad0f39 C |
61 | if (err) return next(err) |
62 | ||
528a9efa | 63 | return res.json({ cert: cert }) |
9f10b292 C |
64 | }) |
65 | } | |
8c308c2b | 66 | |
528a9efa | 67 | function listPodsUrl (req, res, next) { |
a3ee6fa2 | 68 | Pod.listOnlyUrls(function (err, podsUrlList) { |
9f10b292 | 69 | if (err) return next(err) |
45239549 | 70 | |
528a9efa | 71 | res.json(podsUrlList) |
9f10b292 C |
72 | }) |
73 | } | |
45239549 | 74 | |
9f10b292 C |
75 | function makeFriends (req, res, next) { |
76 | friends.makeFriends(function (err) { | |
77 | if (err) return next(err) | |
45239549 | 78 | |
dc8bc31b | 79 | res.type('json').status(204).end() |
9f10b292 C |
80 | }) |
81 | } | |
45239549 | 82 | |
9f10b292 | 83 | function removePods (req, res, next) { |
f0f5567b | 84 | const url = req.body.signature.url |
8c308c2b | 85 | |
1a42c9e2 | 86 | waterfall([ |
a3ee6fa2 C |
87 | function loadPod (callback) { |
88 | Pod.loadByUrl(url, callback) | |
89 | }, | |
90 | ||
91 | function removePod (pod, callback) { | |
92 | pod.remove(function (err) { | |
93 | // Be sure we only return one argument in the callback | |
1cad0f39 C |
94 | return callback(err) |
95 | }) | |
96 | }, | |
97 | ||
98 | function (callback) { | |
aaf61f38 | 99 | Video.listByUrls([ url ], function (err, videosList) { |
1cad0f39 C |
100 | if (err) { |
101 | logger.error('Cannot list videos from url.', { error: err }) | |
102 | return callback(err) | |
103 | } | |
104 | ||
105 | return callback(null, videosList) | |
106 | }) | |
107 | }, | |
8425cb89 | 108 | |
1cad0f39 | 109 | function removeTheRemoteVideos (videosList, callback) { |
1a42c9e2 | 110 | each(videosList, function (video, callbackEach) { |
aaf61f38 C |
111 | video.remove(callbackEach) |
112 | }, callback) | |
1cad0f39 C |
113 | } |
114 | ], function (err) { | |
115 | if (err) return next(err) | |
116 | ||
117 | return res.type('json').status(204).end() | |
9f10b292 C |
118 | }) |
119 | } | |
8c308c2b | 120 | |
9f10b292 C |
121 | function quitFriends (req, res, next) { |
122 | friends.quitFriends(function (err) { | |
123 | if (err) return next(err) | |
8c308c2b | 124 | |
dc8bc31b | 125 | res.type('json').status(204).end() |
9f10b292 C |
126 | }) |
127 | } |