_ = require('underscore') character = require './character' browser = require './browser' 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), and the party's members. 3 subscriptions. If the user is solo, just subscribe to the user. If in a an empty party, just subscribe to the party. If full party, subscribe to everything. Note a strange hack - later model.queries override previous model.queries' returned fields. Aka, we need this here otherwise we only get the "public" fields for the current user, which are defined in model.query('users').party(). So we need to subscribe to the main user last ### module.exports.partySubscribe = partySubscribe = (model, cb) -> # unsubscribe from everything - we're starting over # partyUnsubscribe model, -> # Restart subscription to the main user selfQ = model.query('users').withId(model.get('_userId') or model.session.userId) selfQ.subscribe (err, self) -> throw err if err u = self.at(0) uObj = u.get() finished = -> model.unsubscribe selfQ, -> selfQ.subscribe (err, self) -> model.ref '_user', self.at(0) browser.resetDom(model) if window? cb() if cb? ## (1) User is solo, just return that subscription unless uObj.party?.current model.ref '_user', u browser.resetDom(model) if window? return if cb then cb() else null # User in a party partiesQ = model.query('parties').withId(uObj.party.current) partiesQ.subscribe (err, res) -> throw err if err p = res.at(0) model.ref '_party', p ids = p.get('members') # FIXME this is the kicker right here. This isn't getting triggered, and it's the reason why we have to refresh # after every event. Get this working #p.on '*', 'members', (ids) -> # console.log("members listener got called") # debugger # membersSubscribe model, ids ## (2) Party has no members, just subscribe to the party itself if _.isEmpty(ids) return finished() else ## (3) Party has members, subscribe to those users too membersQ = model.query('users').party(ids) membersQ.subscribe (err, members) -> throw err if err model.ref '_partyMembers', members finished() module.exports.app = (appExports, model) -> user = model.at('_user') user.on 'set', 'flags.partyEnabled', (captures, args) -> return unless captures == true $('.main-avatar').popover('destroy') #remove previous popovers html = """
Congratulations, you have unlocked the Party System! You can now group with your friends by adding their User Ids.
[Close]