Skip to content

Node.js based forum software built for the modern web

api controllers refactor

barisusaklibea2344ad4db
diff --git a/src/controllers/api.js b/src/controllers/api.js
index 8a109aa9df..5da2498989 100644
--- a/src/controllers/api.js
+++ b/src/controllers/api.js
@@ -172,55 +172,43 @@ apiController.getObjectByType = function(uid, type, id, callback) {
};
apiController.getUserByUID = function(req, res, next) {
- var uid = req.params.uid ? req.params.uid : 0;
-
- apiController.getUserDataByUID(req.uid, uid, function(err, data) {
- if (err) {
- return next(err);
- }
- res.json(data);
- });
+ byType('uid', req, res, next);
};
apiController.getUserByUsername = function(req, res, next) {
- var username = req.params.username ? req.params.username : 0;
-
- apiController.getUserDataByUsername(req.uid, username, function(err, data) {
- if (err) {
- return next(err);
- }
- res.json(data);
- });
+ byType('username', req, res, next);
};
apiController.getUserByEmail = function(req, res, next) {
- var email = req.params.email ? req.params.email : 0;
+ byType('email', req, res, next);
+};
- apiController.getUserDataByEmail(req.uid, email, function(err, data) {
- if (err) {
+function byType(type, req, res, next) {
+ apiController.getUserDataByField(req.uid, type, req.params[type], function(err, data) {
+ if (err || !data) {
return next(err);
}
res.json(data);
});
-};
-
-apiController.getUserDataByUsername = function(callerUid, username, callback) {
- async.waterfall([
- function(next) {
- user.getUidByUsername(username, next);
- },
- function(uid, next) {
- apiController.getUserDataByUID(callerUid, uid, next);
- }
- ], callback);
-};
+}
-apiController.getUserDataByEmail = function(callerUid, email, callback) {
+apiController.getUserDataByField = function(callerUid, field, fieldValue, callback) {
async.waterfall([
- function(next) {
- user.getUidByEmail(email, next);
+ function (next) {
+ if (field === 'uid') {
+ next(null, fieldValue);
+ } else if (field === 'username') {
+ user.getUidByUsername(fieldValue, next);
+ } else if (field === 'email') {
+ user.getUidByEmail(fieldValue, next);
+ } else {
+ next();
+ }
},
- function(uid, next) {
+ function (uid, next) {
+ if (!uid) {
+ return next();
+ }
apiController.getUserDataByUID(callerUid, uid, next);
}
], callback);
diff --git a/src/socket.io/user.js b/src/socket.io/user.js
index 8ca3e4231e..75ecd949fd 100644
--- a/src/socket.io/user.js
+++ b/src/socket.io/user.js
@@ -339,15 +339,15 @@ SocketUser.invite = function(socket, email, callback) {
};
SocketUser.getUserByUID = function(socket, uid, callback) {
- apiController.getUserDataByUID(socket.uid, uid, callback);
+ apiController.getUserDataByField(socket.uid, 'uid', uid, callback);
};
SocketUser.getUserByUsername = function(socket, username, callback) {
- apiController.getUserDataByUsername(socket.uid, username, callback);
+ apiController.getUserDataByField(socket.uid, 'username', username, callback);
};
SocketUser.getUserByEmail = function(socket, email, callback) {
- apiController.getUserDataByEmail(socket.uid, email, callback);
+ apiController.getUserDataByField(socket.uid, 'email', email, callback);
};