From 19b58d86f5c8e9cc761f0e1e0f85be32016e4235 Mon Sep 17 00:00:00 2001 From: mathieui Date: Fri, 29 Jan 2021 17:56:51 +0100 Subject: bookmarks: get rid of callbacks --- poezio/bookmarks.py | 54 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 'poezio/bookmarks.py') diff --git a/poezio/bookmarks.py b/poezio/bookmarks.py index f62f67ce..b1ad1f9c 100644 --- a/poezio/bookmarks.py +++ b/poezio/bookmarks.py @@ -30,9 +30,19 @@ Adding a remote bookmark: import functools import logging -from typing import Optional, List, Union - -from slixmpp import InvalidJID, JID +from typing import ( + Callable, + List, + Optional, + Union, +) + +from slixmpp import ( + InvalidJID, + JID, + ClientXMPP, +) +from slixmpp.exceptions import IqError, IqTimeout from slixmpp.plugins.xep_0048 import Bookmarks, Conference, URL from poezio.config import config @@ -213,17 +223,17 @@ class BookmarkList: self.preferred = value config.set_and_save('use_bookmarks_method', value) - def save_remote(self, xmpp, callback): + async def save_remote(self, xmpp: ClientXMPP): """Save the remote bookmarks.""" if not any(self.available_storage.values()): return method = 'xep_0049' if self.preferred == 'privatexml' else 'xep_0223' if method: - xmpp.plugin['xep_0048'].set_bookmarks( + return await xmpp.plugin['xep_0048'].set_bookmarks( stanza_storage(self.bookmarks), method=method, - callback=callback) + ) def save_local(self): """Save the local bookmarks.""" @@ -231,22 +241,24 @@ class BookmarkList: if bookmark.method == 'local') config.set_and_save('rooms', local) - def save(self, xmpp, core=None, callback=None): + async def save(self, xmpp: ClientXMPP, core=None): """Save all the bookmarks.""" self.save_local() - - def _cb(iq): - if callback: - callback(iq) - if iq["type"] == "error" and core: - core.information('Could not save remote bookmarks.', 'Error') - elif core: - core.information('Bookmarks saved', 'Info') - if config.get('use_remote_bookmarks'): - self.save_remote(xmpp, _cb) - - async def get_pep(self, xmpp): + try: + result = await self.save_remote(xmpp) + if core is not None: + core.information('Bookmarks saved', 'Info') + return result + except (IqError, IqTimeout) as iq: + if core is not None: + core.information( + 'Could not save remote bookmarks.', + 'Error' + ) + raise + + async def get_pep(self, xmpp: ClientXMPP): """Add the remotely stored bookmarks via pep to the list.""" iq = await xmpp.plugin['xep_0048'].get_bookmarks(method='xep_0223') for conf in iq['pubsub']['items']['item']['bookmarks'][ @@ -257,7 +269,7 @@ class BookmarkList: self.append(bookm) return iq - async def get_privatexml(self, xmpp): + async def get_privatexml(self, xmpp: ClientXMPP): """ Fetch the remote bookmarks stored via privatexml. """ @@ -268,7 +280,7 @@ class BookmarkList: self.append(bookm) return iq - async def get_remote(self, xmpp, information): + async def get_remote(self, xmpp: ClientXMPP, information: Callable): """Add the remotely stored bookmarks to the list.""" if xmpp.anon or not any(self.available_storage.values()): information('No remote bookmark storage available', 'Warning') -- cgit v1.2.3