Mercurial > libervia-web
annotate libervia/web/pages/_browser/jid_search.py @ 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 | |
children | 66aa6e140ebb |
rev | line source |
---|---|
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 from bridge import AsyncBridge as Bridge |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 from browser import aio, console as log, window |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 from cache import cache |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 import json |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 log.warning = log.warn |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 profile = window.profile or "" |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 bridge = Bridge() |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 class JidSearch: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 def __init__(self, search_elt, callback, empty_cb=None): |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 self.search_elt = search_elt |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 self.search_elt.bind("input", self.on_search_input) |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 self.last_query = None |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 self.current_query = None |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 self.callback = callback |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 self.empty_cb = empty_cb |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 current_search = search_elt.value.strip() or None |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 if current_search is not None: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 aio.run(self.perform_search(current_search)) |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 def on_search_input(self, evt): |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 search_text = evt.target.value.strip() |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 if not search_text and self.empty_cb is not None: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 self.empty_cb() |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 elif len(search_text) > 2: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 aio.run(self.perform_search(search_text)) |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 async def perform_search(self, query): |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 if self.current_query is None: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 log.debug(f"performing search: {query=}") |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 self.current_query = query |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 jid_items = json.loads(await bridge.jid_search(query, "")) |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 await cache.fill_identities(i["entity"] for i in jid_items) |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 self.last_query = query |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 self.current_query = None |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 current_query = self.search_elt.value.strip() |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 if current_query != query: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 await self.perform_search(current_query) |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 return |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 self.callback(jid_items) |