_ = 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 info like party name, member ids, etc), and the party's members. 3 subscriptions. 1) If the user is solo, just subscribe to the user. 2) If in a an empty party, just subscribe to the user & party meta. 3) If full party, subscribe to everything. ### 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 # see http://goo.gl/TPYIt selfQ.subscribe (err, self) -> throw err if err u = self.at(0) uObj = u.get() ## (1) User is solo, just return that subscription unless uObj.party?.current? model.ref '_user', u return cb() ### Note this strange hack - we subscribe to queries incrementally. First self, then party, then party members. Party members come with limited fields, so you can't hack their stuff. Strangely, subscribing to the members after already subscribing to self limits self's fields to the fields which members are limited to. As a result, we have to re-subscribe to self to get all the fields (otherwise everything breaks). Weirdly, this last subscription doesn't do the opposite - granting all the fields back to members. I dont' know what's going on here Another issue: `model.unsubscribe(selfQ)` would seem to mitigate the above, so we at least don't have a stray subscription floating around - but alas, it doesn't seem to work (or at least never calls the callback) ### finished = -> # model.unsubscribe selfQ, -> selfQ.subscribe (err, self) -> model.ref '_user', self.at(0) cb() # User in a party partiesQ = model.query('parties').withId(uObj.party.current) partiesQ.fetch (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.fetch (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]