mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-26 18:02:39 +00:00
0a12c47b35
Move chat service to ES modules GitOrigin-RevId: c08ae8328b8f3b539e6cfe052834b84bb3756330
60 lines
1.5 KiB
JavaScript
60 lines
1.5 KiB
JavaScript
export 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
|
|
}
|
|
|
|
export function formatMessagesForClientSide(messages) {
|
|
return messages.map(message => formatMessageForClientSide(message))
|
|
}
|
|
|
|
export 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
|
|
}
|