mass assignment on the server (@WIP)

This commit is contained in:
Tyler Renelle 2013-01-18 17:11:45 -05:00
parent 9083b5e6bf
commit 7f0d4ee1ec
3 changed files with 48 additions and 43 deletions

View file

@ -16,65 +16,68 @@ _ = require('lodash')
# ========== ROUTES ==========
get '/:uid?', (page, model, {uid}, next) ->
# Legacy - won't be allowing PURL auth in the future. Remove once password auth in place
# Creates stink here too because :uid accounts for every single-param path (terms, privacy, etc)
if uid
if require('guid').isGuid(uid)
return page.redirect('/users/'+uid)
else
return next()
get '/', (page, model, next) ->
# temporary view variables, so we don't call model.set() too fast
_view = model.get '_view' || {}
# Force SSL # NOTE handled by ngix now
#req = page._res.req
#if req.headers['x-forwarded-proto']!='https' and process.env.NODE_ENV=='production'
# return page.redirect 'https://' + req.headers.host + req.url
sess = model.session
model.set '_userId', sess.userId
model.subscribe "users.#{sess.userId}", (err, user) ->
userPath = "users.#{model.session.userId}"
model.fetch userPath, (err, userFetch) ->
# Set variables which are passed from the controller to the view
model.ref '_user', user
userObj = userFetch.get()
#FIXME remove this eventually, part of user schema
user.setNull 'balance', 2
userObj.balance = userObj.balance || 2
# support legacy Everyauth schema (now using derby-auth, Passport)
if username = user.get('auth.local.username')
model.set('_loginName', username)
else if fb = user.get('auth.facebook')
model.set('_loginName', if fb._raw then "#{fb.name.givenName} #{fb.name.familyName}" else fb.name)
if username = userObj.auth?.local?.username
_view.loginName = username
else if fb = userObj.auth.facebook
_view.loginName = if fb._raw then "#{fb.name.givenName} #{fb.name.familyName}" else fb.name
# Setup Item Store
model.set '_items'
armor: content.items.armor[parseInt(user.get('items.armor')) + 1]
weapon: content.items.weapon[parseInt(user.get('items.weapon')) + 1]
_view.items =
armor: content.items.armor[parseInt(userObj.items.armor) + 1]
weapon: content.items.weapon[parseInt(userObj.items.weapon) + 1]
potion: content.items.potion
reroll: content.items.reroll
# Setup Task Lists
model.refList "_habitList", "_user.tasks", "_user.habitIds"
model.refList "_dailyList", "_user.tasks", "_user.dailyIds"
model.refList "_todoList", "_user.tasks", "_user.todoIds"
model.refList "_completedList", "_user.tasks", "_user.completedIds"
model.refList "_rewardList", "_user.tasks", "_user.rewardIds"
# FIXME temporary hack to remove duplicates. Need to figure out why they're being produced
# FIXME temporary hack to remove duplicates and empty (grey) tasks. Need to figure out why they're being produced
taskIds = _.keys userObj.tasks
_.each ['habitIds','dailyIds','todoIds','rewardIds'], (path) ->
user.set path, _.uniq(user.get(path))
# Setup Model Functions
model.fn '_user._tnl', '_user.stats.lvl', (lvl) ->
# see https://github.com/lefnire/habitrpg/issues/4
# also update in scoring.coffee. TODO create a function accessible in both locations
(lvl*100)/5
original = userObj[path]
unique = _.uniq original #remove duplicates
preened = _.reject unique, (obj, key) -> #remove empty grey tasks
!_.contains(taskIds, key)
# userObj[path]= preened
# ========== Notifiations ==========
unless model.get('_user.notifications.kickstarter')
model.set('_user.notifications.kickstarter', 'show')
unless userObj.notifications.kickstarter
userObj.notifications.kickstarter = 'show'
# Render Page
page.render()
model.set '_view', _view
model.set "users.#{userObj.id}", userObj
model.subscribe userPath, (err, userSubscribe) ->
model.ref '_user', userSubscribe
# Setup Task Lists
model.refList "_habitList", "_user.tasks", "_user.habitIds"
model.refList "_dailyList", "_user.tasks", "_user.dailyIds"
model.refList "_todoList", "_user.tasks", "_user.todoIds"
model.refList "_completedList", "_user.tasks", "_user.completedIds"
model.refList "_rewardList", "_user.tasks", "_user.rewardIds"
# Setup Model Functions
model.fn '_user._tnl', '_user.stats.lvl', (lvl) ->
# see https://github.com/lefnire/habitrpg/issues/4
# also update in scoring.coffee. TODO create a function accessible in both locations
(lvl*100)/5
page.render()
# ========== CONTROLLER FUNCTIONS ==========

View file

@ -89,9 +89,11 @@ mongo_store = new MongoStore {url: process.env.NODE_DB_URI}, ->
# HabitRPG Custom Middleware
.use (req, res, next) ->
model = req.getModel()
_view = model.get('view') || {}
## Set _mobileDevice to true or false so view can exclude portions from mobile device
model.set '_mobileDevice', /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(req.header 'User-Agent')
model.set '_nodeEnv', process.env.NODE_ENV
_view.mobileDevice = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(req.header 'User-Agent')
_view.nodeEnv = process.env.NODE_ENV
model.set '_view', _view
next()
.use(auth(store, strategies, options))

View file

@ -154,7 +154,7 @@
<a class="btn btn-small btn-info" href="#login-modal" data-toggle="modal">Login / Register</a>
{else}
<div class="btn-group">
<button class="btn btn-small">{_loginName}</button>
<button class="btn btn-small">{_view.loginName}</button>
<button class="btn btn-small dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>