Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_xep_0048.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 3fbd1a1285c1 |
children |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
57 | 57 |
58 def __init__(self, host): | 58 def __init__(self, host): |
59 log.info(_("Bookmarks plugin initialization")) | 59 log.info(_("Bookmarks plugin initialization")) |
60 self.host = host | 60 self.host = host |
61 # self.__menu_id = host.register_callback(self._bookmarks_menu, with_data=True) | 61 # self.__menu_id = host.register_callback(self._bookmarks_menu, with_data=True) |
62 self.__bm_save_id = host.register_callback(self._bookmarks_save_cb, with_data=True) | 62 self.__bm_save_id = host.register_callback( |
63 self._bookmarks_save_cb, with_data=True | |
64 ) | |
63 host.import_menu( | 65 host.import_menu( |
64 (D_("Groups"), D_("Bookmarks")), | 66 (D_("Groups"), D_("Bookmarks")), |
65 self._bookmarks_menu, | 67 self._bookmarks_menu, |
66 security_limit=0, | 68 security_limit=0, |
67 help_string=D_("Use and manage bookmarks"), | 69 help_string=D_("Use and manage bookmarks"), |
105 async def profile_connected(self, client): | 107 async def profile_connected(self, client): |
106 local = client.bookmarks_local = PersistentBinaryDict( | 108 local = client.bookmarks_local = PersistentBinaryDict( |
107 NS_BOOKMARKS, client.profile | 109 NS_BOOKMARKS, client.profile |
108 ) | 110 ) |
109 await local.load() | 111 await local.load() |
110 local = cast(dict[str, dict|None]|None, local) | 112 local = cast(dict[str, dict | None] | None, local) |
111 if not local: | 113 if not local: |
112 local = { | 114 local = {XEP_0048.MUC_TYPE: {}, XEP_0048.URL_TYPE: {}} |
113 XEP_0048.MUC_TYPE: {}, | |
114 XEP_0048.URL_TYPE: {} | |
115 } | |
116 private = await self._get_server_bookmarks("private", client.profile) | 115 private = await self._get_server_bookmarks("private", client.profile) |
117 pubsub = client.bookmarks_pubsub = None | 116 pubsub = client.bookmarks_pubsub = None |
118 | 117 |
119 for bookmarks in (local, private, pubsub): | 118 for bookmarks in (local, private, pubsub): |
120 if bookmarks is not None: | 119 if bookmarks is not None: |
121 for (room_jid, data) in list(bookmarks[XEP_0048.MUC_TYPE].items()): | 120 for room_jid, data in list(bookmarks[XEP_0048.MUC_TYPE].items()): |
122 if data.get("autojoin", "false") == "true": | 121 if data.get("autojoin", "false") == "true": |
123 nick = data.get("nick", client.jid.user) | 122 nick = data.get("nick", client.jid.user) |
124 defer.ensureDeferred( | 123 defer.ensureDeferred( |
125 self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) | 124 self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) |
126 ) | 125 ) |
199 | 198 |
200 for attr in XEP_0048.MUC_ATTRS: | 199 for attr in XEP_0048.MUC_ATTRS: |
201 if conference_elt.hasAttribute(attr): | 200 if conference_elt.hasAttribute(attr): |
202 data[attr] = conference_elt[attr] | 201 data[attr] = conference_elt[attr] |
203 try: | 202 try: |
204 data["nick"] = str( | 203 data["nick"] = str(next(conference_elt.elements(NS_BOOKMARKS, "nick"))) |
205 next(conference_elt.elements(NS_BOOKMARKS, "nick")) | |
206 ) | |
207 except StopIteration: | 204 except StopIteration: |
208 pass | 205 pass |
209 # TODO: manage password (need to be secured, see XEP-0049 §4) | 206 # TODO: manage password (need to be secured, see XEP-0049 §4) |
210 | 207 |
211 url_elts = storage_elt.elements(NS_BOOKMARKS, "url") | 208 url_elts = storage_elt.elements(NS_BOOKMARKS, "url") |
275 | 272 |
276 d.addCallbacks(lambda __: {}, join_eb) | 273 d.addCallbacks(lambda __: {}, join_eb) |
277 return d | 274 return d |
278 | 275 |
279 def _bookmarks_menu(self, data, profile): | 276 def _bookmarks_menu(self, data, profile): |
280 """ XMLUI activated by menu: return Gateways UI | 277 """XMLUI activated by menu: return Gateways UI |
281 @param profile: %(doc_profile)s | 278 @param profile: %(doc_profile)s |
282 | 279 |
283 """ | 280 """ |
284 client = self.host.get_client(profile) | 281 client = self.host.get_client(profile) |
285 xmlui = xml_tools.XMLUI(title=_("Bookmarks manager")) | 282 xmlui = xml_tools.XMLUI(title=_("Bookmarks manager")) |
294 client.bookmarks_private, | 291 client.bookmarks_private, |
295 client.bookmarks_pubsub, | 292 client.bookmarks_pubsub, |
296 ): | 293 ): |
297 if bookmarks is None: | 294 if bookmarks is None: |
298 continue | 295 continue |
299 for (room_jid, data) in sorted( | 296 for room_jid, data in sorted( |
300 list(bookmarks[XEP_0048.MUC_TYPE].items()), | 297 list(bookmarks[XEP_0048.MUC_TYPE].items()), |
301 key=lambda item: item[1].get("name", item[0].user), | 298 key=lambda item: item[1].get("name", item[0].user), |
302 ): | 299 ): |
303 room_jid_s = room_jid.full() | 300 room_jid_s = room_jid.full() |
304 adv_list.set_row_index( | 301 adv_list.set_row_index( |
357 - "private": Private XML storage (XEP-0049) | 354 - "private": Private XML storage (XEP-0049) |
358 - "local": Store in SàT database | 355 - "local": Store in SàT database |
359 @param profile_key: %(doc_profile_key)s | 356 @param profile_key: %(doc_profile_key)s |
360 """ | 357 """ |
361 assert storage_type in ("auto", "pubsub", "private", "local") | 358 assert storage_type in ("auto", "pubsub", "private", "local") |
362 if type_ == XEP_0048.URL_TYPE and {"autojoin", "nick"}.intersection(list(data.keys())): | 359 if type_ == XEP_0048.URL_TYPE and {"autojoin", "nick"}.intersection( |
360 list(data.keys()) | |
361 ): | |
363 raise ValueError("autojoin or nick can't be used with URLs") | 362 raise ValueError("autojoin or nick can't be used with URLs") |
364 client = self.host.get_client(profile_key) | 363 client = self.host.get_client(profile_key) |
365 if storage_type == "auto": | 364 if storage_type == "auto": |
366 if client.bookmarks_pubsub is not None: | 365 if client.bookmarks_pubsub is not None: |
367 storage_type = "pubsub" | 366 storage_type = "pubsub" |
444 if bookmarks_ori is None: | 443 if bookmarks_ori is None: |
445 return ret | 444 return ret |
446 try: | 445 try: |
447 data = bookmarks_ori[type_] | 446 data = bookmarks_ori[type_] |
448 except KeyError: | 447 except KeyError: |
449 log.warning( | 448 log.warning(f"{type_!r} missing in {storage_location} storage.") |
450 f"{type_!r} missing in {storage_location} storage." | |
451 ) | |
452 data = bookmarks_ori[type_] = {} | 449 data = bookmarks_ori[type_] = {} |
453 for bookmark in data: | 450 for bookmark in data: |
454 if type_ == XEP_0048.MUC_TYPE: | 451 if type_ == XEP_0048.MUC_TYPE: |
455 ret[_storage_location][bookmark.full()] = data[bookmark].copy() | 452 ret[_storage_location][bookmark.full()] = data[bookmark].copy() |
456 else: | 453 else: |