mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
f3a5e8a0e8
GitOrigin-RevId: 73524dcec49faa05d1fd4dd4ffa6950803175f33
66 lines
1.6 KiB
JavaScript
66 lines
1.6 KiB
JavaScript
function formatMessageForClientSide(message) {
|
|
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
|
|
}
|
|
|
|
function formatMessagesForClientSide(messages) {
|
|
return messages.map(message => formatMessageForClientSide(message))
|
|
}
|
|
|
|
function groupMessagesByThreads(rooms, messages) {
|
|
let room, thread
|
|
const roomsById = {}
|
|
for (room of rooms) {
|
|
roomsById[room._id.toString()] = room
|
|
}
|
|
|
|
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
|
|
}
|
|
threads[threadId] = thread
|
|
return thread
|
|
}
|
|
}
|
|
|
|
for (const message of messages) {
|
|
room = roomsById[message.room_id.toString()]
|
|
if (room) {
|
|
thread = getThread(room)
|
|
thread.messages.push(formatMessageForClientSide(message))
|
|
}
|
|
}
|
|
|
|
for (const threadId in threads) {
|
|
thread = threads[threadId]
|
|
thread.messages.sort((a, b) => a.timestamp - b.timestamp)
|
|
}
|
|
|
|
return threads
|
|
}
|
|
|
|
module.exports = {
|
|
formatMessagesForClientSide,
|
|
formatMessageForClientSide,
|
|
groupMessagesByThreads,
|
|
}
|