Mercurial > libervia-web
annotate libervia/web/pages/_browser/cache.py @ 1533:285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
this is to follow main template module change, as string slicing is not done the same way
in jinja2 and nunjucks.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 22 Jun 2023 16:36:18 +0200 |
parents | d7c78722e4f8 |
children | 9b451115e726 |
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 |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
5 import json |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
7 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
|
8 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
|
9 bridge = Bridge() |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
10 async_bridge = AsyncBridge() |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # 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
|
13 # 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
|
14 # 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
|
15 # other) |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 class Cache: |
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 def __init__(self): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 try: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 cache = storage['libervia_cache'] |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 except KeyError: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 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
|
25 else: |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
26 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
|
27 if cache['metadata']['session_uuid'] != session_uuid: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 print("data in cache are not valid for this session, resetting") |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 del storage['libervia_cache'] |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 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
|
31 else: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 self._cache = cache |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 print("storage cache is used") |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 @property |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 def roster(self): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 return self._cache['roster'] |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 @property |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 def identities(self): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 return self._cache['identities'] |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 def update(self): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 print(f"updating: {self._cache}") |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
45 storage['libervia_cache'] = json.dumps(self._cache) |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 print("cache stored") |
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 def _store_if_complete(self): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self._completed_count -= 1 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 if self._completed_count == 0: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 del self._completed_count |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 self.update() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
54 def get_contacts_cb(self, contacts): |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 print("roster received") |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 roster = self._cache['roster'] |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 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
|
58 roster[contact_jid] = { |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 'attributes': attributes, |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 'groups': groups, |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 } |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 self._store_if_complete() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
64 def identities_base_get_cb(self, identities_raw): |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 print("base identities received") |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
66 identities = json.loads(identities_raw) |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 self._cache['identities'].update(identities) |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 self._store_if_complete() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 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
|
71 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
|
72 self._store_if_complete() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 def request_data_from_backend(self): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 self._cache = { |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 'metadata': { |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 "session_uuid": session_uuid, |
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 'roster': {}, |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 'identities': {}, |
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 self._completed_count = 2 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 print("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 ) |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 print("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: |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
98 try: |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
99 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
|
100 list(to_get), |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
101 ['avatar', 'nicknames'], |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
102 ) |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
103 except Exception as e: |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
104 notification.show( |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
105 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
|
106 "error" |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 ) |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
108 else: |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
109 new_identities = json.loads(new_identities_raw) |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
110 print(f"new identities: {new_identities.keys()}") |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
111 self._cache['identities'].update(new_identities) |
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
112 self.update() |
1329
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 else: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 # we already have all identities |
1531
d7c78722e4f8
browser (cache): make `fill_identities` async + use `json` module:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
115 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
|
116 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 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
|
118 """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
|
119 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 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
|
121 @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
|
122 @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
|
123 @param identity: identity data |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 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
|
125 @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
|
126 """ |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 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
|
128 return True |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 if identity is None: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 try: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 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
|
132 except KeyError: |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 print(f"missing identity: {entity_jid}") |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 return False |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 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
|
136 |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 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
|
138 """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
|
139 |
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 text = text.lower().strip() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 )): |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 yield entity_jid |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 |
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 cache = Cache() |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 roster = cache.roster |
ed28ad7d484c
browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 identities = cache.identities |