Coverage for slidge/core/dispatcher/search.py: 55%
60 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-12-01 19:36 +0000
« prev ^ index » next coverage.py v7.17.0, created at 2026-12-01 19:36 +0000
1from typing import TYPE_CHECKING
3from slixmpp import JID, CoroutineCallback, Iq, StanzaPath
4from slixmpp.exceptions import XMPPError
6from .util import DispatcherMixin, exceptions_to_xmpp_errors
8if TYPE_CHECKING:
9 from slidge.util.types import AnyGateway
12class SearchMixin(DispatcherMixin):
13 __slots__: list[str] = []
15 def __init__(self, xmpp: "AnyGateway") -> None:
16 super().__init__(xmpp)
18 xmpp.plugin["xep_0055"].api.register(self.search_get_form, "search_get_form") # type:ignore[arg-type]
19 xmpp.plugin["xep_0055"].api.register(self._search_query, "search_query") # type:ignore[arg-type]
21 xmpp.plugin["xep_0030"].add_feature("jabber:iq:gateway")
22 xmpp.register_handler(
23 CoroutineCallback(
24 "iq:gateway",
25 StanzaPath("iq/gateway"),
26 self._handle_gateway_iq, # type:ignore[arg-type]
27 )
28 )
30 async def search_get_form(
31 self,
32 _gateway_jid: JID,
33 _node: str,
34 ifrom: JID,
35 iq: Iq,
36 ) -> Iq:
37 """
38 Prepare the search form using :attr:`.BaseSession.SEARCH_FIELDS`
39 """
40 await self._get_session(iq)
42 xmpp = self.xmpp
44 reply = iq.reply()
45 form = reply["search"]["form"]
46 form["title"] = xmpp.SEARCH_TITLE
47 form["instructions"] = xmpp.SEARCH_INSTRUCTIONS
48 for field in xmpp.SEARCH_FIELDS:
49 form.append(field.get_xml())
50 return reply # type:ignore[no-any-return]
52 async def _search_query(
53 self,
54 _gateway_jid: JID,
55 _node: str,
56 ifrom: JID,
57 iq: Iq,
58 ) -> Iq:
59 """
60 Handles a search request
61 """
62 session = await self._get_session(iq)
64 try:
65 result = await session.on_search(iq["search"]["form"].get_values())
66 except NotImplementedError:
67 raise XMPPError(
68 "feature-not-implemented", "The gateway does not implement search"
69 )
70 except Exception as exc: # noqa
71 raise XMPPError("internal-server-error", f"Something went wrong: {exc!r}")
73 if not result:
74 raise XMPPError("item-not-found", text="Nothing was found")
76 reply = iq.reply()
77 form = reply["search"]["form"]
78 for field in result.fields:
79 form.add_reported(field.var, label=field.label, type=field.type)
80 for item in result.items:
81 form.add_item(item)
82 return reply # type:ignore[no-any-return]
84 @exceptions_to_xmpp_errors
85 async def _handle_gateway_iq(self, iq: Iq) -> None:
86 if iq.get_to() != self.xmpp.boundjid.bare:
87 raise XMPPError("bad-request", "This can only be used on the component JID")
89 if len(self.xmpp.SEARCH_FIELDS) > 1:
90 raise XMPPError(
91 "feature-not-implemented", "Use jabber search for this gateway"
92 )
94 session = await self._get_session(iq)
95 field = self.xmpp.SEARCH_FIELDS[0]
97 reply = iq.reply()
98 if iq["type"] == "get":
99 reply["gateway"]["desc"] = self.xmpp.SEARCH_TITLE
100 reply["gateway"]["prompt"] = field.label
101 elif iq["type"] == "set":
102 prompt = iq["gateway"]["prompt"]
103 result = await session.on_search({field.var: prompt})
104 if result is None or not result.items:
105 raise XMPPError(
106 "item-not-found", "No contact was found with the info you provided."
107 )
108 if len(result.items) > 1:
109 raise XMPPError(
110 "bad-request", "Your search yielded more than one result."
111 )
112 reply["gateway"]["jid"] = result.items[0]["jid"]
114 reply.send()