aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users.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/users.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/users.ts')
-rw-r--r--server/controllers/api/users.ts86
1 files changed, 34 insertions, 52 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts
index ce15353ef..6e0bb474a 100644
--- a/server/controllers/api/users.ts
+++ b/server/controllers/api/users.ts
@@ -1,8 +1,7 @@
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, USER_ROLES } from '../../initializers' 4import { USER_ROLES } from '../../initializers'
6import { logger, getFormatedObjects } from '../../helpers' 5import { logger, getFormatedObjects } from '../../helpers'
7import { 6import {
8 authenticate, 7 authenticate,
@@ -87,78 +86,61 @@ function createUser (req: express.Request, res: express.Response, next: express.
87 role: USER_ROLES.USER 86 role: USER_ROLES.USER
88 }) 87 })
89 88
90 user.save().asCallback(function (err) { 89 user.save()
91 if (err) return next(err) 90 .then(() => res.type('json').status(204).end())
92 91 .catch(err => next(err))
93 return res.type('json').status(204).end()
94 })
95} 92}
96 93
97function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { 94function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
98 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { 95 db.User.loadByUsername(res.locals.oauth.token.user.username)
99 if (err) return next(err) 96 .then(user => res.json(user.toFormatedJSON()))
100 97 .catch(err => next(err))
101 return res.json(user.toFormatedJSON())
102 })
103} 98}
104 99
105function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { 100function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
106 const videoId = '' + req.params.videoId 101 const videoId = '' + req.params.videoId
107 const userId = +res.locals.oauth.token.User.id 102 const userId = +res.locals.oauth.token.User.id
108 103
109 db.UserVideoRate.load(userId, videoId, null, function (err, ratingObj) { 104 db.UserVideoRate.load(userId, videoId, null)
110 if (err) return next(err) 105 .then(ratingObj => {
111 106 const rating = ratingObj ? ratingObj.type : 'none'
112 const rating = ratingObj ? ratingObj.type : 'none' 107 const json: FormatedUserVideoRate = {
113 108 videoId,
114 const json: FormatedUserVideoRate = { 109 rating
115 videoId, 110 }
116 rating 111 res.json(json)
117 } 112 })
118 res.json(json) 113 .catch(err => next(err))
119 })
120} 114}
121 115
122function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { 116function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
123 db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { 117 db.User.listForApi(req.query.start, req.query.count, req.query.sort)
124 if (err) return next(err) 118 .then(resultList => {
125 119 res.json(getFormatedObjects(resultList.data, resultList.total))
126 res.json(getFormatedObjects(usersList, usersTotal)) 120 })
127 }) 121 .catch(err => next(err))
128} 122}
129 123
130function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { 124function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
131 waterfall([ 125 db.User.loadById(req.params.id)
132 function loadUser (callback) { 126 .then(user => user.destroy())
133 db.User.loadById(req.params.id, callback) 127 .then(() => res.sendStatus(204))
134 }, 128 .catch(err => {
135
136 function deleteUser (user, callback) {
137 user.destroy().asCallback(callback)
138 }
139 ], function andFinally (err) {
140 if (err) {
141 logger.error('Errors when removed the user.', { error: err }) 129 logger.error('Errors when removed the user.', { error: err })
142 return next(err) 130 return next(err)
143 } 131 })
144
145 return res.sendStatus(204)
146 })
147} 132}
148 133
149function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { 134function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
150 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { 135 db.User.loadByUsername(res.locals.oauth.token.user.username)
151 if (err) return next(err) 136 .then(user => {
152 137 if (req.body.password) user.password = req.body.password
153 if (req.body.password) user.password = req.body.password 138 if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW
154 if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW
155 139
156 user.save().asCallback(function (err) { 140 return user.save()
157 if (err) return next(err)
158
159 return res.sendStatus(204)
160 }) 141 })
161 }) 142 .then(() => res.sendStatus(204))
143 .catch(err => next(err))
162} 144}
163 145
164function success (req: express.Request, res: express.Response, next: express.NextFunction) { 146function success (req: express.Request, res: express.Response, next: express.NextFunction) {