diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 8b1555aa9..3063eb611 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -461,6 +461,7 @@ processChatCommand' vr = \case ts <- liftIO $ getCurrentTime >>= if pastTimestamp then coupleDaysAgo else pure user <- withStore $ \db -> createUserRecordAt db (AgentUserId auId) p True ts when (auId == 1) $ withStore (\db -> createContact db user simplexContactProfile) `catchChatError` \_ -> pure () + withStore $ \db -> createNoteFolder db user storeServers user smpServers storeServers user xftpServers atomically . writeTVar u $ Just user @@ -6149,7 +6150,9 @@ getCreateActiveUser st testView = do putStrLn "chosen display name is already used by another profile on this device, choose another one" loop Left e -> putStrLn ("database error " <> show e) >> exitFailure - Right user -> pure user + Right user -> do + void . withTransaction st $ \db -> runExceptT $ createNoteFolder db user + pure user selectUser :: [User] -> IO User selectUser [user@User {userId}] = do withTransaction st (`setActiveUser` userId) diff --git a/src/Simplex/Chat/Migrations/M20240102_note_folders.hs b/src/Simplex/Chat/Migrations/M20240102_note_folders.hs index 6e0fbe5e4..6a5d6a021 100644 --- a/src/Simplex/Chat/Migrations/M20240102_note_folders.hs +++ b/src/Simplex/Chat/Migrations/M20240102_note_folders.hs @@ -23,7 +23,7 @@ m20240102_note_folders = CREATE INDEX chat_items_note_folder_id ON chat_items(note_folder_id); CREATE INDEX files_note_folder_id ON files(note_folder_id); - CREATE INDEX note_folders_user_id ON note_folders(user_id); + CREATE UNIQUE INDEX note_folders_user_id ON note_folders(user_id); -- drop UNIQUE for more folders per user INSERT INTO note_folders (user_id) SELECT user_id FROM users; |] diff --git a/src/Simplex/Chat/Store/NoteFolders.hs b/src/Simplex/Chat/Store/NoteFolders.hs index 2a3f7d09d..0102d8afd 100644 --- a/src/Simplex/Chat/Store/NoteFolders.hs +++ b/src/Simplex/Chat/Store/NoteFolders.hs @@ -5,15 +5,21 @@ module Simplex.Chat.Store.NoteFolders where import Control.Monad.Except (ExceptT (..)) +import Control.Monad.IO.Class (liftIO) import Data.Time (getCurrentTime) import Database.SQLite.Simple (Only (..)) import Database.SQLite.Simple.QQ (sql) -import Simplex.Chat.Store.Shared (StoreError (..)) +import Simplex.Chat.Store.Shared (StoreError (..), checkConstraint) import Simplex.Chat.Types (NoteFolder (..), NoteFolderId, NoteFolderName, User (..)) import Simplex.Messaging.Agent.Protocol (UserId) import Simplex.Messaging.Agent.Store.SQLite (firstRow) import qualified Simplex.Messaging.Agent.Store.SQLite.DB as DB +createNoteFolder :: DB.Connection -> User -> ExceptT StoreError IO () +createNoteFolder db User {userId} = + checkConstraint SENoteFolderAlreadyCreated . liftIO $ + DB.execute db [sql| INSERT INTO note_folders (user_id) VALUES (?) |] (Only userId) + getNoteFolderIdByName :: DB.Connection -> User -> NoteFolderName -> ExceptT StoreError IO NoteFolderId getNoteFolderIdByName db User {userId} ldn = ExceptT . firstRow fromOnly (SENoteFolderNotFoundByName ldn) $ diff --git a/src/Simplex/Chat/Store/Profiles.hs b/src/Simplex/Chat/Store/Profiles.hs index de8295327..6d25c120a 100644 --- a/src/Simplex/Chat/Store/Profiles.hs +++ b/src/Simplex/Chat/Store/Profiles.hs @@ -121,10 +121,6 @@ createUserRecordAt db (AgentUserId auId) Profile {displayName, fullName, image, (profileId, displayName, userId, True, currentTs, currentTs, currentTs) contactId <- insertedRowId db DB.execute db "UPDATE users SET contact_id = ? WHERE user_id = ?" (contactId, userId) - DB.execute - db - "INSERT INTO note_folders (user_id, created_at, updated_at, chat_ts, favorite, unread_chat) VALUES (?, ?, ?, ?, ?, ?)" - (userId, currentTs, currentTs, currentTs, False, False) pure $ toUser $ (userId, auId, contactId, profileId, activeUser, displayName, fullName, image, Nothing, userPreferences) :. (showNtfs, sendRcptsContacts, sendRcptsSmallGroups, Nothing, Nothing) diff --git a/src/Simplex/Chat/Store/Shared.hs b/src/Simplex/Chat/Store/Shared.hs index 9240f7d0c..58fc9ce4b 100644 --- a/src/Simplex/Chat/Store/Shared.hs +++ b/src/Simplex/Chat/Store/Shared.hs @@ -69,6 +69,7 @@ data StoreError | SEDuplicateGroupMember | SEGroupAlreadyJoined | SEGroupInvitationNotFound + | SENoteFolderAlreadyCreated | SENoteFolderNotFound {noteFolderId :: NoteFolderId} | SENoteFolderNotFoundByName {noteFolderName :: NoteFolderName} | SESndFileNotFound {fileId :: FileTransferId} diff --git a/tests/ChatClient.hs b/tests/ChatClient.hs index 942f43bbe..2bd8e48f0 100644 --- a/tests/ChatClient.hs +++ b/tests/ChatClient.hs @@ -26,6 +26,7 @@ import Simplex.Chat.Controller (ChatConfig (..), ChatController (..), ChatDataba import Simplex.Chat.Core import Simplex.Chat.Options import Simplex.Chat.Store +import Simplex.Chat.Store.NoteFolders (createNoteFolder) import Simplex.Chat.Store.Profiles import Simplex.Chat.Terminal import Simplex.Chat.Terminal.Output (newChatTerminal) @@ -191,7 +192,10 @@ groupLinkViaContactVRange = mkVersionRange 1 2 createTestChat :: FilePath -> ChatConfig -> ChatOpts -> String -> Profile -> IO TestCC createTestChat tmp cfg opts@ChatOpts {coreOptions = CoreChatOpts {dbKey}} dbPrefix profile = do Right db@ChatDatabase {chatStore} <- createChatDatabase (tmp dbPrefix) dbKey False MCError - Right user <- withTransaction chatStore $ \db' -> runExceptT $ createUserRecord db' (AgentUserId 1) profile True + Right user <- withTransaction chatStore $ \db' -> runExceptT $ do + user <- createUserRecord db' (AgentUserId 1) profile True + createNoteFolder db' user + pure user startTestChat_ db cfg opts user startTestChat :: FilePath -> ChatConfig -> ChatOpts -> String -> IO TestCC