feat(cli): add display-name option to cli

This commit is contained in:
mervyn
2024-10-22 22:10:01 +08:00
parent d1ae3ba2d3
commit 587e0a18a9
3 changed files with 29 additions and 12 deletions
+18 -12
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 (..), ContactName, Profile (..), NewUser (..), LocalProfile (..))
import Simplex.Chat.View (serializeChatResponse)
import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore, withTransaction)
import System.Exit (exitFailure)
@@ -32,7 +32,7 @@ import Text.Read (readMaybe)
import UnliftIO.Async
simplexChatCore :: ChatConfig -> ChatOpts -> (User -> ChatController -> IO ()) -> IO ()
simplexChatCore cfg@ChatConfig {confirmMigrations, testView} opts@ChatOpts {coreOptions = CoreChatOpts {dbFilePrefix, dbKey, logAgent}} chat =
simplexChatCore cfg@ChatConfig {confirmMigrations, testView} opts@ChatOpts {coreOptions = CoreChatOpts {dbFilePrefix, dbKey, logAgent, displayName}} chat =
case logAgent of
Just level -> do
setLogLevel level
@@ -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 displayName
cc <- newChatController db u_ cfg opts False
u <- maybe (createActiveUser cc) pure u_
u <- maybe (createActiveUser cc displayName) 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 -> Maybe ContactName -> IO (Maybe User)
getSelectActiveUser st displayName = do
users <- withTransaction st getUsers
case find activeUser users of
Just u -> pure $ Just u
Nothing -> selectUser users
case displayName of
Just name -> case find (\User {localDisplayName} -> localDisplayName == name) users of
Just u -> pure $ Just u
Nothing -> pure Nothing
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 -> Maybe ContactName -> IO User
createActiveUser cc 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
@@ -189,6 +189,7 @@ mobileChatOpts dbFilePrefix =
CoreChatOpts
{ dbFilePrefix,
dbKey = "", -- for API database is already opened, and the key in options is not used
displayName = Nothing,
smpServers = [],
xftpServers = [],
simpleNetCfg = defaultSimpleNetCfg,
+10
View File
@@ -35,6 +35,7 @@ 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,
@@ -56,6 +57,7 @@ data ChatOpts = ChatOpts
data CoreChatOpts = CoreChatOpts
{ dbFilePrefix :: String,
dbKey :: ScrubbedBytes,
displayName :: Maybe ContactName,
smpServers :: [SMPServerWithAuth],
xftpServers :: [XFTPServerWithAuth],
simpleNetCfg :: SimpleNetCfg,
@@ -99,6 +101,13 @@ coreChatOptsP appDir defaultDbFileName = do
<> help "Database encryption key/pass-phrase"
<> value ""
)
displayName <-
optional $
strOption
( long "display-name"
<> metavar "DISPLAY_NAME"
<> help "Display name will be sent to your contacts when you connect and only stored on your device and you can change it later."
)
smpServers <-
option
parseProtocolServers
@@ -244,6 +253,7 @@ coreChatOptsP appDir defaultDbFileName = do
CoreChatOpts
{ dbFilePrefix,
dbKey,
displayName,
smpServers,
xftpServers,
simpleNetCfg =