diff --git a/src/app/index.coffee b/src/app/index.coffee index cc8ab91734..9eeb2a7dfe 100644 --- a/src/app/index.coffee +++ b/src/app/index.coffee @@ -71,28 +71,7 @@ get '/', (page, model, next) -> model.set '_view', _view - ## Task List Cleanup - # FIXME temporary hack to fix lists (Need to figure out why these are happening) - # FIXME consolidate these all under user.listIds so we can set them en-masse - _.each ['habit','daily','todo','reward'], (type) -> - path = "#{type}Ids" - - # 1. remove duplicates - # 2. restore missing zombie tasks back into list - where = {type:type} - taskIds = _.pluck( _.where(userObj.tasks, where), 'id') - union = _.union userObj[path], taskIds - - # 2. remove empty (grey) tasks - preened = _.filter(union, (val) -> _.contains(taskIds, val)) - - # There were indeed issues found, set the new list - user.set(path, preened) if _.size(preened) != _.size(userObj[path]) - - ## Notifiations - unless userObj.notifications?.kickstarter? - user.set('notifications.kickstarter', 'show') - + schema.updateUser(user, userObj) setupListReferences(model) setupModelFns(model) diff --git a/src/app/schema.coffee b/src/app/schema.coffee index f60700673f..f591bfee85 100644 --- a/src/app/schema.coffee +++ b/src/app/schema.coffee @@ -1,21 +1,22 @@ content = require './content' moment = require 'moment' _ = require 'underscore' +derby = require 'derby' -module.exports.userSchema = -> +module.exports.newUserObject = -> # deep clone, else further new users get duplicate objects newUser = require('lodash').cloneDeep lastCron: 'new' #this will be replaced with `+new Date` on first run balance: 2 stats: { money: 0, exp: 0, lvl: 1, hp: 50 } items: { itemsEnabled: false, armor: 0, weapon: 0 } + notifications: { kickstarter: 'show' } preferences: { gender: 'm', armorSet: 'v1' } tasks: {} habitIds: [] dailyIds: [] todoIds: [] rewardIds: [] - for task in content.defaultTasks guid = task.id = require('racer').uuid() newUser.tasks[guid] = task @@ -24,4 +25,31 @@ module.exports.userSchema = -> when 'daily' then newUser.dailyIds.push guid when 'todo' then newUser.todoIds.push guid when 'reward' then newUser.rewardIds.push guid - return newUser \ No newline at end of file + return newUser + +module.exports.updateUser = (user, userObj) -> + user.set 'notifications.kickstarter', 'show' unless userObj.notifications?.kickstarter? + + # Preferences, including API key + # Some side-stepping to avoid unecessary set (one day, model.update... one day..) + prefs = _.clone(userObj.preferences) + _.defaults prefs, { gender: 'm', armorSet: 'v1', api_token: derby.uuid() } + user.set 'preferences', prefs unless _.isEqual(prefs, userObj.preferences) + + ## Task List Cleanup + # FIXME temporary hack to fix lists (Need to figure out why these are happening) + # FIXME consolidate these all under user.listIds so we can set them en-masse + _.each ['habit','daily','todo','reward'], (type) -> + path = "#{type}Ids" + + # 1. remove duplicates + # 2. restore missing zombie tasks back into list + where = {type:type} + taskIds = _.pluck( _.where(userObj.tasks, where), 'id') + union = _.union userObj[path], taskIds + + # 2. remove empty (grey) tasks + preened = _.filter(union, (val) -> _.contains(taskIds, val)) + + # There were indeed issues found, set the new list + user.set(path, preened) if _.size(preened) != _.size(userObj[path]) \ No newline at end of file diff --git a/src/server/index.coffee b/src/server/index.coffee index fb7d9ac9c5..1ac2071144 100644 --- a/src/server/index.coffee +++ b/src/server/index.coffee @@ -47,7 +47,7 @@ strategies = options = domain: process.env.BASE_URL || 'http://localhost:3000' allowPurl: true - schema: require('../app/schema').userSchema() + schema: require('../app/schema').newUserObject() mongo_store = new MongoStore {url: process.env.NODE_DB_URI}, -> expressApp