175 lines
4.4 KiB
CoffeeScript
175 lines
4.4 KiB
CoffeeScript
$ = jQuery
|
|
context = window
|
|
logger = context.JK.logger
|
|
SessionStore = context.SessionStore
|
|
|
|
@ChatStore = Reflux.createStore(
|
|
{
|
|
listenables: @ChatActions
|
|
|
|
limit: 20,
|
|
currentPage: 0,
|
|
next: null,
|
|
channel: 'global',
|
|
systemMsgId: 0
|
|
msgs: {global:[], session:[]}
|
|
|
|
init: () ->
|
|
# Register with the app store to get @app
|
|
this.listenTo(context.AppStore, this.onAppInit)
|
|
this.listenTo(context.UserActivityStore, this.onUserActivityChanged)
|
|
|
|
onAppInit: (@app) ->
|
|
if context.JK.currentUserId?
|
|
@fetchHistory()
|
|
|
|
onUserActivityChanged: (state) ->
|
|
systemMsg = {}
|
|
@systemMsgId = @systemMsgId + 1
|
|
systemMsg.sender_name = 'System'
|
|
systemMsg.sender_id = 'system'
|
|
systemMsg.msg_id = @systemMsgId
|
|
systemMsg.created_at = new Date().toISOString()
|
|
systemMsg.channel = 'global'
|
|
if state.active
|
|
systemMsg.msg = "You've come back!"
|
|
else
|
|
systemMsg.msg = "You've become inactive. Any global chat messages while away will be missed."
|
|
|
|
@onMsgReceived(systemMsg)
|
|
|
|
# after the animation to open the chat panel is done, do it!
|
|
onFullyOpened: () ->
|
|
@changed()
|
|
|
|
# called from ChatPanel
|
|
onSessionStarted: () ->
|
|
@msgs['session'] = []
|
|
@channel = 'session'
|
|
@fetchHistory()
|
|
@onEmptyChannel(@channel)
|
|
|
|
buildQuery: () ->
|
|
query = {type: 'CHAT_MESSAGE', limit:@limit, page: @currentPage, channel: @channel};
|
|
|
|
if @channel == 'session' && SessionStore.currentSessionId?
|
|
query.music_session = SessionStore.currentSessionId
|
|
|
|
if @next
|
|
query.start = next
|
|
return query
|
|
|
|
fetchHistory: () ->
|
|
# load previous chat messages
|
|
rest.getChatMessages(@buildQuery())
|
|
.done((response) =>
|
|
@onLoadMessages(@channel, response)
|
|
).fail((jqXHR) =>
|
|
@app.notifyServerError(jqXHR, 'Unable to Load Session Conversations')
|
|
)
|
|
|
|
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.push(convert)
|
|
converted
|
|
|
|
# called from ChatPanel
|
|
onLoadMessages: (channel, msgs) ->
|
|
channelMsgs = @msgs[channel]
|
|
|
|
if !channelMsgs?
|
|
channelMsgs = []
|
|
@msgs[channel] = channelMsgs
|
|
|
|
history = @convertServerMessages(msgs.chats)
|
|
|
|
for oldMsg in history
|
|
skip = false
|
|
for channelMsg in channelMsgs
|
|
if oldMsg.msg_id == channelMsg.msg_id
|
|
skip = true
|
|
break
|
|
|
|
if !skip
|
|
channelMsgs.unshift(oldMsg)
|
|
|
|
#totalMsgs = history.concat(channelMsgs)
|
|
channelMsgs.sort((a, b) =>
|
|
c = new Date(a.created_at)
|
|
d = new Date(b.created_at)
|
|
return (c > d) - (c < d)
|
|
)
|
|
@msgs[channel] = channelMsgs
|
|
|
|
@changed()
|
|
|
|
onMsgReceived: (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
|
|
@fetchHistory()
|
|
@changed()
|
|
|
|
onSendMsg: (msg, done, fail) ->
|
|
rest.createChatMessage(@buildMessage(msg))
|
|
.done((response) =>
|
|
|
|
done(response)
|
|
|
|
if response.channel == 'session'
|
|
@onMsgReceived({
|
|
sender_name: "me",
|
|
sender_id: context.JK.currentUserId,
|
|
msg: msg,
|
|
msg_id: response.id,
|
|
created_at: response.created_at,
|
|
channel: response.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())
|
|
}
|
|
)
|