From f80f56de61d992ca0246149f11727ffd7e196833 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 9 Nov 2022 21:11:05 +0400 Subject: [PATCH] core: allow repeat connection via group link if group was deleted but contact with host is present (#1335) --- src/Simplex/Chat/Store.hs | 16 ++++++++++ tests/ChatTests.hs | 61 +++++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/Simplex/Chat/Store.hs b/src/Simplex/Chat/Store.hs index fd20689a49..c8c43467b1 100644 --- a/src/Simplex/Chat/Store.hs +++ b/src/Simplex/Chat/Store.hs @@ -1736,8 +1736,24 @@ deleteGroupConnectionsAndFiles db User {userId} GroupInfo {groupId} members = do deleteGroupItemsAndMembers :: DB.Connection -> User -> GroupInfo -> [GroupMember] -> IO () deleteGroupItemsAndMembers db user@User {userId} GroupInfo {groupId} members = do DB.execute db "DELETE FROM chat_items WHERE user_id = ? AND group_id = ?" (userId, groupId) + void $ runExceptT cleanupHostGroupLinkConn_ -- to allow repeat connection via the same group link if one was used DB.execute db "DELETE FROM group_members WHERE user_id = ? AND group_id = ?" (userId, groupId) forM_ members $ \m -> cleanupMemberContactAndProfile_ db user m + where + cleanupHostGroupLinkConn_ = do + hostId <- getHostMemberId_ db user groupId + liftIO $ + DB.execute + db + [sql| + UPDATE connections SET via_contact_uri_hash = NULL, xcontact_id = NULL + WHERE user_id = ? AND via_group_link = 1 AND contact_id IN ( + SELECT contact_id + FROM group_members + WHERE user_id = ? AND group_member_id = ? + ) + |] + (userId, userId, hostId) deleteGroup :: DB.Connection -> User -> GroupInfo -> IO () deleteGroup db User {userId} GroupInfo {groupId, localDisplayName} = do diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index a5857531c9..6bec4a1982 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -144,6 +144,7 @@ chatTests = do it "set chat item TTL" testSetChatItemTTL describe "group links" $ do it "create group link, join via group link" testGroupLink + it "delete group, re-join via same link" testGroupLinkDeleteGroupRejoin it "sending message to contact created via group link marks it used" testGroupLinkContactUsed it "create group link, join via group link - incognito membership" testGroupLinkIncognitoMembership describe "queue rotation" $ do @@ -3558,6 +3559,60 @@ testGroupLink = alice ##> "/show link #team" alice <## "no group link, to create: /create link #team" +testGroupLinkDeleteGroupRejoin :: IO () +testGroupLinkDeleteGroupRejoin = + testChat2 aliceProfile bobProfile $ + \alice bob -> do + alice ##> "/g team" + alice <## "group #team is created" + alice <## "use /a team to add members" + alice ##> "/create link #team" + gLink <- getGroupLink alice "team" True + bob ##> ("/c " <> gLink) + bob <## "connection request sent!" + alice <## "bob (Bob): accepting request to join group #team..." + concurrentlyN_ + [ do + alice <## "bob (Bob): contact is connected" + alice <## "bob invited to group #team via your group link" + alice <## "#team: bob joined the group", + do + bob <## "alice (Alice): contact is connected" + bob <## "#team: you joined the group" + ] + -- use contact so it's not deleted when deleting group + bob <##> alice + bob ##> "/l team" + concurrentlyN_ + [ do + bob <## "#team: you left the group" + bob <## "use /d #team to delete the group", + alice <## "#team: bob left the group" + ] + bob ##> "/d #team" + bob <## "#team: you deleted the group" + -- re-join via same link + bob ##> ("/c " <> gLink) + bob <## "connection request sent!" + alice <## "bob_1 (Bob): accepting request to join group #team..." + concurrentlyN_ + [ do + alice <## "bob_1 (Bob): contact is connected" + alice <## "bob_1 invited to group #team via your group link" + alice <## "contact bob_1 is merged into bob" + alice <## "use @bob to send messages" + alice <## "#team: bob joined the group", + do + bob <## "alice_1 (Alice): contact is connected" + bob <## "contact alice_1 is merged into alice" + bob <## "use @alice to send messages" + bob <## "#team: you joined the group" + ] + alice #> "#team hello" + bob <# "#team alice> hello" + bob #> "#team hi there" + alice <# "#team bob> hi there" + testGroupLinkContactUsed :: IO () testGroupLinkContactUsed = testChat2 aliceProfile bobProfile $ @@ -3565,14 +3620,8 @@ testGroupLinkContactUsed = alice ##> "/g team" alice <## "group #team is created" alice <## "use /a team to add members" - alice ##> "/show link #team" - alice <## "no group link, to create: /create link #team" alice ##> "/create link #team" gLink <- getGroupLink alice "team" True - alice ##> "/show link #team" - _ <- getGroupLink alice "team" False - alice ##> "/create link #team" - alice <## "you already have link for this group, to show: /show link #team" bob ##> ("/c " <> gLink) bob <## "connection request sent!" alice <## "bob (Bob): accepting request to join group #team..."