diff options
Diffstat (limited to 'server/controllers/api/users.js')
-rw-r--r-- | server/controllers/api/users.js | 72 |
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 | ||
3 | const each = require('async/each') | ||
4 | const express = require('express') | 3 | const express = require('express') |
5 | const mongoose = require('mongoose') | ||
6 | const waterfall = require('async/waterfall') | 4 | const waterfall = require('async/waterfall') |
7 | 5 | ||
8 | const constants = require('../../initializers/constants') | 6 | const constants = require('../../initializers/constants') |
9 | const friends = require('../../lib/friends') | 7 | const db = require('../../initializers/database') |
10 | const logger = require('../../helpers/logger') | 8 | const logger = require('../../helpers/logger') |
9 | const utils = require('../../helpers/utils') | ||
11 | const middlewares = require('../../middlewares') | 10 | const middlewares = require('../../middlewares') |
12 | const admin = middlewares.admin | 11 | const admin = middlewares.admin |
13 | const oAuth = middlewares.oauth | 12 | const oAuth = middlewares.oauth |
@@ -17,9 +16,6 @@ const validatorsPagination = middlewares.validators.pagination | |||
17 | const validatorsSort = middlewares.validators.sort | 16 | const validatorsSort = middlewares.validators.sort |
18 | const validatorsUsers = middlewares.validators.users | 17 | const validatorsUsers = middlewares.validators.users |
19 | 18 | ||
20 | const User = mongoose.model('User') | ||
21 | const Video = mongoose.model('Video') | ||
22 | |||
23 | const router = express.Router() | 19 | const router = express.Router() |
24 | 20 | ||
25 | router.get('/me', oAuth.authenticate, getUserInformation) | 21 | router.get('/me', oAuth.authenticate, getUserInformation) |
@@ -62,13 +58,13 @@ module.exports = router | |||
62 | // --------------------------------------------------------------------------- | 58 | // --------------------------------------------------------------------------- |
63 | 59 | ||
64 | function createUser (req, res, next) { | 60 | function 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 | ||
78 | function getUserInformation (req, res, next) { | 74 | function 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 | ||
86 | function listUsers (req, res, next) { | 82 | function 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 | ||
94 | function removeUser (req, res, next) { | 90 | function 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 | ||
140 | function updateUser (req, res, next) { | 109 | function 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) { | |||
153 | function success (req, res, next) { | 122 | function success (req, res, next) { |
154 | res.end() | 123 | res.end() |
155 | } | 124 | } |
156 | |||
157 | // --------------------------------------------------------------------------- | ||
158 | |||
159 | function 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 | } | ||