2013-02-05 05:20:58 +00:00
|
|
|
_ = require('underscore')
|
2013-02-05 21:11:28 +00:00
|
|
|
schema = require './schema'
|
2013-02-06 18:59:56 +00:00
|
|
|
browser = require './browser'
|
2013-02-05 06:37:13 +00:00
|
|
|
|
2013-02-08 00:13:14 +00:00
|
|
|
partyQ = null
|
|
|
|
|
membersQ = null
|
|
|
|
|
|
|
|
|
|
partyUnsubscribe = (model) ->
|
|
|
|
|
if window?
|
|
|
|
|
subs = model._subs()
|
|
|
|
|
model.unsubscribe.apply(model, subs)
|
|
|
|
|
|
|
|
|
|
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, res) ->
|
2013-02-06 06:11:12 +00:00
|
|
|
throw err if err
|
2013-02-08 00:13:14 +00:00
|
|
|
u = res.at(0)
|
|
|
|
|
uObj = u.get()
|
|
|
|
|
|
|
|
|
|
# If user not in a party, just send over that subscription
|
|
|
|
|
unless uObj.party?.current
|
|
|
|
|
model.ref '_user', u
|
|
|
|
|
return cb()
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
finished = (cb) ->
|
|
|
|
|
# Here's a hack we need to get fixed (hopefully Lever will) - 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()
|
|
|
|
|
model.subscribe selfQ, (err, users) ->
|
|
|
|
|
model.ref '_user', users.at(0)
|
|
|
|
|
browser.resetDom(model) if window?
|
|
|
|
|
cb() if cb?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ids = p.get('members')
|
|
|
|
|
# Party has no members, just subscribe to the party itself
|
|
|
|
|
if _.isEmpty(ids)
|
|
|
|
|
finished(cb)
|
|
|
|
|
|
|
|
|
|
# Party has members, subscribe to those users too
|
|
|
|
|
else
|
|
|
|
|
membersQ = model.query('users').party(ids)
|
|
|
|
|
membersQ.subscribe (err, m) ->
|
|
|
|
|
throw err if err
|
|
|
|
|
model.ref '_partyMembers', m
|
|
|
|
|
finished(cb)
|
2013-02-06 18:59:56 +00:00
|
|
|
|
2013-02-06 04:42:30 +00:00
|
|
|
|
2013-02-06 05:47:08 +00:00
|
|
|
module.exports.app = (appExports, model) ->
|
|
|
|
|
user = model.at('_user')
|
2013-02-06 01:21:05 +00:00
|
|
|
|
2013-02-08 00:13:14 +00:00
|
|
|
model.on 'set', '_user.party.invitation', -> partySubscribe(model)
|
2013-02-06 01:21:05 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyCreate = ->
|
2013-02-05 05:20:58 +00:00
|
|
|
newParty = model.get("_newParty")
|
2013-02-05 23:12:39 +00:00
|
|
|
id = model.add 'parties', { name: newParty, leader: user.get('id'), members: [user.get('id')], invites:[] }
|
2013-02-05 05:20:58 +00:00
|
|
|
user.set 'party', {current: id, invitation: null, leader: true}
|
2013-02-08 00:13:14 +00:00
|
|
|
partySubscribe model, -> $('#party-modal').modal('show')
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyInvite = ->
|
2013-02-05 05:20:58 +00:00
|
|
|
id = model.get('_newPartyMember').replace(/[\s"]/g, '')
|
|
|
|
|
return if _.isEmpty(id)
|
|
|
|
|
|
|
|
|
|
obj = user.get()
|
|
|
|
|
query = model.query('users').party([id])
|
2013-02-06 05:47:08 +00:00
|
|
|
model.fetch query, (err, res) ->
|
2013-02-05 21:11:28 +00:00
|
|
|
throw err if err
|
2013-02-06 05:47:08 +00:00
|
|
|
u = res.at(0).get()
|
|
|
|
|
if !u?.id?
|
2013-02-05 05:20:58 +00:00
|
|
|
model.set "_view.partyError", "User with id #{id} not found."
|
|
|
|
|
return
|
2013-02-06 05:47:08 +00:00
|
|
|
else if u.party.current? or u.party.invitation?
|
2013-02-05 05:20:58 +00:00
|
|
|
model.set "_view.partyError", "User already in a party or pending invitation."
|
|
|
|
|
return
|
|
|
|
|
else
|
2013-02-06 05:15:00 +00:00
|
|
|
p = model.at '_party'
|
|
|
|
|
p.push "invites", id
|
|
|
|
|
model.set "users.#{id}.party.invitation", p.get('id')
|
2013-02-05 05:20:58 +00:00
|
|
|
$.bootstrapGrowl "Invitation Sent."
|
|
|
|
|
$('#party-modal').modal('hide')
|
2013-02-05 23:12:39 +00:00
|
|
|
model.set '_newPartyMember', ''
|
2013-02-08 00:13:14 +00:00
|
|
|
partySubscribe model
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyAccept = ->
|
2013-02-08 00:13:14 +00:00
|
|
|
user.set 'party.current', user.get('party.invitation')
|
|
|
|
|
user.set 'party.invitation', null
|
|
|
|
|
partySubscribe model, ->
|
|
|
|
|
p = model.at('_party')
|
2013-02-06 05:15:00 +00:00
|
|
|
p.push 'members', user.get('id')
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyReject = ->
|
2013-02-05 05:20:58 +00:00
|
|
|
user.set 'party.invitation', null
|
2013-02-06 18:59:56 +00:00
|
|
|
browser.resetDom(model)
|
2013-02-05 21:11:28 +00:00
|
|
|
# TODO splice parties.*.invites[key]
|
2013-02-05 05:20:58 +00:00
|
|
|
# TODO notify sender
|
|
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyLeave = ->
|
2013-02-05 23:52:29 +00:00
|
|
|
id = user.set 'party.current', null
|
2013-02-06 05:15:00 +00:00
|
|
|
p = model.at '_party'
|
|
|
|
|
members = p.get('members')
|
2013-02-05 23:12:39 +00:00
|
|
|
index = members.indexOf(user.get('id'))
|
2013-02-06 03:27:19 +00:00
|
|
|
members.splice(index,1)
|
2013-02-06 05:15:00 +00:00
|
|
|
p.set 'members', members
|
2013-02-05 23:23:56 +00:00
|
|
|
if (members.length == 0)
|
2013-02-05 23:12:39 +00:00
|
|
|
# last member out, kill the party
|
2013-02-05 23:52:29 +00:00
|
|
|
model.del "parties.#{id}"
|
2013-02-08 00:13:14 +00:00
|
|
|
model.set '_party', null
|
|
|
|
|
model.set '_partyMembers', null
|
|
|
|
|
partyUnsubscribe model
|
|
|
|
|
selfQ = model.query('users').withId(model.get('_userId') or model.session.userId)
|
|
|
|
|
selfQ.subscribe (err, u) ->
|
|
|
|
|
model.ref '_user', u.at(0)
|
|
|
|
|
browser.resetDom model
|
|
|
|
|
#setTimeout (-> window.location.reload true), 1
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
#exports.partyDisband = ->
|