Mercurial > libervia-web
changeset 1546:7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 06 Jul 2023 12:11:17 +0200 |
parents | be20e6ac9f22 |
children | 383e1fee29f6 |
files | libervia/web/pages/_browser/jid_search.py libervia/web/server/restricted_bridge.py |
diffstat | 2 files changed, 52 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libervia/web/pages/_browser/jid_search.py Thu Jul 06 12:11:17 2023 +0200 @@ -0,0 +1,44 @@ +from bridge import AsyncBridge as Bridge +from browser import aio, console as log, window +from cache import cache +import json + +log.warning = log.warn +profile = window.profile or "" +bridge = Bridge() + + +class JidSearch: + + def __init__(self, search_elt, callback, empty_cb=None): + self.search_elt = search_elt + self.search_elt.bind("input", self.on_search_input) + self.last_query = None + self.current_query = None + self.callback = callback + self.empty_cb = empty_cb + current_search = search_elt.value.strip() or None + if current_search is not None: + aio.run(self.perform_search(current_search)) + + def on_search_input(self, evt): + search_text = evt.target.value.strip() + if not search_text and self.empty_cb is not None: + self.empty_cb() + elif len(search_text) > 2: + aio.run(self.perform_search(search_text)) + + async def perform_search(self, query): + if self.current_query is None: + log.debug(f"performing search: {query=}") + self.current_query = query + jid_items = json.loads(await bridge.jid_search(query, "")) + await cache.fill_identities(i["entity"] for i in jid_items) + + self.last_query = query + self.current_query = None + current_query = self.search_elt.value.strip() + if current_query != query: + await self.perform_search(current_query) + return + self.callback(jid_items)
--- a/libervia/web/server/restricted_bridge.py Thu Jul 06 12:10:25 2023 +0200 +++ b/libervia/web/server/restricted_bridge.py Thu Jul 06 12:11:17 2023 +0200 @@ -206,3 +206,11 @@ return await self.host.bridge_call( "url_preview_get", url, options_s, profile ) + + async def jid_search( + self, search_term: str, options_s: str, profile: str + ) -> str: + self.no_service_profile(profile) + return await self.host.bridge_call( + "jid_search", search_term, options_s, profile + )