From e8b89513d484aa4f628e3a2d3b7f2a01142609fe Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 12 Jun 2014 16:45:00 -0400 Subject: [PATCH] some cleanup in addUserInfoToPost --- src/posts.js | 56 +++++++++++++++++--------------------------- src/topics/create.js | 5 ++-- src/topics/posts.js | 39 +++++++++++++++++++++++------- 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/posts.js b/src/posts.js index 5861a58042..f00fbe06f5 100644 --- a/src/posts.js +++ b/src/posts.js @@ -1,8 +1,19 @@ 'use strict'; -var db = require('./database'), +var async = require('async'), + path = require('path'), + fs = require('fs'), + nconf = require('nconf'), + validator = require('validator'), + winston = require('winston'), + gravatar = require('gravatar'), + S = require('string'), + + + db = require('./database'), utils = require('./../public/src/utils'), user = require('./user'), + groups = require('./groups'), topics = require('./topics'), favourites = require('./favourites'), postTools = require('./postTools'), @@ -10,20 +21,9 @@ var db = require('./database'), categories = require('./categories'), plugins = require('./plugins'), meta = require('./meta'), - emitter = require('./emitter'), - - async = require('async'), - path = require('path'), - fs = require('fs'), - nconf = require('nconf'), - validator = require('validator'), - winston = require('winston'), - gravatar = require('gravatar'), - S = require('string'); + emitter = require('./emitter'); (function(Posts) { - var customUserInfo = {}; - require('./posts/delete')(Posts); Posts.create = function(data, callback) { @@ -188,13 +188,13 @@ var db = require('./database'), }); }; - Posts.addUserInfoToPost = function(post, callback) { + Posts.getUserInfoForPost = function(post, callback) { user.getUserFields(post.uid, ['username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned'], function(err, userData) { if (err) { return callback(err); } - post.user = { + var userInfo = { username: userData.username || '[[global:guest]]', userslug: userData.userslug || '', reputation: userData.reputation || 0, @@ -203,24 +203,12 @@ var db = require('./database'), picture: userData.picture || user.createGravatarURLFromEmail('') }; - for (var info in customUserInfo) { - if (customUserInfo.hasOwnProperty(info)) { - post.user[info] = userData[info] || customUserInfo[info]; - } - } - async.parallel({ signature: function(next) { - if (parseInt(meta.config.disableSignatures, 10) !== 1) { - return postTools.parseSignature(userData.signature, next); - } - next(); - }, - editor: function(next) { - if (!post.editor) { + if (parseInt(meta.config.disableSignatures, 10) === 1) { return next(); } - user.getUserFields(post.editor, ['username', 'userslug'], next); + postTools.parseSignature(userData.signature, next); }, customProfileInfo: function(next) { plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: post.uid, pid: post.pid}, next); @@ -229,12 +217,10 @@ var db = require('./database'), if (err) { return callback(err); } - - post.user.signature = results.signature; - post.editor = results.editor; - post.custom_profile_info = results.profile; - - callback(null, post); + userInfo.signature = results.signature; + userInfo.custom_profile_info = results.custom_profile_info; + userInfo.groups = results.groups; + callback(null, userInfo); }); }); }; diff --git a/src/topics/create.js b/src/topics/create.js index 3205938112..d6e4c8ccd9 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -224,9 +224,10 @@ module.exports = function(Topics) { }, function(result, next) { Topics.pushUnreadCount(); - posts.addUserInfoToPost(postData, next); + posts.getUserInfoForPost(postData, next); }, - function(postData, next) { + function(userInfo, next) { + postData.user = userInfo; Topics.getTopicFields(tid, ['tid', 'title', 'slug'], next); }, function(topicData, next) { diff --git a/src/topics/posts.js b/src/topics/posts.js index 2819c6e647..7771865638 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -4,10 +4,11 @@ var async = require('async'), - db = require('./../database'), - emitter = require('./../emitter'), - favourites = require('./../favourites'), - posts = require('./../posts'), + db = require('../database'), + user = require('../user'), + emitter = require('../emitter'), + favourites = require('../favourites'), + posts = require('../posts'), privileges = require('../privileges'); module.exports = function(Topics) { @@ -45,16 +46,36 @@ module.exports = function(Topics) { }); async.parallel({ - favourites : function(next) { + favourites: function(next) { favourites.getFavouritesByPostIDs(pids, uid, next); }, - voteData : function(next) { + voteData: function(next) { favourites.getVoteStatusByPostIDs(pids, uid, next); }, - userData : function(next) { - async.each(postData, posts.addUserInfoToPost, next); + userData: function(next) { + async.each(postData, function(post, next) { + async.parallel({ + editor: function(next) { + if (!post.editor) { + return next(); + } + user.getUserFields(post.editor, ['username', 'userslug'], next); + }, + user: function(next) { + posts.getUserInfoForPost(post, next); + } + }, function(err, results) { + if (err) { + return next(err); + } + + post.user = results.user; + post.editor = results.editor; + next(); + }); + }, next); }, - privileges : function(next) { + privileges: function(next) { async.map(pids, function (pid, next) { privileges.posts.get(pid, uid, next); }, next);