mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2024-12-17 17:20:21 +01:00
fa313caa82
* terminal: refactor chat core used in terminal app and in bot examples * fix tests * refactor
39 lines
1.3 KiB
Haskell
39 lines
1.3 KiB
Haskell
{-# 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
|
|
import Simplex.Chat.Options (ChatOpts (..))
|
|
import Simplex.Chat.Store
|
|
import Simplex.Chat.Types
|
|
import UnliftIO.Async
|
|
|
|
simplexChatCore :: ChatConfig -> ChatOpts -> Maybe (Notification -> IO ()) -> (User -> ChatController -> IO ()) -> IO ()
|
|
simplexChatCore cfg@ChatConfig {dbPoolSize, yesToMigrations} opts sendToast chat
|
|
| logAgent opts = do
|
|
setLogLevel LogInfo -- LogError
|
|
withGlobalLogging logCfg initRun
|
|
| otherwise = initRun
|
|
where
|
|
initRun = do
|
|
let f = chatStoreFile $ dbFilePrefix opts
|
|
st <- createStore f dbPoolSize yesToMigrations
|
|
u <- getCreateActiveUser st
|
|
cc <- newChatController st (Just u) cfg opts sendToast
|
|
runSimplexChat u cc chat
|
|
|
|
runSimplexChat :: User -> ChatController -> (User -> ChatController -> IO ()) -> IO ()
|
|
runSimplexChat u cc chat = do
|
|
a1 <- async $ chat u cc
|
|
a2 <- runReaderT (startChatController u) cc
|
|
waitEither_ a1 a2
|
|
|
|
sendChatCmd :: ChatController -> String -> IO ChatResponse
|
|
sendChatCmd cc s = runReaderT (execChatCommand . encodeUtf8 $ T.pack s) cc
|