annotate libervia/backend/plugins/plugin_xep_0424.py @ 4167:319a0e47dc8b

plugin ad-hoc D-Bus: fix deprecated use of python-dbus: the plugin was using python-dbus which is deprecated in the backend in the favor of TxDBus. Methods calls have been updated, and the plugin works again, but there seems to be still some issues (warnings in the logs). Those will be fixed later.
author Goffi <goffi@goffi.org>
date Fri, 01 Dec 2023 15:22:55 +0100
parents a1f7040b5a15
children 7eda7cb8a15c
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"],
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_MAIN: "XEP_0424",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_HANDLER: "yes",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_DESCRIPTION: _("""Implementation Message Retraction"""),
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 }
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
51 NS_MESSAGE_RETRACT = "urn:xmpp:message-retract:1"
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 CATEGORY = "Privacy"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 NAME = "retract_history"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 LABEL = D_("Keep History of Retracted Messages")
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 PARAMS = """
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 <individual>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 <category name="{category_name}">
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 <param name="{name}" label="{label}" type="bool" value="false" />
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 </category>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 </individual>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 </params>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 """.format(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 category_name=CATEGORY, name=NAME, label=_(LABEL)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 )
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
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
69 class XEP_0424:
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70
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"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
76 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
77 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
78 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
79 "message_retract",
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 ".plugin",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 in_sign="ss",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 out_sign="",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 method=self._retract,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 async_=True,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
87 def get_handler(self, __):
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 return XEP_0424_handler()
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 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
91 client = self.host.get_client(profile)
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 return defer.ensureDeferred(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 self.retract(client, message_id)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
96 def retract_by_origin_id(
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 self,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 client: SatXMPPEntity,
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
99 peer_jid: jid.JID,
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 origin_id: str
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 ) -> None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 """Send a message retraction using origin-id
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 [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
105 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
106 (notably for some components), and then this method can be used
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 @param origin_id: origin-id as specified in XEP-0359
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 """
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 message_elt = domish.Element((None, "message"))
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 message_elt["from"] = client.jid.full()
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
111 message_elt["to"] = peer_jid.full()
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"))
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
113 retract_elt["id"] = origin_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 "
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 "it]"
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
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
122 async def retract_by_history(
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 self,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 client: SatXMPPEntity,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 history: History
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 ) -> None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 """Send a message retraction using History instance
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 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
130 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
131 @param history: history instance of the message to retract
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 """
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 try:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 origin_id = history.origin_id
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 except KeyError:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 raise exceptions.FeatureNotFound(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 f"message to retract doesn't have the necessary origin-id, the sending "
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 "client is probably not supporting message retraction."
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 else:
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
141 if history.type == C.MESS_TYPE_GROUPCHAT:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
142 is_group_chat = True
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
143 peer_jid = history.dest_jid
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
144 else:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
145 is_group_chat = False
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
146 peer_jid = jid.JID(history.dest)
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
147 self.retract_by_origin_id(client, peer_jid, origin_id)
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
148 if not is_group_chat:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
149 # retraction will happen when <retract> message will be received in the
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
150 # chat.
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
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(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 client, History, History.uid, message_id,
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
169 joined_loads=[History.messages, History.subjects, History.thread]
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 if history is None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 raise exceptions.NotFound(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 f"message to retract not found in database ({message_id})"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
175 await self.retract_by_history(client, history)
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
177 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
178 """Mark an history instance in database as retracted
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 @param history: history instance
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 "messages" and "subjects" must be loaded too
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 """
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
183 # 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
184 # 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
185 flag_modified(history, "extra")
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
186 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
187 NAME, CATEGORY, profile_key=client.profile
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 old_version: Dict[str, Any] = {
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 "timestamp": time.time()
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 }
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 if keep_history:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 old_version.update({
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 "messages": [m.serialise() for m in history.messages],
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
195 "subjects": [s.serialise() for s in history.subjects],
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 })
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 history.extra.setdefault("old_versions", []).append(old_version)
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
199
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
200 history.messages.clear()
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
201 history.subjects.clear()
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
202 history.extra["retracted"] = True
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
203 # 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
204 if "editions" in history.extra:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
205 del history.extra["editions"]
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
206 if "attachments" in history.extra:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
207 del history.extra["attachments"]
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
208 await self.host.memory.storage.add(history)
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
209
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
210 retract_data = MessageData(history.serialise())
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
211 self.host.bridge.message_update(
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
212 history.uid,
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
213 C.MESS_UPDATE_RETRACT,
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
214 data_format.serialise(retract_data),
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
215 client.profile,
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
218 async def _message_received_trigger(
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 self,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 client: SatXMPPEntity,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 message_elt: domish.Element,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 post_treat: defer.Deferred
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 ) -> bool:
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
224 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
225 if not retract_elt:
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 return True
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
227 try:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
228 if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
229 col_id = History.stanza_id
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
230 else:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
231 col_id = History.origin_id
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
232 history = await self.host.memory.storage.get(
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
233 client, History, col_id, retract_elt["id"],
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
234 joined_loads=[History.messages, History.subjects, History.thread]
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
235 )
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
236 except KeyError:
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
237 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
238 return False
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
239 from_jid = jid.JID(message_elt["from"])
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
240
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
241 if (
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
242 history is not None
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
243 and history.source_jid.userhostJID() != from_jid.userhostJID()
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
244 ):
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
245 log.warning(
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
246 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
247 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
248 f"attempt.\n{message_elt.toXml()}"
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
249 )
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
250 return False
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
251
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3887
diff changeset
252 if not await self.host.trigger.async_point(
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
253 "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
254 ):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 return False
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
256
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
257 if history is None:
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 # 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
259 # doesn't store messages in database.
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 log.warning(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 f"No message found with given origin-id: {message_elt.toXml()}"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 return False
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
264 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
265 await self.retract_db_history(client, history)
3801
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 return False
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
267
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
268
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 @implementer(disco.IDisco)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 class XEP_0424_handler(xmlstream.XMPPHandler):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
271
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 def getDiscoInfo(self, __, target, nodeIdentifier=""):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 return [disco.DiscoFeature(NS_MESSAGE_RETRACT)]
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 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 return []