Mercurial > libervia-web
annotate libervia/web/pages/_browser/cache.py @ 1622:c2065de5f6d0
browser (cache): better handling of roster and identities:
roster and identities are now handled and updated separately.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 15 May 2025 17:52:59 +0200 |
parents | a2cd4222c702 |
children |
rev | line source |
---|---|
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
1 from browser import window, console as log |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 from browser.local_storage import storage |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 from dialog import notification |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
4 from bridge import Bridge, AsyncBridge |
1619 | 5 import javascript |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
6 import json |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
8 log.warning = log.warn |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 session_uuid = window.session_uuid |
1510
5ea06e8b06ed
browser: make bridge API closer to the one use with other frontends:
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
10 bridge = Bridge() |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
11 async_bridge = AsyncBridge() |
1622
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
12 profile = window.profile or "" |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
13 STORAGE_KEY = f"libervia_cache_{profile}" |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # XXX: we don't use browser.object_storage because it is affected by |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # https://github.com/brython-dev/brython/issues/1467 and mixing local_storage.storage |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # and object_storage was resulting in weird behaviour (keys found in one not in the |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # other) |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 class Cache: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 def __init__(self): |
1622
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
24 self._cache = { |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
25 'metadata': { |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
26 "session_uuid": session_uuid, |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
27 }, |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
28 'roster': {}, |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
29 'identities': {}, |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
30 } |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 try: |
1622
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
32 cache = storage[STORAGE_KEY] |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 except KeyError: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 self.request_data_from_backend() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 else: |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
36 cache = json.loads(cache) |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 if cache['metadata']['session_uuid'] != session_uuid: |
1622
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
38 log.debug("Data in cache are not valid for this session, resetting.") |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
39 del storage[STORAGE_KEY] |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
40 self.request_data_from_backend() |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
41 elif not cache.get("roster") or not cache.get("identities"): |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
42 log.debug("Incomplete data in cache, requesting backend.") |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 self.request_data_from_backend() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 else: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 self._cache = cache |
1622
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
46 log.debug("Storage cache is used.") |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 @property |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 def roster(self): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 return self._cache['roster'] |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 @property |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 def identities(self): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 return self._cache['identities'] |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 def update(self): |
1544
9b451115e726
browser (cache): replace `print` by `log` calls
Goffi <goffi@goffi.org>
parents:
1531
diff
changeset
|
57 log.debug(f"updating: {self._cache}") |
1619 | 58 # FIXME: workaround in Brython 3.13.0 |
1622
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
59 # storage[STORAGE_KEY] = json.dumps(self._cache) |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
60 storage[STORAGE_KEY] = javascript.JSON.stringify(self._cache) |
1544
9b451115e726
browser (cache): replace `print` by `log` calls
Goffi <goffi@goffi.org>
parents:
1531
diff
changeset
|
61 log.debug("cache stored") |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
63 def get_contacts_cb(self, contacts): |
1544
9b451115e726
browser (cache): replace `print` by `log` calls
Goffi <goffi@goffi.org>
parents:
1531
diff
changeset
|
64 log.debug("roster received") |
1622
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
65 roster = self._cache['roster'] = {} |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 for contact_jid, attributes, groups in contacts: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 roster[contact_jid] = { |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 'attributes': attributes, |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 'groups': groups, |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 } |
1622
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
71 self.update() |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
73 def identities_base_get_cb(self, identities_raw): |
1544
9b451115e726
browser (cache): replace `print` by `log` calls
Goffi <goffi@goffi.org>
parents:
1531
diff
changeset
|
74 log.debug("base identities received") |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
75 identities = json.loads(identities_raw) |
1622
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
76 self._cache['identities'] = identities |
c2065de5f6d0
browser (cache): better handling of roster and identities:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
77 self.update() |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 def request_failed(self, exc, message): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 notification.show(message.format(exc=exc), "error") |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 def request_data_from_backend(self): |
1544
9b451115e726
browser (cache): replace `print` by `log` calls
Goffi <goffi@goffi.org>
parents:
1531
diff
changeset
|
83 log.debug("requesting roster to backend") |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
84 bridge.contacts_get( |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
85 callback=self.get_contacts_cb, |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 errback=lambda e: self.request_failed(e, "Can't get contacts: {exc}") |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 ) |
1544
9b451115e726
browser (cache): replace `print` by `log` calls
Goffi <goffi@goffi.org>
parents:
1531
diff
changeset
|
88 log.debug("requesting base identities to backend") |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
89 bridge.identities_base_get( |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
90 callback=self.identities_base_get_cb, |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 errback=lambda e: self.request_failed(e, "Can't get base identities: {exc}") |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 ) |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
94 async def fill_identities(self, entities) -> None: |
1431
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1329
diff
changeset
|
95 """Check that identities for entities exist, request them otherwise""" |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 to_get = {e for e in entities if e not in self._cache['identities']} |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 if to_get: |
1544
9b451115e726
browser (cache): replace `print` by `log` calls
Goffi <goffi@goffi.org>
parents:
1531
diff
changeset
|
98 log.debug(f"we don't have all identities in cache, getting {to_get}") |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
99 try: |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
100 new_identities_raw = await async_bridge.identities_get( |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
101 list(to_get), |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
102 ['avatar', 'nicknames'], |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
103 ) |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
104 except Exception as e: |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
105 notification.show( |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
106 f"Can't get identities: {e}", |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 "error" |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 ) |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
109 else: |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
110 new_identities = json.loads(new_identities_raw) |
1544
9b451115e726
browser (cache): replace `print` by `log` calls
Goffi <goffi@goffi.org>
parents:
1531
diff
changeset
|
111 log.debug(f"new identities: {new_identities.keys()}") |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
112 self._cache['identities'].update(new_identities) |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
113 self.update() |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 else: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 # we already have all identities |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
116 log.debug("no missing identity") |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 def match_identity(self, entity_jid, text, identity=None): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 """Returns True if a text match an entity identity |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 identity will be matching if its jid or any of its name contain text |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 @param entity_jid: jid of the entity to check |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 @param text: text to use for filtering. Must be in lowercase and stripped |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 @param identity: identity data |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 if None, it will be retrieved if jid is not matching |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 @return: True if entity is matching |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 """ |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 if text in entity_jid: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 return True |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 if identity is None: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 try: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 identity = self.identities[entity_jid] |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 except KeyError: |
1544
9b451115e726
browser (cache): replace `print` by `log` calls
Goffi <goffi@goffi.org>
parents:
1531
diff
changeset
|
134 log.debug(f"missing identity: {entity_jid}") |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 return False |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 return any(text in n.lower() for n in identity['nicknames']) |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 def matching_identities(self, text): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 """Return identities corresponding to a text |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 """ |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 text = text.lower().strip() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 for entity_jid, identity in self._cache['identities'].items(): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 if ((text in entity_jid |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 or any(text in n.lower() for n in identity['nicknames']) |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 )): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 yield entity_jid |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 cache = Cache() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 roster = cache.roster |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 identities = cache.identities |