overleaf/services/web/public/coffee/ide/chat/services/chatMessages.coffee

101 lines
2.9 KiB
CoffeeScript
Raw Normal View History

2014-07-15 13:25:12 -04:00
define [
"base"
], (App) ->
App.factory "chatMessages", ($http, ide) ->
MESSAGES_URL = "/project/#{ide.project_id}/messages"
MESSAGE_LIMIT = 50
2014-07-15 13:25:12 -04:00
CONNECTED_USER_URL = "/project/#{ide.project_id}/connected_users"
chat = {
state:
messages: []
loading: false
atEnd: false
errored:false
2014-07-15 13:25:12 -04:00
nextBeforeTimestamp: null
2014-07-17 06:05:08 -04:00
newMessage: null
2014-07-15 13:25:12 -04:00
}
2014-07-17 08:12:34 -04:00
justSent = false
2014-07-15 13:25:12 -04:00
ide.socket.on "new-chat-message", (message) =>
if message?.user?.id == ide.$scope.user.id and justSent
2014-07-17 08:12:34 -04:00
# Nothing to do
else
ide.$scope.$apply () ->
appendMessage(message)
justSent = false
chat.sendMessage = (message) ->
body =
content: message
_csrf : window.csrfToken
justSent = true
appendMessage({
user: ide.$scope.user
content: message
timestamp: Date.now()
})
return $http.post(MESSAGES_URL, body)
2014-07-15 13:25:12 -04:00
chat.loadMoreMessages = () ->
return if chat.state.atEnd
return if chat.state.errored
2014-07-15 13:25:12 -04:00
url = MESSAGES_URL + "?limit=#{MESSAGE_LIMIT}"
if chat.state.nextBeforeTimestamp?
url += "&before=#{chat.state.nextBeforeTimestamp}"
chat.state.loading = true
return $http
.get(url)
.success (messages = [])->
chat.state.loading = false
if messages.length < MESSAGE_LIMIT
chat.state.atEnd = true
if !messages.reverse?
Raven?.captureException(new Error("messages has no reverse property #{typeof(messages)}"))
if typeof messages.reverse isnt 'function'
Raven?.captureException(new Error("messages.reverse not a function #{typeof(messages.reverse)} #{typeof(messages)}"))
chat.state.errored = true
else
messages.reverse()
prependMessages(messages)
chat.state.nextBeforeTimestamp = chat.state.messages[0]?.timestamp
2014-07-15 13:25:12 -04:00
TIMESTAMP_GROUP_SIZE = 5 * 60 * 1000 # 5 minutes
prependMessage = (message) ->
firstMessage = chat.state.messages[0]
shouldGroup = firstMessage? and
firstMessage.user.id == message?.user?.id and
2014-07-15 13:25:12 -04:00
firstMessage.timestamp - message.timestamp < TIMESTAMP_GROUP_SIZE
if shouldGroup
firstMessage.timestamp = message.timestamp
firstMessage.contents.unshift message.content
else
chat.state.messages.unshift({
user: message.user
timestamp: message.timestamp
contents: [message.content]
})
prependMessages = (messages) ->
for message in messages.slice(0).reverse()
prependMessage(message)
appendMessage = (message) ->
2014-07-17 08:12:34 -04:00
chat.state.newMessage = message
2014-07-15 13:25:12 -04:00
lastMessage = chat.state.messages[chat.state.messages.length - 1]
shouldGroup = lastMessage? and
lastMessage.user.id == message?.user?.id and
2014-07-15 13:25:12 -04:00
message.timestamp - lastMessage.timestamp < TIMESTAMP_GROUP_SIZE
if shouldGroup
lastMessage.timestamp = message.timestamp
lastMessage.contents.push message.content
else
chat.state.messages.push({
user: message.user
timestamp: message.timestamp
contents: [message.content]
})
2014-07-15 13:25:12 -04:00
return chat