annotate sat/plugins/plugin_xep_0424.py @ 3886:1f88ca90c3de

plugin XEP-0444: Message Reactions implementation: rel 371
author Goffi <goffi@goffi.org>
date Wed, 31 Aug 2022 17:07:03 +0200
parents 6952a002abc7
children 6090141b1b70
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 from typing import Dict, Any
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 import time
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from copy import deepcopy
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber import xmlstream, jid
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import defer
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
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.core.constants import Const as C
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.core.i18n import _, D_
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sat.core import exceptions
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from sat.core.core_types import SatXMPPEntity
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from sat.core.log import getLogger
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from sat.memory.sqla_mapping import History
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 log = getLogger(__name__)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
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 PLUGIN_INFO = {
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_NAME: "Message Retraction",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_IMPORT_NAME: "XEP-0424",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_TYPE: "XEP",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_MODES: C.PLUG_MODE_BOTH,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_PROTOCOLS: ["XEP-0334", "XEP-0424", "XEP-0428"],
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_DEPENDENCIES: ["XEP-0422"],
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_MAIN: "XEP_0424",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_HANDLER: "yes",
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_DESCRIPTION: _("""Implementation Message Retraction"""),
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 }
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 NS_MESSAGE_RETRACT = "urn:xmpp:message-retract:0"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 CATEGORY = "Privacy"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 NAME = "retract_history"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 LABEL = D_("Keep History of Retracted Messages")
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 PARAMS = """
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 <individual>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 <category name="{category_name}">
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 <param name="{name}" label="{label}" type="bool" value="false" />
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 </category>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 </individual>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 </params>
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 """.format(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 category_name=CATEGORY, name=NAME, label=_(LABEL)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 )
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 class XEP_0424(object):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def __init__(self, host):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 log.info(_("XEP-0424 (Message Retraction) plugin initialization"))
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.host = host
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 host.memory.updateParams(PARAMS)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self._h = host.plugins["XEP-0334"]
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self._f = host.plugins["XEP-0422"]
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 host.registerNamespace("message-retract", NS_MESSAGE_RETRACT)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 host.trigger.add("messageReceived", self._messageReceivedTrigger, 100)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 host.bridge.addMethod(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 "messRetract",
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
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def getHandler(self, __):
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:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 client = self.host.getClient(profile)
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
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def retractByOriginId(
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,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 dest_jid: jid.JID,
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()
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 message_elt["to"] = dest_jid.full()
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 apply_to_elt = self._f.applyToElt(message_elt, origin_id)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 apply_to_elt.addElement((NS_MESSAGE_RETRACT, "retract"))
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 self.host.plugins["XEP-0428"].addFallbackElt(
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 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 self._h.addHintElements(message_elt, [self._h.HINT_STORE])
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
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 async def retractByHistory(
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:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 self.retractByOriginId(client, history.dest_jid, origin_id)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 await self.retractDBHistory(client, history)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 async def retract(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 self,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 client: SatXMPPEntity,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 message_id: str,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 ) -> None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 """Send a message retraction request
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 @param message_id: ID of the message
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 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
153 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
154 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
155 """
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 if not message_id:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 raise ValueError("message_id can't be empty")
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 history = await self.host.memory.storage.get(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 client, History, History.uid, message_id,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 joined_loads=[History.messages, History.subjects]
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if history is None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 raise exceptions.NotFound(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 f"message to retract not found in database ({message_id})"
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 await self.retractByHistory(client, history)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 async def retractDBHistory(self, client, history: History) -> None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 """Mark an history instance in database as retracted
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 @param history: history instance
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 "messages" and "subjects" must be loaded too
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 # FIXME: should be keep history? This is useful to check why a message has been
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 # retracted, but if may be bad if the user think it's really deleted
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 # we assign a new object to be sure to trigger an update
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 history.extra = deepcopy(history.extra) if history.extra else {}
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 history.extra["retracted"] = True
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 keep_history = self.host.memory.getParamA(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 NAME, CATEGORY, profile_key=client.profile
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 old_version: Dict[str, Any] = {
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 "timestamp": time.time()
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 }
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 if keep_history:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 old_version.update({
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 "messages": [m.serialise() for m in history.messages],
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 "subjects": [s.serialise() for s in history.subjects]
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 })
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 history.extra.setdefault("old_versions", []).append(old_version)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 await self.host.memory.storage.delete(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 history.messages + history.subjects,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 session_add=[history]
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 )
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 async def _messageReceivedTrigger(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 self,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 client: SatXMPPEntity,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 message_elt: domish.Element,
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 post_treat: defer.Deferred
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 ) -> bool:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 fastened_elts = await self._f.getFastenedElts(client, message_elt)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 if fastened_elts is None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 return True
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 for elt in fastened_elts.elements:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 if elt.name == "retract" and elt.uri == NS_MESSAGE_RETRACT:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 if fastened_elts.history is not None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 source_jid = fastened_elts.history.source_jid
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 from_jid = jid.JID(message_elt["from"])
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 if source_jid.userhostJID() != from_jid.userhostJID():
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 log.warning(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 f"Received message retraction from {from_jid.full()}, but "
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 f"the message to retract is from {source_jid.full()}. This "
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 f"maybe a hack attempt.\n{message_elt.toXml()}"
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 return False
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 break
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 else:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 return True
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 if not await self.host.trigger.asyncPoint(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 "XEP-0424_retractReceived", client, message_elt, elt, fastened_elts
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 ):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 return False
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 if fastened_elts.history is None:
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 # 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
227 # doesn't store messages in database.
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 log.warning(
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 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
230 )
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 return False
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 log.info(f"[{client.profile}] retracting message {fastened_elts.id!r}")
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 await self.retractDBHistory(client, fastened_elts.history)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 # TODO: send bridge signal
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
235
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 return False
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
237
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
238
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 @implementer(disco.IDisco)
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 class XEP_0424_handler(xmlstream.XMPPHandler):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
241
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 def getDiscoInfo(self, __, target, nodeIdentifier=""):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 return [disco.DiscoFeature(NS_MESSAGE_RETRACT)]
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
244
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
6952a002abc7 plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 return []