Commit 746fe441 authored by JC Brand's avatar JC Brand

Updates the message parsing.

Only set `id` once we've actually parsed the sender attributes,
otherwise `attrs.from` is `undefined`.

Use `Array.prototype.reduce` to generate stanza_ids attributes map.

Add `from_muc` attribute which contains the MUC JID
parent d2b6b411
...@@ -46,23 +46,26 @@ const stanza_utils = { ...@@ -46,23 +46,26 @@ const stanza_utils = {
*/ */
getStanzaIDs (stanza, original_stanza) { getStanzaIDs (stanza, original_stanza) {
const attrs = {}; const attrs = {};
const stanza_ids = sizzle(`stanza-id[xmlns="${Strophe.NS.SID}"]`, stanza); // Store generic stanza ids
if (stanza_ids.length) { const sids = sizzle(`stanza-id[xmlns="${Strophe.NS.SID}"]`, stanza);
stanza_ids.forEach(s => (attrs[`stanza_id ${s.getAttribute('by')}`] = s.getAttribute('id'))); const sid_attrs = sids.reduce((acc, s) => {
} acc[`stanza_id ${s.getAttribute('by')}`] = s.getAttribute('id');
return acc;
}, {});
Object.assign(attrs, sid_attrs);
// Store the archive id
const result = sizzle(`message > result[xmlns="${Strophe.NS.MAM}"]`, original_stanza).pop(); const result = sizzle(`message > result[xmlns="${Strophe.NS.MAM}"]`, original_stanza).pop();
if (result) { if (result) {
const by_jid = original_stanza.getAttribute('from'); const by_jid = original_stanza.getAttribute('from');
attrs[`stanza_id ${by_jid}`] = result.getAttribute('id'); attrs[`stanza_id ${by_jid}`] = result.getAttribute('id');
} }
// Store the origin id
const origin_id = sizzle(`origin-id[xmlns="${Strophe.NS.SID}"]`, stanza).pop(); const origin_id = sizzle(`origin-id[xmlns="${Strophe.NS.SID}"]`, stanza).pop();
if (origin_id) { if (origin_id) {
attrs['origin_id'] = origin_id.getAttribute('id'); attrs['origin_id'] = origin_id.getAttribute('id');
} }
// We prefer to use one of the XEP-0359 unique and stable stanza IDs
// as the Model id, to avoid duplicates.
attrs['id'] = attrs['origin_id'] || attrs[`stanza_id ${attrs.from}`] || u.getUniqueId();
return attrs; return attrs;
}, },
...@@ -172,6 +175,7 @@ const stanza_utils = { ...@@ -172,6 +175,7 @@ const stanza_utils = {
const nick = Strophe.unescapeNode(Strophe.getResourceFromJid(from)); const nick = Strophe.unescapeNode(Strophe.getResourceFromJid(from));
return { return {
'from': from, 'from': from,
'from_muc': Strophe.getBareJidFromJid(from),
'nick': nick, 'nick': nick,
'sender': nick === chatbox.get('nick') ? 'me': 'them', 'sender': nick === chatbox.get('nick') ? 'me': 'them',
'received': (new Date()).toISOString(), 'received': (new Date()).toISOString(),
...@@ -317,6 +321,9 @@ const stanza_utils = { ...@@ -317,6 +321,9 @@ const stanza_utils = {
stanza_utils.getSpoilerAttributes(stanza), stanza_utils.getSpoilerAttributes(stanza),
stanza_utils.getCorrectionAttributes(stanza, original_stanza) stanza_utils.getCorrectionAttributes(stanza, original_stanza)
) )
// We prefer to use one of the XEP-0359 unique and stable stanza IDs
// as the Model id, to avoid duplicates.
attrs['id'] = attrs['origin_id'] || attrs[`stanza_id ${(attrs.from_muc || attrs.from)}`] || u.getUniqueId();
return attrs; return attrs;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment