2022-04-10 17:13:06 +01:00
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
|
|
|
|
module Simplex.Chat.Core where
|
|
|
|
|
|
|
|
import Control.Logger.Simple
|
|
|
|
import Control.Monad.Reader
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Text.Encoding (encodeUtf8)
|
|
|
|
import Simplex.Chat
|
|
|
|
import Simplex.Chat.Controller
|
2023-02-18 17:39:16 +00:00
|
|
|
import Simplex.Chat.Options (ChatOpts (..), CoreChatOpts (..))
|
2022-04-10 17:13:06 +01:00
|
|
|
import Simplex.Chat.Types
|
2023-03-27 18:34:48 +01:00
|
|
|
import System.Exit (exitFailure)
|
2022-04-10 17:13:06 +01:00
|
|
|
import UnliftIO.Async
|
|
|
|
|
2023-10-11 09:50:11 +01:00
|
|
|
simplexChatCore :: ChatConfig -> ChatOpts -> (User -> ChatController -> IO ()) -> IO ()
|
|
|
|
simplexChatCore cfg@ChatConfig {confirmMigrations, testView} opts@ChatOpts {coreOptions = CoreChatOpts {dbFilePrefix, dbKey, logAgent}} chat =
|
2023-02-28 23:26:08 +00:00
|
|
|
case logAgent of
|
|
|
|
Just level -> do
|
|
|
|
setLogLevel level
|
|
|
|
withGlobalLogging logCfg initRun
|
|
|
|
_ -> initRun
|
2022-04-10 17:13:06 +01:00
|
|
|
where
|
2023-12-09 21:59:40 +00:00
|
|
|
initRun = createChatDatabase dbFilePrefix dbKey False confirmMigrations >>= either exit run
|
2023-03-27 18:34:48 +01:00
|
|
|
exit e = do
|
|
|
|
putStrLn $ "Error opening database: " <> show e
|
|
|
|
exitFailure
|
|
|
|
run db@ChatDatabase {chatStore} = do
|
2023-08-07 08:25:15 +01:00
|
|
|
u <- getCreateActiveUser chatStore testView
|
2023-12-23 13:06:59 +00:00
|
|
|
cc <- newChatController db (Just u) cfg opts False
|
2022-06-06 16:23:47 +01:00
|
|
|
runSimplexChat opts u cc chat
|
2022-04-10 17:13:06 +01:00
|
|
|
|
2022-06-06 16:23:47 +01:00
|
|
|
runSimplexChat :: ChatOpts -> User -> ChatController -> (User -> ChatController -> IO ()) -> IO ()
|
|
|
|
runSimplexChat ChatOpts {maintenance} u cc chat
|
|
|
|
| maintenance = wait =<< async (chat u cc)
|
|
|
|
| otherwise = do
|
2024-01-08 12:53:16 +00:00
|
|
|
a1 <- runReaderT (startChatController True) cc
|
2023-11-26 18:16:37 +00:00
|
|
|
a2 <- async $ chat u cc
|
|
|
|
waitEither_ a1 a2
|
2022-04-10 17:13:06 +01:00
|
|
|
|
2023-08-01 20:54:51 +01:00
|
|
|
sendChatCmdStr :: ChatController -> String -> IO ChatResponse
|
2023-09-27 11:41:02 +03:00
|
|
|
sendChatCmdStr cc s = runReaderT (execChatCommand Nothing . encodeUtf8 $ T.pack s) cc
|
2023-08-01 20:54:51 +01:00
|
|
|
|
|
|
|
sendChatCmd :: ChatController -> ChatCommand -> IO ChatResponse
|
|
|
|
sendChatCmd cc cmd = runReaderT (execChatCommand' cmd) cc
|