diff --git a/src/plugins/sib-reactions.js b/src/plugins/sib-reactions.js index b9deb96b37dc5bd5b51d12771b717307e9466552..7a3fbf0c134f86a9e5ab3f2fa3e66e32f5d559c3 100644 --- a/src/plugins/sib-reactions.js +++ b/src/plugins/sib-reactions.js @@ -29,12 +29,6 @@ converse.plugins.add('sib-reactions', { }); if (message) { - let reactions_order = message.get('reactions_order') || []; - reactions_order = _.uniq(reactions_order.concat(attrs.reaction_emojis)); - - message.set('reactions_order', []); // Forces refresh - message.save('reactions_order', reactions_order); - const reactions = message.get('reactions') || {}; // If there are reactions for this user, replace with the latest ones @@ -44,8 +38,16 @@ converse.plugins.add('sib-reactions', { from_real_jid: attrs.from_real_jid, }; - message.set('reactions', {}); // Forces refresh - message.save('reactions', reactions); + let reactions_order = message.get('reactions_order') || []; + reactions_order = _.uniq(reactions_order.concat(attrs.reaction_emojis)); + + // Remove the emojis that are not in here anymore + const all_emojis = Object.values(reactions).map(r => r.emojis).flat(); + reactions_order = reactions_order.filter(e => all_emojis.includes(e)); + + // Forces refresh + message.set({ 'reactions': {}, 'reactions_order': [] }); + message.save({ reactions, reactions_order }); } } }, @@ -94,11 +96,6 @@ converse.plugins.add('sib-reactions', { // Force UI update in the model if (update) { - let reactions_order = message.get('reactions_order') || []; - reactions_order = _.uniq(reactions_order.concat(emojis)); - message.set('reactions_order', []); // Forces refresh - message.save('reactions_order', reactions_order); - const from_real_jid = _converse.bare_jid; const reactions = message.get('reactions') || {}; reactions[from_real_jid] = { @@ -107,8 +104,15 @@ converse.plugins.add('sib-reactions', { from_real_jid, }; - message.set('reactions', {}); // Forces refresh - message.save('reactions', reactions); + let reactions_order = message.get('reactions_order') || []; + reactions_order = _.uniq(reactions_order.concat(emojis)); + + // Remove the emojis that are not in here anymore + const all_emojis = Object.values(reactions).map(r => r.emojis).flat(); + reactions_order = reactions_order.filter(e => all_emojis.includes(e)); + + message.set({ 'reactions': {}, 'reactions_order': [] }); + message.save({ reactions, reactions_order }); } // TODO: Remove