aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/users.js')
-rw-r--r--server/controllers/api/users.js72
1 files changed, 13 insertions, 59 deletions
diff --git a/server/controllers/api/users.js b/server/controllers/api/users.js
index b4d687312..6cd0e84f7 100644
--- a/server/controllers/api/users.js
+++ b/server/controllers/api/users.js
@@ -1,13 +1,12 @@
1'use strict' 1'use strict'
2 2
3const each = require('async/each')
4const express = require('express') 3const express = require('express')
5const mongoose = require('mongoose')
6const waterfall = require('async/waterfall') 4const waterfall = require('async/waterfall')
7 5
8const constants = require('../../initializers/constants') 6const constants = require('../../initializers/constants')
9const friends = require('../../lib/friends') 7const db = require('../../initializers/database')
10const logger = require('../../helpers/logger') 8const logger = require('../../helpers/logger')
9const utils = require('../../helpers/utils')
11const middlewares = require('../../middlewares') 10const middlewares = require('../../middlewares')
12const admin = middlewares.admin 11const admin = middlewares.admin
13const oAuth = middlewares.oauth 12const oAuth = middlewares.oauth
@@ -17,9 +16,6 @@ const validatorsPagination = middlewares.validators.pagination
17const validatorsSort = middlewares.validators.sort 16const validatorsSort = middlewares.validators.sort
18const validatorsUsers = middlewares.validators.users 17const validatorsUsers = middlewares.validators.users
19 18
20const User = mongoose.model('User')
21const Video = mongoose.model('Video')
22
23const router = express.Router() 19const router = express.Router()
24 20
25router.get('/me', oAuth.authenticate, getUserInformation) 21router.get('/me', oAuth.authenticate, getUserInformation)
@@ -62,13 +58,13 @@ module.exports = router
62// --------------------------------------------------------------------------- 58// ---------------------------------------------------------------------------
63 59
64function createUser (req, res, next) { 60function createUser (req, res, next) {
65 const user = new User({ 61 const user = db.User.build({
66 username: req.body.username, 62 username: req.body.username,
67 password: req.body.password, 63 password: req.body.password,
68 role: constants.USER_ROLES.USER 64 role: constants.USER_ROLES.USER
69 }) 65 })
70 66
71 user.save(function (err, createdUser) { 67 user.save().asCallback(function (err, createdUser) {
72 if (err) return next(err) 68 if (err) return next(err)
73 69
74 return res.type('json').status(204).end() 70 return res.type('json').status(204).end()
@@ -76,7 +72,7 @@ function createUser (req, res, next) {
76} 72}
77 73
78function getUserInformation (req, res, next) { 74function getUserInformation (req, res, next) {
79 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { 75 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
80 if (err) return next(err) 76 if (err) return next(err)
81 77
82 return res.json(user.toFormatedJSON()) 78 return res.json(user.toFormatedJSON())
@@ -84,48 +80,21 @@ function getUserInformation (req, res, next) {
84} 80}
85 81
86function listUsers (req, res, next) { 82function listUsers (req, res, next) {
87 User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { 83 db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
88 if (err) return next(err) 84 if (err) return next(err)
89 85
90 res.json(getFormatedUsers(usersList, usersTotal)) 86 res.json(utils.getFormatedObjects(usersList, usersTotal))
91 }) 87 })
92} 88}
93 89
94function removeUser (req, res, next) { 90function removeUser (req, res, next) {
95 waterfall([ 91 waterfall([
96 function getUser (callback) { 92 function loadUser (callback) {
97 User.loadById(req.params.id, callback) 93 db.User.loadById(req.params.id, callback)
98 },
99
100 function getVideos (user, callback) {
101 Video.listOwnedByAuthor(user.username, function (err, videos) {
102 return callback(err, user, videos)
103 })
104 },
105
106 function removeVideosFromDB (user, videos, callback) {
107 each(videos, function (video, callbackEach) {
108 video.remove(callbackEach)
109 }, function (err) {
110 return callback(err, user, videos)
111 })
112 },
113
114 function sendInformationToFriends (user, videos, callback) {
115 videos.forEach(function (video) {
116 const params = {
117 name: video.name,
118 magnetUri: video.magnetUri
119 }
120
121 friends.removeVideoToFriends(params)
122 })
123
124 return callback(null, user)
125 }, 94 },
126 95
127 function removeUserFromDB (user, callback) { 96 function deleteUser (user, callback) {
128 user.remove(callback) 97 user.destroy().asCallback(callback)
129 } 98 }
130 ], function andFinally (err) { 99 ], function andFinally (err) {
131 if (err) { 100 if (err) {
@@ -138,11 +107,11 @@ function removeUser (req, res, next) {
138} 107}
139 108
140function updateUser (req, res, next) { 109function updateUser (req, res, next) {
141 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { 110 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
142 if (err) return next(err) 111 if (err) return next(err)
143 112
144 user.password = req.body.password 113 user.password = req.body.password
145 user.save(function (err) { 114 user.save().asCallback(function (err) {
146 if (err) return next(err) 115 if (err) return next(err)
147 116
148 return res.sendStatus(204) 117 return res.sendStatus(204)
@@ -153,18 +122,3 @@ function updateUser (req, res, next) {
153function success (req, res, next) { 122function success (req, res, next) {
154 res.end() 123 res.end()
155} 124}
156
157// ---------------------------------------------------------------------------
158
159function getFormatedUsers (users, usersTotal) {
160 const formatedUsers = []
161
162 users.forEach(function (user) {
163 formatedUsers.push(user.toFormatedJSON())
164 })
165
166 return {
167 total: usersTotal,
168 data: formatedUsers
169 }
170}