core: fix change connection user (#5992)

* core: fix change connection user

* plans
This commit is contained in:
spaced4ndy 2025-06-16 21:38:02 +00:00 committed by GitHub
parent c08189108e
commit 3d22b738d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 34 additions and 54 deletions

View file

@ -1671,25 +1671,10 @@ processChatCommand' vr = \case
case (pccConnStatus, connLinkInv) of
(ConnNew, Just (CCLink cReqInv _)) -> do
newUser <- privateGetUser newUserId
conn' <- ifM (canKeepLink cReqInv newUser) (updateConnRecord user conn newUser) (recreateConn user conn newUser)
conn' <- recreateConn user conn newUser
pure $ CRConnectionUserChanged user conn conn' newUser
_ -> throwChatError CEConnectionUserChangeProhibited
where
canKeepLink :: ConnReqInvitation -> User -> CM Bool
canKeepLink (CRInvitationUri crData _) newUser = do
let ConnReqUriData {crSmpQueues = q :| _} = crData
SMPQueueUri {queueAddress = SMPQueueAddress {smpServer}} = q
newUserServers <-
map protoServer' . L.filter (\ServerCfg {enabled} -> enabled)
<$> getKnownAgentServers SPSMP newUser
pure $ smpServer `elem` newUserServers
updateConnRecord user@User {userId} conn@PendingContactConnection {customUserProfileId} newUser = do
withAgent $ \a -> changeConnectionUser a (aUserId user) (aConnId' conn) (aUserId newUser)
withFastStore' $ \db -> do
conn' <- updatePCCUser db userId conn newUserId
forM_ customUserProfileId $ \profileId ->
deletePCCIncognitoProfile db user profileId
pure conn'
recreateConn user conn@PendingContactConnection {customUserProfileId, connLinkInv} newUser = do
subMode <- chatReadVar subscriptionMode
let userData = shortLinkUserData $ isJust $ connShortLink =<< connLinkInv

View file

@ -66,7 +66,6 @@ module Simplex.Chat.Store.Direct
updateContactAccepted,
getUserByContactRequestId,
getPendingContactConnections,
updatePCCUser,
getContactConnections,
getConnectionById,
getConnectionsContacts,
@ -440,19 +439,6 @@ updatePCCIncognito db User {userId} conn customUserProfileId = do
(customUserProfileId, updatedAt, userId, pccConnId conn)
pure (conn :: PendingContactConnection) {customUserProfileId, updatedAt}
updatePCCUser :: DB.Connection -> UserId -> PendingContactConnection -> UserId -> IO PendingContactConnection
updatePCCUser db userId conn newUserId = do
updatedAt <- getCurrentTime
DB.execute
db
[sql|
UPDATE connections
SET user_id = ?, custom_user_profile_id = NULL, updated_at = ?
WHERE user_id = ? AND connection_id = ?
|]
(newUserId, updatedAt, userId, pccConnId conn)
pure (conn :: PendingContactConnection) {customUserProfileId = Nothing, updatedAt}
deletePCCIncognitoProfile :: DB.Connection -> User -> ProfileId -> IO ()
deletePCCIncognitoProfile db User {userId} profileId =
DB.execute

View file

@ -1071,10 +1071,6 @@ Query: UPDATE connections SET smp_agent_version = ? WHERE conn_id = ?
Plan:
SEARCH connections USING PRIMARY KEY (conn_id=?)
Query: UPDATE connections SET user_id = ? WHERE conn_id = ? and user_id = ?
Plan:
SEARCH connections USING PRIMARY KEY (conn_id=?)
Query: UPDATE messages SET msg_body = x'' WHERE conn_id = ? AND internal_id = ?
Plan:
SEARCH messages USING PRIMARY KEY (conn_id=? AND internal_id=?)

View file

@ -4215,14 +4215,6 @@ Query:
Plan:
SEARCH connections USING INTEGER PRIMARY KEY (rowid=?)
Query:
UPDATE connections
SET user_id = ?, custom_user_profile_id = NULL, updated_at = ?
WHERE user_id = ? AND connection_id = ?
Plan:
SEARCH connections USING INTEGER PRIMARY KEY (rowid=?)
Query:
UPDATE contact_profiles
SET contact_link = ?, updated_at = ?

View file

@ -1747,10 +1747,9 @@ viewConnectionIncognitoUpdated PendingContactConnection {pccConnId, customUserPr
| otherwise = ["connection " <> sShow pccConnId <> " changed to non incognito"]
viewConnectionUserChanged :: User -> PendingContactConnection -> User -> PendingContactConnection -> [StyledString]
viewConnectionUserChanged User {localDisplayName = n} PendingContactConnection {pccConnId, connLinkInv} User {localDisplayName = n'} PendingContactConnection {connLinkInv = connLinkInv'} =
case (connLinkInv, connLinkInv') of
(Just ccLink, Just ccLink')
| ccLink /= ccLink' -> [userChangedStr <> ", new link:"] <> newLink ccLink'
viewConnectionUserChanged User {localDisplayName = n} PendingContactConnection {pccConnId} User {localDisplayName = n'} PendingContactConnection {connLinkInv = connLinkInv'} =
case connLinkInv' of
Just ccLink' -> [userChangedStr <> ", new link:"] <> newLink ccLink'
_ -> [userChangedStr]
where
userChangedStr = "connection " <> sShow pccConnId <> " changed from user " <> plain n <> " to user " <> plain n'

View file

@ -1827,7 +1827,7 @@ testChangePCCUser = testChat2 aliceProfile bobProfile $
\alice bob -> do
-- Create a new invite
alice ##> "/connect"
inv <- getInvitation alice
_ <- getInvitation alice
-- Create new user and go back to original user
alice ##> "/create user alisa"
showActiveUser alice "alisa"
@ -1837,12 +1837,18 @@ testChangePCCUser = testChat2 aliceProfile bobProfile $
showActiveUser alice "alice (Alice)"
-- Change connection to newly created user
alice ##> "/_set conn user :1 2"
alice <## "connection 1 changed from user alice to user alisa"
alice <## "connection 1 changed from user alice to user alisa, new link:"
alice <## ""
_ <- getTermLine alice
alice <## ""
alice ##> "/user alisa"
showActiveUser alice "alisa"
-- Change connection back to other user
alice ##> "/_set conn user :1 3"
alice <## "connection 1 changed from user alisa to user alisa2"
alice <## "connection 1 changed from user alisa to user alisa2, new link:"
alice <## ""
inv <- getTermLine alice
alice <## ""
alice ##> "/user alisa2"
showActiveUser alice "alisa2"
-- Connect
@ -1851,13 +1857,14 @@ testChangePCCUser = testChat2 aliceProfile bobProfile $
concurrently_
(alice <## "bob (Bob): contact is connected")
(bob <## "alisa2: contact is connected")
alice <##> bob
testChangePCCUserFromIncognito :: HasCallStack => TestParams -> IO ()
testChangePCCUserFromIncognito = testChat2 aliceProfile bobProfile $
\alice bob -> do
-- Create a new invite and set as incognito
alice ##> "/connect"
inv <- getInvitation alice
_ <- getInvitation alice
alice ##> "/_set incognito :1 on"
alice <## "connection 1 changed to incognito"
-- Create new user and go back to original user
@ -1867,13 +1874,19 @@ testChangePCCUserFromIncognito = testChat2 aliceProfile bobProfile $
showActiveUser alice "alice (Alice)"
-- Change connection to newly created user
alice ##> "/_set conn user :1 2"
alice <## "connection 1 changed from user alice to user alisa"
alice <## "connection 1 changed from user alice to user alisa, new link:"
alice <## ""
_ <- getTermLine alice
alice <## ""
alice `hasContactProfiles` ["alice"]
alice ##> "/user alisa"
showActiveUser alice "alisa"
-- Change connection back to initial user
alice ##> "/_set conn user :1 1"
alice <## "connection 1 changed from user alisa to user alice"
alice <## "connection 1 changed from user alisa to user alice, new link:"
alice <## ""
inv <- getTermLine alice
alice <## ""
alice ##> "/user alice"
showActiveUser alice "alice (Alice)"
-- Connect
@ -1882,13 +1895,14 @@ testChangePCCUserFromIncognito = testChat2 aliceProfile bobProfile $
concurrently_
(alice <## "bob (Bob): contact is connected")
(bob <## "alice (Alice): contact is connected")
alice <##> bob
testChangePCCUserAndThenIncognito :: HasCallStack => TestParams -> IO ()
testChangePCCUserAndThenIncognito = testChat2 aliceProfile bobProfile $
\alice bob -> do
-- Create a new invite and set as incognito
alice ##> "/connect"
inv <- getInvitation alice
_ <- getInvitation alice
-- Create new user and go back to original user
alice ##> "/create user alisa"
showActiveUser alice "alisa"
@ -1896,7 +1910,10 @@ testChangePCCUserAndThenIncognito = testChat2 aliceProfile bobProfile $
showActiveUser alice "alice (Alice)"
-- Change connection to newly created user
alice ##> "/_set conn user :1 2"
alice <## "connection 1 changed from user alice to user alisa"
alice <## "connection 1 changed from user alice to user alisa, new link:"
alice <## ""
inv <- getTermLine alice
alice <## ""
alice ##> "/user alisa"
showActiveUser alice "alisa"
-- Change connection to incognito and make sure it's attached to the newly created user profile
@ -1911,6 +1928,10 @@ testChangePCCUserAndThenIncognito = testChat2 aliceProfile bobProfile $
alice <## ("bob (Bob): contact is connected, your incognito profile for this contact is " <> alisaIncognito)
alice <## ("use /i bob to print out this incognito profile again")
]
alice ?#> "@bob hi"
bob <# (alisaIncognito <> "> hi")
bob #> ("@" <> alisaIncognito <> " hey")
alice ?<# "bob> hey"
testChangePCCUserDiffSrv :: HasCallStack => TestParams -> IO ()
testChangePCCUserDiffSrv ps = do
@ -1951,6 +1972,7 @@ testChangePCCUserDiffSrv ps = do
concurrently_
(alice <## "bob (Bob): contact is connected")
(bob <## "alisa: contact is connected")
alice <##> bob
where
serverCfg' =
smpServerCfg