From 3084df657e20b3a3e873828dbf116b7b753d4214 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Sun, 28 Jun 2026 08:49:50 +0000 Subject: [PATCH] load own names --- src/Simplex/Chat/Library/Commands.hs | 20 +++------------ src/Simplex/Chat/Store/Groups.hs | 37 +++++++++++++++++++--------- src/Simplex/Chat/Store/Profiles.hs | 17 +++++++++---- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/Simplex/Chat/Library/Commands.hs b/src/Simplex/Chat/Library/Commands.hs index c1dc7bdf80..ed6874324b 100644 --- a/src/Simplex/Chat/Library/Commands.hs +++ b/src/Simplex/Chat/Library/Commands.hs @@ -4208,18 +4208,12 @@ processChatCommand cxt nm = \case verified (con sl cReq, plan) where knownLinkPlans = withFastStore $ \db -> - ownLinkPlan db >>= \case - Just r -> pure (Just r) + liftIO (getUserContactLinkViaTarget db user nameOrLink') >>= \case + Just UserContactLink {connLinkContact = CCLink cReq _} -> pure $ Just (knownCon cReq, CPContactAddress CAPOwnLink) Nothing -> getContactToConnect db cxt user nameOrLink' >>= \case Just (cReq, ct') -> pure $ if contactDeleted ct' then Nothing else Just (knownCon cReq, CPContactAddress (CAPKnown ct')) Nothing -> (gPlan =<<) <$> getGroupToConnect db cxt user nameOrLink' - ownLinkPlan db = case nameOrLink' of - CTLink sl -> - liftIO (getUserContactLinkViaShortLink db user sl) >>= \case - Just UserContactLink {connLinkContact = CCLink cReq _} -> pure $ Just (knownCon cReq, CPContactAddress CAPOwnLink) - Nothing -> pure Nothing - CTName _ -> pure Nothing CCTGroup -> groupShortLinkPlan CCTChannel -> groupShortLinkPlan CCTRelay -> throwCmdError "chat relay links are not supported in this version" @@ -4271,15 +4265,9 @@ processChatCommand cxt nm = \case Just GroupShortLinkData {groupProfile = GroupProfile {publicGroup = Just PublicGroupProfile {groupType}}} -> groupType /= GTChannel _ -> False knownLinkPlans = withFastStore $ \db -> - ownGroupLinkPlan db >>= \case - Just r -> pure (Just r) + liftIO (getGroupInfoViaUserTarget db cxt user nameOrLink') >>= \case + Just (cReq, g) -> pure $ Just (knownCon cReq, CPGroupLink (GLPOwnLink g)) Nothing -> (gPlan =<<) <$> getGroupToConnect db cxt user nameOrLink' - ownGroupLinkPlan db = case nameOrLink' of - CTLink sl -> - liftIO (getGroupInfoViaUserShortLink db cxt user sl) >>= \case - Just (cReq, g) -> pure $ Just (knownCon cReq, CPGroupLink (GLPOwnLink g)) - Nothing -> pure Nothing - CTName _ -> pure Nothing resolveKnownGroup g = do sl <- resolveSLink (fd@FixedLinkData {rootKey = rk}, cData@(ContactLinkData _ UserContactData {owners})) <- getShortLinkConnReq' nm user sl diff --git a/src/Simplex/Chat/Store/Groups.hs b/src/Simplex/Chat/Store/Groups.hs index d73ebcf870..aabf047e27 100644 --- a/src/Simplex/Chat/Store/Groups.hs +++ b/src/Simplex/Chat/Store/Groups.hs @@ -42,7 +42,7 @@ module Simplex.Chat.Store.Groups setGroupInvitationChatItemId, getGroup, getGroupInfoByUserContactLinkConnReq, - getGroupInfoViaUserShortLink, + getGroupInfoViaUserTarget, getGroupViaShortLinkToConnect, getGroupInfoByGroupLinkHash, updateGroupProfile, @@ -2753,21 +2753,34 @@ getGroupInfoByUserContactLinkConnReq db cxt user@User {userId} (cReqSchema1, cRe (userId, cReqSchema1, cReqSchema2) maybe (pure Nothing) (fmap eitherToMaybe . runExceptT . getGroupInfo db cxt user) groupId_ -getGroupInfoViaUserShortLink :: DB.Connection -> StoreCxt -> User -> ShortLinkContact -> IO (Maybe (ConnReqContact, GroupInfo)) -getGroupInfoViaUserShortLink db cxt user@User {userId} shortLink = fmap eitherToMaybe $ runExceptT $ do +-- own hosted group found by the target: by its address short link, or (for a name) by the group's registered name +getGroupInfoViaUserTarget :: DB.Connection -> StoreCxt -> User -> ContactNameOrLink -> IO (Maybe (ConnReqContact, GroupInfo)) +getGroupInfoViaUserTarget db cxt user@User {userId} target = fmap eitherToMaybe $ runExceptT $ do (cReq, groupId) <- ExceptT getConnReqGroup (cReq,) <$> getGroupInfo db cxt user groupId where getConnReqGroup = - firstRow' toConnReqGroupId (SEInternalError "group link not found") $ - DB.query - db - [sql| - SELECT conn_req_contact, group_id - FROM user_contact_links - WHERE user_id = ? AND short_link_contact = ? - |] - (userId, shortLink) + firstRow' toConnReqGroupId (SEInternalError "group link not found") $ case target of + CTLink shortLink -> + DB.query + db + [sql| + SELECT conn_req_contact, group_id + FROM user_contact_links + WHERE user_id = ? AND short_link_contact = ? + |] + (userId, shortLink) + CTName ni -> + DB.query + db + [sql| + SELECT ucl.conn_req_contact, ucl.group_id + FROM user_contact_links ucl + JOIN groups g ON g.group_id = ucl.group_id + JOIN group_profiles gp ON gp.group_profile_id = g.group_profile_id + WHERE ucl.user_id = ? AND gp.group_domain = ? + |] + (userId, ni) toConnReqGroupId = \case -- cReq is "not null", group_id is nullable (cReq, Just groupId) -> Right (cReq, groupId) diff --git a/src/Simplex/Chat/Store/Profiles.hs b/src/Simplex/Chat/Store/Profiles.hs index fcbd8fcbbd..6dee102ce9 100644 --- a/src/Simplex/Chat/Store/Profiles.hs +++ b/src/Simplex/Chat/Store/Profiles.hs @@ -55,7 +55,7 @@ module Simplex.Chat.Store.Profiles getUserContactLinkById, getGroupLinkInfo, getUserContactLinkByConnReq, - getUserContactLinkViaShortLink, + getUserContactLinkViaTarget, setUserContactLinkShortLink, getContactWithoutConnViaAddress, getContactWithoutConnViaShortAddress, @@ -562,10 +562,17 @@ getUserContactLinkByConnReq db User {userId} (cReqSchema1, cReqSchema2) = maybeFirstRow toUserContactLink $ DB.query db (userContactLinkQuery <> " WHERE user_id = ? AND conn_req_contact IN (?,?)") (userId, cReqSchema1, cReqSchema2) -getUserContactLinkViaShortLink :: DB.Connection -> User -> ShortLinkContact -> IO (Maybe UserContactLink) -getUserContactLinkViaShortLink db User {userId} shortLink = - maybeFirstRow toUserContactLink $ - DB.query db (userContactLinkQuery <> " WHERE user_id = ? AND short_link_contact = ?") (userId, shortLink) +-- own contact address found by the target: by its short link, or (for a name) when it is this user's own registered address name +getUserContactLinkViaTarget :: DB.Connection -> User -> ContactNameOrLink -> IO (Maybe UserContactLink) +getUserContactLinkViaTarget db User {userId, profile = LocalProfile {contactDomain}} = \case + CTLink shortLink -> + maybeFirstRow toUserContactLink $ + DB.query db (userContactLinkQuery <> " WHERE user_id = ? AND short_link_contact = ?") (userId, shortLink) + CTName ni + | contactDomain == Just ni -> + maybeFirstRow toUserContactLink $ + DB.query db (userContactLinkQuery <> " WHERE user_id = ? AND group_id IS NULL") (Only userId) + | otherwise -> pure Nothing userContactLinkQuery :: Query userContactLinkQuery =