From 73d65dd1230193e040e95796fb3124ab5bb8fc7b Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 4 May 2015 18:01:45 -0400 Subject: [PATCH] filter:users.search filter:users.build hooks --- src/controllers/users.js | 15 ++++-- src/user/create.js | 114 ++++++++++++++++++++------------------- src/user/delete.js | 2 +- src/user/search.js | 6 ++- 4 files changed, 77 insertions(+), 60 deletions(-) diff --git a/src/controllers/users.js b/src/controllers/users.js index 7b0047b7ad..29dd552b87 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -43,7 +43,7 @@ usersController.getOnlineUsers = function(req, res, next) { anonymousUserCount: websockets.getOnlineAnonCount() }; - res.render('users', userData); + render(req, res, userData, next); }); }; @@ -72,7 +72,7 @@ usersController.getUsers = function(set, count, req, res, next) { pagination: pagination.create(1, pageCount) }; userData['route_' + set] = true; - res.render('users', userData); + render(req, res, userData, next); }); }; @@ -113,10 +113,19 @@ usersController.getUsersForSearch = function(req, res, next) { users: data.users }; - res.render('users', userData); + render(req, res, userData, next); }); }; +function render(req, res, data, next) { + plugins.fireHook('filter:users.build', {req: req, res: res, templateData: data}, function(err, data) { + if (err) { + return next(err); + } + res.render('users', data.templateData); + }); +} + module.exports = usersController; diff --git a/src/user/create.js b/src/user/create.js index db85c81508..4349b8db53 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -87,72 +87,76 @@ module.exports = function(User) { function(uid, next) { userData.uid = uid; db.setObject('user:' + uid, userData, next); - } - ], function(err) { - if (err) { - return callback(err); - } - - async.parallel([ - function(next) { - db.incrObjectField('global', 'userCount', next); - }, - function(next) { - db.setObjectField('username:uid', userData.username, userData.uid, next); - }, - function(next) { - db.setObjectField('userslug:uid', userData.userslug, userData.uid, next); - }, - function(next) { - db.sortedSetAdd('users:joindate', timestamp, userData.uid, next); - }, - function(next) { - db.sortedSetsAdd(['users:postcount', 'users:reputation'], 0, userData.uid, next); - }, - function(next) { - groups.join('registered-users', userData.uid, next); - }, - function(next) { - if (userData.email) { - db.setObjectField('email:uid', userData.email.toLowerCase(), userData.uid, next); - if (parseInt(userData.uid, 10) !== 1 && parseInt(meta.config.requireEmailConfirmation, 10) === 1) { - User.email.sendValidationEmail(userData.uid, userData.email); + }, + function(next) { + async.parallel([ + function(next) { + db.setObjectField('username:uid', userData.username, userData.uid, next); + }, + function(next) { + db.setObjectField('userslug:uid', userData.userslug, userData.uid, next); + }, + function(next) { + db.sortedSetAdd('users:joindate', timestamp, userData.uid, next); + }, + function(next) { + db.sortedSetsAdd(['users:postcount', 'users:reputation'], 0, userData.uid, next); + }, + function(next) { + groups.join('registered-users', userData.uid, next); + }, + function(next) { + if (userData.email) { + db.setObjectField('email:uid', userData.email.toLowerCase(), userData.uid, next); + if (parseInt(userData.uid, 10) !== 1 && parseInt(meta.config.requireEmailConfirmation, 10) === 1) { + User.email.sendValidationEmail(userData.uid, userData.email); + } + } else { + next(); } - } else { - next(); - } - }, - function(next) { - if (!data.password) { - return next(); - } - - User.hashPassword(data.password, function(err, hash) { - if (err) { - return next(err); + }, + function(next) { + if (!data.password) { + return next(); } - async.parallel([ - async.apply(User.setUserField, userData.uid, 'password', hash), - async.apply(User.reset.updateExpiry, userData.uid) - ], next); - }); - } - ], function(err) { - if (err) { - return callback(err); - } + User.hashPassword(data.password, function(err, hash) { + if (err) { + return next(err); + } + + async.parallel([ + async.apply(User.setUserField, userData.uid, 'password', hash), + async.apply(User.reset.updateExpiry, userData.uid) + ], next); + }); + } + ], next); + }, + function(results, next) { + User.updateUserCount(next); + }, + function(next) { if (userNameChanged) { User.notifications.sendNameChangeNotification(userData.uid, userData.username); } plugins.fireHook('action:user.create', userData); - callback(null, userData.uid); - }); - }); + next(null, userData.uid); + } + ], callback); }); }); }; + User.updateUserCount = function(callback) { + db.sortedSetCard('users:joindate', function(err, count) { + if (err) { + return callback(err); + } + db.setObjectField('global', 'userCount', count, callback); + }); + }; + function isDataValid(userData, callback) { async.parallel({ emailValid: function(next) { diff --git a/src/user/delete.js b/src/user/delete.js index c1d67bd92f..49e21553c9 100644 --- a/src/user/delete.js +++ b/src/user/delete.js @@ -106,7 +106,7 @@ module.exports = function(User) { db.deleteAll(['followers:' + uid, 'following:' + uid, 'user:' + uid], next); }, function(next) { - db.decrObjectField('global', 'userCount', next); + User.updateUserCount(next); } ], callback); }); diff --git a/src/user/search.js b/src/user/search.js index ec1420e676..bba115dabd 100644 --- a/src/user/search.js +++ b/src/user/search.js @@ -5,6 +5,7 @@ var async = require('async'), meta = require('../meta'), user = require('../user'), pagination = require('../pagination'), + plugins = require('../plugins'), db = require('../database'); module.exports = function(User) { @@ -52,7 +53,10 @@ module.exports = function(User) { searchResult.timing = (process.elapsedTimeSince(startTime) / 1000).toFixed(2); searchResult.users = userData; - next(null, searchResult); + plugins.fireHook('filter:users.search', {result: searchResult, uid: uid}, next); + }, + function(data, next) { + next(null, data.result); } ], callback); };