cli: address PR review comments

- rename APICreateMyAddress field srv_ to server_
- extract repeated `loop` and putStrLn from createActiveUser via
  prompt where-clause
- fuse u_ inspection: validate active user display name in the same
  case that creates the user when missing
This commit is contained in:
shum
2026-05-05 07:19:21 +00:00
parent 221ca9a2de
commit d6baf125cd
3 changed files with 25 additions and 24 deletions
+1 -1
View File
@@ -488,7 +488,7 @@ data ChatCommand
| ClearContact ContactName
| APIListContacts {userId :: UserId}
| ListContacts
| APICreateMyAddress {userId :: UserId, srv_ :: Maybe SMPServerWithAuth}
| APICreateMyAddress {userId :: UserId, server_ :: Maybe SMPServerWithAuth}
| CreateMyAddress
| APIDeleteMyAddress {userId :: UserId}
| DeleteMyAddress
+21 -20
View File
@@ -64,14 +64,17 @@ simplexChatCore cfg@ChatConfig {confirmMigrations, testView, chatHooks} opts@Cha
run db@ChatDatabase {chatStore} = do
users <- withTransaction chatStore getUsers
u_ <- selectActiveUser coreOptions chatStore users
forM_ ((,) <$> userDisplayName <*> u_) $ \(name, User {localDisplayName}) ->
when (localDisplayName /= name) $ do
putStrLn $ "Active user display name " <> show localDisplayName <> " does not match --user-display-name " <> show name
exitFailure
let backgroundMode = maintenance
cc <- newChatController db u_ cfg opts backgroundMode
forM_ (preStartHook chatHooks) ($ cc)
u0 <- maybe (noMaintenance >> createActiveUser cc coreOptions createBot userDisplayName) pure u_
u0 <- case u_ of
Nothing -> noMaintenance >> createActiveUser cc coreOptions createBot userDisplayName
Just u@User {localDisplayName} -> do
forM_ userDisplayName $ \name ->
when (localDisplayName /= name) $ do
putStrLn $ "Active user display name " <> show localDisplayName <> " does not match --user-display-name " <> show name
exitFailure
pure u
u <- maybe (pure u0) (applyUserImage cc chatStore u0) userImageFile
unless testView $ putStrLn $ "Current user: " <> userStr u
runSimplexChat cfg opts u cc chat
@@ -134,19 +137,17 @@ createActiveUser cc CoreChatOpts {chatRelay} createBot_ userDisplayName_ = case
createUser exitFailure $ (mkProfile botDisplayName) {peerType = Just CPTBot, preferences}
Nothing -> case userDisplayName_ of
Just displayName -> createUser exitFailure $ mkProfile displayName
Nothing
| chatRelay -> do
putStrLn
"No chat relay user profile found, it will be created now.\n\
\Please choose chat relay display name."
loop
| otherwise -> do
putStrLn
"No user profiles found, it will be created now.\n\
\Please choose your display name.\n\
\It will be sent to your contacts when you connect.\n\
\It is only stored on your device and you can change it later."
loop
Nothing -> putStrLn prompt >> loop
where
prompt
| chatRelay =
"No chat relay user profile found, it will be created now.\n\
\Please choose chat relay display name."
| otherwise =
"No user profiles found, it will be created now.\n\
\Please choose your display name.\n\
\It will be sent to your contacts when you connect.\n\
\It is only stored on your device and you can change it later."
where
loop = do
displayName <- T.pack <$> withPrompt "display name: " getLine
@@ -158,7 +159,7 @@ createActiveUser cc CoreChatOpts {chatRelay} createBot_ userDisplayName_ = case
r -> printResponseEvent (Nothing, Nothing) (config cc) r >> onError
askCreateRelayAddress :: ChatController -> User -> Maybe SMPServerWithAuth -> IO ()
askCreateRelayAddress cc@ChatController {chatStore} user@User {userId} srv_ =
askCreateRelayAddress cc@ChatController {chatStore} user@User {userId} server_ =
withTransaction chatStore (\db -> runExceptT $ getUserAddress db user) >>= \case
Right _ -> pure ()
Left SEUserContactLinkNotFound -> promptCreate
@@ -168,7 +169,7 @@ askCreateRelayAddress cc@ChatController {chatStore} user@User {userId} srv_ =
promptCreate = do
ok <- onOffPrompt "Create relay address" True
when ok $
execChatCommand' (APICreateMyAddress userId srv_) 0 `runReaderT` cc >>= \case
execChatCommand' (APICreateMyAddress userId server_) 0 `runReaderT` cc >>= \case
Right (CRUserContactLinkCreated _ address) -> do
putStrLn "Chat relay address is created:"
putStrLn $ addressStr address
+3 -3
View File
@@ -2253,7 +2253,7 @@ processChatCommand vr nm = \case
CRContactsList user <$> withFastStore' (\db -> getUserContacts db vr user)
ListContacts -> withUser $ \User {userId} ->
processChatCommand vr nm $ APIListContacts userId
APICreateMyAddress userId srv_ -> withUserId userId $ \user@User {userChatRelay} -> do
APICreateMyAddress userId server_ -> withUserId userId $ \user@User {userChatRelay} -> do
withFastStore' (\db -> runExceptT $ getUserAddress db user) >>= \case
Left SEUserContactLinkNotFound -> pure ()
Left e -> throwError $ ChatErrorStore e
@@ -2265,8 +2265,8 @@ processChatCommand vr nm = \case
| otherwise = contactShortLinkData (userProfileDirect user Nothing Nothing True) Nothing
userLinkData = UserContactLinkData UserContactData {direct = True, owners = [], relays = [], userData}
-- TODO [certs rcv]
(connId, (ccLink, _serviceId)) <- withAgent $ \a -> case srv_ of
Just srv -> createConnectionOnServer a nm (aUserId user) True True SCMContact (Just userLinkData) Nothing srv IKPQOn subMode
(connId, (ccLink, _serviceId)) <- withAgent $ \a -> case server_ of
Just server -> createConnectionOnServer a nm (aUserId user) True True SCMContact (Just userLinkData) Nothing server IKPQOn subMode
Nothing -> createConnection a nm (aUserId user) True True SCMContact (Just userLinkData) Nothing IKPQOn subMode
ccLink' <- shortenCreatedLink ccLink
let ccLink'' = if isTrue userChatRelay then setShortLinkType CCTRelay ccLink' else ccLink'