From 7245ffa94a587e510b23f374c87c6c349724ed7a Mon Sep 17 00:00:00 2001 From: Will Date: Thu, 30 Jul 2015 00:21:41 -0700 Subject: [PATCH] refactor user.toJSON() --- website/src/controllers/user.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/website/src/controllers/user.js b/website/src/controllers/user.js index 0c95d49d99..b42eab8200 100644 --- a/website/src/controllers/user.js +++ b/website/src/controllers/user.js @@ -109,32 +109,39 @@ api.score = function(req, res, next) { user.save(function(err,saved){ if (err) return next(err); + + // Convert Mongoose model to JS object and store copy of stats object to send to client + var userStats = saved.toJSON().stats; + // TODO this should be return {_v,task,stats,_tmp}, instead of merging everything togther at top-level response // However, this is the most commonly used API route, and changing it will mess with all 3rd party consumers. Bad idea :( res.json(200, _.extend({ delta: delta, _tmp: user._tmp - }, saved.toJSON().stats)); + }, userStats)); // Webhooks - // Select character data to send - var userData = _.pick(user.toJSON(), ['_id', '_tmp', 'stats']); // user.toJSON to copy-by-value - userData.stats.toNextLevel = shared.tnl(user.stats.lvl); - userData.stats.maxHealth = shared.maxHealth; - userData.stats.maxMP = user._statsComputed.maxMP; + var userData = { + _id: user._id, + _tmp: user._tmp, + stats: _.extend({}, userStats, { // send stats as well as exp tnl, max health, and max mp + toNextLevel: shared.tnl(user.stats.lvl), + maxHealth: shared.maxHealth, + maxMP: user._statsComputed.maxMP + }) + }; - // for each webhook _.each(user.preferences.webhooks, function(h){ if (!h.enabled || !validator.isURL(h.url)) return; request.post({ url: h.url, //form: {task: task, delta: delta, user: _.pick(user, ['stats', '_tmp'])} // this is causing "Maximum Call Stack Exceeded" body: { - direction: direction, // direction of change - task: task, // task object + direction: direction, + task: task, delta: delta, - user: userData // character/profile data + user: userData }, json:true });