summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/xep_0444/stanza.py
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2020-11-27 18:34:31 +0100
committermathieui <mathieui@mathieui.net>2020-11-27 19:44:13 +0100
commitddc3974d1b4cadf6ec0e830bad5379f9b87c8ee0 (patch)
treeb6038c68c25caad232d2ec4906290615eb10b94f /slixmpp/plugins/xep_0444/stanza.py
parent9b5ab741c83b33a466664f360247488d1484f9a2 (diff)
downloadslixmpp-ddc3974d1b4cadf6ec0e830bad5379f9b87c8ee0.tar.gz
slixmpp-ddc3974d1b4cadf6ec0e830bad5379f9b87c8ee0.tar.bz2
slixmpp-ddc3974d1b4cadf6ec0e830bad5379f9b87c8ee0.tar.xz
slixmpp-ddc3974d1b4cadf6ec0e830bad5379f9b87c8ee0.zip
Update protoxep_reactions to XEP-0444
Diffstat (limited to 'slixmpp/plugins/xep_0444/stanza.py')
-rw-r--r--slixmpp/plugins/xep_0444/stanza.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/slixmpp/plugins/xep_0444/stanza.py b/slixmpp/plugins/xep_0444/stanza.py
new file mode 100644
index 00000000..338a244e
--- /dev/null
+++ b/slixmpp/plugins/xep_0444/stanza.py
@@ -0,0 +1,60 @@
+"""
+ Slixmpp: The Slick XMPP Library
+ Copyright (C) 2020 Mathieu Pasquet
+ This file is part of Slixmpp.
+
+ See the file LICENSE for copying permission.
+"""
+
+from typing import Set, Iterable
+from slixmpp.xmlstream import ElementBase
+try:
+ from emoji import UNICODE_EMOJI
+except ImportError:
+ UNICODE_EMOJI = None
+
+
+NS = 'urn:xmpp:reactions:0'
+
+class Reactions(ElementBase):
+ name = 'reactions'
+ plugin_attrib = 'reactions'
+ namespace = NS
+ interfaces = {'id', 'values'}
+
+ def get_values(self, *, all_chars=False) -> Set[str]:
+ """"Get all reactions as str"""
+ reactions = set()
+ for reaction in self:
+ value = reaction['value']
+ if UNICODE_EMOJI and not all_chars:
+ if value in UNICODE_EMOJI:
+ reactions.add(reaction['value'])
+ else:
+ reactions.add(reaction['value'])
+ return reactions
+
+ def set_values(self, values: Iterable[str], *, all_chars=False):
+ """"Set all reactions as str"""
+ for element in self.xml.findall('reaction'):
+ self.xml.remove(element)
+ for reaction_txt in values:
+ reaction = Reaction()
+ reaction.set_value(reaction_txt, all_chars=all_chars)
+ self.append(reaction)
+
+
+class Reaction(ElementBase):
+ name = 'reaction'
+ namespace = NS
+ interfaces = {'value'}
+
+ def get_value(self) -> str:
+ return self.xml.text
+
+ def set_value(self, value: str, *, all_chars=False):
+ if UNICODE_EMOJI and not all_chars:
+ if not value in UNICODE_EMOJI:
+ raise ValueError("%s is not a valid emoji" % value)
+ self.xml.text = value
+