aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/pods.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-07-05 13:26:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-07-05 14:14:16 +0200
commit6fcd19ba737f1f5614a56c6925adb882dea43b8d (patch)
tree3365a96d82bc7f00ae504a568725c8e914150cf8 /server/controllers/api/pods.ts
parent5fe7e898316e18369c3e1aba307b55077adc7bfb (diff)
downloadPeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.gz
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.zst
PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.zip
Move to promises
Closes https://github.com/Chocobozzz/PeerTube/issues/74
Diffstat (limited to 'server/controllers/api/pods.ts')
-rw-r--r--server/controllers/api/pods.ts71
1 files changed, 22 insertions, 49 deletions
diff --git a/server/controllers/api/pods.ts b/server/controllers/api/pods.ts
index 283105f6c..0f85ab51d 100644
--- a/server/controllers/api/pods.ts
+++ b/server/controllers/api/pods.ts
@@ -1,5 +1,4 @@
1import * as express from 'express' 1import * as express from 'express'
2import { waterfall } from 'async'
3 2
4import { database as db } from '../../initializers/database' 3import { database as db } from '../../initializers/database'
5import { CONFIG } from '../../initializers' 4import { CONFIG } from '../../initializers'
@@ -57,65 +56,39 @@ export {
57function addPods (req: express.Request, res: express.Response, next: express.NextFunction) { 56function addPods (req: express.Request, res: express.Response, next: express.NextFunction) {
58 const informations = req.body 57 const informations = req.body
59 58
60 waterfall<string, Error>([ 59 const pod = db.Pod.build(informations)
61 function addPod (callback) { 60 pod.save()
62 const pod = db.Pod.build(informations) 61 .then(podCreated => {
63 pod.save().asCallback(function (err, podCreated) { 62 return sendOwnedVideosToPod(podCreated.id)
64 // Be sure about the number of parameters for the callback 63 })
65 return callback(err, podCreated) 64 .then(() => {
66 }) 65 return getMyPublicCert()
67 }, 66 })
68 67 .then(cert => {
69 function sendMyVideos (podCreated: PodInstance, callback) { 68 return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
70 sendOwnedVideosToPod(podCreated.id) 69 })
71 70 .catch(err => next(err))
72 callback(null)
73 },
74
75 function fetchMyCertificate (callback) {
76 getMyPublicCert(function (err, cert) {
77 if (err) {
78 logger.error('Cannot read cert file.')
79 return callback(err)
80 }
81
82 return callback(null, cert)
83 })
84 }
85 ], function (err, cert) {
86 if (err) return next(err)
87
88 return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
89 })
90} 71}
91 72
92function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { 73function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
93 db.Pod.list(function (err, podsList) { 74 db.Pod.list()
94 if (err) return next(err) 75 .then(podsList => res.json(getFormatedObjects(podsList, podsList.length)))
95 76 .catch(err => next(err))
96 res.json(getFormatedObjects(podsList, podsList.length))
97 })
98} 77}
99 78
100function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { 79function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
101 const hosts = req.body.hosts as string[] 80 const hosts = req.body.hosts as string[]
102 81
103 makeFriends(hosts, function (err) { 82 makeFriends(hosts)
104 if (err) { 83 .then(() => logger.info('Made friends!'))
105 logger.error('Could not make friends.', { error: err }) 84 .catch(err => logger.error('Could not make friends.', { error: err }))
106 return
107 }
108
109 logger.info('Made friends!')
110 })
111 85
86 // Don't wait the process that could be long
112 res.type('json').status(204).end() 87 res.type('json').status(204).end()
113} 88}
114 89
115function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { 90function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
116 quitFriends(function (err) { 91 quitFriends()
117 if (err) return next(err) 92 .then(() => res.type('json').status(204).end())
118 93 .catch(err => next(err))
119 res.type('json').status(204).end()
120 })
121} 94}