mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-05 03:52:14 +00:00
move the subscriptions setup into index.coffee
This commit is contained in:
parent
2ebde9bc1a
commit
da4738ac1a
2 changed files with 37 additions and 52 deletions
|
|
@ -20,8 +20,6 @@ helpers.viewHelpers view
|
|||
|
||||
_ = require('underscore')
|
||||
|
||||
# ========== ROUTES ==========
|
||||
|
||||
###
|
||||
Cleanup task-corruption (null tasks, rogue/invisible tasks, etc)
|
||||
Obviously none of this should be happening, but we'll stop-gap until we can find & fix
|
||||
|
|
@ -61,28 +59,58 @@ cleanupCorruptTasks = (model) ->
|
|||
|
||||
batch.commit() if batch?
|
||||
|
||||
###
|
||||
Subscribe to the user, the users's party (meta info like party name, member ids, etc), and the party's members. 3 subscriptions.
|
||||
###
|
||||
setupSubscriptions = (page, model, params, next, cb) ->
|
||||
uuid = model.get('_userId') or model.session.userId # see http://goo.gl/TPYIt
|
||||
selfQ = model.query('users').withId(uuid) #keep this for later
|
||||
partyQ = model.query('parties').withMember(uuid)
|
||||
|
||||
partyQ.fetch (err, party) ->
|
||||
return next(err) if err
|
||||
|
||||
finished = (descriptors, paths) ->
|
||||
model.subscribe.apply model, descriptors.concat ->
|
||||
[err, refs] = [arguments[0], arguments]
|
||||
return next(err) if err
|
||||
_.each paths, (path, idx) -> model.ref path, refs[idx+1]
|
||||
unless model.get('_user')
|
||||
console.error "User not found - this shouldn't be happening!"
|
||||
return page.redirect('/logout') #delete model.session.userId
|
||||
return cb()
|
||||
|
||||
party = party.get()
|
||||
|
||||
# (1) Solo player
|
||||
return finished([selfQ, 'tavern'], ['_user', '_tavern']) unless party
|
||||
|
||||
## (2) Party has members, subscribe to those users too
|
||||
membersQ = model.query('users').party(party.members)
|
||||
# Note - selfQ *must* come after membersQ in subscribe, otherwise _user will only get the fields restricted by party-members in
|
||||
# store.coffee. Strang bug, but easy to get around
|
||||
return finished [partyQ, membersQ, selfQ, 'tavern'], ['_party', '_partyMembers', '_user', '_tavern']
|
||||
|
||||
# ========== ROUTES ==========
|
||||
|
||||
get '/', (page, model, params, next) ->
|
||||
return page.redirect '/' if page.params?.query?.play?
|
||||
|
||||
# removed force-ssl (handled in nginx), see git for code
|
||||
|
||||
require('./party').partySubscribe page, model, params, next, ->
|
||||
model.setNull '_user.apiToken', derby.uuid()
|
||||
|
||||
setupSubscriptions page, model, params, next, ->
|
||||
cleanupCorruptTasks(model) # https://github.com/lefnire/habitrpg/issues/634
|
||||
|
||||
require('./items').server(model)
|
||||
|
||||
#refLists
|
||||
_.each ['habit', 'daily', 'todo', 'reward'], (type) ->
|
||||
model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids"
|
||||
|
||||
page.render()
|
||||
|
||||
|
||||
# ========== CONTROLLER FUNCTIONS ==========
|
||||
|
||||
ready (model) ->
|
||||
user = model.at('_user')
|
||||
model.setNull '_user.apiToken', derby.uuid()
|
||||
|
||||
#set cron immediately
|
||||
lastCron = user.get('lastCron')
|
||||
|
|
|
|||
|
|
@ -1,49 +1,6 @@
|
|||
_ = require('underscore')
|
||||
helpers = require './helpers'
|
||||
|
||||
partyUnsubscribe = (model, cb) ->
|
||||
if window?
|
||||
throw "unsubscribe requires cb" unless cb?
|
||||
subs = model._subs()
|
||||
subs.concat cb
|
||||
model.unsubscribe.apply(model, subs)
|
||||
else
|
||||
cb()
|
||||
|
||||
###
|
||||
Subscribe to the user, the users's party (meta info like party name, member ids, etc), and the party's members. 3 subscriptions.
|
||||
|
||||
Note - selfQ *must* come after membersQ in subscribe, otherwise _user will only get the fields restricted by party-members in
|
||||
store.coffee. Strang bug, but easy to get around
|
||||
###
|
||||
module.exports.partySubscribe = partySubscribe = (page, model, params, next, cb) ->
|
||||
|
||||
uuid = model.get('_userId') or model.session.userId # see http://goo.gl/TPYIt
|
||||
selfQ = model.query('users').withId(uuid) #keep this for later
|
||||
partyQ = model.query('parties').withMember(uuid)
|
||||
|
||||
partyQ.fetch (err, party) ->
|
||||
return next(err) if err
|
||||
|
||||
finished = (descriptors, paths) ->
|
||||
model.subscribe.apply model, descriptors.concat ->
|
||||
[err, refs] = [arguments[0], arguments]
|
||||
return next(err) if err
|
||||
_.each paths, (path, idx) -> model.ref path, refs[idx+1]
|
||||
unless model.get('_user')
|
||||
console.error "User not found - this shouldn't be happening!"
|
||||
return page.redirect('/logout') #delete model.session.userId
|
||||
cb()
|
||||
|
||||
party = party.get()
|
||||
|
||||
# (1) Solo player
|
||||
return finished([selfQ, 'tavern'], ['_user', '_tavern']) unless party
|
||||
|
||||
## (2) Party has members, subscribe to those users too
|
||||
membersQ = model.query('users').party(party.members)
|
||||
return finished [partyQ, membersQ, selfQ, 'tavern'], ['_party', '_partyMembers', '_user', '_tavern']
|
||||
|
||||
module.exports.app = (appExports, model, app) ->
|
||||
character = require './character'
|
||||
browser = require './browser'
|
||||
|
|
|
|||
Loading…
Reference in a new issue