diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-12-11 21:50:51 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-12-19 21:22:28 +0100 |
commit | feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c (patch) | |
tree | 2abc9fbc9569760e218fd52835850b757344b420 /server/controllers/api/users.js | |
parent | 108626609eda75e4ecc0a83a650a4d53c46220e0 (diff) | |
download | PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.gz PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.zst PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.zip |
First version with PostgreSQL
Diffstat (limited to 'server/controllers/api/users.js')
-rw-r--r-- | server/controllers/api/users.js | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/server/controllers/api/users.js b/server/controllers/api/users.js index b4d687312..890028b36 100644 --- a/server/controllers/api/users.js +++ b/server/controllers/api/users.js | |||
@@ -2,10 +2,10 @@ | |||
2 | 2 | ||
3 | const each = require('async/each') | 3 | const each = require('async/each') |
4 | const express = require('express') | 4 | const express = require('express') |
5 | const mongoose = require('mongoose') | ||
6 | const waterfall = require('async/waterfall') | 5 | const waterfall = require('async/waterfall') |
7 | 6 | ||
8 | const constants = require('../../initializers/constants') | 7 | const constants = require('../../initializers/constants') |
8 | const db = require('../../initializers/database') | ||
9 | const friends = require('../../lib/friends') | 9 | const friends = require('../../lib/friends') |
10 | const logger = require('../../helpers/logger') | 10 | const logger = require('../../helpers/logger') |
11 | const middlewares = require('../../middlewares') | 11 | const middlewares = require('../../middlewares') |
@@ -17,9 +17,6 @@ const validatorsPagination = middlewares.validators.pagination | |||
17 | const validatorsSort = middlewares.validators.sort | 17 | const validatorsSort = middlewares.validators.sort |
18 | const validatorsUsers = middlewares.validators.users | 18 | const validatorsUsers = middlewares.validators.users |
19 | 19 | ||
20 | const User = mongoose.model('User') | ||
21 | const Video = mongoose.model('Video') | ||
22 | |||
23 | const router = express.Router() | 20 | const router = express.Router() |
24 | 21 | ||
25 | router.get('/me', oAuth.authenticate, getUserInformation) | 22 | router.get('/me', oAuth.authenticate, getUserInformation) |
@@ -62,13 +59,13 @@ module.exports = router | |||
62 | // --------------------------------------------------------------------------- | 59 | // --------------------------------------------------------------------------- |
63 | 60 | ||
64 | function createUser (req, res, next) { | 61 | function createUser (req, res, next) { |
65 | const user = new User({ | 62 | const user = db.User.build({ |
66 | username: req.body.username, | 63 | username: req.body.username, |
67 | password: req.body.password, | 64 | password: req.body.password, |
68 | role: constants.USER_ROLES.USER | 65 | role: constants.USER_ROLES.USER |
69 | }) | 66 | }) |
70 | 67 | ||
71 | user.save(function (err, createdUser) { | 68 | user.save().asCallback(function (err, createdUser) { |
72 | if (err) return next(err) | 69 | if (err) return next(err) |
73 | 70 | ||
74 | return res.type('json').status(204).end() | 71 | return res.type('json').status(204).end() |
@@ -76,7 +73,7 @@ function createUser (req, res, next) { | |||
76 | } | 73 | } |
77 | 74 | ||
78 | function getUserInformation (req, res, next) { | 75 | function getUserInformation (req, res, next) { |
79 | User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { | 76 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { |
80 | if (err) return next(err) | 77 | if (err) return next(err) |
81 | 78 | ||
82 | return res.json(user.toFormatedJSON()) | 79 | return res.json(user.toFormatedJSON()) |
@@ -84,7 +81,7 @@ function getUserInformation (req, res, next) { | |||
84 | } | 81 | } |
85 | 82 | ||
86 | function listUsers (req, res, next) { | 83 | function listUsers (req, res, next) { |
87 | User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { | 84 | db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { |
88 | if (err) return next(err) | 85 | if (err) return next(err) |
89 | 86 | ||
90 | res.json(getFormatedUsers(usersList, usersTotal)) | 87 | res.json(getFormatedUsers(usersList, usersTotal)) |
@@ -94,18 +91,19 @@ function listUsers (req, res, next) { | |||
94 | function removeUser (req, res, next) { | 91 | function removeUser (req, res, next) { |
95 | waterfall([ | 92 | waterfall([ |
96 | function getUser (callback) { | 93 | function getUser (callback) { |
97 | User.loadById(req.params.id, callback) | 94 | db.User.loadById(req.params.id, callback) |
98 | }, | 95 | }, |
99 | 96 | ||
97 | // TODO: use foreignkey? | ||
100 | function getVideos (user, callback) { | 98 | function getVideos (user, callback) { |
101 | Video.listOwnedByAuthor(user.username, function (err, videos) { | 99 | db.Video.listOwnedByAuthor(user.username, function (err, videos) { |
102 | return callback(err, user, videos) | 100 | return callback(err, user, videos) |
103 | }) | 101 | }) |
104 | }, | 102 | }, |
105 | 103 | ||
106 | function removeVideosFromDB (user, videos, callback) { | 104 | function removeVideosFromDB (user, videos, callback) { |
107 | each(videos, function (video, callbackEach) { | 105 | each(videos, function (video, callbackEach) { |
108 | video.remove(callbackEach) | 106 | video.destroy().asCallback(callbackEach) |
109 | }, function (err) { | 107 | }, function (err) { |
110 | return callback(err, user, videos) | 108 | return callback(err, user, videos) |
111 | }) | 109 | }) |
@@ -115,7 +113,7 @@ function removeUser (req, res, next) { | |||
115 | videos.forEach(function (video) { | 113 | videos.forEach(function (video) { |
116 | const params = { | 114 | const params = { |
117 | name: video.name, | 115 | name: video.name, |
118 | magnetUri: video.magnetUri | 116 | remoteId: video.id |
119 | } | 117 | } |
120 | 118 | ||
121 | friends.removeVideoToFriends(params) | 119 | friends.removeVideoToFriends(params) |
@@ -125,7 +123,7 @@ function removeUser (req, res, next) { | |||
125 | }, | 123 | }, |
126 | 124 | ||
127 | function removeUserFromDB (user, callback) { | 125 | function removeUserFromDB (user, callback) { |
128 | user.remove(callback) | 126 | user.destroy().asCallback(callback) |
129 | } | 127 | } |
130 | ], function andFinally (err) { | 128 | ], function andFinally (err) { |
131 | if (err) { | 129 | if (err) { |
@@ -138,11 +136,11 @@ function removeUser (req, res, next) { | |||
138 | } | 136 | } |
139 | 137 | ||
140 | function updateUser (req, res, next) { | 138 | function updateUser (req, res, next) { |
141 | User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { | 139 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { |
142 | if (err) return next(err) | 140 | if (err) return next(err) |
143 | 141 | ||
144 | user.password = req.body.password | 142 | user.password = req.body.password |
145 | user.save(function (err) { | 143 | user.save().asCallback(function (err) { |
146 | if (err) return next(err) | 144 | if (err) return next(err) |
147 | 145 | ||
148 | return res.sendStatus(204) | 146 | return res.sendStatus(204) |