118 lines
2.8 KiB
CoffeeScript
118 lines
2.8 KiB
CoffeeScript
$ = jQuery
|
|
context = window
|
|
logger = context.JK.logger
|
|
|
|
@ChatStore = Reflux.createStore(
|
|
{
|
|
listenables: @ChatActions
|
|
|
|
channel: 'global',
|
|
msgs: {global:[], session:[]}
|
|
|
|
init: () ->
|
|
# Register with the app store to get @app
|
|
this.listenTo(context.AppStore, this.onAppInit)
|
|
|
|
onAppInit: (@app) ->
|
|
#$(document).on(EVENTS.SESSION_STARTED, ((e, data) =>
|
|
#@sessionStarted(e, data);
|
|
# return false
|
|
#))
|
|
|
|
# $(context.AppStore).on('SessionStarted', @onSessionStarted)
|
|
|
|
# called from ChatPanel
|
|
onSessionStarted: () ->
|
|
logger.debug("ChatStore: onSessionStarted", this)
|
|
@msgs['session'] = []
|
|
@channel = 'session'
|
|
@onEmptyChannel(@channel)
|
|
|
|
onEmptyChannel: (channel) ->
|
|
@msgs[channel] = []
|
|
@changed()
|
|
|
|
convertServerMessages: (chats) ->
|
|
converted = []
|
|
for chat in chats
|
|
convert = {}
|
|
convert.sender_name = chat.user?.name
|
|
convert.sender_id = chat.user_id
|
|
convert.msg = chat.message
|
|
convert.msg_id = chat.id
|
|
convert.created_at = chat.created_at
|
|
convert.channel = chat.channel
|
|
converted
|
|
|
|
# called from ChatPanel
|
|
onLoadMessages: (channel, msgs) ->
|
|
channelMsgs = @msgs[channel]
|
|
|
|
if !channelMsgs?
|
|
channelMsgs = []
|
|
@msgs[channel] = channelMsgs
|
|
|
|
history = @convertServerMessages(msgs.chats)
|
|
|
|
@msgs[channel] = history.concat(channelMsgs)
|
|
@changed()
|
|
|
|
onMsgReceived: (msg) ->
|
|
logger.debug("ChatStore.msgReceived", msg)
|
|
|
|
channelMsgs = @msgs[msg.channel]
|
|
|
|
if !channelMsgs?
|
|
channelMsgs = []
|
|
@msgs[msg.channel] = channelMsgs
|
|
|
|
channelMsgs.push(msg)
|
|
@changed()
|
|
|
|
buildMessage:(msg) ->
|
|
payload = {message: msg}
|
|
if @channel == 'session'
|
|
payload.music_session = SessionStore.currentSessionId
|
|
payload.channel = @channel
|
|
payload.client_id = @app.clientId
|
|
payload
|
|
|
|
onActivateChannel: (channel) ->
|
|
@channel = channel
|
|
@changed()
|
|
|
|
onSendMsg: (msg, done, fail) ->
|
|
rest.createChatMessage(@buildMessage(msg))
|
|
.done((response) =>
|
|
|
|
done(response)
|
|
|
|
#console.log("ON SEND MESSAGE SIMULATE", response)
|
|
|
|
@onMsgReceived({
|
|
sender_name: "me",
|
|
sender_id: context.JK.currentUserId,
|
|
msg: msg,
|
|
msg_id: response.id,
|
|
created_at: response.created_at,
|
|
channel: @channel
|
|
})
|
|
)
|
|
.fail((jqXHR) =>
|
|
fail(jqXHR)
|
|
)
|
|
|
|
# unused/untested. send direct to gateway
|
|
onSendMsgInstant: (msg) ->
|
|
logger.debug("ChatStore.sendMsg", msg)
|
|
|
|
window.JK.JamServer.sendChatMessage(@channel, msg)
|
|
|
|
getState: () ->
|
|
return {msgs: @msgs, channel: @channel}
|
|
|
|
changed: () ->
|
|
@trigger(@getState())
|
|
}
|
|
)
|