2018-12-20 19:13:56 +00:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2018-12-20 19:13:53 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2018-12-20 19:13:59 +00:00
|
|
|
let MessageFormatter
|
|
|
|
module.exports = MessageFormatter = {
|
|
|
|
formatMessageForClientSide(message) {
|
|
|
|
if (message._id != null) {
|
|
|
|
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 != null) {
|
|
|
|
formattedMessage.edited_at = message.edited_at
|
|
|
|
}
|
|
|
|
return formattedMessage
|
|
|
|
},
|
2014-08-15 09:50:36 +00:00
|
|
|
|
2018-12-20 19:13:59 +00:00
|
|
|
formatMessagesForClientSide(messages) {
|
2020-08-10 16:01:11 +00:00
|
|
|
return Array.from(messages).map((message) =>
|
2018-12-20 19:13:59 +00:00
|
|
|
this.formatMessageForClientSide(message)
|
|
|
|
)
|
|
|
|
},
|
2017-01-04 13:51:08 +00:00
|
|
|
|
2018-12-20 19:13:59 +00:00
|
|
|
groupMessagesByThreads(rooms, messages) {
|
|
|
|
let room, thread
|
|
|
|
const rooms_by_id = {}
|
|
|
|
for (room of Array.from(rooms)) {
|
|
|
|
rooms_by_id[room._id.toString()] = room
|
|
|
|
}
|
|
|
|
|
|
|
|
const threads = {}
|
2020-08-10 16:01:11 +00:00
|
|
|
const getThread = function (room) {
|
2018-12-20 19:13:59 +00:00
|
|
|
const thread_id = room.thread_id.toString()
|
|
|
|
if (threads[thread_id] != null) {
|
|
|
|
return threads[thread_id]
|
|
|
|
} else {
|
|
|
|
const thread = { messages: [] }
|
|
|
|
if (room.resolved != null) {
|
|
|
|
thread.resolved = true
|
|
|
|
thread.resolved_at = room.resolved.ts
|
|
|
|
thread.resolved_by_user_id = room.resolved.user_id
|
|
|
|
}
|
|
|
|
threads[thread_id] = thread
|
|
|
|
return thread
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:03:17 +00:00
|
|
|
for (const message of Array.from(messages)) {
|
2018-12-20 19:13:59 +00:00
|
|
|
room = rooms_by_id[message.room_id.toString()]
|
|
|
|
if (room != null) {
|
|
|
|
thread = getThread(room)
|
|
|
|
thread.messages.push(
|
|
|
|
MessageFormatter.formatMessageForClientSide(message)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:03:17 +00:00
|
|
|
for (const thread_id in threads) {
|
2018-12-20 19:13:59 +00:00
|
|
|
thread = threads[thread_id]
|
|
|
|
thread.messages.sort((a, b) => a.timestamp - b.timestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return threads
|
|
|
|
}
|
|
|
|
}
|