From 4ccdd3a2dab12076adee2af08846a3aa97b7e89e Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Fri, 5 Jan 2024 17:08:25 +0400 Subject: [PATCH] remove redundant filter query, NoteFolderName --- src/Simplex/Chat.hs | 25 +++++++++++++++---------- src/Simplex/Chat/Controller.hs | 2 +- src/Simplex/Chat/Store/NoteFolders.hs | 10 +++++----- src/Simplex/Chat/Store/Shared.hs | 2 +- src/Simplex/Chat/Types.hs | 2 -- src/Simplex/Chat/View.hs | 5 +---- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 3547664148..c8b7d77db3 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -1559,9 +1559,11 @@ processChatCommand' vr = \case gId <- withStore $ \db -> getGroupIdByName db user name let chatRef = ChatRef CTGroup gId processChatCommand . APISendMessage chatRef False Nothing $ ComposedMessage Nothing Nothing mc - CTLocal -> do - folderId <- withStore $ \db -> getNoteFolderIdByName db user name - processChatCommand . APICreateChatItem folderId $ ComposedMessage Nothing Nothing mc + CTLocal + | name == "" -> do + folderId <- withStore (`getUserNoteFolderId` user) + processChatCommand . APICreateChatItem folderId $ ComposedMessage Nothing Nothing mc + | otherwise -> throwChatError $ CECommandError "not supported" _ -> throwChatError $ CECommandError "not supported" SendMemberContactMessage gName mName msg -> withUser $ \user -> do (gId, mId) <- getGroupAndMemberId user gName mName @@ -1855,8 +1857,8 @@ processChatCommand' vr = \case quotedItemId <- withStore $ \db -> getGroupChatItemIdByText db user groupId cName quotedMsg let mc = MCText msg processChatCommand . APISendMessage (ChatRef CTGroup groupId) False Nothing $ ComposedMessage Nothing (Just quotedItemId) mc - ClearNoteFolder displayName -> withUser $ \user -> do - folderId <- withStore $ \db -> getNoteFolderIdByName db user displayName + ClearNoteFolder -> withUser $ \user -> do + folderId <- withStore (`getUserNoteFolderId` user) processChatCommand $ APIClearChat (ChatRef CTLocal folderId) LastChats count_ -> withUser' $ \user -> do let count = fromMaybe 5000 count_ @@ -2082,7 +2084,9 @@ processChatCommand' vr = \case ChatRef cType <$> case cType of CTDirect -> withStore $ \db -> getContactIdByName db user name CTGroup -> withStore $ \db -> getGroupIdByName db user name - CTLocal -> withStore $ \db -> getNoteFolderIdByName db user name + CTLocal + | name == "" -> withStore (`getUserNoteFolderId` user) + | otherwise -> throwChatError $ CECommandError "not supported" _ -> throwChatError $ CECommandError "not supported" checkChatStopped :: m ChatResponse -> m ChatResponse checkChatStopped a = asks agentAsync >>= readTVarIO >>= maybe a (const $ throwChatError CEChatNotStopped) @@ -6403,7 +6407,7 @@ chatCommandP = ("/leave " <|> "/l ") *> char_ '#' *> (LeaveGroup <$> displayName), ("/delete #" <|> "/d #") *> (DeleteGroup <$> displayName), ("/delete " <|> "/d ") *> char_ '@' *> (DeleteContact <$> displayName), - "/clear *" $> ClearNoteFolder "", + "/clear *" $> ClearNoteFolder, "/clear #" *> (ClearGroup <$> displayName), "/clear " *> char_ '@' *> (ClearContact <$> displayName), ("/members " <|> "/ms ") *> char_ '#' *> (ListMembers <$> displayName), @@ -6601,9 +6605,10 @@ chatCommandP = " member" $> GRMember, " observer" $> GRObserver ] - chatNameP = chatTypeP >>= \case - CTLocal -> pure $ ChatName CTLocal "" - ct -> ChatName ct <$> displayName + chatNameP = + chatTypeP >>= \case + CTLocal -> pure $ ChatName CTLocal "" + ct -> ChatName ct <$> displayName chatNameP' = ChatName <$> (chatTypeP <|> pure CTDirect) <*> displayName chatRefP = ChatRef <$> chatTypeP <*> A.decimal msgCountP = A.space *> A.decimal <|> pure 10 diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index 8f8a86faeb..011c180ac9 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -408,7 +408,7 @@ data ChatCommand | DeleteGroupLink GroupName | ShowGroupLink GroupName | SendGroupMessageQuote {groupName :: GroupName, contactName_ :: Maybe ContactName, quotedMsg :: Text, message :: Text} - | ClearNoteFolder NoteFolderName + | ClearNoteFolder | LastChats (Maybe Int) -- UserId (not used in UI) | LastMessages (Maybe ChatName) Int (Maybe String) -- UserId (not used in UI) | LastChatItemId (Maybe ChatName) Int -- UserId (not used in UI) diff --git a/src/Simplex/Chat/Store/NoteFolders.hs b/src/Simplex/Chat/Store/NoteFolders.hs index b480432def..c395e977fa 100644 --- a/src/Simplex/Chat/Store/NoteFolders.hs +++ b/src/Simplex/Chat/Store/NoteFolders.hs @@ -13,7 +13,7 @@ import Data.Time (getCurrentTime) import Database.SQLite.Simple (Only (..)) import Database.SQLite.Simple.QQ (sql) import Simplex.Chat.Store.Shared (StoreError (..)) -import Simplex.Chat.Types (NoteFolder (..), NoteFolderId, NoteFolderName, User (..)) +import Simplex.Chat.Types (NoteFolder (..), NoteFolderId, User (..)) import Simplex.Messaging.Agent.Protocol (UserId) import Simplex.Messaging.Agent.Store.SQLite (firstRow) import qualified Simplex.Messaging.Agent.Store.SQLite.DB as DB @@ -24,10 +24,10 @@ createNoteFolder db User {userId} = do [] -> liftIO $ DB.execute db "INSERT INTO note_folders (user_id) VALUES (?)" (Only userId) Only noteFolderId : _ -> throwError $ SENoteFolderAlreadyCreated {noteFolderId} -getNoteFolderIdByName :: DB.Connection -> User -> NoteFolderName -> ExceptT StoreError IO NoteFolderId -getNoteFolderIdByName db User {userId} ldn = - ExceptT . firstRow fromOnly (SENoteFolderNotFoundByName ldn) $ - DB.query db "SELECT note_folder_id FROM note_folders WHERE user_id = ? AND '' = ?" (userId, ldn) +getUserNoteFolderId :: DB.Connection -> User -> ExceptT StoreError IO NoteFolderId +getUserNoteFolderId db User {userId} = + ExceptT . firstRow fromOnly SEUserNoteFolderNotFound $ + DB.query db "SELECT note_folder_id FROM note_folders WHERE user_id = ?" (Only userId) getNoteFolder :: DB.Connection -> User -> NoteFolderId -> ExceptT StoreError IO NoteFolder getNoteFolder db User {userId} noteFolderId = diff --git a/src/Simplex/Chat/Store/Shared.hs b/src/Simplex/Chat/Store/Shared.hs index a2d18105a5..e5bfd6e0f8 100644 --- a/src/Simplex/Chat/Store/Shared.hs +++ b/src/Simplex/Chat/Store/Shared.hs @@ -71,7 +71,7 @@ data StoreError | SEGroupInvitationNotFound | SENoteFolderAlreadyCreated {noteFolderId :: NoteFolderId} | SENoteFolderNotFound {noteFolderId :: NoteFolderId} - | SENoteFolderNotFoundByName {noteFolderName :: NoteFolderName} + | SEUserNoteFolderNotFound | SESndFileNotFound {fileId :: FileTransferId} | SESndFileInvalid {fileId :: FileTransferId} | SERcvFileNotFound {fileId :: FileTransferId} diff --git a/src/Simplex/Chat/Types.hs b/src/Simplex/Chat/Types.hs index 16ab063235..003b3a489b 100644 --- a/src/Simplex/Chat/Types.hs +++ b/src/Simplex/Chat/Types.hs @@ -326,8 +326,6 @@ type ContactName = Text type GroupName = Text -type NoteFolderName = Text - optionalFullName :: ContactName -> Text -> Text optionalFullName displayName fullName | T.null fullName || displayName == fullName = "" diff --git a/src/Simplex/Chat/View.hs b/src/Simplex/Chat/View.hs index fcb9f6f458..e3cc411cac 100644 --- a/src/Simplex/Chat/View.hs +++ b/src/Simplex/Chat/View.hs @@ -1907,7 +1907,7 @@ viewChatError logLevel testView = \case SEDuplicateGroupMessage {groupId, sharedMsgId} | testView -> ["duplicate group message, group id: " <> sShow groupId <> ", message id: " <> sShow sharedMsgId] | otherwise -> [] - SENoteFolderNotFoundByName f -> ["no notes folder " <> ttyLocal f] + SEUserNoteFolderNotFound -> ["no notes folder"] e -> ["chat db error: " <> sShow e] ChatErrorDatabase err -> case err of DBErrorEncrypted -> ["error: chat database is already encrypted"] @@ -2022,9 +2022,6 @@ ttyGroup g = styled (colored Blue) $ "#" <> viewName g ttyGroup' :: GroupInfo -> StyledString ttyGroup' = ttyGroup . groupName' -ttyLocal :: NoteFolderName -> StyledString -ttyLocal l = styled (colored Green) $ "*" <> viewName l - viewContactName :: Contact -> Text viewContactName = viewName . localDisplayName'