annotate libervia/backend/plugins/plugin_xep_0461.py @ 4380:2e3ce128973c

minor type update and unused import removal.
author Goffi <goffi@goffi.org>
date Fri, 04 Jul 2025 12:28:40 +0200
parents 0eaa50f21efb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4370
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin for Extended Channel Search (XEP-0461)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import TYPE_CHECKING, Any, Final, Self, cast
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from pydantic import BaseModel, Field
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.internet import defer
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber import jid
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco, iwokkel
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implementer
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from libervia.backend import G
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from libervia.backend.core import exceptions
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from libervia.backend.core.constants import Const as C
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from libervia.backend.core.core_types import SatXMPPEntity
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from libervia.backend.core.i18n import _
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from libervia.backend.core.log import getLogger
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from libervia.backend.memory.sqla_mapping import History
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from libervia.backend.models.core import MessageData
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from libervia.backend.models.types import JIDType
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from libervia.backend.tools.utils import ensure_deferred
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 if TYPE_CHECKING:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from libervia.backend.core.main import LiberviaBackend
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 log = getLogger(__name__)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 PLUGIN_INFO = {
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_NAME: "Message Replies",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_IMPORT_NAME: "XEP-0461",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_TYPE: "XEP",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_MODES: C.PLUG_MODE_BOTH,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_DEPENDENCIES: [],
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_RECOMMENDATIONS: [],
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 C.PI_MAIN: "XEP_0461",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 C.PI_HANDLER: "yes",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 C.PI_DESCRIPTION: _("Message replies support."),
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 }
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 NS_REPLY = "urn:xmpp:reply:0"
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 host: "LiberviaBackend | None" = None
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 class ReplyTo(BaseModel):
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 to: JIDType | None = None
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 id: str
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 internal_uid: bool = Field(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 default=True,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 description="True if the id is a message UID, i.e. Libervia internal ID of "
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "message, otherwise it's an XMPP stanza ID or origin ID as specified in the XEP"
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 async def ensure_xmpp_id(self, client: SatXMPPEntity) -> None:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 if not self.internal_uid:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 history = await G.storage.get(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 client,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 History,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 History.uid,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self.id,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 if history.uid != self.id:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 raise exceptions.InternalError(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 f"Inconsistency between given history UID {history.uid!r} and ReplyTo "
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 f"id ({self.id!r})."
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 if history.type == C.MESS_TYPE_GROUPCHAT:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.id = history.stanza_id
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self.id = history.origin_id
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 assert self.id
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.internal_uid = False
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 @classmethod
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 def from_element(cls, reply_elt: domish.Element) -> Self:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 """Create a ReplyTo instance from a <reply> element or its parent.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 @param reply_elt: The <reply> element or a parent element.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 @return: ReplyTo instance.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 @raise exceptions.NotFound: If the <reply> element is not found.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 """
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if reply_elt.uri != NS_REPLY or reply_elt.name != "reply":
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 child_file_metadata_elt = next(reply_elt.elements(NS_REPLY, "reply"), None)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 if child_file_metadata_elt is None:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 raise exceptions.NotFound("<reply> element not found")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 reply_elt = child_file_metadata_elt
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 message_id = reply_elt["id"]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 except KeyError:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 raise exceptions.DataError('Missing "id" attribute.')
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 return cls(to=reply_elt.getAttribute("to"), id=message_id, internal_uid=False)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def to_element(self) -> domish.Element:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 """Build the <reply> element from this instance's data.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 @return: <reply> element.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 """
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 if self.internal_uid:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 raise exceptions.DataError(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 '"id" must be converted to XMPP id before calling "to_element". Please '
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 '"use ensure_xmpp_id" first.')
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 reply_elt = domish.Element((NS_REPLY, "reply"), attribs={"id": self.id})
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if self.to is not None:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 reply_elt["to"] = self.to.full()
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 return reply_elt
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 class XEP_0461:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 """Implementation of XEP-0461 Message Replies."""
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 namespace: Final[str] = NS_REPLY
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 def __init__(self, host: Any):
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 log.info(f"Plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization.")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 self.host = host
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 host.register_namespace("reply", NS_REPLY)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 host.trigger.add("message_received", self._message_received_trigger)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 host.trigger.add("sendMessage", self._sendMessage_trigger)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 def get_handler(self, client):
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 return XEP_0461_handler()
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 async def _parse_reply(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 self, client: SatXMPPEntity, message_elt: domish.Element, mess_data: MessageData
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 ) -> MessageData:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 reply_to = ReplyTo.from_element(message_elt)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 except exceptions.NotFound:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 pass
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 thread_id = mess_data["extra"]["thread"]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 except KeyError:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 pass
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 message_type = message_elt.getAttribute("type")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 storage = G.storage
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 history = await storage.get_history_from_xmpp_id(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 client, reply_to.id, message_type
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 if history is None:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 log.warning(f"Received a <reply> to an unknown message: {reply_to!r}")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 if not history.thread:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 await storage.add_thread(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 history.uid, thread_id, None, is_retroactive=True
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 # We can to use internal UID to make frontends life easier.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 reply_to.id = history.uid
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 reply_to.internal_uid = True
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 # FIXME: We use mode=json to have a serialisable data in storage. When
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 # Pydantic models are fully integrated with MessageData and storage, the
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 # ReplyTo model should be used directly here instead.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 mess_data["extra"]["reply"] = reply_to.model_dump(mode="json")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 return mess_data
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 def _message_received_trigger(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 self,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 client: SatXMPPEntity,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 message_elt: domish.Element,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 post_treat: defer.Deferred,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 ) -> bool:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 post_treat.addCallback(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 lambda mess_data: defer.ensureDeferred(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 self._parse_reply(client, message_elt, mess_data)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 return True
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 @ensure_deferred
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 async def _add_reply_to_elt(self, mess_data: MessageData, client: SatXMPPEntity) -> MessageData:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 # FIXME: Once MessageData is fully moved to Pydantic, we should have directly
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 # the ReplyTo instance here.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 reply_to = ReplyTo(**mess_data["extra"]["reply"])
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 except KeyError:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 log.error('"_add_reply_to_elt" should not be called when there is no reply.')
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 await reply_to.ensure_xmpp_id(client)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 message_elt: domish.Element = mess_data["xml"]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 message_elt.addChild(reply_to.to_element())
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 return mess_data
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 def _sendMessage_trigger(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 self,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 client: SatXMPPEntity,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 mess_data: MessageData,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 pre_xml_treatments: defer.Deferred,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 post_xml_treatments: defer.Deferred,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 ) -> bool:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 extra = mess_data["extra"]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 reply_to = ReplyTo(**extra["reply"])
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 except KeyError:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 pass
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 if "thread" not in extra:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 # FIXME: once MessageData is fully moved to Pydantic, ReplyTo should be
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 # used directly here instead of a dump.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 extra["reply"] = reply_to.model_dump(mode="json")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 # We use parent message ID as thread ID.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 extra["thread"] = reply_to.id
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 post_xml_treatments.addCallback(self._add_reply_to_elt, client)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 return True
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
228
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 @implementer(iwokkel.IDisco)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 class XEP_0461_handler(XMPPHandler):
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
231
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 def getDiscoInfo(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 ) -> list[disco.DiscoFeature]:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 return [disco.DiscoFeature(NS_REPLY)]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
236
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 def getDiscoItems(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 ) -> list[disco.DiscoItems]:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 return []