annotate libervia/backend/plugins/plugin_xep_0444.py @ 4366:1ef32316a55e

plugin XEP-0444, storage: move `get_history_from_xmpp_id` to storage as it is generally useful.
author Goffi <goffi@goffi.org>
date Tue, 06 May 2025 00:33:57 +0200
parents 5d2de6c1156d
children
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
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
19 from typing import Iterable, List
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
21 from twisted.internet import defer
3886
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 wokkel import disco, iwokkel
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from zope.interface import implementer
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
27 import emoji
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
28 from libervia.backend.core import exceptions
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
29 from libervia.backend.core.constants import Const as C
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
30 from libervia.backend.core.core_types import SatXMPPEntity
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
31 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
32 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
33 from libervia.backend.memory.sqla_mapping import History
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
34 from libervia.backend.models.core import MessageReactionData
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
35 from libervia.backend.tools.utils import aio
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
36 from libervia.backend.memory.sqla import select
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
37 from sqlalchemy.orm.attributes import flag_modified
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 log = getLogger(__name__)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 PLUGIN_INFO = {
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_NAME: "Message Reactions",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_IMPORT_NAME: "XEP-0444",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_TYPE: C.PLUG_TYPE_XEP,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_MODES: C.PLUG_MODE_BOTH,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_PROTOCOLS: ["XEP-0444"],
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
47 C.PI_DEPENDENCIES: ["XEP-0045", "XEP-0334"],
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_MAIN: "XEP_0444",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_HANDLER: "yes",
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_DESCRIPTION: _("""Message Reactions implementation"""),
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
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 NS_REACTIONS = "urn:xmpp:reactions:0"
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 class XEP_0444:
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
57 # TODO: implement and use occupant-ID (XEP-0421), and check sender (see
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
58 # https://xmpp.org/extensions/xep-0444.html#acceptable-reactions).
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def __init__(self, host):
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents: 4157
diff changeset
60 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
61 host.register_namespace("reactions", NS_REACTIONS)
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.host = host
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
63 self._m = host.plugins["XEP-0045"]
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self._h = host.plugins["XEP-0334"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
65 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
66 "message_reactions_set",
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 ".plugin",
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
68 in_sign="sasss",
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
70 method=self._reactions_set,
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 async_=True,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 )
4051
c23cad65ae99 core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
73 host.trigger.add("message_received", self._message_received_trigger)
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
75 def get_handler(self, client):
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 return XEP_0444_Handler()
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
78 async def _message_received_trigger(
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 client: SatXMPPEntity,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 message_elt: domish.Element,
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
82 post_treat: defer.Deferred,
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 ) -> bool:
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
84 reactions_elt = next(message_elt.elements(NS_REACTIONS, "reactions"), None)
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
85 if reactions_elt is not None:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
86 reaction_id = reactions_elt.getAttribute("id")
4366
1ef32316a55e plugin XEP-0444, storage: move `get_history_from_xmpp_id` to storage as it is generally useful.
Goffi <goffi@goffi.org>
parents: 4200
diff changeset
87 message_type = message_elt.getAttribute("type")
1ef32316a55e plugin XEP-0444, storage: move `get_history_from_xmpp_id` to storage as it is generally useful.
Goffi <goffi@goffi.org>
parents: 4200
diff changeset
88 history = await self.host.memory.storage.get_history_from_xmpp_id(
1ef32316a55e plugin XEP-0444, storage: move `get_history_from_xmpp_id` to storage as it is generally useful.
Goffi <goffi@goffi.org>
parents: 4200
diff changeset
89 client, reaction_id, message_type
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
90 )
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
91 if history is None:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
92 log.warning(
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
93 f"Can't find matching message for reaction: {reactions_elt.toXml()}"
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
94 )
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
95 else:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
96 if not reaction_id:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
97 log.warning(f"Invalid reaction: {reactions_elt.toXml()}")
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
98 return False
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
99 from_jid = jid.JID(message_elt["from"])
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
100 reactions = set()
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
101 for reaction_elt in reactions_elt.elements("reaction"):
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
102 reaction = str(reaction_elt)
4200
5d2de6c1156d plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
103 if (
5d2de6c1156d plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
104 not emoji.purely_emoji(reaction)
5d2de6c1156d plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
105 or emoji.emoji_count(reaction) != 1
5d2de6c1156d plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
106 ):
5d2de6c1156d plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
107 log.warning(f"Ignoring invalid reaction: {reaction_elt.toXml()}.")
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
108 continue
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
109 reactions.add(reaction)
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
110 await self.update_history_reactions(client, history, from_jid, reactions)
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
111
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
112 return False
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
113
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 return True
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
116 def _reactions_set(
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
117 self,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
118 message_id: str,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
119 reactions: List[str],
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
120 update_type: str = "replace",
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
121 profile: str = C.PROF_KEY_NONE,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
122 ) -> defer.Deferred[None]:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
123 client = self.host.get_client(profile)
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 return defer.ensureDeferred(
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
125 self.set_reactions(client, message_id, reactions, update_type)
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 )
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
128 async def get_history_from_uid(
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
129 self, client: SatXMPPEntity, message_id: str
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
130 ) -> History:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
131 """Retrieve the chat history associated with a given message ID.
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
132
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
133 @param message_id: The Libervia specific identifier of the message
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
134
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
135 @return: An instance of History containing the chat history, messages, and
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
136 subjects related to the specified message ID.
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
137
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
138 @raises exceptions.NotFound: The history corresponding to the given message ID is
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
139 not found in the database.
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
140 """
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
141 history = await self.host.memory.storage.get(
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
142 client,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
143 History,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
144 History.uid,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
145 message_id,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
146 )
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
147 if history is None:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
148 raise exceptions.NotFound(
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
149 f"message to retract not found in database ({message_id})"
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
150 )
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
151 return history
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
152
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
153 def send_reactions(
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 self,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 client: SatXMPPEntity,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 dest_jid: jid.JID,
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
157 message_type: str,
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 message_id: str,
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
159 reactions: Iterable[str],
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 ) -> None:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 """Send the <message> stanza containing the 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 @param dest_jid: recipient of the reaction
4200
5d2de6c1156d plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
164 @param message_id: either origin ID or stanza ID
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 see https://xmpp.org/extensions/xep-0444.html#business-id
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 message_elt = domish.Element((None, "message"))
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 message_elt["from"] = client.jid.full()
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 message_elt["to"] = dest_jid.full()
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
170 message_elt["type"] = message_type
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 reactions_elt = message_elt.addElement((NS_REACTIONS, "reactions"))
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 reactions_elt["id"] = message_id
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 for r in set(reactions):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 reactions_elt.addElement("reaction", content=r)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
175 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE])
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 client.send(message_elt)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
178 def convert_to_replace_update(
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 self,
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
180 current_reactions: set[str],
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
181 reactions_new: Iterable[str],
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
182 update_type: str,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
183 ) -> set[str]:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
184 """Convert the given update to a replace update.
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
185
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
186 @param current_reactions: reaction of reacting JID before update
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
187 @param reactions_new: New reactions to be updated.
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
188 @param update_type: Original type of update (add, remove, toggle, replace).
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
189 @return: reactions for a replace operation.
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
190
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
191 @raise ValueError: invalid ``update_type``.
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
192 """
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
193 new_reactions_set = set(reactions_new)
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
194
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
195 if update_type == "replace":
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
196 return new_reactions_set
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
197
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
198 if update_type == "add":
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
199 replace_reactions = current_reactions | new_reactions_set
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
200 elif update_type == "remove":
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
201 replace_reactions = current_reactions - new_reactions_set
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
202 elif update_type == "toggle":
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
203 replace_reactions = current_reactions ^ new_reactions_set
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
204 else:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
205 raise ValueError(f"Invalid update type: {update_type!r}")
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
206
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
207 return replace_reactions
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
208
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
209 async def update_history_reactions(
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
210 self,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
211 client: SatXMPPEntity,
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 history: History,
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
213 reacting_jid: jid.JID,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
214 reactions_new: Iterable[str],
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
215 update_type: str = "replace",
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
216 store: bool = True,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
217 ) -> set[str]:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
218 """Update reactions in History instance and optionally store and signal it.
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
220 @param history: storage History instance to be updated
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
221 @param reacting_jid: author of the reactions
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
222 @param reactions_new: new reactions to update
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
223 @param update_type: Original type of update (add, remove, toggle, replace).
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
224 @param store: if True, update history in storage, and send a `message_update`
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
225 signal with the new reactions.
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
226 @return: set of reactions for this JID for a "replace" update
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 """
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
228 # FIXME: handle race conditions
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
229 if history.type == C.MESS_TYPE_GROUPCHAT:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
230 entity_jid_s = reacting_jid.full()
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
231 else:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
232 entity_jid_s = reacting_jid.userhost()
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
233 if history.extra is None:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
234 history.extra = {}
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
235 extra = history.extra
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
236 reactions = extra.get("reactions", {})
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
237
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
238 current_reactions = {
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
239 reaction for reaction, jids in reactions.items() if entity_jid_s in jids
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
240 }
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
241 reactions_replace_set = self.convert_to_replace_update(
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
242 current_reactions, reactions_new, update_type
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
243 )
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
244
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
245 for reaction in current_reactions - reactions_replace_set:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
246 reaction_jids = reactions[reaction]
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
247 reaction_jids.remove(entity_jid_s)
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
248 if not reaction_jids:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
249 del reactions[reaction]
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
250
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
251 for reaction in reactions_replace_set - current_reactions:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
252 reactions.setdefault(reaction, []).append(entity_jid_s)
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
253
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
254 # we want to have a constant order in reactions
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
255 extra["reactions"] = dict(sorted(reactions.items()))
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
256
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
257 if store:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
258 # FIXME: this is not a clean way to flag "extra" as modified, but a deepcopy
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
259 # is for some reason not working here.
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
260 flag_modified(history, "extra")
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
261 await self.host.memory.storage.add(history)
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
262 # we send the signal for frontends update
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
263 data = MessageReactionData(reactions=extra["reactions"])
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
264 self.host.bridge.message_update(
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
265 history.uid,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
266 C.MESS_UPDATE_REACTION,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
267 data.model_dump_json(),
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
268 client.profile,
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
269 )
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
270
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
271 return reactions_replace_set
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
272
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3886
diff changeset
273 async def set_reactions(
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 self,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 client: SatXMPPEntity,
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 message_id: str,
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
277 reactions: Iterable[str],
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
278 update_type: str = "replace",
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 ) -> None:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 """Set and replace reactions to a message
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
281
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 @param message_id: internal ID of the message
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 @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
284 use empty list to remove all reactions
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
285 @param update_type: how to use the reaction to make the update, can be "replace",
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
286 "add", "remove" or "toggle".
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 """
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 if not message_id:
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 raise ValueError("message_id can't be empty")
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
290
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
291 history = await self.get_history_from_uid(client, message_id)
4200
5d2de6c1156d plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
292 if history.type == C.MESS_TYPE_GROUPCHAT:
5d2de6c1156d plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
293 mess_id = history.stanza_id
5d2de6c1156d plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
294 else:
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
295 mess_id = history.origin_id
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
296
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
297 if mess_id is None:
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 raise exceptions.DataError(
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 "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
300 "reaction"
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 )
4157
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
302
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
303 if history.source == client.jid.userhost():
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
304 dest_jid = history.dest_jid
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
305 else:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
306 dest_jid = history.source_jid
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
307
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
308 if history.type == C.MESS_TYPE_GROUPCHAT:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
309 # the reaction is for the chat room itself
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
310 dest_jid = dest_jid.userhostJID()
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
311 reacting_jid = self._m.get_room_user_jid(client, dest_jid)
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
312 # we don't store reactions for MUC, at this will be done when we receive back
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
313 # the reaction
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
314 store = False
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
315 else:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
316 # we use bare JIDs for one2one message, except if the recipient is from a MUC
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
317 if self._m.is_room(client, dest_jid):
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
318 # this is a private message in a room, we need to use the MUC JID
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
319 reacting_jid = self._m.get_room_user_jid(client, dest_jid.userhostJID())
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
320 else:
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
321 # this is a classic one2one message, we need the bare JID
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
322 reacting_jid = client.jid.userhostJID()
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
323 dest_jid = dest_jid.userhostJID()
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
324 store = True
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
325
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
326 reaction_replace_set = await self.update_history_reactions(
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
327 client, history, reacting_jid, reactions, update_type, store
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
328 )
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
329
04cdcb3fd713 plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
330 self.send_reactions(client, dest_jid, history.type, mess_id, reaction_replace_set)
3886
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
331
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
332
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 @implementer(iwokkel.IDisco)
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 class XEP_0444_Handler(xmlstream.XMPPHandler):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 return [disco.DiscoFeature(NS_REACTIONS)]
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
337
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
1f88ca90c3de plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 return []