diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 92ec04d11b..7a48914abf 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -4007,13 +4007,13 @@ acceptGroupJoinRequestAsync acceptBusinessJoinRequestAsync :: User -> UserContactRequest -> CM GroupInfo acceptBusinessJoinRequestAsync user - ucr@UserContactRequest {agentInvitationId = AgentInvId invId, cReqChatVRange} = do + ucr@UserContactRequest {contactRequestId, agentInvitationId = AgentInvId invId, cReqChatVRange} = do vr <- chatVersionRange gVar <- asks random let userProfile@Profile {displayName, preferences} = profileToSendOnAccept user Nothing True groupPreferences = maybe defaultBusinessGroupPrefs businessGroupPrefs preferences (gInfo, clientMember) <- withStore $ \db -> do - liftIO $ deleteContactRequestRec db user ucr + liftIO $ deleteContactRequest db user contactRequestId createBusinessRequestGroup db vr gVar user ucr groupPreferences let GroupInfo {membership} = gInfo GroupMember {memberRole = userRole, memberId = userMemberId} = membership diff --git a/src/Simplex/Chat/Store/Groups.hs b/src/Simplex/Chat/Store/Groups.hs index b07adf407b..bb5252ddb0 100644 --- a/src/Simplex/Chat/Store/Groups.hs +++ b/src/Simplex/Chat/Store/Groups.hs @@ -155,7 +155,7 @@ import Simplex.Messaging.Util (eitherToMaybe, ($>>=), (<$$>)) import Simplex.Messaging.Version import UnliftIO.STM -type GroupInfoRow = (Int64, GroupName, GroupName, Text, Maybe Text, Maybe ImageData, Maybe ProfileId, Maybe MsgFilter, Maybe Bool, Bool, Maybe GroupPreferences) :. (UTCTime, UTCTime, Maybe UTCTime, Maybe UTCTime, Maybe MemberId, Maybe BusinessChatType, Maybe UIThemeEntityOverrides, Maybe CustomData) :. GroupMemberRow +type GroupInfoRow = (Int64, GroupName, GroupName, Text, Maybe Text, Maybe ImageData, Maybe ProfileId, Maybe MsgFilter, Maybe Bool, Bool, Maybe GroupPreferences) :. (UTCTime, UTCTime, Maybe UTCTime, Maybe UTCTime, Maybe MemberId, Maybe BusinessChatType, Maybe UIThemeEntityOverrides, Maybe CustomData) :. GroupMemberRow type GroupMemberRow = ((Int64, Int64, MemberId, VersionChat, VersionChat, GroupMemberRole, GroupMemberCategory, GroupMemberStatus, Bool, Maybe MemberRestrictionStatus) :. (Maybe Int64, Maybe GroupMemberId, ContactName, Maybe ContactId, ProfileId, ProfileId, ContactName, Text, Maybe ImageData, Maybe ConnReqContact, LocalAlias, Maybe Preferences)) @@ -936,18 +936,17 @@ createBusinessRequestGroup db vr gVar - user@User {userId} - ucr@UserContactRequest {profile} + user@User {userId, userContactId} + UserContactRequest {cReqChatVRange, profile = Profile {displayName, fullName, image, contactLink, preferences}} groupPreferences = do currentTs <- liftIO getCurrentTime groupInfo <- insertGroup_ currentTs - (groupMemberId, memberId) <- createAcceptedMember db gVar user groupInfo ucr GRMember + (groupMemberId, memberId) <- insertClientMember_ currentTs groupInfo liftIO $ setBusinessMemberId groupInfo memberId - acceptedMember <- getGroupMemberById db vr user groupMemberId - pure (groupInfo, acceptedMember) + clientMember <- getGroupMemberById db vr user groupMemberId + pure (groupInfo, clientMember) where - insertGroup_ currentTs = ExceptT $ do - let Profile {displayName, fullName, image} = profile + insertGroup_ currentTs = ExceptT $ withLocalDisplayName db userId displayName $ \localDisplayName -> runExceptT $ do groupId <- liftIO $ do DB.execute @@ -968,6 +967,31 @@ createBusinessRequestGroup memberId <- liftIO $ encodedRandomBytes gVar 12 void $ createContactMemberInv_ db user groupId Nothing user (MemberIdRole (MemberId memberId) GROwner) GCUserMember GSMemCreator IBUser Nothing currentTs vr getGroupInfo db vr user groupId + VersionRange minV maxV = cReqChatVRange + insertClientMember_ currentTs GroupInfo {groupId, membership} = ExceptT $ do + withLocalDisplayName db userId displayName $ \localDisplayName -> runExceptT $ do + liftIO $ + DB.execute + db + "INSERT INTO contact_profiles (display_name, full_name, image, contact_link, user_id, preferences, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?)" + (displayName, fullName, image, contactLink, userId, preferences, currentTs, currentTs) + profileId <- liftIO $ insertedRowId db + createWithRandomId gVar $ \memId -> do + DB.execute + db + [sql| + INSERT INTO group_members + ( group_id, member_id, member_role, member_category, member_status, invited_by, invited_by_group_member_id, + user_id, local_display_name, contact_id, contact_profile_id, created_at, updated_at, + peer_chat_min_version, peer_chat_max_version) + VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) + |] + ( (groupId, MemberId memId, GRMember, GCInviteeMember, GSMemAccepted, fromInvitedBy userContactId IBUser, groupMemberId' membership) + :. (userId, localDisplayName, Nothing :: (Maybe Int64), profileId, currentTs, currentTs) + :. (minV, maxV) + ) + groupMemberId <- liftIO $ insertedRowId db + pure (groupMemberId, MemberId memId) setBusinessMemberId GroupInfo {groupId} businessMemberId = do DB.execute db "UPDATE groups SET business_member_id = ? WHERE group_id = ?" (businessMemberId, groupId) diff --git a/tests/ChatTests/Profiles.hs b/tests/ChatTests/Profiles.hs index b2db679f29..3ffe57ba2e 100644 --- a/tests/ChatTests/Profiles.hs +++ b/tests/ChatTests/Profiles.hs @@ -688,18 +688,18 @@ testBusinessAddress = testChat3 businessProfile aliceProfile {fullName = "Alice biz <## "auto_accept on, business" bob ##> ("/c " <> cLink) bob <## "connection request sent!" - biz <## "#bob_1 (Bob): accepting business address request..." - biz <## "#bob_1: bob joined the group" + biz <## "#bob (Bob): accepting business address request..." + biz <## "#bob: bob_1 joined the group" bob <## "#biz: joining the group..." bob <## "#biz: you joined the group" - biz #> "#bob_1 hi" + biz #> "#bob hi" bob <# "#biz biz_1> hi" bob #> "#biz hello" - biz <# "#bob_1 bob> hello" + biz <# "#bob bob_1> hello" connectUsers biz alice biz <##> alice - biz ##> "/a #bob_1 alice" - biz <## "invitation to join the group #bob_1 sent to alice" + biz ##> "/a #bob alice" + biz <## "invitation to join the group #bob sent to alice" alice <## "#bob (Bob): biz invites you to join the group as member" alice <## "use /j bob to accept" alice ##> "/j bob" @@ -708,7 +708,7 @@ testBusinessAddress = testChat3 businessProfile aliceProfile {fullName = "Alice alice <## "#bob: you joined the group" alice <### [WithTime "#bob biz> hi [>>]", WithTime "#bob bob_1> hello [>>]"] alice <## "#bob: member bob_1 (Bob) is connected", - biz <## "#bob_1: alice joined the group", + biz <## "#bob: alice joined the group", do bob <## "#biz: biz_1 added alice (Alice @ Biz) to the group (connecting...)" bob <## "#biz: new member alice is connected" @@ -716,11 +716,11 @@ testBusinessAddress = testChat3 businessProfile aliceProfile {fullName = "Alice alice #> "#bob hey" concurrently_ (bob <# "#biz alice> hey") - (biz <# "#bob_1 alice> hey") + (biz <# "#bob alice> hey") bob #> "#biz hey there" concurrently_ (alice <# "#bob bob_1> hey there") - (biz <# "#bob_1 bob> hey there") + (biz <# "#bob bob_1> hey there") testPlanAddressOkKnown :: HasCallStack => FilePath -> IO () testPlanAddressOkKnown =