Use async properly on session start

This commit is contained in:
mathieui 2021-01-29 12:05:01 +01:00 committed by Link Mauve
parent 262583133e
commit f5ad5199ae
3 changed files with 45 additions and 54 deletions

View file

@ -246,46 +246,37 @@ class BookmarkList:
if config.get('use_remote_bookmarks'):
self.save_remote(xmpp, _cb)
def get_pep(self, xmpp, callback):
async def get_pep(self, xmpp):
"""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'][
'conferences']:
if isinstance(conf, URL):
continue
bookm = Bookmark.parse(conf)
self.append(bookm)
return iq
def _cb(iq):
if iq['type'] == 'result':
for conf in iq['pubsub']['items']['item']['bookmarks'][
'conferences']:
if isinstance(conf, URL):
continue
b = Bookmark.parse(conf)
self.append(b)
if callback:
callback(iq)
xmpp.plugin['xep_0048'].get_bookmarks(method='xep_0223', callback=_cb)
def get_privatexml(self, xmpp, callback):
async def get_privatexml(self, xmpp):
"""
Fetch the remote bookmarks stored via privatexml.
"""
def _cb(iq):
if iq['type'] == 'result':
for conf in iq['private']['bookmarks']['conferences']:
b = Bookmark.parse(conf)
self.append(b)
if callback:
callback(iq)
iq = await xmpp.plugin['xep_0048'].get_bookmarks(method='xep_0049')
for conf in iq['private']['bookmarks']['conferences']:
bookm = Bookmark.parse(conf)
self.append(bookm)
return iq
xmpp.plugin['xep_0048'].get_bookmarks(method='xep_0049', callback=_cb)
def get_remote(self, xmpp, information, callback):
async def get_remote(self, xmpp, information):
"""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')
return
if self.preferred == 'pep':
self.get_pep(xmpp, callback=callback)
return await self.get_pep(xmpp)
else:
self.get_privatexml(xmpp, callback=callback)
return await self.get_privatexml(xmpp)
def get_local(self):
"""Add the locally stored bookmarks to the list."""

View file

@ -44,7 +44,10 @@ from poezio import theming
from poezio import timed_events
from poezio import windows
from poezio.bookmarks import BookmarkList
from poezio.bookmarks import (
BookmarkList,
Bookmark,
)
from poezio.common import get_error_message
from poezio.config import config, firstrun
from poezio.contact import Contact, Resource
@ -1710,7 +1713,7 @@ class Core:
shortdesc='Send your gaming activity.',
completion=None)
def check_blocking(self, features):
def check_blocking(self, features: List[str]):
if 'urn:xmpp:blocking' in features and not self.xmpp.anon:
self.register_command(
'block',
@ -1729,7 +1732,7 @@ class Core:
####################### Random things to move #################################
def join_initial_rooms(self, bookmarks):
def join_initial_rooms(self, bookmarks: List[Bookmark]):
"""Join all rooms given in the iterator `bookmarks`"""
for bm in bookmarks:
if not (bm.autojoin or config.get('open_all_bookmarks')):
@ -1745,14 +1748,16 @@ class Core:
if bm.autojoin:
tab.join()
def check_bookmark_storage(self, features):
async def check_bookmark_storage(self, features: List[str]):
private = 'jabber:iq:private' in features
pep_ = 'http://jabber.org/protocol/pubsub#publish' in features
self.bookmarks.available_storage['private'] = private
self.bookmarks.available_storage['pep'] = pep_
def _join_remote_only(iq):
if iq['type'] == 'error':
if not self.xmpp.anon and config.get('use_remote_bookmarks'):
try:
await self.bookmarks.get_remote(self.xmpp, self.information)
except IqError as iq:
type_ = iq['error']['type']
condition = iq['error']['condition']
if not (type_ == 'cancel' and condition == 'item-not-found'):
@ -1763,10 +1768,6 @@ class Core:
remote_bookmarks = self.bookmarks.remote()
self.join_initial_rooms(remote_bookmarks)
if not self.xmpp.anon and config.get('use_remote_bookmarks'):
self.bookmarks.get_remote(self.xmpp, self.information,
_join_remote_only)
def room_error(self, error: IqError, room_name: str) -> None:
"""
Display the error in the tab

View file

@ -79,27 +79,26 @@ class HandlerCore:
def __init__(self, core):
self.core = core
def on_session_start_features(self, _):
async def on_session_start_features(self, _):
"""
Enable carbons & blocking on session start if wanted and possible
"""
def callback(iq):
if not iq:
return
features = iq['disco_info']['features']
rostertab = self.core.tabs.by_name_and_class(
'Roster', tabs.RosterInfoTab)
rostertab.check_blocking(features)
rostertab.check_saslexternal(features)
self.core.check_blocking(features)
if (config.get('enable_carbons')
and 'urn:xmpp:carbons:2' in features):
self.core.xmpp.plugin['xep_0280'].enable()
self.core.check_bookmark_storage(features)
self.core.xmpp.plugin['xep_0030'].get_info(
jid=self.core.xmpp.boundjid.domain, callback=callback)
iq = await self.core.xmpp.plugin['xep_0030'].get_info(
jid=self.core.xmpp.boundjid.domain
)
features = iq['disco_info']['features']
rostertab = self.core.tabs.by_name_and_class(
'Roster', tabs.RosterInfoTab)
rostertab.check_saslexternal(features)
rostertab.check_blocking(features)
self.core.check_blocking(features)
if (config.get('enable_carbons')
and 'urn:xmpp:carbons:2' in features):
self.core.xmpp.plugin['xep_0280'].enable()
await self.core.check_bookmark_storage(features)
def find_identities(self, _):
asyncio.ensure_future(
@ -1348,7 +1347,7 @@ class HandlerCore:
pres.send()
self.core.bookmarks.get_local()
# join all the available bookmarks. As of yet, this is just the local ones
self.core.join_initial_rooms(self.core.bookmarks)
self.core.join_initial_rooms(self.core.bookmarks.local())
if config.get('enable_user_nick'):
self.core.xmpp.plugin['xep_0172'].publish_nick(