mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-10-22 11:52:32 +00:00
refactor
This commit is contained in:
1 parent
5b659c8858
commit
2a7084960b
3 files changed
+50
-83
No files matched your search
@@ -46,11 +46,10 @@ module Simplex.Chat.Store.Direct
|
||||
deleteContactWithoutGroups,
|
||||
getDeletedContacts,
|
||||
getContactByName,
|
||||
getContactBySimplexName,
|
||||
getContactToConnect,
|
||||
getContact,
|
||||
getContactViaShortLinkToConnect,
|
||||
getContactIdByName,
|
||||
getContactIdBySimplexName,
|
||||
updateContactProfile,
|
||||
setContactDomainVerified,
|
||||
updateContactUserPreferences,
|
||||
@@ -114,7 +113,7 @@ import Simplex.Chat.Store.Shared
|
||||
import Simplex.Chat.Types
|
||||
import Simplex.Chat.Types.Preferences
|
||||
import Simplex.Chat.Types.UITheme
|
||||
import Simplex.Messaging.Agent.Protocol (AConnectionRequestUri (..), ACreatedConnLink (..), ConnId, ConnShortLink, ConnectionModeI (..), ConnectionRequestUri, CreatedConnLink (..), SimplexNameInfo, UserId)
|
||||
import Simplex.Messaging.Agent.Protocol (AConnectionLink (..), AConnectionRequestUri (..), ACreatedConnLink (..), ConnId, ConnShortLink, ConnectionLink (..), ConnectionModeI (..), ConnectionRequestUri, ConnectTarget (..), CreatedConnLink (..), SConnectionMode (..), SimplexNameInfo, UserId)
|
||||
import Simplex.Messaging.Encoding.String (StrJSON (..))
|
||||
import Simplex.Messaging.Agent.Store.AgentStore (firstRow, maybeFirstRow)
|
||||
import Simplex.Messaging.Agent.Store.DB (BoolInt (..))
|
||||
@@ -799,23 +798,23 @@ getContactByName db cxt user localDisplayName = do
|
||||
cId <- getContactIdByName db user localDisplayName
|
||||
getContact db cxt user cId
|
||||
|
||||
getContactBySimplexName :: DB.Connection -> StoreCxt -> User -> SimplexNameInfo -> ExceptT StoreError IO (Maybe Contact)
|
||||
getContactBySimplexName db cxt user ni =
|
||||
liftIO (getContactIdBySimplexName db user ni) >>= \case
|
||||
Nothing -> pure Nothing
|
||||
Just cId -> Just <$> getContact db cxt user cId
|
||||
|
||||
getContactIdBySimplexName :: DB.Connection -> User -> SimplexNameInfo -> IO (Maybe Int64)
|
||||
getContactIdBySimplexName db User {userId} ni =
|
||||
maybeFirstRow fromOnly $
|
||||
DB.query
|
||||
db
|
||||
-- a contact to connect to, found by the connect target: by its address short link, or by its verified name
|
||||
getContactToConnect :: DB.Connection -> StoreCxt -> User -> ConnectTarget -> ExceptT StoreError IO (Maybe (ConnReqContact, Contact))
|
||||
getContactToConnect db cxt user@User {userId} = \case
|
||||
CTLink (ACL SCMContact (CLShort sl)) -> getContactViaShortLinkToConnect db cxt user sl
|
||||
CTName ni ->
|
||||
liftIO (maybeFirstRow id $ DB.query db byNameQuery (userId, ni)) >>= \case
|
||||
Just (ctId :: Int64, Just (ACR cMode cReq)) | Just Refl <- testEquality cMode SCMContact ->
|
||||
Just . (cReq,) <$> getContact db cxt user ctId
|
||||
_ -> pure Nothing
|
||||
_ -> pure Nothing
|
||||
where
|
||||
byNameQuery =
|
||||
[sql|
|
||||
SELECT ct.contact_id FROM contacts ct
|
||||
SELECT ct.contact_id, ct.conn_full_link_to_connect FROM contacts ct
|
||||
JOIN contact_profiles cp ON cp.contact_profile_id = ct.contact_profile_id
|
||||
WHERE ct.user_id = ? AND cp.contact_domain = ? AND cp.contact_domain_verification = 1 AND ct.deleted = 0
|
||||
|]
|
||||
(userId, ni)
|
||||
|
||||
getUserContacts :: DB.Connection -> StoreCxt -> User -> IO [Contact]
|
||||
getUserContacts db cxt user@User {userId} = do
|
||||
|
||||
@@ -50,11 +50,10 @@ module Simplex.Chat.Store.Groups
|
||||
updateGroupPreferences,
|
||||
updateGroupProfileFromMember,
|
||||
getGroupIdByName,
|
||||
getGroupIdBySimplexName,
|
||||
getGroupMemberIdByName,
|
||||
getActiveMembersByName,
|
||||
getGroupInfoByName,
|
||||
getGroupInfoBySimplexName,
|
||||
getGroupToConnect,
|
||||
getGroupMember,
|
||||
getHostMember,
|
||||
getMentionedGroupMember,
|
||||
@@ -234,7 +233,7 @@ import Simplex.Chat.Types.MemberRelations (IntroductionDirection (..), MemberRel
|
||||
import Simplex.Chat.Types.Preferences
|
||||
import Simplex.Chat.Types.Shared
|
||||
import Simplex.Chat.Types.UITheme
|
||||
import Simplex.Messaging.Agent.Protocol (ConfirmationId, ConnId, CreatedConnLink (..), InvitationId, OwnerAuth (..), SimplexNameInfo, UserId)
|
||||
import Simplex.Messaging.Agent.Protocol (AConnectionLink (..), ConfirmationId, ConnId, ConnectionLink (..), ConnectTarget (..), CreatedConnLink (..), InvitationId, OwnerAuth (..), SConnectionMode (..), SimplexNameInfo, UserId)
|
||||
import Simplex.Messaging.Agent.Store.AgentStore (firstRow, fromOnlyBI, maybeFirstRow)
|
||||
import qualified Simplex.FileTransfer.Description as FD
|
||||
import Simplex.Messaging.Encoding (smpDecode, smpEncode)
|
||||
@@ -1075,23 +1074,22 @@ getGroupInfoByName db cxt user gName = do
|
||||
gId <- getGroupIdByName db user gName
|
||||
getGroupInfo db cxt user gId
|
||||
|
||||
getGroupInfoBySimplexName :: DB.Connection -> StoreCxt -> User -> SimplexNameInfo -> ExceptT StoreError IO (Maybe GroupInfo)
|
||||
getGroupInfoBySimplexName db cxt user ni =
|
||||
liftIO (getGroupIdBySimplexName db user ni) >>= \case
|
||||
Nothing -> pure Nothing
|
||||
Just gId -> Just <$> getGroupInfo db cxt user gId
|
||||
|
||||
getGroupIdBySimplexName :: DB.Connection -> User -> SimplexNameInfo -> IO (Maybe GroupId)
|
||||
getGroupIdBySimplexName db User {userId} ni =
|
||||
maybeFirstRow fromOnly $
|
||||
DB.query
|
||||
db
|
||||
-- a group to connect to, found by the connect target: by its address short link, or by its verified name
|
||||
getGroupToConnect :: DB.Connection -> StoreCxt -> User -> ConnectTarget -> ExceptT StoreError IO (Maybe (ConnReqContact, GroupInfo))
|
||||
getGroupToConnect db cxt user@User {userId} = \case
|
||||
CTLink (ACL SCMContact (CLShort sl)) -> getGroupViaShortLinkToConnect db cxt user sl
|
||||
CTName ni ->
|
||||
liftIO (maybeFirstRow id $ DB.query db byNameQuery (userId, ni)) >>= \case
|
||||
Just (gId :: Int64, Just cReq) -> Just . (cReq,) <$> getGroupInfo db cxt user gId
|
||||
_ -> pure Nothing
|
||||
_ -> pure Nothing
|
||||
where
|
||||
byNameQuery =
|
||||
[sql|
|
||||
SELECT g.group_id FROM groups g
|
||||
SELECT g.group_id, g.conn_full_link_to_connect FROM groups g
|
||||
JOIN group_profiles gp ON gp.group_profile_id = g.group_profile_id
|
||||
WHERE g.user_id = ? AND gp.group_domain = ? AND g.group_domain_verification = 1
|
||||
|]
|
||||
(userId, ni)
|
||||
|
||||
getGroupMember :: DB.Connection -> StoreCxt -> User -> GroupId -> GroupMemberId -> ExceptT StoreError IO GroupMember
|
||||
getGroupMember db cxt user@User {userId} groupId groupMemberId = do
|
||||
|
||||
Reference in new issue
Block a user