mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
more refactors
This commit is contained in:
@@ -13,9 +13,8 @@ var privileges = require('../privileges');
|
||||
var plugins = require('../plugins');
|
||||
var widgets = require('../widgets');
|
||||
var translator = require('../../public/src/modules/translator');
|
||||
var accountHelpers = require('../controllers/accounts/helpers');
|
||||
|
||||
var apiController = {};
|
||||
var apiController = module.exports;
|
||||
|
||||
apiController.getConfig = function (req, res, next) {
|
||||
var config = {};
|
||||
@@ -220,92 +219,6 @@ apiController.getObject = function (req, res, next) {
|
||||
});
|
||||
};
|
||||
|
||||
apiController.getCurrentUser = function (req, res, next) {
|
||||
if (!req.uid) {
|
||||
return res.status(401).json('not-authorized');
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.getUserField(req.uid, 'userslug', next);
|
||||
},
|
||||
function (userslug, next) {
|
||||
accountHelpers.getUserDataByUserSlug(userslug, req.uid, next);
|
||||
},
|
||||
], function (err, userData) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.json(userData);
|
||||
});
|
||||
};
|
||||
|
||||
apiController.getUserByUID = function (req, res, next) {
|
||||
byType('uid', req, res, next);
|
||||
};
|
||||
|
||||
apiController.getUserByUsername = function (req, res, next) {
|
||||
byType('username', req, res, next);
|
||||
};
|
||||
|
||||
apiController.getUserByEmail = function (req, res, next) {
|
||||
byType('email', req, res, next);
|
||||
};
|
||||
|
||||
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.getUserDataByField = function (callerUid, field, fieldValue, callback) {
|
||||
async.waterfall([
|
||||
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) {
|
||||
if (!uid) {
|
||||
return next();
|
||||
}
|
||||
apiController.getUserDataByUID(callerUid, uid, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
apiController.getUserDataByUID = function (callerUid, uid, callback) {
|
||||
if (!parseInt(callerUid, 10) && parseInt(meta.config.privateUserInfo, 10) === 1) {
|
||||
return callback(new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
|
||||
if (!parseInt(uid, 10)) {
|
||||
return callback(new Error('[[error:no-user]]'));
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
userData: async.apply(user.getUserData, uid),
|
||||
settings: async.apply(user.getSettings, uid),
|
||||
}, function (err, results) {
|
||||
if (err || !results.userData) {
|
||||
return callback(err || new Error('[[error:no-user]]'));
|
||||
}
|
||||
|
||||
results.userData.email = results.settings.showemail ? results.userData.email : undefined;
|
||||
results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined;
|
||||
|
||||
callback(null, results.userData);
|
||||
});
|
||||
};
|
||||
|
||||
apiController.getModerators = function (req, res, next) {
|
||||
categories.getModerators(req.params.cid, function (err, moderators) {
|
||||
if (err) {
|
||||
@@ -314,16 +227,3 @@ apiController.getModerators = function (req, res, next) {
|
||||
res.json({ moderators: moderators });
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
apiController.getRecentPosts = function (req, res, next) {
|
||||
posts.getRecentPosts(req.uid, 0, 19, req.params.term, function (err, data) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
res.json(data);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = apiController;
|
||||
|
||||
Reference in New Issue
Block a user