]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
8c308c2b | 2 | |
f0f5567b | 3 | const express = require('express') |
f0f5567b C |
4 | |
5 | const logger = require('../../../helpers/logger') | |
6 | const friends = require('../../../lib/friends') | |
b3b92647 | 7 | const middlewares = require('../../../middlewares') |
f0f5567b | 8 | const Pods = require('../../../models/pods') |
b3b92647 C |
9 | const oAuth2 = middlewares.oauth2 |
10 | const reqValidator = middlewares.reqValidators.pods | |
11 | const secureMiddleware = middlewares.secure | |
12 | const secureRequest = middlewares.reqValidators.remote.secureRequest | |
cbe2f7c3 | 13 | const videos = require('../../../lib/videos') |
f0f5567b C |
14 | const Videos = require('../../../models/videos') |
15 | ||
16 | const router = express.Router() | |
8c308c2b | 17 | |
5dda52c9 C |
18 | router.get('/', listPods) |
19 | router.post('/', reqValidator.podsAdd, addPods) | |
b3b92647 C |
20 | router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends) |
21 | router.get('/quitfriends', oAuth2.authenticate, quitFriends) | |
9f10b292 C |
22 | // Post because this is a secured request |
23 | router.post('/remove', secureRequest, secureMiddleware.decryptBody, 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) { |
f0f5567b | 32 | const informations = req.body.data |
9f10b292 C |
33 | Pods.add(informations, function (err) { |
34 | if (err) return next(err) | |
8c308c2b | 35 | |
cbe2f7c3 C |
36 | // Create the remote videos from the new pod |
37 | videos.createRemoteVideos(informations.videos, function (err) { | |
38 | if (err) logger.error('Cannot create remote videos.', { error: err }) | |
39 | }) | |
c173e565 | 40 | |
cbe2f7c3 | 41 | friends.getMyCertificate(function (err, cert) { |
9f10b292 C |
42 | if (err) { |
43 | logger.error('Cannot read cert file.') | |
44 | return next(err) | |
45 | } | |
46 | ||
bc503c2a | 47 | Videos.listOwned(function (err, videosList) { |
c173e565 | 48 | if (err) { |
9f10b292 | 49 | logger.error('Cannot get the list of owned videos.') |
c173e565 C |
50 | return next(err) |
51 | } | |
52 | ||
bc503c2a | 53 | res.json({ cert: cert, videos: videosList }) |
c173e565 | 54 | }) |
8c308c2b | 55 | }) |
9f10b292 C |
56 | }) |
57 | } | |
8c308c2b | 58 | |
9f10b292 | 59 | function listPods (req, res, next) { |
bc503c2a | 60 | Pods.list(function (err, podsList) { |
9f10b292 | 61 | if (err) return next(err) |
45239549 | 62 | |
bc503c2a | 63 | res.json(podsList) |
9f10b292 C |
64 | }) |
65 | } | |
45239549 | 66 | |
9f10b292 C |
67 | function makeFriends (req, res, next) { |
68 | friends.makeFriends(function (err) { | |
69 | if (err) return next(err) | |
45239549 | 70 | |
dc8bc31b | 71 | res.type('json').status(204).end() |
9f10b292 C |
72 | }) |
73 | } | |
45239549 | 74 | |
9f10b292 | 75 | function removePods (req, res, next) { |
f0f5567b | 76 | const url = req.body.signature.url |
9f10b292 C |
77 | Pods.remove(url, function (err) { |
78 | if (err) return next(err) | |
8c308c2b | 79 | |
bc503c2a | 80 | Videos.listFromUrl(url, function (err, videosList) { |
cbe2f7c3 C |
81 | if (err) { |
82 | logger.error('Cannot list videos from url.', { error: err }) | |
83 | next(err) | |
84 | } | |
8425cb89 | 85 | |
bc503c2a | 86 | videos.removeRemoteVideos(videosList, function (err) { |
cbe2f7c3 C |
87 | if (err) { |
88 | logger.error('Cannot remove remote videos.', { error: err }) | |
89 | next(err) | |
90 | } | |
91 | ||
92 | res.type('json').status(204).end() | |
93 | }) | |
8c308c2b | 94 | }) |
9f10b292 C |
95 | }) |
96 | } | |
8c308c2b | 97 | |
9f10b292 C |
98 | function quitFriends (req, res, next) { |
99 | friends.quitFriends(function (err) { | |
100 | if (err) return next(err) | |
8c308c2b | 101 | |
dc8bc31b | 102 | res.type('json').status(204).end() |
9f10b292 C |
103 | }) |
104 | } |