Source code for slixmpp.plugins.xep_0444.reactions
# Slixmpp: The Slick XMPP Library# Copyright (C) 2020 Mathieu Pasquet# This file is part of Slixmpp.# See the file LICENSE for copying permission.fromtypingimportIterablefromslixmppimportJIDfromslixmpp.pluginsimportBasePluginfromslixmpp.stanzaimportMessagefromslixmpp.xmlstreamimportregister_stanza_pluginfromslixmpp.xmlstream.matcherimportStanzaPathfromslixmpp.xmlstream.handlerimportCallbackfromslixmpp.plugins.xep_0444importstanza
[docs]classXEP_0444(BasePlugin):""" XEP-0444: Message Reactions. If the python-emoji library is present, setting emojis as reactions will be checked against known emoji, and trying to set non-emoji characters as reactions will raise a ``ValueError``. This behavior can be disabled by passing ``all_chars=True`` to the ``Reaction.set_value()`` call. """name='xep_0444'description='XEP-0444: Message Reactions'dependencies={'xep_0030','xep_0334'}stanza=stanzanamespace=stanza.NSdefplugin_init(self):self.xmpp.register_handler(Callback('Reaction received',StanzaPath("message/reactions"),self._handle_reactions,))register_stanza_plugin(Message,stanza.Reactions)register_stanza_plugin(stanza.Reactions,stanza.Reaction,iterable=True)defsession_bind(self,event):self.xmpp['xep_0030'].add_feature(stanza.NS)defplugin_end(self):self.xmpp.remove_handler('Reaction received')self.xmpp['xep_0030'].del_feature(feature=stanza.NS)def_handle_reactions(self,message:Message):self.xmpp.event('reactions',message)
[docs]defsend_reactions(self,to:JID,to_id:str,reactions:Iterable[str],*,store=True):"""Send reactions related to a message"""msg=self.xmpp.make_message(mto=to)self.set_reactions(msg,to_id,reactions)ifstore:msg.enable('store')msg.send()
[docs]@staticmethoddefset_reactions(message:Message,to_id:str,reactions:Iterable[str]):"""Add reactions to a Message object."""message['reactions']['id']=to_idforreactioninreactions:reaction_stanza=stanza.Reaction()reaction_stanza['value']=reactionmessage['reactions'].append(reaction_stanza)