From d6baf125cd1b179091464ca27e3c9cc0aa3810b4 Mon Sep 17 00:00:00 2001 From: shum Date: Tue, 5 May 2026 07:19:21 +0000 Subject: [PATCH] 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 --- src/Simplex/Chat/Controller.hs | 2 +- src/Simplex/Chat/Core.hs | 41 ++++++++++++++-------------- src/Simplex/Chat/Library/Commands.hs | 6 ++-- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index 2873a131a4..5910a49bc1 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -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 diff --git a/src/Simplex/Chat/Core.hs b/src/Simplex/Chat/Core.hs index 6c77beff45..ecfc222de7 100644 --- a/src/Simplex/Chat/Core.hs +++ b/src/Simplex/Chat/Core.hs @@ -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 diff --git a/src/Simplex/Chat/Library/Commands.hs b/src/Simplex/Chat/Library/Commands.hs index 9407bcc29f..ce50f4c452 100644 --- a/src/Simplex/Chat/Library/Commands.hs +++ b/src/Simplex/Chat/Library/Commands.hs @@ -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'