diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/chat.py b/deltachat-rpc-client/src/deltachat_rpc_client/chat.py index 82b104bd72..eef858b920 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/chat.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/chat.py @@ -219,10 +219,12 @@ def mark_noticed(self) -> None: """Mark all messages in this chat as noticed.""" self._rpc.marknoticed_chat(self.account.id, self.id) - def add_contact(self, *contact: Union[int, str, Contact]) -> None: + def add_contact(self, *contact: Union[int, str, Contact, "Account"]) -> None: """Add contacts to this group.""" + from .account import Account + for cnt in contact: - if isinstance(cnt, str): + if isinstance(cnt, (str, Account)): contact_id = self.account.create_contact(cnt).id elif not isinstance(cnt, int): contact_id = cnt.id @@ -230,10 +232,12 @@ def add_contact(self, *contact: Union[int, str, Contact]) -> None: contact_id = cnt self._rpc.add_contact_to_chat(self.account.id, self.id, contact_id) - def remove_contact(self, *contact: Union[int, str, Contact]) -> None: + def remove_contact(self, *contact: Union[int, str, Contact, "Account"]) -> None: """Remove members from this group.""" + from .account import Account + for cnt in contact: - if isinstance(cnt, str): + if isinstance(cnt, (str, Account)): contact_id = self.account.create_contact(cnt).id elif not isinstance(cnt, int): contact_id = cnt.id @@ -249,6 +253,10 @@ def get_contacts(self) -> list[Contact]: contacts = self._rpc.get_chat_contacts(self.account.id, self.id) return [Contact(self.account, contact_id) for contact_id in contacts] + def num_contacts(self) -> int: + """Return number of contacts in this chat.""" + return len(self.get_contacts()) + def get_past_contacts(self) -> list[Contact]: """Get past contacts for this chat.""" past_contacts = self._rpc.get_past_chat_contacts(self.account.id, self.id) diff --git a/deltachat-rpc-client/tests/test_something.py b/deltachat-rpc-client/tests/test_something.py index d8177eca69..b272aa4d25 100644 --- a/deltachat-rpc-client/tests/test_something.py +++ b/deltachat-rpc-client/tests/test_something.py @@ -1012,3 +1012,47 @@ def test_message_exists(acfactory): ac1.remove() assert not message1.exists() assert not message2.exists() + + +def test_synchronize_member_list_on_group_rejoin(acfactory, log): + """ + Test that user recreates group member list when it joins the group again. + ac1 creates a group with two other accounts: ac2 and ac3 + Then it removes ac2, removes ac3 and adds ac2 back. + ac2 did not see that ac3 is removed, so it should rebuild member list from scratch. + """ + log.section("setting up accounts, accepted with each other") + ac1, ac2, ac3 = accounts = acfactory.get_online_accounts(3) + + log.section("ac1: creating group chat with 2 other members") + chat = ac1.create_group("title1") + chat.add_contact(ac2) + chat.add_contact(ac3) + + log.section("ac1: send message to new group chat") + msg = chat.send_text("hello") + assert chat.num_contacts() == 3 + + log.section("checking that the chat arrived correctly") + for ac in accounts[1:]: + msg = ac.wait_for_incoming_msg().get_snapshot() + assert msg.text == "hello" + assert msg.chat.num_contacts() == 3 + msg.chat.accept() + + log.section("ac1: removing ac2") + chat.remove_contact(ac2) + + log.section("ac2: wait for a message about removal from the chat") + ac2.wait_for_incoming_msg() + log.section("ac1: removing ac3") + chat.remove_contact(ac3) + + log.section("ac1: adding ac2 back") + chat.add_contact(ac2) + + log.section("ac2: check that ac3 is removed") + msg = ac2.wait_for_incoming_msg() + + assert chat.num_contacts() == 2 + assert msg.get_snapshot().chat.num_contacts() == 2 diff --git a/python/tests/test_0_complex_or_slow.py b/python/tests/test_0_complex_or_slow.py index 5a370e0f22..6d436e54b8 100644 --- a/python/tests/test_0_complex_or_slow.py +++ b/python/tests/test_0_complex_or_slow.py @@ -1,4 +1,3 @@ -import sys import time import deltachat as dc @@ -63,56 +62,6 @@ def test_group_many_members_add_leave_remove(self, acfactory, lp): # Message should be encrypted because keys of other members are gossiped assert msg.is_encrypted() - def test_synchronize_member_list_on_group_rejoin(self, acfactory, lp): - """ - Test that user recreates group member list when it joins the group again. - ac1 creates a group with two other accounts: ac2 and ac3 - Then it removes ac2, removes ac3 and adds ac2 back. - ac2 did not see that ac3 is removed, so it should rebuild member list from scratch. - """ - lp.sec("setting up accounts, accepted with each other") - accounts = acfactory.get_online_accounts(3) - acfactory.introduce_each_other(accounts) - ac1, ac2, ac3 = accounts - - lp.sec("ac1: creating group chat with 2 other members") - chat = ac1.create_group_chat("title1", contacts=[ac2, ac3]) - assert not chat.is_promoted() - - lp.sec("ac1: send message to new group chat") - msg = chat.send_text("hello") - assert chat.is_promoted() and msg.is_encrypted() - - assert chat.num_contacts() == 3 - - lp.sec("checking that the chat arrived correctly") - for ac in accounts[1:]: - msg = ac._evtracker.wait_next_incoming_message() - assert msg.text == "hello" - print("chat is", msg.chat) - assert msg.chat.num_contacts() == 3 - - lp.sec("ac1: removing ac2") - chat.remove_contact(ac2) - - lp.sec("ac2: wait for a message about removal from the chat") - msg = ac2._evtracker.wait_next_incoming_message() - - lp.sec("ac1: removing ac3") - chat.remove_contact(ac3) - - lp.sec("ac1: adding ac2 back") - # Group is promoted, message is sent automatically - assert chat.is_promoted() - chat.add_contact(ac2) - - lp.sec("ac2: check that ac3 is removed") - msg = ac2._evtracker.wait_next_incoming_message() - - assert chat.num_contacts() == 2 - assert msg.chat.num_contacts() == 2 - acfactory.dump_imap_summary(sys.stdout) - def test_qr_verified_group_and_chatting(acfactory, lp): ac1, ac2, ac3 = acfactory.get_online_accounts(3)