From f68f88452593399ba02c84de2e816acc66985210 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Thu, 20 Nov 2014 16:22:42 -0700 Subject: [PATCH] refactor(reflist): helper fn for reflist.push (not used now, but might need soon) --- dist/habitrpg-shared.js | 21 ++++++++++++++++----- script/index.coffee | 11 ++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/dist/habitrpg-shared.js b/dist/habitrpg-shared.js index 852715f3b8..5513c58db8 100644 --- a/dist/habitrpg-shared.js +++ b/dist/habitrpg-shared.js @@ -13730,6 +13730,17 @@ api.dotGet = function(obj, path) { })(this)), obj); }; +api.refPush = function(reflist, item, prune) { + if (prune == null) { + prune = 0; + } + item.sort = _.isEmpty(reflist) ? 0 : _.max(reflist, 'sort').sort + 1; + if (!(item.id && !reflist[item.id])) { + item.id = api.uuid(); + } + return reflist[item.id] = item; +}; + api.planGemLimits = { convRate: 20, convCap: 25 @@ -14659,11 +14670,11 @@ api.wrap = function(user, main) { addWebhook: function(req, cb) { var wh; wh = user.preferences.webhooks; - wh[req.body.id || api.uuid()] = { + api.refPush(wh, { url: req.body.url, enabled: req.body.enabled || true, - sort: _.isEmpty(wh) ? 0 : _.max(wh, 'sort').sort + 1 - }; + id: req.body.id + }); if (typeof user.markModified === "function") { user.markModified('preferences.webhooks'); } @@ -15914,5 +15925,5 @@ api.wrap = function(user, main) { }; -}).call(this,require("/Users/lefnire/Dropbox/Sites/habitrpg/modules/habitrpg-shared/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js")) -},{"./content.coffee":5,"./i18n.coffee":6,"/Users/lefnire/Dropbox/Sites/habitrpg/modules/habitrpg-shared/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js":2,"lodash":3,"moment":4}]},{},[1]) \ No newline at end of file +}).call(this,require("/Users/lefnire/Google Drive/Sync/Sites/habitrpg/modules/habitrpg-shared/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js")) +},{"./content.coffee":5,"./i18n.coffee":6,"/Users/lefnire/Google Drive/Sync/Sites/habitrpg/modules/habitrpg-shared/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js":2,"lodash":3,"moment":4}]},{},[1]) \ No newline at end of file diff --git a/script/index.coffee b/script/index.coffee index bdf2168e64..00f92dc7ab 100644 --- a/script/index.coffee +++ b/script/index.coffee @@ -19,6 +19,11 @@ api.dotSet = (obj,path,val)-> api.dotGet = (obj,path)-> _.reduce path.split('.'), ((curr, next) => curr?[next]), obj +api.refPush = (reflist, item, prune=0) -> + item.sort = if _.isEmpty(reflist) then 0 else _.max(reflist,'sort').sort+1 + item.id = api.uuid() unless item.id and !reflist[item.id] + reflist[item.id] = item + api.planGemLimits = convRate: 20 #how much does a gem cost? convCap: 25 #how many gems can be converted / month? @@ -72,7 +77,6 @@ api.shouldDo = (day, repeat, options={}) -> yesterday = moment(o.now).subtract({days:1}).day() # have to wrap o.now so as not to modify original return repeat[api.dayMapping[yesterday]] # FIXME is this correct?? Do I need to do any timezone calcaulation here? - ### ------------------------------------------------------ Scoring @@ -604,10 +608,7 @@ api.wrap = (user, main=true) -> # ------ addWebhook: (req, cb) -> wh = user.preferences.webhooks - wh[req.body.id or api.uuid()] = - url: req.body.url - enabled: req.body.enabled or true - sort: if _.isEmpty(wh) then 0 else _.max(wh,'sort').sort+1 + api.refPush(wh, {url:req.body.url, enabled: req.body.enabled or true, id:req.body.id}) user.markModified? 'preferences.webhooks' cb? null, user.preferences.webhooks updateWebhook: (req, cb) ->