diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 5146093ed..44a104534 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -213,21 +213,18 @@ class Account(object): """ return Contact(self, const.DC_CONTACT_ID_SELF) - def create_contact(self, addr, name=None): - """ create a (new) Contact. If there already is a Contact - with that e-mail address, it is unblocked and its display - name is updated. + def create_contact(self, obj, name=None): + """ create a (new) Contact or return an existing one. - :param addr: email-address, Account or Contact instance. - :param name: display name for this contact (optional) + Calling this method will always resulut in the same + underlying contact id. If there already is a Contact + with that e-mail address, it is unblocked and its display + `name` is updated if specified. + + :param obj: email-address, Account or Contact instance. + :param name: (optional) display name for this contact :returns: :class:`deltachat.contact.Contact` instance. """ - if not isinstance(addr, (Account, Contact, str)): - raise TypeError(str(addr)) - return self.as_contact(addr, name=name) - - def as_contact(self, obj, name=None): - """ Create a contact from an Account, Contact or e-mail address. """ if isinstance(obj, Account): if not obj.is_configured(): raise ValueError("can only add addresses from configured accounts") @@ -309,7 +306,7 @@ class Account(object): def create_chat(self, obj): """ Create a 1:1 chat with Account, Contact or e-mail address. """ - return self.as_contact(obj).create_chat() + return self.create_contact(obj).create_chat() def _create_chat_by_message_id(self, msg_id): return Chat(self, lib.dc_create_chat_by_msg_id(self._dc_context, msg_id)) diff --git a/python/src/deltachat/chat.py b/python/src/deltachat/chat.py index 543deb0b9..f512f8e7b 100644 --- a/python/src/deltachat/chat.py +++ b/python/src/deltachat/chat.py @@ -337,7 +337,7 @@ class Chat(object): :raises ValueError: if contact could not be added :returns: None """ - contact = self.account.as_contact(obj) + contact = self.account.create_contact(obj) 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)) @@ -350,7 +350,7 @@ class Chat(object): :raises ValueError: if contact could not be removed :returns: None """ - contact = self.account.as_contact(obj) + contact = self.account.create_contact(obj) 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/tests/test_account.py b/python/tests/test_account.py index d52593506..03002cc46 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -105,8 +105,8 @@ class TestOfflineAccountBasic: class TestOfflineContact: def test_contact_attr(self, acfactory): ac1 = acfactory.get_configured_offline_account() - contact1 = ac1.create_contact(addr="some1@hello.com", name="some1") - contact2 = ac1.create_contact(addr="some1@hello.com", name="some1") + contact1 = ac1.create_contact("some1@hello.com", name="some1") + contact2 = ac1.create_contact("some1@hello.com", name="some1") str(contact1) repr(contact1) assert contact1 == contact2 @@ -118,7 +118,7 @@ class TestOfflineContact: def test_get_contacts_and_delete(self, acfactory): ac1 = acfactory.get_configured_offline_account() - contact1 = ac1.create_contact(addr="some1@hello.com", name="some1") + contact1 = ac1.create_contact("some1@hello.com", name="some1") contacts = ac1.get_contacts() assert len(contacts) == 1 assert contact1 in contacts @@ -133,7 +133,7 @@ class TestOfflineContact: def test_get_contacts_and_delete_fails(self, acfactory): ac1 = acfactory.get_configured_offline_account() - contact1 = ac1.create_contact(addr="some1@example.com", name="some1") + contact1 = ac1.create_contact("some1@example.com", name="some1") msg = contact1.create_chat().send_text("one message") assert not ac1.delete_contact(contact1) assert not msg.filemime @@ -802,7 +802,7 @@ class TestOnlineAccount: lp.sec("ac1: creating group chat, and forward own message") group = ac1.create_group_chat("newgroup2") - group.add_contact(ac1.create_contact(ac2.get_config("addr"))) + group.add_contact(ac2) ac1.forward_messages([msg_out], group) # wait for other account to receive @@ -1485,7 +1485,7 @@ class TestOnlineAccount: assert locations[0].accuracy == 0.5 assert locations[0].timestamp > now - contact = ac2.create_contact(ac1.get_config("addr")) + contact = ac2.create_contact(ac1) locations2 = chat2.get_locations(contact=contact) assert len(locations2) == 1 assert locations2 == locations diff --git a/python/tests/test_lowlevel.py b/python/tests/test_lowlevel.py index 62fce7275..7477eb588 100644 --- a/python/tests/test_lowlevel.py +++ b/python/tests/test_lowlevel.py @@ -69,7 +69,7 @@ def test_sig(): def test_markseen_invalid_message_ids(acfactory): ac1 = acfactory.get_configured_offline_account() - contact1 = ac1.create_contact(addr="some1@example.com", name="some1") + contact1 = ac1.create_contact("some1@example.com", name="some1") chat = contact1.create_chat() chat.send_text("one messae") ac1._evtracker.get_matching("DC_EVENT_MSGS_CHANGED")