2018-12-20 14:13:56 -05: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 14:13:53 -05: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
|
|
|
|
*/
|
|
|
|
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,
|
2017-01-06 08:14:47 -05:00
|
|
|
user_id: message.user_id
|
2018-12-20 14:13:53 -05:00
|
|
|
};
|
|
|
|
if (message.edited_at != null) {
|
|
|
|
formattedMessage.edited_at = message.edited_at;
|
|
|
|
}
|
|
|
|
return formattedMessage;
|
|
|
|
},
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2018-12-20 14:13:53 -05:00
|
|
|
formatMessagesForClientSide(messages) {
|
|
|
|
return (Array.from(messages).map((message) => this.formatMessageForClientSide(message)));
|
|
|
|
},
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2018-12-20 14:13:53 -05:00
|
|
|
groupMessagesByThreads(rooms, messages) {
|
|
|
|
let room, thread;
|
|
|
|
const rooms_by_id = {};
|
|
|
|
for (room of Array.from(rooms)) {
|
|
|
|
rooms_by_id[room._id.toString()] = room;
|
|
|
|
}
|
2017-01-04 08:51:08 -05:00
|
|
|
|
2018-12-20 14:13:53 -05:00
|
|
|
const threads = {};
|
|
|
|
const getThread = function(room) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
2017-01-04 08:51:08 -05:00
|
|
|
|
2018-12-20 14:13:53 -05:00
|
|
|
for (let message of Array.from(messages)) {
|
|
|
|
room = rooms_by_id[message.room_id.toString()];
|
|
|
|
if (room != null) {
|
|
|
|
thread = getThread(room);
|
|
|
|
thread.messages.push(MessageFormatter.formatMessageForClientSide(message));
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2018-12-20 14:13:53 -05:00
|
|
|
for (let thread_id in threads) {
|
|
|
|
thread = threads[thread_id];
|
|
|
|
thread.messages.sort((a,b) => a.timestamp - b.timestamp);
|
|
|
|
}
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2018-12-20 14:13:53 -05:00
|
|
|
return threads;
|
|
|
|
}
|
|
|
|
});
|