core: support accepting contact requests for non active users (for accepting via notification) (#1809)

* core: support accepting contact requests for non active users (for accepting via notification)

* getContactRequest'
This commit is contained in:
JRoberts
2023-01-20 17:55:57 +04:00
committed by GitHub
parent 980c7a9ddd
commit 7dd4dc3b40
2 changed files with 27 additions and 13 deletions
+22 -8
View File
@@ -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}