evict note folders from createUserRecord

This commit is contained in:
IC Rainbow
2024-01-03 22:21:05 +02:00
parent 211a440e78
commit cf408778d4
6 changed files with 18 additions and 8 deletions

View File

@@ -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)

View File

@@ -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;
|]

View File

@@ -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) $

View File

@@ -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)

View File

@@ -69,6 +69,7 @@ data StoreError
| SEDuplicateGroupMember
| SEGroupAlreadyJoined
| SEGroupInvitationNotFound
| SENoteFolderAlreadyCreated
| SENoteFolderNotFound {noteFolderId :: NoteFolderId}
| SENoteFolderNotFoundByName {noteFolderName :: NoteFolderName}
| SESndFileNotFound {fileId :: FileTransferId}

View File

@@ -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