diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-05-13 21:34:36 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-05-13 21:34:36 +0200 |
commit | 1cad0f395f928c16d0b36bb72d2e5bbfc81cd760 (patch) | |
tree | 6e3b1120ab47a8c391402d26dba8ca82f665de76 | |
parent | 807df9e6682d1995d3d46e2461ca1b7b47eaf9d5 (diff) | |
download | PeerTube-1cad0f395f928c16d0b36bb72d2e5bbfc81cd760.tar.gz PeerTube-1cad0f395f928c16d0b36bb72d2e5bbfc81cd760.tar.zst PeerTube-1cad0f395f928c16d0b36bb72d2e5bbfc81cd760.zip |
Use async waterfall in pods controller for better readability
-rw-r--r-- | server/controllers/api/v1/pods.js | 83 |
1 files changed, 59 insertions, 24 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index cec51f272..e216714d6 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js | |||
@@ -1,5 +1,6 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const async = require('async') | ||
3 | const express = require('express') | 4 | const express = require('express') |
4 | 5 | ||
5 | const logger = require('../../../helpers/logger') | 6 | const logger = require('../../../helpers/logger') |
@@ -30,29 +31,48 @@ module.exports = router | |||
30 | 31 | ||
31 | function addPods (req, res, next) { | 32 | function addPods (req, res, next) { |
32 | const informations = req.body.data | 33 | const informations = req.body.data |
33 | Pods.add(informations, function (err) { | ||
34 | if (err) return next(err) | ||
35 | 34 | ||
36 | // Create the remote videos from the new pod | 35 | async.waterfall([ |
37 | videos.createRemoteVideos(informations.videos, function (err) { | 36 | function addPod (callback) { |
38 | if (err) logger.error('Cannot create remote videos.', { error: err }) | 37 | Pods.add(informations, function (err) { |
39 | }) | 38 | return callback(err) |
39 | }) | ||
40 | }, | ||
41 | |||
42 | function createVideosOfThisPod (callback) { | ||
43 | // Create the remote videos from the new pod | ||
44 | videos.createRemoteVideos(informations.videos, function (err) { | ||
45 | if (err) logger.error('Cannot create remote videos.', { error: err }) | ||
46 | |||
47 | return callback(err) | ||
48 | }) | ||
49 | }, | ||
50 | |||
51 | function fetchMyCertificate (callback) { | ||
52 | friends.getMyCertificate(function (err, cert) { | ||
53 | if (err) { | ||
54 | logger.error('Cannot read cert file.') | ||
55 | return callback(err) | ||
56 | } | ||
40 | 57 | ||
41 | friends.getMyCertificate(function (err, cert) { | 58 | return callback(null, cert) |
42 | if (err) { | 59 | }) |
43 | logger.error('Cannot read cert file.') | 60 | }, |
44 | return next(err) | ||
45 | } | ||
46 | 61 | ||
62 | function getListOfMyVideos (cert, callback) { | ||
47 | Videos.listOwned(function (err, videosList) { | 63 | Videos.listOwned(function (err, videosList) { |
48 | if (err) { | 64 | if (err) { |
49 | logger.error('Cannot get the list of owned videos.') | 65 | logger.error('Cannot get the list of owned videos.') |
50 | return next(err) | 66 | return callback(err) |
51 | } | 67 | } |
52 | 68 | ||
53 | res.json({ cert: cert, videos: videosList }) | 69 | return callback(null, cert, videosList) |
54 | }) | 70 | }) |
55 | }) | 71 | } |
72 | ], function (err, cert, videosList) { | ||
73 | if (err) return next(err) | ||
74 | |||
75 | return res.json({ cert: cert, videos: videosList }) | ||
56 | }) | 76 | }) |
57 | } | 77 | } |
58 | 78 | ||
@@ -74,24 +94,39 @@ function makeFriends (req, res, next) { | |||
74 | 94 | ||
75 | function removePods (req, res, next) { | 95 | function removePods (req, res, next) { |
76 | const url = req.body.signature.url | 96 | const url = req.body.signature.url |
77 | Pods.remove(url, function (err) { | ||
78 | if (err) return next(err) | ||
79 | 97 | ||
80 | Videos.listFromUrl(url, function (err, videosList) { | 98 | async.waterfall([ |
81 | if (err) { | 99 | function (callback) { |
82 | logger.error('Cannot list videos from url.', { error: err }) | 100 | Pods.remove(url, function (err) { |
83 | next(err) | 101 | return callback(err) |
84 | } | 102 | }) |
103 | }, | ||
104 | |||
105 | function (callback) { | ||
106 | Videos.listFromUrl(url, function (err, videosList) { | ||
107 | if (err) { | ||
108 | logger.error('Cannot list videos from url.', { error: err }) | ||
109 | return callback(err) | ||
110 | } | ||
111 | |||
112 | return callback(null, videosList) | ||
113 | }) | ||
114 | }, | ||
85 | 115 | ||
116 | function removeTheRemoteVideos (videosList, callback) { | ||
86 | videos.removeRemoteVideos(videosList, function (err) { | 117 | videos.removeRemoteVideos(videosList, function (err) { |
87 | if (err) { | 118 | if (err) { |
88 | logger.error('Cannot remove remote videos.', { error: err }) | 119 | logger.error('Cannot remove remote videos.', { error: err }) |
89 | next(err) | 120 | callback(err) |
90 | } | 121 | } |
91 | 122 | ||
92 | res.type('json').status(204).end() | 123 | return callback(null) |
93 | }) | 124 | }) |
94 | }) | 125 | } |
126 | ], function (err) { | ||
127 | if (err) return next(err) | ||
128 | |||
129 | return res.type('json').status(204).end() | ||
95 | }) | 130 | }) |
96 | } | 131 | } |
97 | 132 | ||