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-08 02:54:34 +00:00
|
|
|
import type { IAttachment, IMessage, IThreadResult } from '../../../definitions';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-03-08 02:54:34 +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 = [];
|
|
|
|
}
|
2023-07-14 17:17:48 +00:00
|
|
|
|
|
|
|
msg.attachments = msg.attachments
|
|
|
|
.filter(att => !!att)
|
|
|
|
.map(att => {
|
|
|
|
att.fields = att.fields || [];
|
|
|
|
if (att.ts) {
|
|
|
|
att.ts = moment(att.ts).toDate();
|
|
|
|
}
|
|
|
|
att = normalizeAttachments(att as TMsg);
|
|
|
|
return att;
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2022-03-08 02:54:34 +00:00
|
|
|
export default (msg: any): IMessage | IThreadResult | null => {
|
2020-01-08 12:26:35 +00:00
|
|
|
if (!msg) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-08 02:54:34 +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-08 02:54:34 +00:00
|
|
|
|
2018-10-15 20:22:42 +00:00
|
|
|
if (!Array.isArray(msg.reactions)) {
|
2021-09-13 20:41:05 +00:00
|
|
|
msg.reactions = Object.keys(msg.reactions).map(key => ({
|
|
|
|
_id: `${msg._id}${key}`,
|
|
|
|
emoji: key,
|
2022-09-29 19:42:04 +00:00
|
|
|
usernames: msg.reactions ? msg.reactions[key].usernames : [],
|
|
|
|
names: msg.reactions ? msg.reactions[key].names : []
|
2021-09-13 20:41:05 +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-09-13 20:41:05 +00:00
|
|
|
msg.translations = Object.keys(msg.translations).map(key => ({
|
|
|
|
_id: `${msg._id}${key}`,
|
|
|
|
language: key,
|
2022-03-08 02:54:34 +00:00
|
|
|
value: msg.translations ? msg.translations[key] : ''
|
2021-09-13 20:41:05 +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;
|
|
|
|
};
|