2022-12-13 07:37:49 -05:00
|
|
|
export function formatMessageForClientSide(message) {
|
2022-01-07 06:48:39 -05:00
|
|
|
if (message._id) {
|
|
|
|
message.id = message._id.toString()
|
|
|
|
delete message._id
|
|
|
|
}
|
|
|
|
const formattedMessage = {
|
|
|
|
id: message.id,
|
|
|
|
content: message.content,
|
|
|
|
timestamp: message.timestamp,
|
|
|
|
user_id: message.user_id,
|
|
|
|
}
|
|
|
|
if (message.edited_at) {
|
|
|
|
formattedMessage.edited_at = message.edited_at
|
|
|
|
}
|
|
|
|
return formattedMessage
|
|
|
|
}
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export function formatMessagesForClientSide(messages) {
|
2022-01-07 06:48:39 -05:00
|
|
|
return messages.map(message => formatMessageForClientSide(message))
|
|
|
|
}
|
2017-01-04 08:51:08 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export function groupMessagesByThreads(rooms, messages) {
|
2022-01-07 06:48:39 -05:00
|
|
|
let room, thread
|
|
|
|
const roomsById = {}
|
|
|
|
for (room of rooms) {
|
|
|
|
roomsById[room._id.toString()] = room
|
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-01-07 06:48:39 -05:00
|
|
|
const threads = {}
|
|
|
|
const getThread = function (room) {
|
|
|
|
const threadId = room.thread_id.toString()
|
|
|
|
if (threads[threadId]) {
|
|
|
|
return threads[threadId]
|
|
|
|
} else {
|
|
|
|
const thread = { messages: [] }
|
|
|
|
if (room.resolved) {
|
|
|
|
thread.resolved = true
|
|
|
|
thread.resolved_at = room.resolved.ts
|
|
|
|
thread.resolved_by_user_id = room.resolved.user_id
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-07 06:48:39 -05:00
|
|
|
threads[threadId] = thread
|
|
|
|
return thread
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-01-07 06:48:39 -05:00
|
|
|
for (const message of messages) {
|
|
|
|
room = roomsById[message.room_id.toString()]
|
|
|
|
if (room) {
|
|
|
|
thread = getThread(room)
|
|
|
|
thread.messages.push(formatMessageForClientSide(message))
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-01-07 06:48:39 -05:00
|
|
|
for (const threadId in threads) {
|
|
|
|
thread = threads[threadId]
|
|
|
|
thread.messages.sort((a, b) => a.timestamp - b.timestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return threads
|
|
|
|
}
|