From fcfe9972825c48b446112ad0cf5964f9781263f6 Mon Sep 17 00:00:00 2001
From: ubermanu <e.vodor@gmail.com>
Date: Tue, 16 Mar 2021 21:44:30 +0100
Subject: [PATCH] bugfix: save all attrs of the model to avoid double rendering
 of the messages

---
 src/plugins/sib-reactions.js | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/src/plugins/sib-reactions.js b/src/plugins/sib-reactions.js
index b9deb96..7a3fbf0 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
-- 
GitLab