diff options
author | lovetox <philipp@hoerist.com> | 2022-08-08 00:58:45 +0300 |
---|---|---|
committer | lovetox <philipp@hoerist.com> | 2022-08-09 17:48:39 +0300 |
commit | 759fb3312e4dc5df1c43c88dd35fe7d887990a5e (patch) | |
tree | d15c535215e1a2453484654b02fa1f2f9b939a30 | |
parent | 68544d554e53d08117738ea6834138506f8204ea (diff) |
other: Contacts: Add dedicated methods for adding contacts
-rw-r--r-- | gajim/common/modules/contacts.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/gajim/common/modules/contacts.py b/gajim/common/modules/contacts.py index 6cba719fa..2ed88ca1f 100644 --- a/gajim/common/modules/contacts.py +++ b/gajim/common/modules/contacts.py @@ -21,6 +21,7 @@ from typing import Union from typing import overload import cairo +from gi.repository.GObject import Value from nbxmpp.const import Affiliation from nbxmpp.const import Chatstate from nbxmpp.const import Role @@ -122,6 +123,56 @@ class Contacts(BaseModule): if state.is_disconnected: self._reset_presence() + def add_chat_contact(self, jid: Union[str, JID]) -> BareContact: + if isinstance(jid, str): + jid = JID.from_string(jid) + + contact = self._contacts.get(jid) + if contact is not None: + if not isinstance(contact, BareContact): + raise ValueError(f'Trying to add GroupchatContact {jid}, ' + f'but contact exists already as {contact}') + return contact + + contact = BareContact(self._log, jid, self._account) + + self._contacts[jid] = contact + return contact + + def add_group_chat_contact(self, jid: Union[str, JID]) -> GroupchatContact: + if isinstance(jid, str): + jid = JID.from_string(jid) + + contact = self._contacts.get(jid) + if contact is not None: + if not isinstance(contact, GroupchatContact): + raise ValueError(f'Trying to add GroupchatContact {jid}, ' + f'but contact exists already as {contact}') + return contact + + contact = GroupchatContact(self._log, jid, self._account) + + self._contacts[jid] = contact + return contact + + def add_private_contact(self, jid: Union[str, JID]) -> GroupchatParticipant: + if isinstance(jid, str): + jid = JID.from_string(jid) + + if jid.resource is None: + raise ValueError(f'Trying to add a bare JID as private {jid}') + + contact = self._contacts.get(jid.bare) + if not isinstance(contact, GroupchatContact): + raise ValueError(f'Trying to add GroupchatParticipant {jid}, ' + f'to BareContact {contact}') + + if contact is None: + group_chat_contact = self.add_group_chat_contact(jid.bare) + return group_chat_contact.add_resource(jid.resource) + + return contact.add_resource(jid.resource) + def add_contact(self, jid: Union[str, JID], groupchat: bool = False) -> Union[BareContact, @@ -597,6 +648,10 @@ class GroupchatContact(CommonContact): # codebase is type checked and it creates hard to track # problems if we create a GroupchatParticipant without resource + contact = self._resources.get(resource) + if contact is not None: + return contact + jid = self._jid.new_with(resource=resource) assert isinstance(self._log, LogAdapter) contact = GroupchatParticipant(self._log, jid, self._account) |