Mercurial > libervia-backend
diff libervia/backend/plugins/plugin_xep_0045.py @ 4359:a987a8ce34b9
plugin XEP-0045: add MUC search to plugin JID search results.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 11 Apr 2025 18:19:28 +0200 |
parents | 0d7bb4df2343 |
children | b74a76a8e168 |
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0045.py Fri Apr 11 18:19:28 2025 +0200 +++ b/libervia/backend/plugins/plugin_xep_0045.py Fri Apr 11 18:19:28 2025 +0200 @@ -17,8 +17,9 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import difflib import time -from typing import Optional +from typing import Optional, cast import uuid import shortuuid @@ -37,6 +38,7 @@ from libervia.backend.core.i18n import D_, _ from libervia.backend.core.log import getLogger from libervia.backend.memory import memory +from libervia.backend.plugins import plugin_misc_jid_search from libervia.backend.tools import xml_tools, utils @@ -49,7 +51,7 @@ C.PI_TYPE: "XEP", C.PI_MODES: C.PLUG_MODE_BOTH, C.PI_PROTOCOLS: ["XEP-0045"], - C.PI_DEPENDENCIES: ["XEP-0359"], + C.PI_DEPENDENCIES: ["XEP-0359", "JID_SEARCH"], C.PI_RECOMMENDATIONS: [C.TEXT_CMDS, "XEP-0313"], C.PI_MAIN: "XEP_0045", C.PI_HANDLER: "yes", @@ -89,6 +91,9 @@ log.info(_("Plugin XEP_0045 initialization")) self.host = host self._sessions = memory.Sessions() + self._jid_search = cast( + plugin_misc_jid_search.JidSearch, host.plugins["JID_SEARCH"] + ) # return same arguments as muc_room_joined + a boolean set to True is the room was # already joined (first argument) host.bridge.add_method( @@ -204,6 +209,9 @@ "message_received", self.message_received_trigger, priority=1000000 ) host.trigger.add("message_parse", self._message_parse_trigger) + host.trigger.add( + "JID_SEARCH_perform_search", self.jid_search_perform_search_trigger + ) async def profile_connected(self, client): client.muc_service = await self.get_muc_service(client) @@ -250,6 +258,33 @@ return False return True + async def jid_search_perform_search_trigger( + self, + client: SatXMPPEntity, + search_term: str, + options: plugin_misc_jid_search.Options, + sequence_matcher: difflib.SequenceMatcher, + matches: plugin_misc_jid_search.SearchItems + ) -> bool: + if options.groupchat: + if client.muc_service is not None: + # FIXME: RSM is not handled, we need proper RSM handling as number of + # rooms may be large. + disco_items = await self.host.memory.disco.get_items( + client, + client.muc_service + ) + for disco_item in disco_items: + search_item = plugin_misc_jid_search.RoomSearchItem( + entity = disco_item.entity, + name = disco_item.name or "", + local = True + ) + self._jid_search.process_matching( + search_term, sequence_matcher, matches,search_item + ) + return True + def get_room(self, client: SatXMPPEntity, room_jid: jid.JID) -> muc.Room: """Retrieve Room instance from its jid