annotate libervia/backend/plugins/plugin_xep_0424.py @ 4230:314d3c02bb67

core (xmpp): Add a timeout for messages processing to avoid blocking the queue.
author Goffi <goffi@goffi.org>
date Sat, 06 Apr 2024 12:21:04 +0200
parents 7eda7cb8a15c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # This program is free software: you can redistribute it and/or modify
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # it under the terms of the GNU Affero General Public License as published by
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # (at your option) any later version.
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # This program is distributed in the hope that it will be useful,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # GNU Affero General Public License for more details.
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # You should have received a copy of the GNU Affero General Public License
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 import time
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
19 from typing import Any, Dict
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
21 from sqlalchemy.orm.attributes import flag_modified
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
22 from twisted.internet import defer
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
23 from twisted.words.protocols.jabber import jid, xmlstream
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implementer
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
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
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
30 from libervia.backend.core.core_types import MessageData, SatXMPPEntity
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
31 from libervia.backend.core.i18n import D_, _
4071
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
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
34 from libervia.backend.tools.common import data_format
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 log = getLogger(__name__)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 PLUGIN_INFO = {
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_NAME: "Message Retraction",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_IMPORT_NAME: "XEP-0424",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_TYPE: "XEP",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_MODES: C.PLUG_MODE_BOTH,
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
44 C.PI_PROTOCOLS: ["XEP-0424"],
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
45 C.PI_DEPENDENCIES: ["XEP-0334", "XEP-0428"],
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
46 C.PI_RECOMMENDATIONS: ["XEP-0045"],
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_MAIN: "XEP_0424",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_HANDLER: "yes",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_DESCRIPTION: _("""Implementation Message Retraction"""),
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 }
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
52 NS_MESSAGE_RETRACT = "urn:xmpp:message-retract:1"
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 CATEGORY = "Privacy"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 NAME = "retract_history"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 LABEL = D_("Keep History of Retracted Messages")
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 PARAMS = """
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 <params>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 <individual>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 <category name="{category_name}">
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 <param name="{name}" label="{label}" type="bool" value="false" />
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 </category>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 </individual>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 </params>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 """.format(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 category_name=CATEGORY, name=NAME, label=_(LABEL)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
70 class XEP_0424:
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def __init__(self, host):
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
72 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
74 host.memory.update_params(PARAMS)
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self._h = host.plugins["XEP-0334"]
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
76 self._m = host.plugins.get("XEP-0045")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
77 host.register_namespace("message-retract", NS_MESSAGE_RETRACT)
4051
c23cad65ae99 core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
78 host.trigger.add("message_received", self._message_received_trigger, 100)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
79 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
80 "message_retract",
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 ".plugin",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 in_sign="ss",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 out_sign="",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 method=self._retract,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 async_=True,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
88 def get_handler(self, __):
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return XEP_0424_handler()
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 def _retract(self, message_id: str, profile: str) -> None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
92 client = self.host.get_client(profile)
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
93 return defer.ensureDeferred(self.retract(client, message_id))
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
95 def send_retract(
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
96 self, client: SatXMPPEntity, peer_jid: jid.JID, retract_id: str, history: History
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 ) -> None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 """Send a message retraction using origin-id
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 [retract] should be prefered: internal ID should be used as it is independant of
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 XEPs changes. However, in some case messages may not be stored in database
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 (notably for some components), and then this method can be used
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
103
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
104 @param retract_id: ID (origin or stanza according to XEP-0359) of message to
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
105 retract
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
106 @param history: history instance of the message to retract
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 """
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 message_elt = domish.Element((None, "message"))
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 message_elt["from"] = client.jid.full()
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
110 message_elt["to"] = peer_jid.full()
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
111 message_elt["type"] = history.type
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
112 retract_elt = message_elt.addElement((NS_MESSAGE_RETRACT, "retract"))
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
113 retract_elt["id"] = retract_id
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
114 self.host.plugins["XEP-0428"].add_fallback_elt(
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 message_elt,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 "[A message retraction has been requested, but your client doesn't support "
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
117 "it]",
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
119 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE])
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 client.send(message_elt)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
122 async def retract_by_history(self, client: SatXMPPEntity, history: History) -> None:
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 """Send a message retraction using History instance
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 This method is to use instead of [retract] when the history instance is already
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 retrieved. Note that the instance must have messages and subjets loaded
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 @param history: history instance of the message to retract
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 """
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
129 if history.type == C.MESS_TYPE_GROUPCHAT:
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
130 is_group_chat = True
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
131 peer_jid = jid.JID(history.source)
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
132 retract_id = history.stanza_id
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 else:
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
134 is_group_chat = False
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
135 peer_jid = jid.JID(history.dest)
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
136 if self._m is not None and self._m.is_joined_room(client, peer_jid):
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
137 # it's actually a private MUC message, we need the full JID.
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
138 peer_jid = history.dest_jid
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
139 retract_id = history.origin_id
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
140
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
141 if not retract_id:
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
142 raise exceptions.FeatureNotFound(
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
143 "Message to retract doesn't have the necessary ID, the sending client is "
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
144 "probably not supporting message retraction."
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
145 )
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
146
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
147 self.send_retract(client, peer_jid, retract_id, history)
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
148 if not is_group_chat:
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
149 # retraction will happen when <retract> message will be received in the
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
150 # chat.
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
151 await self.retract_db_history(client, history)
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 async def retract(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 self,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 client: SatXMPPEntity,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 message_id: str,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 ) -> None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 """Send a message retraction request
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 @param message_id: ID of the message
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 This ID is the Libervia internal ID of the message. It will be retrieve from
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 database to find the ID used by XMPP (i.e. XEP-0359's "origin ID"). If the
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 message is not found in database, an exception will be raised
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 """
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 if not message_id:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 raise ValueError("message_id can't be empty")
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 history = await self.host.memory.storage.get(
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
168 client,
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
169 History,
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
170 History.uid,
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
171 message_id,
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
172 joined_loads=[History.messages, History.subjects, History.thread],
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if history is None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 raise exceptions.NotFound(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 f"message to retract not found in database ({message_id})"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
178 await self.retract_by_history(client, history)
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
180 async def retract_db_history(self, client, history: History) -> None:
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 """Mark an history instance in database as retracted
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 @param history: history instance
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 "messages" and "subjects" must be loaded too
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 """
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
186 # FIXME: should we keep history? This is useful to check why a message has been
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 # retracted, but if may be bad if the user think it's really deleted
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
188 flag_modified(history, "extra")
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
189 keep_history = await self.host.memory.param_get_a_async(
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 NAME, CATEGORY, profile_key=client.profile
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 )
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
192 old_version: Dict[str, Any] = {"timestamp": time.time()}
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 if keep_history:
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
194 old_version.update(
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
195 {
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
196 "messages": [m.serialise() for m in history.messages],
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
197 "subjects": [s.serialise() for s in history.subjects],
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
198 }
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
199 )
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 history.extra.setdefault("old_versions", []).append(old_version)
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
202
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
203 history.messages.clear()
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
204 history.subjects.clear()
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
205 history.extra["retracted"] = True
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
206 # we remove editions history to keep no trace of old messages
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
207 if "editions" in history.extra:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
208 del history.extra["editions"]
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
209 if "attachments" in history.extra:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
210 del history.extra["attachments"]
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
211 await self.host.memory.storage.add(history)
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
212
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
213 retract_data = MessageData(history.serialise())
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
214 self.host.bridge.message_update(
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
215 history.uid,
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
216 C.MESS_UPDATE_RETRACT,
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
217 data_format.serialise(retract_data),
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
218 client.profile,
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
221 async def _message_received_trigger(
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 self,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 client: SatXMPPEntity,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 message_elt: domish.Element,
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
225 post_treat: defer.Deferred,
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 ) -> bool:
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
227 retract_elt = next(message_elt.elements(NS_MESSAGE_RETRACT, "retract"), None)
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
228 if not retract_elt:
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 return True
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
230 try:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
231 if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
232 col_id = History.stanza_id
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
233 else:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
234 col_id = History.origin_id
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
235 history = await self.host.memory.storage.get(
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
236 client,
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
237 History,
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
238 col_id,
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
239 retract_elt["id"],
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
240 joined_loads=[History.messages, History.subjects, History.thread],
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
241 )
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
242 except KeyError:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
243 log.warning(f"invalid retract element, missing id: {retract_elt.toXml()}")
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
244 return False
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
245 from_jid = jid.JID(message_elt["from"])
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
246
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
247 if history is not None and (
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
248 (history.type == C.MESS_TYPE_GROUPCHAT and history.source_jid != from_jid)
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
249 or (
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
250 history.type != C.MESS_TYPE_GROUPCHAT
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
251 and history.source_jid.userhostJID() != from_jid.userhostJID()
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
252 )
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
253 ):
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
254 log.warning(
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
255 f"Received message retraction from {from_jid.full()}, but the message to "
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
256 f"retract is from {history.source_jid.full()}. This maybe a hack "
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
257 f"attempt.\n{message_elt.toXml()}"
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
258 )
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
259 return False
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
260
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
261 if not await self.host.trigger.async_point(
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
262 "XEP-0424_retract_received", client, message_elt, retract_elt, history
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 ):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 return False
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
265
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
266 if history is None:
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 # we check history after the trigger because we may be in a component which
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 # doesn't store messages in database.
4199
7eda7cb8a15c plugin XEP-0424: fix ID used and recipients
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
269 log.warning(f"No message found with given id: {message_elt.toXml()}")
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 return False
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
271 log.info(f"[{client.profile}] retracting message {history.uid!r}")
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
272 await self.retract_db_history(client, history)
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 return False
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
274
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
275
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 @implementer(disco.IDisco)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 class XEP_0424_handler(xmlstream.XMPPHandler):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 def getDiscoInfo(self, __, target, nodeIdentifier=""):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 return [disco.DiscoFeature(NS_MESSAGE_RETRACT)]
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
280
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 return []