Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0444.py @ 4232:0fbe5c605eb6
tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
fix 441
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 06 Apr 2024 12:59:50 +0200 |
parents | 5d2de6c1156d |
children |
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 |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
78 @aio |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
79 async def get_history_from_reaction_id( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
80 self, client: SatXMPPEntity, message_elt: domish.Element, reaction_id: str |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
81 ) -> History | None: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
82 """Retrieves history rows that match a specific reaction_id. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
83 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
84 The retrieval criteria vary based on the message type, according to XEP-0444. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
85 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
86 @param message_elt: The message element, used to determine the type of message. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
87 @param reaction_id: The reaction ID to match in the history. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
88 """ |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
89 profile_id = self.host.memory.storage.profiles[client.profile] |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
90 async with self.host.memory.storage.session() as session: |
4200
5d2de6c1156d
plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents:
4163
diff
changeset
|
91 if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT: |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
92 query = select(History).where( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
93 History.profile_id == profile_id, History.stanza_id == reaction_id |
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 query = select(History).where( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
97 History.profile_id == profile_id, History.origin_id == reaction_id |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
98 ) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
99 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
100 result = await session.execute(query) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
101 history = result.scalars().first() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
102 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
103 return history |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
104 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3886
diff
changeset
|
105 async def _message_received_trigger( |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 self, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 client: SatXMPPEntity, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 message_elt: domish.Element, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
109 post_treat: defer.Deferred, |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 ) -> bool: |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
111 reactions_elt = next(message_elt.elements(NS_REACTIONS, "reactions"), None) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
112 if reactions_elt is not None: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
113 reaction_id = reactions_elt.getAttribute("id") |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
114 history = await self.get_history_from_reaction_id( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
115 client, message_elt, reaction_id |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
116 ) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
117 if history is None: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
118 log.warning( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
119 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
|
120 ) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
121 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
122 if not reaction_id: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
123 log.warning(f"Invalid reaction: {reactions_elt.toXml()}") |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
124 return False |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
125 from_jid = jid.JID(message_elt["from"]) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
126 reactions = set() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
127 for reaction_elt in reactions_elt.elements("reaction"): |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
128 reaction = str(reaction_elt) |
4200
5d2de6c1156d
plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents:
4163
diff
changeset
|
129 if ( |
5d2de6c1156d
plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents:
4163
diff
changeset
|
130 not emoji.purely_emoji(reaction) |
5d2de6c1156d
plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents:
4163
diff
changeset
|
131 or emoji.emoji_count(reaction) != 1 |
5d2de6c1156d
plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents:
4163
diff
changeset
|
132 ): |
5d2de6c1156d
plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents:
4163
diff
changeset
|
133 log.warning(f"Ignoring invalid reaction: {reaction_elt.toXml()}.") |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
134 continue |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
135 reactions.add(reaction) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
136 await self.update_history_reactions(client, history, from_jid, reactions) |
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 return False |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
139 |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 return True |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
142 def _reactions_set( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
143 self, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
144 message_id: str, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
145 reactions: List[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
146 update_type: str = "replace", |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
147 profile: str = C.PROF_KEY_NONE, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
148 ) -> defer.Deferred[None]: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3886
diff
changeset
|
149 client = self.host.get_client(profile) |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 return defer.ensureDeferred( |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
151 self.set_reactions(client, message_id, reactions, update_type) |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 ) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
154 async def get_history_from_uid( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
155 self, client: SatXMPPEntity, message_id: str |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
156 ) -> History: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
157 """Retrieve the chat history associated with a given message ID. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
158 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
159 @param message_id: The Libervia specific identifier of the message |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
160 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
161 @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
|
162 subjects related to the specified message ID. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
163 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
164 @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
|
165 not found in the database. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
166 """ |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
167 history = await self.host.memory.storage.get( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
168 client, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
169 History, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
170 History.uid, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
171 message_id, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
172 ) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
173 if history is None: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
174 raise exceptions.NotFound( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
175 f"message to retract not found in database ({message_id})" |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
176 ) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
177 return history |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
178 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3886
diff
changeset
|
179 def send_reactions( |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 self, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 client: SatXMPPEntity, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 dest_jid: jid.JID, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
183 message_type: str, |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 message_id: str, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
185 reactions: Iterable[str], |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 ) -> None: |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 """Send the <message> stanza containing the reactions |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 @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
|
190 @param message_id: either origin ID or stanza ID |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 see https://xmpp.org/extensions/xep-0444.html#business-id |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 """ |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 message_elt = domish.Element((None, "message")) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 message_elt["from"] = client.jid.full() |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 message_elt["to"] = dest_jid.full() |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
196 message_elt["type"] = message_type |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 reactions_elt = message_elt.addElement((NS_REACTIONS, "reactions")) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 reactions_elt["id"] = message_id |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 for r in set(reactions): |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 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
|
201 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
|
202 client.send(message_elt) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
204 def convert_to_replace_update( |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 self, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
206 current_reactions: set[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
207 reactions_new: Iterable[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
208 update_type: str, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
209 ) -> set[str]: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
210 """Convert the given update to a replace update. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
211 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
212 @param current_reactions: reaction of reacting JID before update |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
213 @param reactions_new: New reactions to be updated. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
214 @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
|
215 @return: reactions for a replace operation. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
216 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
217 @raise ValueError: invalid ``update_type``. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
218 """ |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
219 new_reactions_set = set(reactions_new) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
220 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
221 if update_type == "replace": |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
222 return new_reactions_set |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
223 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
224 if update_type == "add": |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
225 replace_reactions = current_reactions | new_reactions_set |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
226 elif update_type == "remove": |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
227 replace_reactions = current_reactions - new_reactions_set |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
228 elif update_type == "toggle": |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
229 replace_reactions = current_reactions ^ new_reactions_set |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
230 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
231 raise ValueError(f"Invalid update type: {update_type!r}") |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
232 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
233 return replace_reactions |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
234 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
235 async def update_history_reactions( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
236 self, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
237 client: SatXMPPEntity, |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 history: History, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
239 reacting_jid: jid.JID, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
240 reactions_new: Iterable[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
241 update_type: str = "replace", |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
242 store: bool = True, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
243 ) -> set[str]: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
244 """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
|
245 |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
246 @param history: storage History instance to be updated |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
247 @param reacting_jid: author of the reactions |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
248 @param reactions_new: new reactions to update |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
249 @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
|
250 @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
|
251 signal with the new reactions. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
252 @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
|
253 """ |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
254 # FIXME: handle race conditions |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
255 if history.type == C.MESS_TYPE_GROUPCHAT: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
256 entity_jid_s = reacting_jid.full() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
257 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
258 entity_jid_s = reacting_jid.userhost() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
259 if history.extra is None: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
260 history.extra = {} |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
261 extra = history.extra |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
262 reactions = extra.get("reactions", {}) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
263 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
264 current_reactions = { |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
265 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
|
266 } |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
267 reactions_replace_set = self.convert_to_replace_update( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
268 current_reactions, reactions_new, update_type |
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 for reaction in current_reactions - reactions_replace_set: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
272 reaction_jids = reactions[reaction] |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
273 reaction_jids.remove(entity_jid_s) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
274 if not reaction_jids: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
275 del reactions[reaction] |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
276 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
277 for reaction in reactions_replace_set - current_reactions: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
278 reactions.setdefault(reaction, []).append(entity_jid_s) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
279 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
280 # we want to have a constant order in reactions |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
281 extra["reactions"] = dict(sorted(reactions.items())) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
282 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
283 if store: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
284 # 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
|
285 # is for some reason not working here. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
286 flag_modified(history, "extra") |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
287 await self.host.memory.storage.add(history) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
288 # we send the signal for frontends update |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
289 data = MessageReactionData(reactions=extra["reactions"]) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
290 self.host.bridge.message_update( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
291 history.uid, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
292 C.MESS_UPDATE_REACTION, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
293 data.model_dump_json(), |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
294 client.profile, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
295 ) |
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 return reactions_replace_set |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3886
diff
changeset
|
299 async def set_reactions( |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 self, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 client: SatXMPPEntity, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 message_id: str, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
303 reactions: Iterable[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
304 update_type: str = "replace", |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 ) -> None: |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 """Set and replace reactions to a message |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 @param message_id: internal ID of the message |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
309 @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
|
310 use empty list to remove all reactions |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
311 @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
|
312 "add", "remove" or "toggle". |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 """ |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 if not message_id: |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 raise ValueError("message_id can't be empty") |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
316 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
317 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
|
318 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
|
319 mess_id = history.stanza_id |
5d2de6c1156d
plugin XEP-0444: fix emoji check and ID used
Goffi <goffi@goffi.org>
parents:
4163
diff
changeset
|
320 else: |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
321 mess_id = history.origin_id |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
322 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
323 if mess_id is None: |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 raise exceptions.DataError( |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 "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
|
326 "reaction" |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 ) |
4157
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 if history.source == client.jid.userhost(): |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
330 dest_jid = history.dest_jid |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
331 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
332 dest_jid = history.source_jid |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
333 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
334 if history.type == C.MESS_TYPE_GROUPCHAT: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
335 # the reaction is for the chat room itself |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
336 dest_jid = dest_jid.userhostJID() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
337 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
|
338 # 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
|
339 # the reaction |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
340 store = False |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
341 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
342 # 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
|
343 if self._m.is_room(client, dest_jid): |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
344 # 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
|
345 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
|
346 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
347 # 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
|
348 reacting_jid = client.jid.userhostJID() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
349 dest_jid = dest_jid.userhostJID() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
350 store = True |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
351 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
352 reaction_replace_set = await self.update_history_reactions( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
353 client, history, reacting_jid, reactions, update_type, store |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
354 ) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
355 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
356 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
|
357 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 @implementer(iwokkel.IDisco) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 class XEP_0444_Handler(xmlstream.XMPPHandler): |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 def getDiscoInfo(self, requestor, service, nodeIdentifier=""): |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 return [disco.DiscoFeature(NS_REACTIONS)] |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
363 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
364 def getDiscoItems(self, requestor, service, nodeIdentifier=""): |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
365 return [] |