2018-08-01 19:35:06 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import parseUrls from './parseUrls';
|
|
|
|
|
|
|
|
function normalizeAttachments(msg) {
|
|
|
|
if (typeof msg.attachments !== typeof [] || !msg.attachments || !msg.attachments.length) {
|
|
|
|
msg.attachments = [];
|
|
|
|
}
|
|
|
|
msg.attachments = msg.attachments.map((att) => {
|
|
|
|
att.fields = att.fields || [];
|
2018-08-01 19:35:06 +00:00
|
|
|
if (att.ts) {
|
|
|
|
att.ts = moment(att.ts).toDate();
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
att = normalizeAttachments(att);
|
|
|
|
return att;
|
|
|
|
});
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default (msg) => {
|
2019-03-29 19:36:07 +00:00
|
|
|
/**
|
|
|
|
* 2019-03-29: Realm object properties are *always* optional, but `u.username` is required
|
|
|
|
* https://realm.io/docs/javascript/latest/#to-one-relationships
|
|
|
|
*/
|
|
|
|
if (!msg || !msg.u || !msg.u.username) { return; }
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
msg = normalizeAttachments(msg);
|
|
|
|
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 })) }));
|
|
|
|
// }
|
|
|
|
if (!Array.isArray(msg.reactions)) {
|
2019-05-20 20:43:50 +00:00
|
|
|
msg.reactions = Object.keys(msg.reactions).map(key => ({ _id: `${ msg._id }${ key }`, emoji: key, usernames: msg.reactions[key].usernames }));
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2019-06-28 17:02:30 +00:00
|
|
|
if (msg.translations && Object.keys(msg.translations).length) {
|
|
|
|
msg.translations = Object.keys(msg.translations).map(key => ({ _id: `${ msg._id }${ key }`, language: key, value: msg.translations[key] }));
|
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;
|
|
|
|
};
|