2018-08-01 19:35:06 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import parseUrls from './parseUrls';
|
2022-03-29 18:53:27 +00:00
|
|
|
import type { IAttachment, IMessage, IThreadResult } from '../../../definitions';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
type TMsg = IMessage & IAttachment;
|
|
|
|
|
|
|
|
function normalizeAttachments(msg: TMsg) {
|
2018-04-24 19:34:03 +00:00
|
|
|
if (typeof msg.attachments !== typeof [] || !msg.attachments || !msg.attachments.length) {
|
|
|
|
msg.attachments = [];
|
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
msg.attachments = msg.attachments.map(att => {
|
2018-04-24 19:34:03 +00:00
|
|
|
att.fields = att.fields || [];
|
2018-08-01 19:35:06 +00:00
|
|
|
if (att.ts) {
|
|
|
|
att.ts = moment(att.ts).toDate();
|
|
|
|
}
|
2022-03-29 18:53:27 +00:00
|
|
|
att = normalizeAttachments(att as TMsg);
|
2018-04-24 19:34:03 +00:00
|
|
|
return att;
|
|
|
|
});
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
export default (msg: any): IMessage | IThreadResult | null => {
|
2020-01-08 12:26:35 +00:00
|
|
|
if (!msg) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-29 18:53:27 +00:00
|
|
|
msg = normalizeAttachments(msg as TMsg);
|
2018-04-24 19:34:03 +00:00
|
|
|
msg.reactions = msg.reactions || [];
|
2019-06-10 18:36:31 +00:00
|
|
|
msg.unread = msg.unread || false;
|
2018-04-24 19:34:03 +00:00
|
|
|
// TODO: api problems
|
2018-10-15 20:22:42 +00:00
|
|
|
// if (Array.isArray(msg.reactions)) {
|
|
|
|
// msg.reactions = msg.reactions.map((value, key) => ({ emoji: key, usernames: value.usernames.map(username => ({ value: username })) }));
|
|
|
|
// } else {
|
|
|
|
// msg.reactions = Object.keys(msg.reactions).map(key => ({ emoji: key, usernames: msg.reactions[key].usernames.map(username => ({ value: username })) }));
|
|
|
|
// }
|
2022-03-29 18:53:27 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
if (!Array.isArray(msg.reactions)) {
|
2021-10-01 18:12:19 +00:00
|
|
|
msg.reactions = Object.keys(msg.reactions).map(key => ({
|
|
|
|
_id: `${msg._id}${key}`,
|
|
|
|
emoji: key,
|
2022-03-29 18:53:27 +00:00
|
|
|
usernames: msg.reactions ? msg.reactions[key].usernames : []
|
2021-10-01 18:12:19 +00:00
|
|
|
}));
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2019-06-28 17:02:30 +00:00
|
|
|
if (msg.translations && Object.keys(msg.translations).length) {
|
2021-10-01 18:12:19 +00:00
|
|
|
msg.translations = Object.keys(msg.translations).map(key => ({
|
|
|
|
_id: `${msg._id}${key}`,
|
|
|
|
language: key,
|
2022-03-29 18:53:27 +00:00
|
|
|
value: msg.translations ? msg.translations[key] : ''
|
2021-10-01 18:12:19 +00:00
|
|
|
}));
|
2019-07-15 16:56:52 +00:00
|
|
|
msg.autoTranslate = true;
|
2019-06-28 17:02:30 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
msg.urls = msg.urls ? parseUrls(msg.urls) : [];
|
|
|
|
msg._updatedAt = new Date();
|
|
|
|
// loadHistory returns msg.starred as object
|
|
|
|
// stream-room-msgs returns msg.starred as an array
|
|
|
|
msg.starred = msg.starred && (Array.isArray(msg.starred) ? msg.starred.length > 0 : !!msg.starred);
|
|
|
|
return msg;
|
|
|
|
};
|