Philipp Hörist pushed to branch reactions at gajim / gajim


Commits:
488460a1 by Philipp Hörist at 2024-05-18T20:41:49+02:00
fix: normalise incoming emojis

- - - - -


2 changed files:

- gajim/common/modules/reactions.py
- gajim/common/util/text.py


Changes:

=====================================
gajim/common/modules/reactions.py
=====================================
@@ -6,7 +6,6 @@
 
 from __future__ import annotations
 
-import emoji
 from nbxmpp.namespaces import Namespace
 from nbxmpp.protocol import Message
 from nbxmpp.protocol import NodeProcessed
@@ -24,6 +23,8 @@
 from gajim.common.storage.archive import models as mod
 from gajim.common.structs import MessageType
 from gajim.common.structs import OutgoingMessage
+from gajim.common.util.text import convert_to_codepoints
+from gajim.common.util.text import normalize_reactions
 
 
 class Reactions(BaseModule):
@@ -112,16 +113,12 @@ def _process_reaction(
         # Set arbitrary limit of max reactions to prevent
         # performance problems when loading and displaying them.
         # Check if reactions qualify as emojis.
-        reactions: list[str] = []
-        for reaction in list(properties.reactions.emojis)[:10]:
-            if not emoji.is_emoji(reaction):
-                self._log.warning(
-                    'Reactions did not qualify as emoji: %s', reaction
-                )
-                continue
-            reactions.append(reaction)
-
-        if not reactions:
+        valid, invalid = normalize_reactions(list(properties.reactions.emojis))
+        if invalid:
+            codepoints = ', '.join([convert_to_codepoints(i) for i in invalid])
+            self._log.warning('Reactions did not qualify as emoji: %s', 
codepoints)
+
+        if not valid:
             raise NodeProcessed
 
         reaction = mod.Reaction(
@@ -130,7 +127,7 @@ def _process_reaction(
             occupant_=occupant,
             id=properties.reactions.id,
             direction=direction,
-            emojis=';'.join(reactions),
+            emojis=';'.join(valid),
             timestamp=timestamp,
         )
 


=====================================
gajim/common/util/text.py
=====================================
@@ -7,6 +7,7 @@
 import math
 import re
 
+import emoji
 from gi.repository import GLib
 
 from gajim.common import regex
@@ -76,3 +77,27 @@ def format_bytes_as_hex(bytes_: bytes, line_count: int = 1) 
-> str:
     for pos in range(0, len(bytes_), line_length):
         lines.append(':'.join(hex_list[pos:pos + line_length]))
     return '\n'.join(lines)
+
+
+def normalize_reactions(reactions: list[str]) -> tuple[set[str], set[str]]:
+    valid: set[str] = set()
+    invalid: set[str] = set()
+    # Set arbitrary limit of max reactions to prevent
+    # performance problems when loading and displaying them.
+    reactions = reactions[:10]
+    for reaction in reactions:
+        # Remote emoji variant selectors they are not needed because
+        # reactions need to be always shown in emoji representation
+        # Further it allows us to make them equal with the version
+        # without selector
+        reaction = reaction.strip('\uFE0E\uFE0F')
+        if not emoji.is_emoji(reaction):
+            invalid.add(reaction)
+            continue
+        valid.add(reaction)
+
+    return valid, invalid
+
+
+def convert_to_codepoints(string: str) -> str:
+    return ''.join(f'\\u{ord(c):04x}' for c in string)



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/488460a1b600dd3d10caeec42380c5a78ad911d2

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/488460a1b600dd3d10caeec42380c5a78ad911d2
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list -- commits@gajim.org
To unsubscribe send an email to commits-le...@gajim.org

Reply via email to