2014-08-15 05:50:36 -04:00
|
|
|
module.exports = MessageFormatter =
|
|
|
|
formatMessageForClientSide: (message) ->
|
|
|
|
if message._id?
|
|
|
|
message.id = message._id.toString()
|
|
|
|
delete message._id
|
|
|
|
formattedMessage =
|
|
|
|
id: message.id
|
|
|
|
content: message.content
|
|
|
|
timestamp: message.timestamp
|
2017-01-06 08:14:47 -05:00
|
|
|
user_id: message.user_id
|
2017-01-24 09:44:32 -05:00
|
|
|
if message.edited_at?
|
|
|
|
formattedMessage.edited_at = message.edited_at
|
2014-08-15 05:50:36 -04:00
|
|
|
return formattedMessage
|
|
|
|
|
|
|
|
formatMessagesForClientSide: (messages) ->
|
|
|
|
(@formatMessageForClientSide(message) for message in messages)
|
2016-12-16 11:52:50 -05:00
|
|
|
|
|
|
|
groupMessagesByThreads: (rooms, messages) ->
|
2017-01-04 08:51:08 -05:00
|
|
|
rooms_by_id = {}
|
2016-12-16 11:52:50 -05:00
|
|
|
for room in rooms
|
2017-01-04 08:51:08 -05:00
|
|
|
rooms_by_id[room._id.toString()] = room
|
|
|
|
|
2016-12-16 11:52:50 -05:00
|
|
|
threads = {}
|
2017-01-04 08:51:08 -05:00
|
|
|
getThread = (room) ->
|
|
|
|
thread_id = room.thread_id.toString()
|
|
|
|
if threads[thread_id]?
|
|
|
|
return threads[thread_id]
|
|
|
|
else
|
|
|
|
thread = { messages: [] }
|
|
|
|
if room.resolved?
|
|
|
|
thread.resolved = true
|
|
|
|
thread.resolved_at = room.resolved.ts
|
2017-01-06 08:14:47 -05:00
|
|
|
thread.resolved_by_user_id = room.resolved.user_id
|
2017-01-04 08:51:08 -05:00
|
|
|
threads[thread_id] = thread
|
|
|
|
return thread
|
|
|
|
|
2016-12-16 11:52:50 -05:00
|
|
|
for message in messages
|
2017-01-04 08:51:08 -05:00
|
|
|
room = rooms_by_id[message.room_id.toString()]
|
|
|
|
if room?
|
|
|
|
thread = getThread(room)
|
|
|
|
thread.messages.push MessageFormatter.formatMessageForClientSide(message)
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2017-01-04 08:51:08 -05:00
|
|
|
for thread_id, thread of threads
|
|
|
|
thread.messages.sort (a,b) -> a.timestamp - b.timestamp
|
2016-12-16 11:52:50 -05:00
|
|
|
|
|
|
|
return threads
|