Compare commits

...

6 Commits

Author SHA1 Message Date
Evgeny Poberezkin e9a443e049 rename option 2024-11-30 20:40:30 +00:00
Evgeny Poberezkin 486c19c1d6 pass ChatOpts 2024-11-30 20:33:29 +00:00
Evgeny Poberezkin 271f0dc6c1 simplify, make passed user active 2024-11-30 20:29:38 +00:00
Evgeny Poberezkin ecbcf05218 Merge branch 'feature/cli_support_non_interactive_mode' of github.com:reply2future/simplex-chat into reply2future-feature/cli_support_non_interactive_mode 2024-11-30 20:19:12 +00:00
mervyn 2dc9c85cf2 refactor(cli): move display-name option to ChatOpts in CLI 2024-11-19 07:48:01 +08:00
mervyn 587e0a18a9 feat(cli): add display-name option to cli 2024-11-19 07:48:01 +08:00
6 changed files with 31 additions and 11 deletions
@@ -76,6 +76,7 @@ mkChatOpts BroadcastBotOpts {coreOptions} =
ChatOpts
{ coreOptions,
deviceName = Nothing,
userDisplayName = Nothing,
chatCmd = "",
chatCmdDelay = 3,
chatCmdLog = CCLNone,
@@ -85,6 +85,7 @@ mkChatOpts DirectoryOpts {coreOptions} =
ChatOpts
{ coreOptions,
deviceName = Nothing,
userDisplayName = Nothing,
chatCmd = "",
chatCmdDelay = 3,
chatCmdLog = CCLNone,
+17 -11
View File
@@ -23,7 +23,7 @@ import Simplex.Chat
import Simplex.Chat.Controller
import Simplex.Chat.Options (ChatOpts (..), CoreChatOpts (..))
import Simplex.Chat.Store.Profiles
import Simplex.Chat.Types
import Simplex.Chat.Types (User (..), Profile (..), NewUser (..), LocalProfile (..))
import Simplex.Chat.View (serializeChatResponse)
import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore, withTransaction)
import System.Exit (exitFailure)
@@ -44,9 +44,9 @@ simplexChatCore cfg@ChatConfig {confirmMigrations, testView} opts@ChatOpts {core
putStrLn $ "Error opening database: " <> show e
exitFailure
run db@ChatDatabase {chatStore} = do
u_ <- getSelectActiveUser chatStore
u_ <- getSelectActiveUser chatStore opts
cc <- newChatController db u_ cfg opts False
u <- maybe (createActiveUser cc) pure u_
u <- maybe (createActiveUser cc opts) pure u_
unless testView $ putStrLn $ "Current user: " <> userStr u
runSimplexChat opts u cc chat
@@ -64,12 +64,16 @@ sendChatCmdStr cc s = runReaderT (execChatCommand Nothing . encodeUtf8 $ T.pack
sendChatCmd :: ChatController -> ChatCommand -> IO ChatResponse
sendChatCmd cc cmd = runReaderT (execChatCommand' cmd) cc
getSelectActiveUser :: SQLiteStore -> IO (Maybe User)
getSelectActiveUser st = do
getSelectActiveUser :: SQLiteStore -> ChatOpts -> IO (Maybe User)
getSelectActiveUser st ChatOpts {userDisplayName} = do
users <- withTransaction st getUsers
case find activeUser users of
Just u -> pure $ Just u
Nothing -> selectUser users
case userDisplayName of
Just name ->
forM (find (\User {localDisplayName} -> localDisplayName == name) users) $ \u ->
if activeUser u then pure u else withTransaction st (`setActiveUser` u)
Nothing -> case find activeUser users of
Just u -> pure $ Just u
Nothing -> selectUser users
where
selectUser :: [User] -> IO (Maybe User)
selectUser = \case
@@ -90,8 +94,8 @@ getSelectActiveUser st = do
let user = users !! (n - 1)
in Just <$> withTransaction st (`setActiveUser` user)
createActiveUser :: ChatController -> IO User
createActiveUser cc = do
createActiveUser :: ChatController -> ChatOpts -> IO User
createActiveUser cc ChatOpts {userDisplayName = name} = do
putStrLn
"No user profiles found, it will be created now.\n\
\Please choose your display name.\n\
@@ -100,7 +104,9 @@ createActiveUser cc = do
loop
where
loop = do
displayName <- T.pack <$> getWithPrompt "display name"
displayName <- case name of
Just n | not (T.null n) -> pure n
_ -> T.pack <$> getWithPrompt "display name"
let profile = Just Profile {displayName, fullName = "", image = Nothing, contactLink = Nothing, preferences = Nothing}
execChatCommand' (CreateActiveUser NewUser {profile, pastTimestamp = False}) `runReaderT` cc >>= \case
CRActiveUser user -> pure user
+1
View File
@@ -202,6 +202,7 @@ mobileChatOpts dbFilePrefix =
yesToUpMigrations = False
},
deviceName = Nothing,
userDisplayName = Nothing,
chatCmd = "",
chatCmdDelay = 3,
chatCmdLog = CCLNone,
+10
View File
@@ -35,10 +35,12 @@ import Simplex.Messaging.Parsers (parseAll)
import Simplex.Messaging.Protocol (ProtoServerWithAuth, ProtocolTypeI, SMPServerWithAuth, XFTPServerWithAuth)
import Simplex.Messaging.Transport.Client (SocksProxyWithAuth (..), SocksAuth (..), defaultSocksProxyWithAuth)
import System.FilePath (combine)
import Simplex.Chat.Types (ContactName)
data ChatOpts = ChatOpts
{ coreOptions :: CoreChatOpts,
deviceName :: Maybe Text,
userDisplayName :: Maybe ContactName,
chatCmd :: String,
chatCmdDelay :: Int,
chatCmdLog :: ChatCmdLog,
@@ -286,6 +288,13 @@ chatOptsP appDir defaultDbFileName = do
<> metavar "DEVICE"
<> help "Device name to use in connections with remote hosts and controller"
)
userDisplayName <-
optional $
strOption
( long "user-display-name"
<> metavar "DISPLAY_NAME"
<> help "Create new or switch to existing user profile with this display name."
)
chatCmd <-
strOption
( long "execute"
@@ -375,6 +384,7 @@ chatOptsP appDir defaultDbFileName = do
pure
ChatOpts
{ coreOptions,
userDisplayName,
deviceName,
chatCmd,
chatCmdDelay,
+1
View File
@@ -74,6 +74,7 @@ testOpts =
ChatOpts
{ coreOptions = testCoreOpts,
deviceName = Nothing,
userDisplayName = Nothing,
chatCmd = "",
chatCmdDelay = 3,
chatCmdLog = CCLNone,