2022-02-06 16:18:01 +00:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
|
2021-07-05 20:05:07 +01:00
|
|
|
module Simplex.Chat.Terminal where
|
2021-06-25 18:18:24 +01:00
|
|
|
|
2022-01-21 11:09:33 +00:00
|
|
|
import Control.Logger.Simple
|
2022-02-06 16:18:01 +00:00
|
|
|
import Control.Monad.Except
|
2022-01-21 11:09:33 +00:00
|
|
|
import Control.Monad.Reader
|
|
|
|
import Simplex.Chat
|
|
|
|
import Simplex.Chat.Controller
|
|
|
|
import Simplex.Chat.Help (chatWelcome)
|
|
|
|
import Simplex.Chat.Options
|
|
|
|
import Simplex.Chat.Store
|
|
|
|
import Simplex.Chat.Terminal.Input
|
|
|
|
import Simplex.Chat.Terminal.Notification
|
|
|
|
import Simplex.Chat.Terminal.Output
|
|
|
|
import Simplex.Chat.Types (User)
|
|
|
|
import Simplex.Messaging.Util (raceAny_)
|
2022-02-06 16:18:01 +00:00
|
|
|
import UnliftIO (async, waitEither_)
|
2022-01-21 11:09:33 +00:00
|
|
|
|
|
|
|
simplexChat :: WithTerminal t => ChatConfig -> ChatOpts -> t -> IO ()
|
|
|
|
simplexChat cfg opts t
|
|
|
|
| logging opts = do
|
|
|
|
setLogLevel LogInfo -- LogError
|
|
|
|
withGlobalLogging logCfg initRun
|
|
|
|
| otherwise = initRun
|
2021-06-25 18:18:24 +01:00
|
|
|
where
|
2022-01-21 11:09:33 +00:00
|
|
|
initRun = do
|
2022-01-24 16:07:17 +00:00
|
|
|
sendNotification' <- initializeNotifications
|
2022-01-21 11:09:33 +00:00
|
|
|
let f = chatStoreFile $ dbFilePrefix opts
|
|
|
|
st <- createStore f $ dbPoolSize cfg
|
2022-01-27 22:01:15 +00:00
|
|
|
u <- getCreateActiveUser st
|
2022-01-21 11:09:33 +00:00
|
|
|
ct <- newChatTerminal t
|
2022-02-06 16:18:01 +00:00
|
|
|
cc <- newChatController st (Just u) cfg opts sendNotification'
|
2022-01-27 22:01:15 +00:00
|
|
|
runSimplexChat u ct cc
|
2022-01-21 11:09:33 +00:00
|
|
|
|
|
|
|
runSimplexChat :: User -> ChatTerminal -> ChatController -> IO ()
|
2022-02-06 16:18:01 +00:00
|
|
|
runSimplexChat u ct cc = do
|
|
|
|
when (firstTime cc) . printToTerminal ct $ chatWelcome u
|
|
|
|
a1 <- async $ runChatTerminal ct cc
|
|
|
|
a2 <- runReaderT (startChatController u) cc
|
|
|
|
waitEither_ a1 a2
|
|
|
|
|
|
|
|
runChatTerminal :: ChatTerminal -> ChatController -> IO ()
|
|
|
|
runChatTerminal ct cc = raceAny_ [runTerminalInput ct cc, runTerminalOutput ct cc, runInputLoop ct cc]
|