clean up .get('/') by moving user updates & cleanup to schema.coffee

This commit is contained in:
Tyler Renelle 2013-01-30 15:50:54 -05:00
parent e0f1d74541
commit cd331dbca5
3 changed files with 33 additions and 26 deletions

View file

@ -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)

View file

@ -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
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])

View file

@ -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