2014-01-03 13:32:13 -05:00
|
|
|
{backboneFailure, genericFailure} = require 'lib/errors'
|
|
|
|
User = require 'models/User'
|
2014-01-26 17:44:08 -05:00
|
|
|
storage = require 'lib/storage'
|
2014-01-03 13:32:13 -05:00
|
|
|
BEEN_HERE_BEFORE_KEY = 'beenHereBefore'
|
|
|
|
|
2014-04-02 16:12:24 -04:00
|
|
|
init = ->
|
|
|
|
module.exports.me = window.me = new User(window.userObject) # inserted into main.html
|
|
|
|
trackFirstArrival()
|
|
|
|
if me and not me.get('testGroupNumber')?
|
|
|
|
# Assign testGroupNumber to returning visitors; new ones in server/routes/auth
|
|
|
|
me.set 'testGroupNumber', Math.floor(Math.random() * 256)
|
|
|
|
me.save()
|
|
|
|
|
|
|
|
Backbone.listenTo(me, 'sync', Backbone.Mediator.publish('me:synced', {me:me}))
|
|
|
|
|
2014-03-31 16:56:13 -04:00
|
|
|
module.exports.createUser = (userObject, failure=backboneFailure, nextURL=null) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
user = new User(userObject)
|
|
|
|
user.save({}, {
|
2014-04-02 16:12:24 -04:00
|
|
|
error: failure,
|
|
|
|
success: -> if nextURL then window.location.href = nextURL else window.location.reload()
|
2014-01-03 13:32:13 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
module.exports.loginUser = (userObject, failure=genericFailure) ->
|
|
|
|
jqxhr = $.post('/auth/login',
|
|
|
|
{
|
|
|
|
username:userObject.email,
|
|
|
|
password:userObject.password
|
|
|
|
},
|
2014-04-02 16:12:24 -04:00
|
|
|
(model) -> window.location.reload()
|
2014-01-03 13:32:13 -05:00
|
|
|
)
|
|
|
|
jqxhr.fail(failure)
|
|
|
|
|
|
|
|
module.exports.logoutUser = ->
|
2014-01-09 13:48:51 -05:00
|
|
|
FB?.logout?()
|
2014-04-02 16:12:24 -04:00
|
|
|
res = $.post('/auth/logout', {}, -> window.location.reload())
|
2014-01-03 13:32:13 -05:00
|
|
|
res.fail(genericFailure)
|
|
|
|
|
|
|
|
onSetVolume = (e) ->
|
|
|
|
return if e.volume is me.get('volume')
|
|
|
|
me.set('volume', e.volume)
|
|
|
|
me.save()
|
|
|
|
|
|
|
|
Backbone.Mediator.subscribe('level-set-volume', onSetVolume, module.exports)
|
|
|
|
|
|
|
|
trackFirstArrival = ->
|
|
|
|
# will have to filter out users who log in with existing accounts separately
|
|
|
|
# but can at least not track logouts as first arrivals using local storage
|
2014-01-26 17:44:08 -05:00
|
|
|
beenHereBefore = storage.load(BEEN_HERE_BEFORE_KEY)
|
2014-01-03 13:32:13 -05:00
|
|
|
return if beenHereBefore
|
2014-01-06 20:45:35 -05:00
|
|
|
window.tracker?.trackEvent 'First Arrived'
|
2014-01-26 17:44:08 -05:00
|
|
|
storage.save(BEEN_HERE_BEFORE_KEY, true)
|
2014-04-02 16:12:24 -04:00
|
|
|
|
|
|
|
init()
|