diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 411a48bea4..2d84c07e88 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -659,18 +659,18 @@ processChatCommand = \case pure $ CRChatCleared user (AChatInfo SCTGroup $ GroupChat gInfo) CTContactConnection -> pure $ chatCmdError (Just user) "not supported" CTContactRequest -> pure $ chatCmdError (Just user) "not supported" - APIAcceptContact connReqId -> withUser $ \user@User {userId} -> withChatLock "acceptContact" $ do - cReq <- withStore $ \db -> getContactRequest db userId connReqId + APIAcceptContact connReqId -> withUser $ \_ -> withChatLock "acceptContact" $ do + (user, cReq) <- withStore $ \db -> getContactRequest' db connReqId -- [incognito] generate profile to send, create connection with incognito profile incognito <- readTVarIO =<< asks incognitoMode incognitoProfile <- if incognito then Just . NewIncognito <$> liftIO generateRandomProfile else pure Nothing ct <- acceptContactRequest user cReq incognitoProfile pure $ CRAcceptingContactRequest user ct - APIRejectContact connReqId -> withUser $ \user@User {userId} -> withChatLock "rejectContact" $ do + APIRejectContact connReqId -> withUser $ \user -> withChatLock "rejectContact" $ do cReq@UserContactRequest {agentContactConnId = AgentConnId connId, agentInvitationId = AgentInvId invId} <- withStore $ \db -> - getContactRequest db userId connReqId - `E.finally` liftIO (deleteContactRequest db userId connReqId) + getContactRequest db user connReqId + `E.finally` liftIO (deleteContactRequest db user connReqId) withAgent $ \a -> rejectContact a connId invId pure $ CRContactRequestRejected user cReq APISendCallInvitation contactId callType -> withUser $ \user -> do diff --git a/src/Simplex/Chat/Store.hs b/src/Simplex/Chat/Store.hs index f1d6546433..7f40a62ae0 100644 --- a/src/Simplex/Chat/Store.hs +++ b/src/Simplex/Chat/Store.hs @@ -35,6 +35,8 @@ module Simplex.Chat.Store getUserByAConnId, getUserByContactId, getUserByGroupId, + getUserByFileId, + getUserByContactRequestId, getUserFileInfo, deleteUserRecord, createDirectConnection, @@ -74,6 +76,7 @@ module Simplex.Chat.Store getGroupLink, getGroupLinkId, createOrUpdateContactRequest, + getContactRequest', getContactRequest, getContactRequestIdByName, deleteContactRequest, @@ -524,6 +527,11 @@ getUserByFileId db fileId = ExceptT . firstRow toUser (SEUserNotFoundByFileId fileId) $ DB.query db (userQuery <> " JOIN files f ON f.user_id = u.user_id WHERE f.file_id = ?") (Only fileId) +getUserByContactRequestId :: DB.Connection -> Int64 -> ExceptT StoreError IO User +getUserByContactRequestId db contactRequestId = + ExceptT . firstRow toUser (SEUserNotFoundByContactRequestId contactRequestId) $ + DB.query db (userQuery <> " JOIN contact_requests cr ON cr.user_id = u.user_id WHERE cr.contact_request_id = ?") (Only contactRequestId) + getUserFileInfo :: DB.Connection -> User -> IO [CIFileInfo] getUserFileInfo db User {userId} = map toFileInfo @@ -1173,10 +1181,10 @@ createOrUpdateContactRequest db user@User {userId} userContactLinkId invId Profi createOrUpdate_ = do cReqId <- ExceptT $ - maybeM getContactRequest' xContactId_ >>= \case + maybeM getContactRequestByXContactId xContactId_ >>= \case Nothing -> createContactRequest Just cr -> updateContactRequest cr $> Right (contactRequestId (cr :: UserContactRequest)) - getContactRequest db userId cReqId + getContactRequest db user cReqId createContactRequest :: IO (Either StoreError Int64) createContactRequest = do currentTs <- getCurrentTime @@ -1218,8 +1226,8 @@ createOrUpdateContactRequest db user@User {userId} userContactLinkId invId Profi LIMIT 1 |] (userId, xContactId) - getContactRequest' :: XContactId -> IO (Maybe UserContactRequest) - getContactRequest' xContactId = + getContactRequestByXContactId :: XContactId -> IO (Maybe UserContactRequest) + getContactRequestByXContactId xContactId = maybeFirstRow toContactRequest $ DB.query db @@ -1264,8 +1272,13 @@ createOrUpdateContactRequest db user@User {userId} userContactLinkId invId Profi |] (displayName, fullName, image, currentTs, userId, cReqId) -getContactRequest :: DB.Connection -> UserId -> Int64 -> ExceptT StoreError IO UserContactRequest -getContactRequest db userId contactRequestId = +getContactRequest' :: DB.Connection -> Int64 -> ExceptT StoreError IO (User, UserContactRequest) +getContactRequest' db contactRequestId = do + user <- getUserByContactRequestId db contactRequestId + (user,) <$> getContactRequest db user contactRequestId + +getContactRequest :: DB.Connection -> User -> Int64 -> ExceptT StoreError IO UserContactRequest +getContactRequest db User {userId} contactRequestId = ExceptT . firstRow toContactRequest (SEContactRequestNotFound contactRequestId) $ DB.query db @@ -1293,8 +1306,8 @@ getContactRequestIdByName db userId cName = ExceptT . firstRow fromOnly (SEContactRequestNotFoundByName cName) $ DB.query db "SELECT contact_request_id FROM contact_requests WHERE user_id = ? AND local_display_name = ?" (userId, cName) -deleteContactRequest :: DB.Connection -> UserId -> Int64 -> IO () -deleteContactRequest db userId contactRequestId = do +deleteContactRequest :: DB.Connection -> User -> Int64 -> IO () +deleteContactRequest db User {userId} contactRequestId = do DB.execute db [sql| @@ -4841,6 +4854,7 @@ data StoreError | SEUserNotFoundByContactId {contactId :: ContactId} | SEUserNotFoundByGroupId {groupId :: GroupId} | SEUserNotFoundByFileId {fileId :: FileTransferId} + | SEUserNotFoundByContactRequestId {contactRequestId :: Int64} | SEContactNotFound {contactId :: ContactId} | SEContactNotFoundByName {contactName :: ContactName} | SEContactNotReady {contactName :: ContactName}