annotate sat/plugins/plugin_xep_0444.py @ 3913:944f51f9c2b4

core (xmpp): make `send` a blocking method, fix `sendMessageData` calls: original `send` method is blocking, and it is used as such by Wokkel and thus can't be changed to an async method easily. However, an Async method is necessary to have an async trigger at the very end of the send workflow for end-to-end encryption. To workaround that, `send` is an async method which call `a_send`, an async method which actually does the sending. This way legacy method can still call `send` while `a_send` can be await otherwise. Fix calls to `sendMessageData`: the method now being an `async` one, `ensureDeferred` had to be used in some calls.
author Goffi <goffi@goffi.org>
date Sat, 24 Sep 2022 16:31:39 +0200
parents 1f88ca90c3de
children 524856bd7b19
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin for XEP-0444
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import List, Iterable
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from copy import deepcopy
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber import jid, xmlstream
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import defer
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco, iwokkel
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implementer
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.core.constants import Const as C
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.core.i18n import _
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sat.core.log import getLogger
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from sat.core import exceptions
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from sat.core.core_types import SatXMPPEntity
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from sat.memory.sqla_mapping import History
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 log = getLogger(__name__)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 PLUGIN_INFO = {
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_NAME: "Message Reactions",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_IMPORT_NAME: "XEP-0444",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_TYPE: C.PLUG_TYPE_XEP,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_MODES: C.PLUG_MODE_BOTH,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_PROTOCOLS: ["XEP-0444"],
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_DEPENDENCIES: ["XEP-0334"],
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_MAIN: "XEP_0444",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_HANDLER: "yes",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_DESCRIPTION: _("""Message Reactions implementation"""),
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 }
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 NS_REACTIONS = "urn:xmpp:reactions:0"
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 class XEP_0444:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def __init__(self, host):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 log.info(_("Message Reactions initialization"))
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 host.registerNamespace("reactions", NS_REACTIONS)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.host = host
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self._h = host.plugins["XEP-0334"]
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 host.bridge.addMethod(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 "messageReactionsSet",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 ".plugin",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 in_sign="ssas",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 out_sign="",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 method=self._reactionsSet,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 async_=True,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 )
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 host.trigger.add("messageReceived", self._messageReceivedTrigger)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def getHandler(self, client):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 return XEP_0444_Handler()
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 async def _messageReceivedTrigger(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 client: SatXMPPEntity,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 message_elt: domish.Element,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 post_treat: defer.Deferred
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 ) -> bool:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 return True
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def _reactionsSet(self, message_id: str, profile: str, reactions: List[str]) -> None:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 client = self.host.getClient(profile)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 return defer.ensureDeferred(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.setReactions(client, message_id)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 )
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def sendReactions(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 client: SatXMPPEntity,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 dest_jid: jid.JID,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 message_id: str,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 reactions: Iterable[str]
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 ) -> None:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 """Send the <message> stanza containing the reactions
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 @param dest_jid: recipient of the reaction
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 @param message_id: either <origin-id> or message's ID
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 see https://xmpp.org/extensions/xep-0444.html#business-id
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 """
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 message_elt = domish.Element((None, "message"))
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 message_elt["from"] = client.jid.full()
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 message_elt["to"] = dest_jid.full()
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 reactions_elt = message_elt.addElement((NS_REACTIONS, "reactions"))
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 reactions_elt["id"] = message_id
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 for r in set(reactions):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 reactions_elt.addElement("reaction", content=r)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self._h.addHintElements(message_elt, [self._h.HINT_STORE])
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 client.send(message_elt)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 async def addReactionsToHistory(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 self,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 history: History,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 from_jid: jid.JID,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 reactions: Iterable[str]
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 ) -> None:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 """Update History instance with given reactions
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 @param history: storage History instance
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 will be updated in DB
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 "summary" field of history.extra["reactions"] will also be updated
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 @param from_jid: author of the reactions
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 @param reactions: list of reactions
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 """
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 history.extra = deepcopy(history.extra) if history.extra else {}
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 h_reactions = history.extra.setdefault("reactions", {})
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 # reactions mapped by originating JID
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 by_jid = h_reactions.setdefault("by_jid", {})
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 # reactions are sorted to in summary to keep a consistent order
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 h_reactions["by_jid"][from_jid.userhost()] = sorted(list(set(reactions)))
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 h_reactions["summary"] = sorted(list(set().union(*by_jid.values())))
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 await self.host.memory.storage.session_add(history)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 async def setReactions(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 client: SatXMPPEntity,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 message_id: str,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 reactions: Iterable[str]
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 ) -> None:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 """Set and replace reactions to a message
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 @param message_id: internal ID of the message
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 @param rections: lsit of emojis to used to react to the message
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 use empty list to remove all reactions
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 """
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 if not message_id:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 raise ValueError("message_id can't be empty")
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 history = await self.host.memory.storage.get(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 client, History, History.uid, message_id,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 joined_loads=[History.messages, History.subjects]
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 )
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 if history is None:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 raise exceptions.NotFound(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 f"message to retract not found in database ({message_id})"
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 )
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 mess_id = history.origin_id or history.stanza_id
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 if not mess_id:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 raise exceptions.DataError(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 "target message has neither origin-id nor message-id, we can't send a "
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 "reaction"
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 )
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 await self.addReactionsToHistory(history, client.jid, reactions)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 self.sendReactions(client, history.dest_jid, mess_id, reactions)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 @implementer(iwokkel.IDisco)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 class XEP_0444_Handler(xmlstream.XMPPHandler):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 return [disco.DiscoFeature(NS_REACTIONS)]
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 return []