2013-02-05 05:20:58 +00:00
|
|
|
_ = require('underscore')
|
2013-02-05 21:11:28 +00:00
|
|
|
schema = require './schema'
|
2013-02-05 06:37:13 +00:00
|
|
|
|
2013-02-06 05:15:00 +00:00
|
|
|
_subscriptions =
|
|
|
|
|
party:
|
|
|
|
|
query: null
|
|
|
|
|
id: null
|
|
|
|
|
members:
|
|
|
|
|
query: null
|
|
|
|
|
ids: null
|
2013-02-06 04:42:30 +00:00
|
|
|
|
2013-02-06 05:15:00 +00:00
|
|
|
module.exports.partySubscribe = partySubscribe = (model, id, cb) ->
|
|
|
|
|
# New subscription coming in, or reset subscription with new parameters
|
|
|
|
|
if true #!_subscriptions.party.query? or _subscriptions.party.id != id
|
|
|
|
|
_subscriptions.party.query = model.query('parties').withId(id)
|
|
|
|
|
_subscriptions.party.id = id
|
|
|
|
|
_subscriptions.party.query.subscribe (err, p) ->
|
|
|
|
|
throw err if err
|
|
|
|
|
model.ref '_party', p.at(0)
|
|
|
|
|
cb(p.at(0)) if cb?
|
|
|
|
|
else
|
|
|
|
|
cb(model.at('_party')) if cb?
|
2013-02-06 04:42:30 +00:00
|
|
|
|
2013-02-06 05:15:00 +00:00
|
|
|
module.exports.membersSubscribe = membersSubscribe = (model, ids, cb) ->
|
|
|
|
|
# New subscription coming in, or reset subscription with new parameters
|
|
|
|
|
if true #!_subscriptions.members.query? or !_.isEmpty(_.difference(_subscriptions.members.ids, ids))
|
|
|
|
|
_subscriptions.members.query = model.query('users').party(ids)
|
|
|
|
|
_subscriptions.members.ids = ids
|
|
|
|
|
_subscriptions.members.query.subscribe (err, m) ->
|
|
|
|
|
throw err if err
|
|
|
|
|
model.ref '_partyMembers', m
|
|
|
|
|
cb(m) if cb?
|
|
|
|
|
else
|
|
|
|
|
cb(model.at('_partyMembers')) if cb?
|
2013-02-06 04:42:30 +00:00
|
|
|
|
2013-02-06 01:21:05 +00:00
|
|
|
setupListeners = (model) ->
|
|
|
|
|
|
|
|
|
|
model.on 'set', '_user.party.invitation', (id) ->
|
2013-02-06 05:15:00 +00:00
|
|
|
partySubscribe model, id
|
2013-02-06 01:21:05 +00:00
|
|
|
|
|
|
|
|
model.on '*', '_party.members', (ids) ->
|
2013-02-06 05:15:00 +00:00
|
|
|
membersSubscribe model, ids
|
2013-02-06 01:21:05 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
module.exports.app = (appExports, model) ->
|
2013-02-05 05:20:58 +00:00
|
|
|
user = model.at('_user')
|
2013-02-06 01:21:05 +00:00
|
|
|
setupListeners(model)
|
2013-02-05 05:20:58 +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-06 05:15:00 +00:00
|
|
|
partySubscribe model, id
|
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])
|
|
|
|
|
model.fetch query, (err, users) ->
|
2013-02-05 21:11:28 +00:00
|
|
|
throw err if err
|
2013-02-05 05:20:58 +00:00
|
|
|
partyMember = users.at(0).get()
|
|
|
|
|
if !partyMember?.id?
|
|
|
|
|
model.set "_view.partyError", "User with id #{id} not found."
|
|
|
|
|
return
|
|
|
|
|
else if partyMember.party.current?.id? or partyMember.party.invitation?
|
|
|
|
|
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-06 05:15:00 +00:00
|
|
|
membersSubscribe model, p.get('members')
|
2013-02-05 23:12:39 +00:00
|
|
|
model.set '_newPartyMember', ''
|
|
|
|
|
#TODO break old subscription, setup new subscript, remove this reload
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyAccept = ->
|
2013-02-05 05:20:58 +00:00
|
|
|
invitation = user.get('party.invitation')
|
2013-02-06 05:15:00 +00:00
|
|
|
partySubscribe model, invitation, (p) ->
|
|
|
|
|
p.push 'members', user.get('id')
|
2013-02-05 23:12:39 +00:00
|
|
|
user.set 'party.invitation', null
|
2013-02-06 05:15:00 +00:00
|
|
|
user.set 'party.current', p.get('id')
|
|
|
|
|
membersSubscribe model, p.get('members'), (m)
|
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-05 23:12:39 +00:00
|
|
|
|
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-06 05:15:00 +00:00
|
|
|
_subscriptions.party.query.unsubscribe()
|
2013-02-05 23:52:29 +00:00
|
|
|
model.set('_party', null)
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
#exports.partyDisband = ->
|