From b2df6863fc83decc5bc3ed2b99920535f59dc63e Mon Sep 17 00:00:00 2001 From: shum Date: Wed, 3 Jun 2026 19:35:19 +0000 Subject: [PATCH] chat: connectPlanName dispatches on nameType first Previously the function always probed contacts.simplex_name first and fell through to groups for NTPublicGroup misses. But the discriminator (`@`/`#`) is embedded in the stored bytes via strEncode, so an `#group.simplex` lookup can never match a contact row. Reorder to case on nameType up front, saving one DB query and one withFastStore transaction acquire on the group path. --- src/Simplex/Chat/Library/Commands.hs | 35 +++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Simplex/Chat/Library/Commands.hs b/src/Simplex/Chat/Library/Commands.hs index 71f013eb62..e8ffa21b35 100644 --- a/src/Simplex/Chat/Library/Commands.hs +++ b/src/Simplex/Chat/Library/Commands.hs @@ -4172,22 +4172,25 @@ processChatCommand vr nm = \case _ -> pure (g, False) pure (con (linkConnReq fd), CPGroupLink (GLPKnown g' (BoolDef updated) ov (ListDef glOwners))) connectPlanName :: User -> SimplexNameInfo -> CM (ACreatedConnLink, ConnectionPlan) - connectPlanName user ni@SimplexNameInfo {nameType} = do - ct_ <- withFastStore $ \db -> getContactBySimplexName db vr user ni - case ct_ of - Just ct -> case preparedContact ct of - Just PreparedContact {connLinkToConnect} -> pure (connLinkToConnect, CPContactAddress (CAPKnown ct)) - Nothing -> throwChatError CEInvalidConnReq - Nothing -> case nameType of - NTContact -> throwChatError $ CESimplexNameNotFound ni - NTPublicGroup -> do - g_ <- withFastStore $ \db -> getGroupInfoBySimplexName db vr user ni - case g_ of - Just g -> case preparedGroup g of - Just PreparedGroup {connLinkToConnect = ccLink} -> - pure (ACCL SCMContact ccLink, CPGroupLink (GLPKnown g (BoolDef False) Nothing (ListDef []))) - Nothing -> throwChatError CEInvalidConnReq - Nothing -> throwChatError $ CESimplexNameNotFound ni + connectPlanName user ni@SimplexNameInfo {nameType} = case nameType of + -- The discriminator (`@` vs `#`) is encoded into the stored simplex_name + -- bytes via strEncode, so an `@contact` lookup can never match a group + -- row (and vice versa). Dispatch on nameType up front to skip a probe. + NTContact -> do + ct_ <- withFastStore $ \db -> getContactBySimplexName db vr user ni + case ct_ of + Just ct -> case preparedContact ct of + Just PreparedContact {connLinkToConnect} -> pure (connLinkToConnect, CPContactAddress (CAPKnown ct)) + Nothing -> throwChatError CEInvalidConnReq + Nothing -> throwChatError $ CESimplexNameNotFound ni + NTPublicGroup -> do + g_ <- withFastStore $ \db -> getGroupInfoBySimplexName db vr user ni + case g_ of + Just g -> case preparedGroup g of + Just PreparedGroup {connLinkToConnect = ccLink} -> + pure (ACCL SCMContact ccLink, CPGroupLink (GLPKnown g (BoolDef False) Nothing (ListDef []))) + Nothing -> throwChatError CEInvalidConnReq + Nothing -> throwChatError $ CESimplexNameNotFound ni connectWithPlan :: User -> IncognitoEnabled -> ACreatedConnLink -> ConnectionPlan -> CM ChatResponse connectWithPlan user@User {userId} incognito ccLink plan | connectionPlanProceed plan = do