From 3a85b671a173aa323433dcee6428f8d4b40406cf Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 8 Jun 2020 17:19:21 +0200 Subject: [PATCH] remove acfactory.get_chat() in favour of account.create_chat(account2) directly working. --- python/src/deltachat/account.py | 37 ++++++++++++++++++++++------- python/src/deltachat/chat.py | 11 +++++++++ python/src/deltachat/contact.py | 5 +++- python/src/deltachat/testplugin.py | 4 ---- python/tests/test_account.py | 38 +++++++++++++++++++++++------- 5 files changed, 74 insertions(+), 21 deletions(-) diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 91b17db01..0dfdb7f9d 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -216,19 +216,24 @@ class Account(object): def create_contact(self, email, name=None): """ create a (new) Contact. If there already is a Contact - with that e-mail address, it is unblocked and its name is - updated. + with that e-mail address, it is unblocked and its display + name is updated. - :param email: email-address (text type) + :param email: email-address (text type) or Contact (from other account). :param name: display name for this contact (optional) :returns: :class:`deltachat.contact.Contact` instance. """ - realname, addr = parseaddr(email) - if name: - realname = name - realname = as_dc_charpointer(realname) + if isinstance(email, Contact): + # might come from another account + name = email.name + addr = email.addr + else: + realname, addr = parseaddr(email) + if not name and realname: + name = realname + name = as_dc_charpointer(name) addr = as_dc_charpointer(addr) - contact_id = lib.dc_create_contact(self._dc_context, realname, addr) + contact_id = lib.dc_create_contact(self._dc_context, name, addr) assert contact_id > const.DC_CHAT_ID_LAST_SPECIAL return Contact(self, contact_id) @@ -280,6 +285,22 @@ class Account(object): ) yield from iter_array(dc_array, lambda x: Message.from_db(self, x)) + def create_chat(self, obj): + """ Create a 1:1 chat with Account/Contact/e-mail addresses. """ + if isinstance(obj, Account): + if not obj.is_configured(): + raise ValueError("can only add addresses from a configured account") + other = obj.get_self_contact() + contact = self.create_contact(other.addr, other.name) + elif isinstance(obj, Contact): + contact = obj + elif isinstance(obj, str): + realname, addr = parseaddr(obj) + contact = self.create_contact(addr, realname) + else: + raise TypeError("don't know how to create chat for %r" % (obj, )) + return contact.get_chat() + def create_chat_by_contact(self, contact): """ create or get an existing 1:1 chat object for the specified contact or contact id. diff --git a/python/src/deltachat/chat.py b/python/src/deltachat/chat.py index 21f4da558..32b50a1a0 100644 --- a/python/src/deltachat/chat.py +++ b/python/src/deltachat/chat.py @@ -328,16 +328,26 @@ class Chat(object): # ------ group management API ------------------------------ + def _port_contact(self, contact): + if contact.account != self.account: + contact = self.account.create_contact(contact.addr, contact.display_name) + return contact + def add_contact(self, contact): """ add a contact to this chat. + If the contact is from another account create a new + contact and add it to the group. + :params: contact object. :raises ValueError: if contact could not be added :returns: None """ + contact = self._port_contact(contact) ret = lib.dc_add_contact_to_chat(self.account._dc_context, self.id, contact.id) if ret != 1: raise ValueError("could not add contact {!r} to chat".format(contact)) + return contact def remove_contact(self, contact): """ remove a contact from this chat. @@ -346,6 +356,7 @@ class Chat(object): :raises ValueError: if contact could not be removed :returns: None """ + contact = self._port_contact(contact) ret = lib.dc_remove_contact_from_chat(self.account._dc_context, self.id, contact.id) if ret != 1: raise ValueError("could not remove contact {!r} from chat".format(contact)) diff --git a/python/src/deltachat/contact.py b/python/src/deltachat/contact.py index 92e46f50c..4eb5efc68 100644 --- a/python/src/deltachat/contact.py +++ b/python/src/deltachat/contact.py @@ -36,10 +36,13 @@ class Contact(object): return from_dc_charpointer(lib.dc_contact_get_addr(self._dc_contact)) @props.with_doc - def display_name(self): + def name(self): """ display name for this contact. """ return from_dc_charpointer(lib.dc_contact_get_display_name(self._dc_contact)) + # deprecated alias + display_name = name + def is_blocked(self): """ Return True if the contact is blocked. """ return lib.dc_contact_is_blocked(self._dc_contact) diff --git a/python/src/deltachat/testplugin.py b/python/src/deltachat/testplugin.py index 7c80863e4..411e4a6af 100644 --- a/python/src/deltachat/testplugin.py +++ b/python/src/deltachat/testplugin.py @@ -387,10 +387,6 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data): imap.dump_account_info(logfile=logfile) imap.dump_imap_structures(tmpdir, logfile=logfile) - def get_chat(self, ac1, ac2): - chat12, chat21 = self.get_chats(ac1, ac2, both=False) - return chat12 - def get_chats(self, ac1, ac2, both=True): chat12 = ac1.create_chat_by_contact( ac1.create_contact(email=ac2.get_config("addr"))) diff --git a/python/tests/test_account.py b/python/tests/test_account.py index dc69e6f0b..c6aad913e 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -140,6 +140,18 @@ class TestOfflineContact: assert not ac1.delete_contact(contact1) assert not msg.filemime + def test_create_chat_flexibility(self, acfactory): + ac1 = acfactory.get_configured_offline_account() + ac2 = acfactory.get_configured_offline_account() + chat1 = ac1.create_chat(ac2) + contact = ac1.create_contact(ac2.get_self_contact()) + chat2 = ac1.create_chat(contact) + chat3 = ac1.create_chat(ac2.get_self_contact().addr) + assert chat1 == chat2 and chat2 == chat3 + ac3 = acfactory.get_unconfigured_account() + with pytest.raises(ValueError): + ac1.create_chat(ac3) + class TestOfflineChat: @pytest.fixture @@ -180,6 +192,15 @@ class TestOfflineChat: else: pytest.fail("could not find chat") + def test_group_chat_add_second_account(self, acfactory): + ac1 = acfactory.get_configured_offline_account() + ac2 = acfactory.get_configured_offline_account() + chat = ac1.create_group_chat(name="title1") + ac2_contact = ac2.get_self_contact() + contact = chat.add_contact(ac2_contact) + assert contact != ac2_contact + assert contact.account == ac1 + def test_group_chat_creation(self, ac1): contact1 = ac1.create_contact("some1@hello.com", name="some1") contact2 = ac1.create_contact("some2@hello.com", name="some2") @@ -759,7 +780,7 @@ class TestOnlineAccount: def test_forward_messages(self, acfactory, lp): ac1, ac2 = acfactory.get_two_online_accounts() - chat = acfactory.get_chat(ac1, ac2) + chat = ac1.create_chat(ac2) lp.sec("ac1: send message to ac2") msg_out = chat.send_text("message2") @@ -834,7 +855,7 @@ class TestOnlineAccount: ac1.set_config("displayname", "รค name") lp.sec("ac1: create chat with ac2") - chat = acfactory.get_chat(ac1, ac2) + chat = ac1.create_chat(ac2) lp.sec("sending text message from ac1 to ac2") msg1 = chat.send_text("message1") @@ -895,7 +916,8 @@ class TestOnlineAccount: ac1, ac2 = acfactory.get_two_online_accounts(move=True) lp.sec("ac1: create chat with ac2") - chat, _ = acfactory.get_chats(ac1, ac2) + chat = ac1.create_chat(ac2) + ac2.create_chat(ac1) # make sure mdns are enabled (usually enabled by default already) ac1.set_config("mdns_enabled", "1") @@ -930,7 +952,7 @@ class TestOnlineAccount: ac1, ac2 = acfactory.get_two_online_accounts() lp.sec("ac1: create chat with ac2") - chat = acfactory.get_chat(ac1, ac2) + chat = ac1.create_chat(ac2) lp.sec("sending text message from ac1 to ac2") msg_out = chat.send_text("message1") @@ -1005,7 +1027,7 @@ class TestOnlineAccount: ac1, ac2 = acfactory.get_two_online_accounts() lp.sec("ac1: create chat with ac2") - chat = acfactory.get_chat(ac1, ac2) + chat = ac1.create_chat(ac2) lp.sec("sending text message from ac1 to ac2") msg_out = chat.send_text("message1") @@ -1059,7 +1081,7 @@ class TestOnlineAccount: lp.sec("configure ac2 to save mime headers, create ac1/ac2 chat") ac2.set_config("save_mime_headers", "1") - chat = acfactory.get_chat(ac1, ac2) + chat = ac1.create_chat(ac2) lp.sec("sending text message from ac1 to ac2") msg_out = chat.send_text("message1") @@ -1104,7 +1126,7 @@ class TestOnlineAccount: def test_send_and_receive_image(self, acfactory, lp, data): ac1, ac2 = acfactory.get_two_online_accounts() - chat = acfactory.get_chat(ac1, ac2) + chat = ac1.create_chat(ac2) message_queue = queue.Queue() @@ -1309,7 +1331,7 @@ class TestOnlineAccount: ac2.set_avatar(p) lp.sec("ac1: send message to ac2") - chat = acfactory.get_chat(ac1, ac2) + chat = ac1.create_chat(ac2) msg1 = chat.send_text("hi -- do you see my brand new avatar?") assert not msg1.is_encrypted()