diff options
Diffstat (limited to 'poezio/contact.py')
-rw-r--r-- | poezio/contact.py | 65 |
1 files changed, 40 insertions, 25 deletions
diff --git a/poezio/contact.py b/poezio/contact.py index 27b0598c..90f34c7e 100644 --- a/poezio/contact.py +++ b/poezio/contact.py @@ -3,7 +3,7 @@ # This file is part of Poezio. # # Poezio is free software: you can redistribute it and/or modify -# it under the terms of the zlib license. See the COPYING file. +# it under the terms of the GPL-3.0+ license. See the COPYING file. """ Defines the Resource and Contact classes, which are used in the roster. @@ -11,10 +11,17 @@ the roster. from collections import defaultdict import logging -from typing import Dict, Iterator, List, Optional, Union - -from poezio.common import safeJID -from slixmpp import JID +from typing import ( + Any, + Dict, + Iterator, + List, + Optional, + Union, +) + +from slixmpp import InvalidJID, JID +from slixmpp.roster import RosterItem log = logging.getLogger(__name__) @@ -30,8 +37,8 @@ class Resource: data: the dict to use as a source """ # Full JID - self._jid = jid # type: str - self._data = data # type: Dict[str, Union[str, int]] + self._jid: str = jid + self._data: Dict[str, Union[str, int]] = data @property def jid(self) -> str: @@ -39,15 +46,18 @@ class Resource: @property def priority(self) -> int: - return self._data.get('priority') or 0 + try: + return int(self._data.get('priority', 0)) + except Exception: + return 0 @property def presence(self) -> str: - return self._data.get('show') or '' + return str(self._data.get('show')) or '' @property def status(self) -> str: - return self._data.get('status') or '' + return str(self._data.get('status')) or '' def __repr__(self) -> str: return '<%s>' % self._jid @@ -65,19 +75,16 @@ class Contact: to get the resource with the highest priority, etc """ - def __init__(self, item): + def __init__(self, item: RosterItem): """ item: a slixmpp RosterItem pointing to that contact """ self.__item = item - self.folded_states = defaultdict(lambda: True) # type: Dict[str, bool] + self.folded_states: Dict[str, bool] = defaultdict(lambda: True) self._name = '' self.avatar = None self.error = None - self.tune = {} # type: Dict[str, str] - self.gaming = {} # type: Dict[str, str] - self.mood = '' - self.activity = '' + self.rich_presence: Dict[str, Any] = defaultdict(lambda: None) @property def groups(self) -> List[str]: @@ -90,7 +97,7 @@ class Contact: return self.__item.jid @property - def name(self): + def name(self) -> str: """The name of the contact or an empty string.""" return self.__item['name'] or self._name or '' @@ -100,26 +107,27 @@ class Contact: self._name = value @property - def ask(self): + def ask(self) -> Optional[str]: if self.__item['pending_out']: return 'asked' + return None @property - def pending_in(self): + def pending_in(self) -> bool: """We received a subscribe stanza from this contact.""" return self.__item['pending_in'] @pending_in.setter - def pending_in(self, value): + def pending_in(self, value: bool): self.__item['pending_in'] = value @property - def pending_out(self): + def pending_out(self) -> bool: """We sent a subscribe stanza to this contact.""" return self.__item['pending_out'] @pending_out.setter - def pending_out(self, value): + def pending_out(self, value: bool): self.__item['pending_out'] = value @property @@ -134,8 +142,12 @@ class Contact: return self.__item['subscription'] def __contains__(self, value): - return value in self.__item.resources or safeJID( - value).resource in self.__item.resources + try: + resource = JID(value).resource + except InvalidJID: + resource = None + return value in self.__item.resources or \ + (resource is not None and resource in self.__item.resources) def __len__(self) -> int: """Number of resources""" @@ -147,7 +159,10 @@ class Contact: def __getitem__(self, key) -> Optional[Resource]: """Return the corresponding Resource object, or None""" - res = safeJID(key).resource + try: + res = JID(key).resource + except InvalidJID: + return None resources = self.__item.resources item = resources.get(res, None) or resources.get(key, None) return Resource(key, item) if item else None |