Commit 004786ca authored by Philipp Hörist's avatar Philipp Hörist
Browse files

Bookmarks: Add annotations

parent 56c26983
......@@ -21,16 +21,19 @@
from typing import Tuple
from typing import Union
from typing import Optional
from typing import cast
import functools
from nbxmpp.namespaces import Namespace
from nbxmpp.protocol import JID
from nbxmpp.structs import BookmarkData
from nbxmpp.task import Task
from gi.repository import GLib
from gajim.common import app
from gajim.common.nec import NetworkEvent
from gajim.common.types import BookmarksDict
from gajim.common.modules.base import BaseModule
from gajim.common.modules.util import event_node
......@@ -47,8 +50,8 @@ def __init__(self, con):
self._compat = False
self._compat_pep = False
self._node_max = False
self._bookmarks = {}
self._join_timeouts = []
self._bookmarks: BookmarksDict = {}
self._join_timeouts: List[int] = []
self._request_in_progress = True
@property
......@@ -65,7 +68,7 @@ def compat_pep(self) -> bool:
@property
def bookmarks(self) -> List[BookmarkData]:
return self._bookmarks.values()
return list(self._bookmarks.values())
@property
def pep_bookmarks_used(self) -> bool:
......@@ -125,11 +128,11 @@ def _bookmark_1_event_received(self, _con, _stanza, properties):
self._log.info('Retract: %s', jid)
bookmark = self._bookmarks.get(jid)
if bookmark is not None:
self._bookmarks.pop(bookmark, None)
self._bookmarks.pop(jid, None)
else:
new_bookmark = properties.pubsub_event.data
self._bookmarks[new_bookmark.jid] = properties.pubsub_event.data
new_bookmark: BookmarkData = properties.pubsub_event.data
self._bookmarks[new_bookmark.jid] = new_bookmark
self._act_on_changed_bookmarks(old_bookmarks)
app.nec.push_incoming_event(
......@@ -156,20 +159,18 @@ def _bookmark_module(self):
return 'PEPBookmarks'
return 'PrivateBookmarks'
def _act_on_changed_bookmarks(
self, old_bookmarks: Dict[str, BookmarkData]) -> None:
def _act_on_changed_bookmarks(self, current_bookmarks: BookmarksDict) -> None:
new_bookmarks = self._convert_to_set(self._bookmarks)
old_bookmarks = self._convert_to_set(old_bookmarks)
old_bookmarks = self._convert_to_set(current_bookmarks)
changed = new_bookmarks - old_bookmarks
if not changed:
return
join = [jid for jid, autojoin in changed if autojoin]
bookmarks = []
bookmarks: List[BookmarkData] = []
for jid in join:
self._log.info('Schedule autojoin in 10s for: %s', jid)
bookmarks.append(self._bookmarks.get(jid))
bookmarks.append(cast(BookmarkData, self._bookmarks.get(jid)))
# If another client creates a MUC, the MUC is locked until the
# configuration is finished. Give the user some time to finish
# the configuration.
......@@ -181,26 +182,24 @@ def _act_on_changed_bookmarks(
# leave = [jid for jid, autojoin in changed if not autojoin]
@staticmethod
def _convert_to_set(
bookmarks: Dict[str, BookmarkData]) -> Set[Tuple[str, bool]]:
set_ = set()
def _convert_to_set(bookmarks: BookmarksDict) -> Set[Tuple[JID, bool]]:
set_: Set[Tuple[JID, bool]] = set()
for jid, bookmark in bookmarks.items():
set_.add((jid, bookmark.autojoin))
return set_
@staticmethod
def _convert_to_dict(bookmarks: List) -> Dict[str, BookmarkData]:
_dict = {} # type: Dict[str, BookmarkData]
if bookmarks is None:
def _convert_to_dict(bookmarks: Optional[List[BookmarkData]]) -> BookmarksDict:
_dict: BookmarksDict = {}
if not bookmarks:
return _dict
for bookmark in bookmarks:
_dict[bookmark.jid] = bookmark
return _dict
def get_bookmark(self, jid: Union[str, JID]) -> BookmarkData:
return self._bookmarks.get(jid)
def get_bookmark(self, jid: Union[str, JID]) -> Optional[BookmarkData]:
return self._bookmarks.get(cast(JID, jid))
def request_bookmarks(self) -> None:
if not app.account_is_available(self._account):
......@@ -210,40 +209,36 @@ def request_bookmarks(self) -> None:
self._nbxmpp(self._bookmark_module()).request_bookmarks(
callback=self._bookmarks_received)
def _bookmarks_received(self, task: Any) -> None:
def _bookmarks_received(self, task: Task) -> None:
try:
bookmarks = task.finish()
bookmarks: List[BookmarkData] = task.finish()
except Exception as error:
self._log.warning(error)
bookmarks = None
bookmarks = []
self._request_in_progress = False
self._bookmarks = self._convert_to_dict(bookmarks)
self.auto_join_bookmarks()
self.auto_join_bookmarks(self.bookmarks)
app.nec.push_incoming_event(
NetworkEvent('bookmarks-received', account=self._account))
def store_bookmarks(self, bookmarks: list = None) -> None:
def store_bookmarks(self, bookmarks: List[BookmarkData]) -> None:
if not app.account_is_available(self._account):
return
if bookmarks is None or not self.nativ_bookmarks_used:
bookmarks = self._bookmarks.values()
if not self.nativ_bookmarks_used:
bookmarks = self.bookmarks
self._nbxmpp(self._bookmark_module()).store_bookmarks(bookmarks)
app.nec.push_incoming_event(
NetworkEvent('bookmarks-received', account=self._account))
def _join_with_timeout(self, bookmarks: List[Any]) -> None:
def _join_with_timeout(self, bookmarks: List[BookmarkData]) -> None:
self._join_timeouts.pop(0)
self.auto_join_bookmarks(bookmarks)
def auto_join_bookmarks(self,
bookmarks: Optional[List[Any]] = None) -> None:
if bookmarks is None:
bookmarks = self._bookmarks.values()
def auto_join_bookmarks(self, bookmarks: List[BookmarkData]) -> None:
for bookmark in bookmarks:
if bookmark.autojoin:
# Only join non-opened groupchats. Opened one are already
......@@ -259,7 +254,7 @@ def auto_join_bookmarks(self,
str(bookmark.jid),
minimized=minimize)
def modify(self, jid: str, **kwargs: Dict[str, str]) -> None:
def modify(self, jid: JID, **kwargs: Any) -> None:
bookmark = self._bookmarks.get(jid)
if bookmark is None:
return
......@@ -273,7 +268,7 @@ def modify(self, jid: str, **kwargs: Dict[str, str]) -> None:
self.store_bookmarks([new_bookmark])
def add_or_modify(self, jid: str, **kwargs: Dict[str, str]) -> None:
def add_or_modify(self, jid: JID, **kwargs: Any) -> None:
bookmark = self._bookmarks.get(jid)
if bookmark is not None:
self.modify(jid, **kwargs)
......@@ -291,17 +286,17 @@ def remove(self, jid: JID, publish: bool = True) -> None:
return
if publish:
if self.nativ_bookmarks_used:
self._nbxmpp('NativeBookmarks').retract_bookmark(str(jid))
self._nbxmpp('NativeBookmarks').retract_bookmark(jid)
else:
self.store_bookmarks()
self.store_bookmarks(self.bookmarks)
def get_name_from_bookmark(self, jid: str) -> str:
bookmark = self._bookmarks.get(jid)
def get_name_from_bookmark(self, jid: Union[str, JID]) -> Optional[str]:
bookmark = self._bookmarks.get(cast(JID, jid))
if bookmark is None:
return ''
return bookmark
return bookmark.name
def is_bookmark(self, jid: str) -> bool:
def is_bookmark(self, jid: Union[str, JID]) -> bool:
return jid in self._bookmarks
def _remove_timeouts(self):
......
......@@ -28,6 +28,7 @@
from collections import defaultdict
from gi.repository import GLib
from nbxmpp.protocol import JID
from gajim import IS_PORTABLE
from gajim.common import app
......@@ -600,7 +601,7 @@ def set_account_setting(self,
def get_group_chat_setting(self,
account: str,
jid: str,
jid: Union[str, JID],
setting: str) -> SETTING_TYPE:
if account not in self._account_settings:
......
......@@ -24,6 +24,8 @@
from pathlib import Path
import nbxmpp
from nbxmpp.protocol import JID
from nbxmpp.structs import BookmarkData
from gajim.common.const import PathType, PathLocation
......@@ -63,3 +65,5 @@
PluginEvents = List['NetworkEvent']
SettingsT = Union['Settings']
BookmarksDict = Dict[JID, BookmarkData]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment