Rebuild member list on group rejoin
When user is removed from the group, it remembers member list of the chat, but does not receive any further updates.

Since change https://github.com/deltachat/deltachat-core-rust/pull/1291 when account is added back to the chat, it only adds itself to the list of members. Because it missed all previous updates, its member list may be very different from the current list of members.
diff --git a/python/tests/test_account.py b/python/tests/test_account.py
index 6e9d1f6f..71c8322d 100644
--- a/python/tests/test_account.py
+++ b/python/tests/test_account.py
@@ -1366,6 +1366,80 @@ 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("creating and configuring five accounts")
+        accounts = [acfactory.get_online_configuring_account() for i in range(3)]
+        for acc in accounts:
+            wait_configuration_progress(acc, 1000)
+        ac1 = accounts.pop()
+
+        lp.sec("ac1: setting up contacts with 2 other members")
+        contacts = []
+        for acc, name in zip(accounts, ["ac2", "ac3"]):
+            contact = ac1.create_contact(acc.get_config("addr"), name=name)
+            contacts.append(contact)
+
+            # make sure we accept the "hi" message
+            ac1.create_chat_by_contact(contact)
+
+            # make sure the other side accepts our messages
+            c1 = acc.create_contact(ac1.get_config("addr"), "a member")
+            chat1 = acc.create_chat_by_contact(c1)
+
+            # send a message to get the contact key via autocrypt header
+            chat1.send_text("hi")
+            msg = ac1.wait_next_incoming_message()
+            assert msg.text == "hi"
+
+        ac2, ac3 = accounts
+
+        lp.sec("ac1: creating group chat with 2 other members")
+        chat = ac1.create_group_chat("title1")
+        for contact in contacts:
+            chat.add_contact(contact)
+        assert not chat.is_promoted()
+
+        lp.sec("ac1: send mesage to new group chat")
+        msg = chat.send_text("hello")
+        assert chat.is_promoted()
+        assert msg.is_encrypted()
+
+        num_contacts = len(chat.get_contacts())
+        assert num_contacts == 3
+
+        lp.sec("checking that the chat arrived correctly")
+        for ac in accounts:
+            msg = ac.wait_next_incoming_message()
+            assert msg.text == "hello"
+            print("chat is", msg.chat)
+            assert len(msg.chat.get_contacts()) == 3
+
+        lp.sec("ac1: removing ac2")
+        chat.remove_contact(contacts[0])
+
+        lp.sec("ac2: wait for a message about removal from the chat")
+        msg = ac2.wait_next_incoming_message()
+
+        lp.sec("ac1: removing ac3")
+        chat.remove_contact(contacts[1])
+
+        lp.sec("ac1: adding ac2 back")
+        # Group is promoted, message is sent automatically
+        assert chat.is_promoted()
+        chat.add_contact(contacts[0])
+
+        lp.sec("ac2: check that ac3 is removed")
+        msg = ac2.wait_next_incoming_message()
+
+        assert len(msg.chat.get_contacts()) == len(chat.get_contacts())
+
 
 class TestOnlineConfigureFails:
     def test_invalid_password(self, acfactory):
diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs
index d6510754..16ddba48 100644
--- a/src/dc_receive_imf.rs
+++ b/src/dc_receive_imf.rs
@@ -1023,6 +1023,17 @@ fn create_or_lookup_group(
     // add members to group/check members
     if recreate_member_list {
         if !chat::is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF) {
+            // Members could have been removed while we were
+            // absent. We can't use existing member list and need to
+            // start from scratch.
+            sql::execute(
+                context,
+                &context.sql,
+                "DELETE FROM chats_contacts WHERE chat_id=?;",
+                params![chat_id],
+            )
+            .ok();
+
             chat::add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF);
         }
         if from_id > DC_CONTACT_ID_LAST_SPECIAL
['src/dc_receive_imf.rs', 'python/tests/test_account.py']