Mercurial > libervia-web
annotate libervia/web/pages/_browser/jid_search.py @ 1577:9ba532041a8e
browser (chat): implement message reactions.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 22 Nov 2023 16:31:36 +0100 |
parents | 02432346e9b2 |
children | f1d09a4d38dc |
rev | line source |
---|---|
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
1 import json |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
2 from urllib.parse import quote, urljoin |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
3 |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 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
|
5 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
|
6 from cache import cache |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
7 from template import Template |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
8 import jid |
1546
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 log.warning = log.warn |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 profile = window.profile or "" |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 bridge = Bridge() |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 class JidSearch: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
16 def __init__( |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
17 self, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
18 search_elt, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
19 container_elt=None, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
20 filter_cb=None, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
21 empty_cb=None, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
22 get_url=None, |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
23 click_cb=None, |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
24 options: dict|None = None, |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
25 submit_filter: bool = True, |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
26 template: str = "components/search_item.html" |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
27 ) -> None: |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
28 """Initialize the JidSearch instance |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
29 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
30 @param search_elt: The HTML <input> element for search |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
31 @param container_elt: The HTML container to display the search results |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
32 @param filter_cb: The callback to filter the search results |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
33 @param empty_cb: The callback when the search box is empty |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
34 @param get_url: The function to get URL for each entity in the search result |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
35 @param click_cb: The function to handle the click event on each entity in the |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
36 search result |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
37 @param options: extra options. Key can be: |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
38 no_group(bool) |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
39 True if groups should not be visible |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
40 extra_cb(dict) |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
41 a map from CSS selector to callback, the callback will be binded to the |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
42 "click" event, and will be called with the ``item`` as argument |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
43 @param submit_filter: if True, only submit when a seemingly valid JID is entered |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
44 @param template: template to use |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
45 """ |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
46 self.search_item_tpl = Template(template) |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 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
|
48 self.search_elt.bind("input", self.on_search_input) |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
49 if submit_filter: |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
50 form_elt = self.search_elt.closest("form") |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
51 form_elt.bind("submit", self.on_form_submit) |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 self.last_query = None |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.current_query = None |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
54 self.container_elt = container_elt |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
55 if options is None: |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
56 options = {} |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
57 self.options = options |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
58 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
59 if click_cb is not None and get_url is None: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
60 self.get_url = lambda _: "#" |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
61 else: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
62 self.get_url = get_url if get_url is not None else self.default_get_url |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
63 self.click_cb = click_cb |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
64 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
65 if filter_cb is None: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
66 if container_elt is None: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
67 raise ValueError("container_elt must be set if filter_cb is not set") |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
68 filter_cb = self.show_items |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
69 self.filter_cb = filter_cb |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
70 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
71 self.empty_cb = empty_cb or self.on_empty_search |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
72 |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 current_search = search_elt.value.strip() or None |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1548
diff
changeset
|
74 if not current_search: |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1548
diff
changeset
|
75 self.empty_cb() |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1548
diff
changeset
|
76 else: |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 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
|
78 |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
79 def default_get_url(self, item): |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
80 """Default method to get the URL for a given entity |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
81 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
82 @param item: The item (entity) for which the URL is required |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
83 """ |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
84 return urljoin(f"{window.location.href}/", quote(item["entity"])) |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
85 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
86 def show_items(self, items): |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
87 """Display the search items in the specified container |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
88 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
89 @param items: The list of search items to be displayed |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
90 """ |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
91 assert self.container_elt is not None |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
92 self.container_elt.clear() |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
93 for item in items: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
94 search_item_elt = self.search_item_tpl.get_elt({ |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
95 "url": self.get_url(item), |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
96 "item": item, |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
97 "identities": cache.identities, |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
98 "options": self.options, |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
99 }) |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
100 if self.click_cb is not None: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
101 search_item_elt.bind('click', lambda evt, item=item: self.click_cb(item)) |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
102 extra_cb = self.options.get("extra_cb") |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
103 if extra_cb: |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
104 for selector, cb in extra_cb.items(): |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
105 for elt in search_item_elt.select(selector): |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
106 log.debug(f"binding {selector=} {elt=} {cb=} {id(cb)=}") |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
107 elt.bind( |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
108 "click", |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
109 lambda evt, item=item, cb=cb: cb(evt, item) |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
110 ) |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
111 self.container_elt <= search_item_elt |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
112 |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 def on_search_input(self, evt): |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
114 """Handle the 'input' event for the search element |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
115 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
116 @param evt: The event object |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
117 """ |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
118 evt.stopPropagation() |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
119 evt.preventDefault() |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 search_text = evt.target.value.strip() |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
121 if not search_text: |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 self.empty_cb() |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 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
|
124 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
|
125 |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
126 def on_form_submit(self, evt): |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
127 search_text = self.search_elt.value.strip() |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
128 search_jid = jid.JID(search_text) |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
129 if not search_jid.is_valid(): |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
130 evt.stopPropagation() |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
131 evt.preventDefault() |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
132 |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 async def perform_search(self, query): |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
134 """Perform the search operation for a given query |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
135 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
136 @param query: The search query |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
137 """ |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 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
|
139 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
|
140 self.current_query = query |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 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
|
142 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
|
143 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 self.last_query = query |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 self.current_query = None |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 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
|
147 if current_query != query: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 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
|
149 return |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
150 self.filter_cb(jid_items) |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
151 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
152 def on_empty_search(self): |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
153 """Handle the situation when the search box is empty""" |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
154 assert self.container_elt is not None |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
155 items = [ |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
156 { |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
157 "entity": jid_, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
158 "groups": data["groups"] |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
159 } |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
160 for jid_, data in cache.roster.items() |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
161 ] |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
162 self.show_items(items) |