lightweight queries

This commit is contained in:
Alexander Bondarenko
2024-05-29 16:18:33 +03:00
parent b94ced6b39
commit 30f00c2f2e
3 changed files with 57 additions and 20 deletions
+14
View File
@@ -54,6 +54,7 @@ module Simplex.Chat.Store.Direct
incConnectionAuthErrCounter,
setConnectionAuthErrCounter,
getUserContacts,
getUserContactConnIds,
createOrUpdateContactRequest,
getContactRequest',
getContactRequest,
@@ -858,6 +859,19 @@ getContactConnections db vr userId Contact {contactId} =
connections [] = pure []
connections rows = pure $ map (toConnection vr) rows
getUserContactConnIds :: DB.Connection -> User -> IO [ConnId]
getUserContactConnIds db User {userId} =
map fromOnly
<$> DB.query
db
[sql|
SELECT c.agent_conn_id
FROM connections c
JOIN contacts ct ON ct.contact_id = c.contact_id
WHERE c.user_id = ? AND ct.user_id = ? AND ct.deleted = 0
|]
(userId, userId)
getConnectionById :: DB.Connection -> VersionRangeChat -> User -> Int64 -> ExceptT StoreError IO Connection
getConnectionById db vr User {userId} connId = ExceptT $ do
firstRow (toConnection vr) (SEConnectionNotFoundById connId) $