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.
This commit is contained in:
shum
2026-06-03 19:35:19 +00:00
parent 98fb416165
commit b2df6863fc
+19 -16
View File
@@ -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