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