Philipp Hörist pushed to branch master at gajim / gajim
Commits: 1657c701 by lovetox at 2022-01-13T00:22:35+01:00 Use JID objects instead of str
- - - - - 5e448f25 by lovetox at 2022-01-13T00:22:35+01:00 Base: Add new attribute alias
So we can migrate from _con -> _client
- - - - - 161e7203 by lovetox at 2022-01-13T00:22:35+01:00 Client: Add type annotations
- - - - - 492fed4b by lovetox at 2022-01-13T00:22:35+01:00 Types: Add type alias
- - - - - f70ecce4 by lovetox at 2022-01-13T00:22:35+01:00 Structs: Add type annotations
- - - - - e32af9e8 by lovetox at 2022-01-13T00:22:35+01:00 Settings: Fix type annotation
- - - - - a0c8587b by lovetox at 2022-01-13T00:22:35+01:00 ChatMarkers: Add type annotations
- - - - -
11 changed files:
- gajim/common/client.py - gajim/common/helpers.py - gajim/common/modules/base.py - gajim/common/modules/caps.py - gajim/common/modules/chat_markers.py - gajim/common/modules/contacts.py - gajim/common/settings.py - gajim/common/structs.py - gajim/common/types.py - gajim/gtk/controls/base.py - pyrightconfig.json
Changes:
===================================== gajim/common/client.py ===================================== @@ -55,6 +55,7 @@ from gajim.common.idle import Monitor from gajim.common.idle import IdleMonitorManager from gajim.common.i18n import _ +from gajim.common.structs import OutgoingMessage
from gajim.gui.util import open_window
@@ -471,13 +472,13 @@ def _finish_connect(self) -> None: app.ged.raise_event(SignedIn(account=self._account, conn=self)) modules.send_stored_publish(self._account)
- def send_stanza(self, stanza): + def send_stanza(self, stanza: Any) -> None: """ Send a stanza untouched """ return self._client.send_stanza(stanza)
- def send_message(self, message): + def send_message(self, message: OutgoingMessage) -> None: if not self._state.is_available: log.warning('Trying to send message while offline') return @@ -504,7 +505,7 @@ def send_message(self, message): message, self._send_message)
- def _send_message(self, message): + def _send_message(self, message: OutgoingMessage) -> None: message.set_sent_timestamp() message.message_id = self.send_stanza(message.stanza)
@@ -525,7 +526,7 @@ def _send_message(self, message):
self.get_module('Message').log_message(message)
- def send_messages(self, jids, message): + def send_messages(self, jids: list[JID], message: OutgoingMessage) -> None: if not self._state.is_available: log.warning('Trying to send message while offline') return
===================================== gajim/common/helpers.py ===================================== @@ -1414,7 +1414,7 @@ def get_group_chat_nick(account: str, room_jid: Union[JID, str]) -> str: return nick
-def get_muc_context(jid: Union[JID, str]) -> Optional[str]: +def get_muc_context(jid: JID) -> Optional[str]: disco_info = app.storage.cache.get_last_disco_info(jid) if disco_info is None: return None
===================================== gajim/common/modules/base.py ===================================== @@ -43,6 +43,7 @@ def __init__(self,
EventHelper.__init__(self) self._con = con + self._client = con self._account = con.account self._log = self._set_logger(plugin) self._nbxmpp_callbacks: dict[str, Any] = {}
===================================== gajim/common/modules/caps.py ===================================== @@ -106,8 +106,9 @@ def _entity_caps(self, _con, _stanza, properties): self._queue_task(task) return
- jid = str(properties.jid) - app.storage.cache.set_last_disco_info(jid, disco_info, cache_only=True) + app.storage.cache.set_last_disco_info(properties.jid, + disco_info, + cache_only=True)
contact = self._con.get_module('Contacts').get_contact(properties.jid) contact.notify('caps-update') @@ -145,7 +146,7 @@ def _on_disco_info(self, nbxmpp_task): return
app.storage.cache.add_caps_entry( - str(disco_info.jid), + disco_info.jid, task.entity.method, disco_info.get_caps_hash(), disco_info)
===================================== gajim/common/modules/chat_markers.py ===================================== @@ -14,10 +14,15 @@
# Chat Markers (XEP-0333)
+from __future__ import annotations + +from typing import Any + from nbxmpp.namespaces import Namespace from nbxmpp.structs import StanzaHandler
from gajim.common import app +from gajim.common import types from gajim.common.events import DisplayedReceived from gajim.common.events import ReadStateSync from gajim.common.modules.base import BaseModule @@ -28,8 +33,8 @@ class ChatMarkers(BaseModule):
_nbxmpp_extends = 'ChatMarkers'
- def __init__(self, con): - BaseModule.__init__(self, con) + def __init__(self, client: types.Client): + BaseModule.__init__(self, client)
self.handlers = [ StanzaHandler(name='message', @@ -38,7 +43,11 @@ def __init__(self, con): priority=47), ]
- def _process_chat_marker(self, _con, _stanza, properties): + def _process_chat_marker(self, + _client: types.xmppClient, + _stanza: Any, + properties: Any) -> None: + if not properties.is_marker or not properties.marker.is_displayed: return
@@ -46,7 +55,7 @@ def _process_chat_marker(self, _con, _stanza, properties): return
if properties.type.is_groupchat: - contact = self._con.get_module('Contacts').get_contact( + contact = self._client.get_module('Contacts').get_contact( properties.muc_jid, groupchat=True) if not contact.is_joined: @@ -64,12 +73,12 @@ def _process_chat_marker(self, _con, _stanza, properties): return
if properties.is_mam_message: - if properties.from_.bare_match(self._con.get_own_jid()): + if properties.from_.bare_match(self._client.get_own_jid()): return
self._raise_event('displayed-received', properties)
- def _raise_event(self, name, properties): + def _raise_event(self, name: str, properties: Any) -> None: self._log.info('%s: %s %s', name, properties.jid, @@ -100,10 +109,15 @@ def _raise_event(self, name, properties): is_muc_pm=properties.is_muc_pm, marker_id=properties.marker.id))
- def _send_marker(self, contact, marker, id_, type_): + def _send_marker(self, + contact: types.ChatContacts, + marker: str, + id_: str, + type_: str) -> None: + jid = contact.jid if contact.is_pm_contact: - jid = contact.jid.bare + jid = contact.jid.new_as_bare()
if type_ in ('gc', 'pm'): if not app.settings.get_group_chat_setting( @@ -121,8 +135,11 @@ def _send_marker(self, contact, marker, id_, type_): type_=typ, marker=(marker, id_), play_sound=False) - self._con.send_message(message) + self._client.send_message(message) self._log.info('Send %s: %s', marker, contact.jid)
- def send_displayed_marker(self, contact, id_, type_): - self._send_marker(contact, 'displayed', id_, str(type_)) + def send_displayed_marker(self, + contact: types.ChatContacts, + id_: str, + type_: str) -> None: + self._send_marker(contact, 'displayed', id_, type_)
===================================== gajim/common/modules/contacts.py ===================================== @@ -45,13 +45,13 @@
class ContactSettings: - def __init__(self, account: str, jid: str) -> None: + def __init__(self, account: str, jid: JID) -> None: self.get = partial(app.settings.get_contact_setting, account, jid) self.set = partial(app.settings.set_contact_setting, account, jid)
class GroupChatSettings: - def __init__(self, account: str, jid: str) -> None: + def __init__(self, account: str, jid: JID) -> None: self._account = account self._jid = jid
@@ -73,7 +73,7 @@ def set(self, setting: BoolGroupChatSettings, value: bool) -> None: ... def set(self, setting: IntGroupChatSettings, value: int) -> None: ... def set(self, setting: Any, value: Any) -> None: app.settings.set_group_chat_setting( - self._account, str(self._jid), setting, value) + self._account, self._jid, setting, value)
class Contacts(BaseModule): @@ -228,7 +228,7 @@ class BareContact(CommonContact): def __init__(self, logger: logging.Logger, jid: JID, account: str) -> None: CommonContact.__init__(self, logger, jid, account)
- self.settings = ContactSettings(account, str(jid)) + self.settings = ContactSettings(account, jid)
self._avatar_sha = app.storage.cache.get_contact(jid, 'avatar')
@@ -474,7 +474,7 @@ class GroupchatContact(CommonContact): def __init__(self, logger: logging.Logger, jid: JID, account: str) -> None: CommonContact.__init__(self, logger, jid, account)
- self.settings = GroupChatSettings(account, str(jid)) + self.settings = GroupChatSettings(account, jid)
@property def is_groupchat(self) -> bool:
===================================== gajim/common/settings.py ===================================== @@ -73,7 +73,7 @@ from gajim.common.setting_values import BoolSettings from gajim.common.setting_values import BoolAccountSettings
-SETTING_TYPE = Union[bool, int, str, object, list] +SETTING_TYPE = Union[bool, int, str, object]
log = logging.getLogger('gajim.c.settings') @@ -705,27 +705,27 @@ def set_account_setting(self, @overload def get_group_chat_setting(self, account: str, - jid: Union[str, JID], + jid: JID, setting: IntGroupChatSettings ) -> int: ...
@overload def get_group_chat_setting(self, account: str, - jid: Union[str, JID], + jid: JID, setting: BoolGroupChatSettings ) -> bool: ...
@overload def get_group_chat_setting(self, account: str, - jid: Union[str, JID], + jid: JID, setting: StringGroupChatSettings ) -> str: ...
def get_group_chat_setting(self, account: str, - jid: Union[str, JID], + jid: JID, setting: AllGroupChatSettings ) -> AllGroupChatSettingsT:
@@ -766,27 +766,27 @@ def get_group_chat_setting(self, @overload def set_group_chat_setting(self, account: str, - jid: str, + jid: JID, setting: StringGroupChatSettings, value: str) -> None: ...
@overload def set_group_chat_setting(self, account: str, - jid: str, + jid: JID, setting: IntGroupChatSettings, value: int) -> None: ...
@overload def set_group_chat_setting(self, account: str, - jid: str, + jid: JID, setting: BoolGroupChatSettings, value: bool) -> None: ...
def set_group_chat_setting(self, account: str, - jid: str, + jid: JID, setting: AllGroupChatSettings, value: AllGroupChatSettingsT) -> None:
@@ -851,7 +851,7 @@ def set_group_chat_settings(self,
def get_contact_setting(self, account: str, - jid: str, + jid: JID, setting: str) -> SETTING_TYPE:
if account not in self._account_settings: @@ -874,7 +874,7 @@ def get_contact_setting(self,
def set_contact_setting(self, account: str, - jid: str, + jid: JID, setting: str, value: SETTING_TYPE) -> None:
===================================== gajim/common/structs.py ===================================== @@ -25,6 +25,7 @@ import time from dataclasses import dataclass from dataclasses import fields + from gi.repository import GLib
from nbxmpp.protocol import JID @@ -49,7 +50,12 @@ class URI(NamedTuple):
class MUCData: - def __init__(self, room_jid, nick, password, config=None): + def __init__(self, + room_jid: str, + nick: str, + password: str, + config=None) -> None: + self._room_jid = JID.from_string(room_jid) self._config = config self.nick = nick @@ -60,11 +66,11 @@ def __init__(self, room_jid, nick, password, config=None): self.subject = None
@property - def jid(self): + def jid(self) -> JID: return self._room_jid
@property - def occupant_jid(self): + def occupant_jid(self) -> JID: return self._room_jid.new_with(resource=self.nick)
@property @@ -74,23 +80,23 @@ def config(self):
class OutgoingMessage: def __init__(self, - account, + account: str, contact, - message, - type_, - subject=None, + message: Optional[str], + type_: str, + subject: Optional[str] = None, chatstate=None, - marker=None, - resource=None, - user_nick=None, - label=None, + marker: Optional[tuple[str, str]] = None, + resource: Optional[str] = None, + user_nick: Optional[str] = None, + label: Optional[str] = None, control=None, - attention=None, - correct_id=None, - oob_url=None, + attention: Optional[bool] = None, + correct_id: Optional[str] = None, + oob_url: Optional[str] = None, xhtml=None, nodes=None, - play_sound=True): + play_sound: bool = True):
if type_ not in ('chat', 'groupchat', 'normal', 'headline'): raise ValueError('Unknown message type: %s' % type_) @@ -157,37 +163,37 @@ def copy(self): return message
@property - def jid(self): + def jid(self) -> JID: return self.contact.jid
@property - def is_groupchat(self): + def is_groupchat(self) -> bool: return self.type_ == 'groupchat'
@property - def is_chat(self): + def is_chat(self) -> bool: return self.type_ == 'chat'
@property - def is_normal(self): + def is_normal(self) -> bool: return self.type_ == 'normal'
- def set_sent_timestamp(self): + def set_sent_timestamp(self) -> None: if self.is_groupchat: return self.timestamp = time.time()
@property - def is_encrypted(self): + def is_encrypted(self) -> bool: return bool(self.additional_data.get_value('encrypted', 'name', False))
@property - def msg_iq(self): + def msg_iq(self) -> Any: # Backwards compatibility for plugins return self.stanza
@msg_iq.setter - def msg_iq(self, value): + def msg_iq(self, value: Any) -> None: # Backwards compatibility for plugins self.stanza = value
@@ -245,6 +251,7 @@ def from_presence(cls, properties): role=Role.NONE, real_jid=None)
+ class VariantMixin:
_type_to_variant_funcs = {
===================================== gajim/common/types.py ===================================== @@ -34,6 +34,7 @@ if TYPE_CHECKING: # pylint: disable=unused-import from gajim.common.client import Client + from nbxmpp.client import Client as xmppClient from gajim.common.zeroconf.connection_zeroconf import ConnectionZeroconf from gajim.common.modules.contacts import CommonContact from gajim.common.modules.contacts import BareContact @@ -41,6 +42,8 @@ from gajim.common.modules.contacts import GroupchatContact from gajim.common.modules.contacts import GroupchatParticipant
+ ChatContacts = Union[BareContact, GroupchatContact, GroupchatParticipant] + from gajim.gui_interface import Interface from gajim.common.settings import Settings from gajim.gtk.css_config import CSSConfig
===================================== gajim/gtk/controls/base.py ===================================== @@ -570,7 +570,7 @@ def mark_as_read(self, send_marker: bool = True) -> None: self._client.get_module('ChatMarkers').send_displayed_marker( self.contact, self.last_msg_id, - self._type) + str(self._type)) self.last_msg_id = None
def change_encryption(self,
===================================== pyrightconfig.json ===================================== @@ -12,6 +12,7 @@ "gajim/common/i18n.py", "gajim/common/idle.py", "gajim/common/logging_helpers.py", + "gajim/common/modules/chat_markers.py", "gajim/common/passwords.py", "gajim/common/regex.py", "gajim/common/sound.py",
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/098274e8b580eb11f555e81b97f76f6c...