Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0444.py @ 4161:2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
add `cascade=delete-orphan` to History's `messages`, `subjects` and `thread`, to make
modification of those attributes easier.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 28 Nov 2023 17:35:20 +0100 |
parents | 04cdcb3fd713 |
children | 3b3cd9453d9b |
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): |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 log.info(_("Message Reactions 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: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
91 if message_elt.type == C.MESS_TYPE_GROUPCHAT: |
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) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
129 if not emoji.is_emoji(reaction): |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
130 log.warning(f"ignoring invalide reaction: {reaction_elt.toXml()}") |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
131 continue |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
132 reactions.add(reaction) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
133 await self.update_history_reactions(client, history, from_jid, reactions) |
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 False |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
136 |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 return True |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
139 def _reactions_set( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
140 self, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
141 message_id: str, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
142 reactions: List[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
143 update_type: str = "replace", |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
144 profile: str = C.PROF_KEY_NONE, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
145 ) -> defer.Deferred[None]: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3886
diff
changeset
|
146 client = self.host.get_client(profile) |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 return defer.ensureDeferred( |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
148 self.set_reactions(client, message_id, reactions, update_type) |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 ) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
151 async def get_history_from_uid( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
152 self, client: SatXMPPEntity, message_id: str |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
153 ) -> History: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
154 """Retrieve the chat history associated with a given message ID. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
155 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
156 @param message_id: The Libervia specific identifier of the message |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
157 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
158 @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
|
159 subjects related to the specified message ID. |
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 @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
|
162 not found in the database. |
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 history = await self.host.memory.storage.get( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
165 client, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
166 History, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
167 History.uid, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
168 message_id, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
169 ) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
170 if history is None: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
171 raise exceptions.NotFound( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
172 f"message to retract not found in database ({message_id})" |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
173 ) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
174 return history |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
175 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3886
diff
changeset
|
176 def send_reactions( |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 self, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 client: SatXMPPEntity, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 dest_jid: jid.JID, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
180 message_type: str, |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 message_id: str, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
182 reactions: Iterable[str], |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 ) -> None: |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 """Send the <message> stanza containing the reactions |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 @param dest_jid: recipient of the reaction |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 @param message_id: either <origin-id> or message's ID |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 see https://xmpp.org/extensions/xep-0444.html#business-id |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 """ |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 message_elt = domish.Element((None, "message")) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 message_elt["from"] = client.jid.full() |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 message_elt["to"] = dest_jid.full() |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
193 message_elt["type"] = message_type |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 reactions_elt = message_elt.addElement((NS_REACTIONS, "reactions")) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 reactions_elt["id"] = message_id |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 for r in set(reactions): |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 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
|
198 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
|
199 client.send(message_elt) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
201 def convert_to_replace_update( |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 self, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
203 current_reactions: set[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
204 reactions_new: Iterable[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
205 update_type: str, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
206 ) -> set[str]: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
207 """Convert the given update to a replace update. |
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 @param current_reactions: reaction of reacting JID before update |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
210 @param reactions_new: New reactions to be updated. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
211 @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
|
212 @return: reactions for a replace operation. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
213 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
214 @raise ValueError: invalid ``update_type``. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
215 """ |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
216 new_reactions_set = set(reactions_new) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
217 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
218 if update_type == "replace": |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
219 return new_reactions_set |
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 == "add": |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
222 replace_reactions = current_reactions | new_reactions_set |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
223 elif update_type == "remove": |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
224 replace_reactions = current_reactions - new_reactions_set |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
225 elif update_type == "toggle": |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
226 replace_reactions = current_reactions ^ new_reactions_set |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
227 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
228 raise ValueError(f"Invalid update type: {update_type!r}") |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
229 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
230 return replace_reactions |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
231 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
232 async def update_history_reactions( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
233 self, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
234 client: SatXMPPEntity, |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 history: History, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
236 reacting_jid: jid.JID, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
237 reactions_new: Iterable[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
238 update_type: str = "replace", |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
239 store: bool = True, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
240 ) -> set[str]: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
241 """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
|
242 |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
243 @param history: storage History instance to be updated |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
244 @param reacting_jid: author of the reactions |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
245 @param reactions_new: new reactions to update |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
246 @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
|
247 @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
|
248 signal with the new reactions. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
249 @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
|
250 """ |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
251 # FIXME: handle race conditions |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
252 if history.type == C.MESS_TYPE_GROUPCHAT: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
253 entity_jid_s = reacting_jid.full() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
254 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
255 entity_jid_s = reacting_jid.userhost() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
256 if history.extra is None: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
257 history.extra = {} |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
258 extra = history.extra |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
259 reactions = extra.get("reactions", {}) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
260 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
261 current_reactions = { |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
262 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
|
263 } |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
264 reactions_replace_set = self.convert_to_replace_update( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
265 current_reactions, reactions_new, update_type |
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 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
268 for reaction in current_reactions - reactions_replace_set: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
269 reaction_jids = reactions[reaction] |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
270 reaction_jids.remove(entity_jid_s) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
271 if not reaction_jids: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
272 del reactions[reaction] |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
273 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
274 for reaction in reactions_replace_set - current_reactions: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
275 reactions.setdefault(reaction, []).append(entity_jid_s) |
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 # we want to have a constant order in reactions |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
278 extra["reactions"] = dict(sorted(reactions.items())) |
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 if store: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
281 # 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
|
282 # is for some reason not working here. |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
283 flag_modified(history, "extra") |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
284 await self.host.memory.storage.add(history) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
285 # we send the signal for frontends update |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
286 data = MessageReactionData(reactions=extra["reactions"]) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
287 self.host.bridge.message_update( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
288 history.uid, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
289 C.MESS_UPDATE_REACTION, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
290 data.model_dump_json(), |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
291 client.profile, |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
292 ) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
293 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
294 return reactions_replace_set |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3886
diff
changeset
|
296 async def set_reactions( |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
297 self, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 client: SatXMPPEntity, |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 message_id: str, |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
300 reactions: Iterable[str], |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
301 update_type: str = "replace", |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 ) -> None: |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 """Set and replace reactions to a message |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 @param message_id: internal ID of the message |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 @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
|
307 use empty list to remove all reactions |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
308 @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
|
309 "add", "remove" or "toggle". |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 """ |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 if not message_id: |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 raise ValueError("message_id can't be empty") |
4157
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
313 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
314 history = await self.get_history_from_uid(client, message_id) |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
315 if history.origin_id is not None: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
316 mess_id = history.origin_id |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
317 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
318 mess_id = history.stanza_id |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
319 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
320 if mess_id is None: |
3886
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 raise exceptions.DataError( |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 "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
|
323 "reaction" |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 ) |
4157
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 if history.source == client.jid.userhost(): |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
327 dest_jid = history.dest_jid |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
328 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
329 dest_jid = history.source_jid |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
330 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
331 if history.type == C.MESS_TYPE_GROUPCHAT: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
332 # the reaction is for the chat room itself |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
333 dest_jid = dest_jid.userhostJID() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
334 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
|
335 # 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
|
336 # the reaction |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
337 store = False |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
338 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
339 # 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
|
340 if self._m.is_room(client, dest_jid): |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
341 # 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
|
342 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
|
343 else: |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
344 # 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
|
345 reacting_jid = client.jid.userhostJID() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
346 dest_jid = dest_jid.userhostJID() |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
347 store = True |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
348 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
349 reaction_replace_set = await self.update_history_reactions( |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
350 client, history, reacting_jid, reactions, update_type, store |
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 |
04cdcb3fd713
plugin XEP-0444: complete implementation:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
353 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
|
354 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 @implementer(iwokkel.IDisco) |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 class XEP_0444_Handler(xmlstream.XMPPHandler): |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 def getDiscoInfo(self, requestor, service, nodeIdentifier=""): |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 return [disco.DiscoFeature(NS_REACTIONS)] |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 def getDiscoItems(self, requestor, service, nodeIdentifier=""): |
1f88ca90c3de
plugin XEP-0444: Message Reactions implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 return [] |