annotate libervia/pages/_browser/cache.py @ 1509:106bae41f5c8

massive refactoring from camelCase -> snake_case. See backend commit log for more details
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:44:11 +0200
parents 7472d5a88006
children 5ea06e8b06ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1329
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 from browser import window
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 javascript import JSON
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from dialog import notification
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1431
diff changeset
5 from bridge import bridge
1329
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 session_uuid = window.session_uuid
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1431
diff changeset
8 bridge = bridge()
1329
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
9
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # 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
11 # 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
12 # 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
13 # other)
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
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 class Cache:
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 def __init__(self):
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 try:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 cache = storage['libervia_cache']
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 except KeyError:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 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
23 else:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 cache = JSON.parse(cache)
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 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
26 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
27 del storage['libervia_cache']
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 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
29 else:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 self._cache = cache
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 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
32
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 @property
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 def roster(self):
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 return self._cache['roster']
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 @property
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def identities(self):
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 return self._cache['identities']
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def update(self):
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 # FIXME: we use window.JSON as a workaround to
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 # https://github.com/brython-dev/brython/issues/1467
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}")
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 storage['libervia_cache'] = window.JSON.stringify(self._cache)
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")
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 identities = JSON.parse(identities_raw)
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
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 def _fill_identities_cb(self, new_identities_raw, callback):
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 new_identities = JSON.parse(new_identities_raw)
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 print(f"new identities: {new_identities.keys()}")
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 self._cache['identities'].update(new_identities)
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 self.update()
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if callback:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 callback()
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 def fill_identities(self, entities, callback=None):
1431
7472d5a88006 browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents: 1329
diff changeset
103 """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
104 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
105 if to_get:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1431
diff changeset
106 bridge.identities_get(
1329
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 list(to_get),
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 ['avatar', 'nicknames'],
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 callback=lambda identities: self._fill_identities_cb(
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 identities, callback),
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 errback=lambda failure_: notification.show(
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 f"Can't get identities: {failure_}",
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 "error"
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 )
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 )
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 else:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 # we already have all identities
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 print("no missing identity")
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 if callback:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 callback()
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
121
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 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
123 """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
124
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 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
126 @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
127 @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
128 @param identity: identity data
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 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
130 @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
131 """
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 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
133 return True
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 if identity is None:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 try:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 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
137 except KeyError:
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 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
139 return False
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 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
141
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 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
143 """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
144
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 text = text.lower().strip()
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 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
148 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
149 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
150 )):
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 yield entity_jid
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
152
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 cache = Cache()
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 roster = cache.roster
ed28ad7d484c browser (cache): new `cache` module to handle cache of roster and identities:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 identities = cache.identities