annotate libervia/backend/plugins/plugin_xep_0461.py @ 4371:ed683d56b64c default tip

test (XEP-0461): some tests for XEP-0461: rel 457
author Goffi <goffi@goffi.org>
date Tue, 06 May 2025 00:34:01 +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 import Storage
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from libervia.backend.memory.sqla_mapping import History
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from libervia.backend.models.core import MessageData
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from libervia.backend.models.types import JIDType
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from libervia.backend.tools.utils import ensure_deferred
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 if TYPE_CHECKING:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from libervia.backend.core.main import LiberviaBackend
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 log = getLogger(__name__)
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
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 PLUGIN_INFO = {
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_NAME: "Message Replies",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_IMPORT_NAME: "XEP-0461",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_TYPE: "XEP",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_MODES: C.PLUG_MODE_BOTH,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_DEPENDENCIES: [],
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 C.PI_RECOMMENDATIONS: [],
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 C.PI_MAIN: "XEP_0461",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 C.PI_HANDLER: "yes",
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 C.PI_DESCRIPTION: _("Message replies support."),
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
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 NS_REPLY = "urn:xmpp:reply:0"
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 host: "LiberviaBackend | None" = None
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
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 class ReplyTo(BaseModel):
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 to: JIDType | None = None
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 id: str
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 internal_uid: bool = Field(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 default=True,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 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
67 "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
68 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 async def ensure_xmpp_id(self, client: SatXMPPEntity) -> None:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if not self.internal_uid:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 history = await G.storage.get(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 client,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 History,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 History.uid,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.id,
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
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 if history.uid != self.id:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 raise exceptions.InternalError(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 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
83 f"id ({self.id!r})."
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if history.type == C.MESS_TYPE_GROUPCHAT:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.id = history.stanza_id
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.id = history.origin_id
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 assert self.id
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.internal_uid = False
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 @classmethod
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def from_element(cls, reply_elt: domish.Element) -> Self:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 """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
95
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 @param reply_elt: The <reply> element or a parent element.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 @return: ReplyTo instance.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 @raise exceptions.NotFound: If the <reply> element is not found.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 """
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 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
101 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
102 if child_file_metadata_elt is None:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 raise exceptions.NotFound("<reply> element not found")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 reply_elt = child_file_metadata_elt
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 message_id = reply_elt["id"]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 except KeyError:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 raise exceptions.DataError('Missing "id" attribute.')
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 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
113
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 def to_element(self) -> domish.Element:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 """Build the <reply> element from this instance's data.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 @return: <reply> element.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 """
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 if self.internal_uid:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 raise exceptions.DataError(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 '"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
122 '"use ensure_xmpp_id" first.')
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 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
124 if self.to is not None:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 reply_elt["to"] = self.to.full()
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 return reply_elt
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
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 class XEP_0461:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 """Implementation of XEP-0461 Message Replies."""
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 namespace: Final[str] = NS_REPLY
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 def __init__(self, host: Any):
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 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
136 self.host = host
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 host.register_namespace("reply", NS_REPLY)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 host.trigger.add("message_received", self._message_received_trigger)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 host.trigger.add("sendMessage", self._sendMessage_trigger)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def get_handler(self, client):
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 return XEP_0461_handler()
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 async def _parse_reply(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 self, client: SatXMPPEntity, message_elt: domish.Element, mess_data: MessageData
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 ) -> MessageData:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 reply_to = ReplyTo.from_element(message_elt)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 except exceptions.NotFound:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 pass
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 thread_id = mess_data["extra"]["thread"]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 except KeyError:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 pass
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 message_type = message_elt.getAttribute("type")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 storage = G.storage
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 history = await storage.get_history_from_xmpp_id(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 client, reply_to.id, message_type
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if history is None:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 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
164 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 if not history.thread:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 await storage.add_thread(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 history.uid, thread_id, None, is_retroactive=True
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 # 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
170 reply_to.id = history.uid
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 reply_to.internal_uid = True
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 # 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
173 # Pydantic models are fully integrated with MessageData and storage, the
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 # ReplyTo model should be used directly here instead.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 mess_data["extra"]["reply"] = reply_to.model_dump(mode="json")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 return mess_data
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 def _message_received_trigger(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 self,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 client: SatXMPPEntity,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 message_elt: domish.Element,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 post_treat: defer.Deferred,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 ) -> bool:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 post_treat.addCallback(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 lambda mess_data: defer.ensureDeferred(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 self._parse_reply(client, message_elt, mess_data)
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 )
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 return True
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 @ensure_deferred
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 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
193 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 # 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
195 # the ReplyTo instance here.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 reply_to = ReplyTo(**mess_data["extra"]["reply"])
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 except KeyError:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 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
199 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 await reply_to.ensure_xmpp_id(client)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 message_elt: domish.Element = mess_data["xml"]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 message_elt.addChild(reply_to.to_element())
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 return mess_data
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 def _sendMessage_trigger(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 self,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 client: SatXMPPEntity,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 mess_data: MessageData,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 pre_xml_treatments: defer.Deferred,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 post_xml_treatments: defer.Deferred,
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 ) -> bool:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 try:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 extra = mess_data["extra"]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 reply_to = ReplyTo(**extra["reply"])
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 except KeyError:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 pass
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 else:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 if "thread" not in extra:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 # 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
221 # used directly here instead of a dump.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 extra["reply"] = reply_to.model_dump(mode="json")
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 # We use parent message ID as thread ID.
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 extra["thread"] = reply_to.id
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 post_xml_treatments.addCallback(self._add_reply_to_elt, client)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 return True
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
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 @implementer(iwokkel.IDisco)
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 class XEP_0461_handler(XMPPHandler):
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 def getDiscoInfo(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 ) -> list[disco.DiscoFeature]:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 return [disco.DiscoFeature(NS_REPLY)]
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
237
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 def getDiscoItems(
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 ) -> list[disco.DiscoItems]:
0eaa50f21efb plugin XEP-0461: Message Replies implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 return []