Skip to content

Commit f736b02

Browse files
committed
wip
1 parent eca5911 commit f736b02

File tree

4 files changed

+64
-27
lines changed

4 files changed

+64
-27
lines changed

src/Simplex/Messaging/Agent.hs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ createConnectionAsync c userId aCorrId enableNtfs = withAgentEnv c .:. newConnAs
347347
{-# INLINE createConnectionAsync #-}
348348

349349
-- | Create or update user's contact connection short link (LSET command) asynchronously, no synchronous response
350-
setConnShortLinkAsync :: AgentClient -> ACorrId -> ConnId -> SConnectionMode c -> UserConnLinkData c -> Maybe CRClientData -> AE ()
350+
setConnShortLinkAsync :: ConnectionModeI c => AgentClient -> ACorrId -> ConnId -> SConnectionMode c -> UserConnLinkData c -> Maybe CRClientData -> AE ()
351351
setConnShortLinkAsync c = withAgentEnv c .::. setConnShortLinkAsync' c
352352
{-# INLINE setConnShortLinkAsync #-}
353353

@@ -892,10 +892,15 @@ checkClientNotices AgentClient {clientNotices, presetServers} (ProtoServerWithAu
892892
when (maybe True (ts <) expires_) $
893893
throwError NOTICE {server = safeDecodeUtf8 $ strEncode $ L.head host, preset = isNothing srvKey, expiresAt = roundedToUTCTime <$> expires_}
894894

895-
setConnShortLinkAsync' :: AgentClient -> ACorrId -> ConnId -> SConnectionMode c -> UserConnLinkData c -> Maybe CRClientData -> AM ()
895+
setConnShortLinkAsync' :: forall c. ConnectionModeI c => AgentClient -> ACorrId -> ConnId -> SConnectionMode c -> UserConnLinkData c -> Maybe CRClientData -> AM ()
896896
setConnShortLinkAsync' c corrId connId cMode userLinkData clientData =
897-
-- enqueue command LSET
898-
undefined
897+
withConnLock c connId "setConnShortLinkAsync" $ do
898+
SomeConn _ conn <- withStore c (`getConn` connId)
899+
srv <- case (conn, cMode, userLinkData) of
900+
(ContactConnection _ RcvQueue {server}, SCMContact, UserContactLinkData {}) -> pure server
901+
(RcvConnection _ RcvQueue {server}, SCMInvitation, UserInvLinkData {}) -> pure server
902+
_ -> throwE $ CMD PROHIBITED "setConnShortLinkAsync: invalid connection or mode"
903+
enqueueCommand c corrId connId (Just srv) $ AClientCommand $ LSET (AUCLD cMode userLinkData) clientData
899904

900905
setConnShortLink' :: AgentClient -> NetworkRequestMode -> ConnId -> SConnectionMode c -> UserConnLinkData c -> Maybe CRClientData -> AM (ConnShortLink c)
901906
setConnShortLink' c nm connId cMode userLinkData clientData =
@@ -1668,10 +1673,10 @@ runCommandProcessing c@AgentClient {subQ} connId server_ Worker {doWork} = do
16681673
tryCommand . withNextSrv c userId storageSrvs triedHosts [] $ \srv -> do
16691674
(CCLink cReq _, service) <- newRcvConnSrv c NRMBackground userId connId enableNtfs cMode Nothing Nothing pqEnc subMode srv
16701675
notify $ INV (ACR cMode cReq) service
1671-
LSET {} ->
1672-
-- create link (reuse setConnShortLink')
1673-
-- notify - LINK
1674-
undefined
1676+
LSET (AUCLD cMode userLinkData) clientData ->
1677+
withServer' . tryCommand $ do
1678+
link <- setConnShortLink' c NRMBackground connId cMode userLinkData clientData
1679+
notify $ LINK (ACSL cMode link)
16751680
JOIN enableNtfs (ACR _ cReq@(CRInvitationUri ConnReqUriData {crSmpQueues = q :| _} _)) pqEnc subMode connInfo -> noServer $ do
16761681
triedHosts <- newTVarIO S.empty
16771682
tryCommand . withNextSrv c userId storageSrvs triedHosts [qServer q] $ \srv -> do

src/Simplex/Messaging/Agent/Protocol.hs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ module Simplex.Messaging.Agent.Protocol
112112
ServiceScheme,
113113
FixedLinkData (..),
114114
ConnLinkData (..),
115+
AUserConnLinkData (..),
115116
UserConnLinkData (..),
116117
UserContactData (..),
117118
UserLinkData (..),
@@ -436,7 +437,7 @@ deriving instance Show AEvtTag
436437

437438
data ACommand
438439
= NEW Bool AConnectionMode InitialKeys SubscriptionMode -- response INV
439-
| LSET AConnectionMode AUserConnLinkData (Maybe CRClientData) -- response LINK
440+
| LSET AUserConnLinkData (Maybe CRClientData) -- response LINK
440441
| JOIN Bool AConnectionRequestUri PQSupport SubscriptionMode ConnInfo
441442
| LET ConfirmationId ConnInfo -- ConnInfo is from client
442443
| ACK AgentMsgId (Maybe MsgReceiptInfo)
@@ -1709,17 +1710,30 @@ data UserContactData = UserContactData
17091710
relays :: [ConnShortLink 'CMContact],
17101711
userData :: UserLinkData
17111712
}
1713+
deriving (Eq, Show)
17121714

17131715
newtype UserLinkData = UserLinkData ByteString
1716+
deriving (Eq, Show)
17141717

17151718
data AConnLinkData = forall m. ConnectionModeI m => ACLD (SConnectionMode m) (ConnLinkData m)
17161719

17171720
data UserConnLinkData c where
17181721
UserInvLinkData :: UserLinkData -> UserConnLinkData 'CMInvitation
17191722
UserContactLinkData :: UserContactData -> UserConnLinkData 'CMContact
17201723

1724+
deriving instance Eq (UserConnLinkData m)
1725+
1726+
deriving instance Show (UserConnLinkData m)
1727+
17211728
data AUserConnLinkData = forall m. ConnectionModeI m => AUCLD (SConnectionMode m) (UserConnLinkData m)
17221729

1730+
instance Eq AUserConnLinkData where
1731+
AUCLD m d == AUCLD m' d' = case testEquality m m' of
1732+
Just Refl -> d == d'
1733+
Nothing -> False
1734+
1735+
deriving instance Show AUserConnLinkData
1736+
17231737
linkUserData :: ConnLinkData c -> UserLinkData
17241738
linkUserData = \case
17251739
InvitationLinkData _ d -> d
@@ -1746,6 +1760,7 @@ data OwnerAuth = OwnerAuth
17461760
-- Owner validation should detect and reject loops.
17471761
authOwnerSig :: C.Signature 'C.Ed25519
17481762
}
1763+
deriving (Eq, Show)
17491764

17501765
instance Encoding OwnerAuth where
17511766
smpEncode OwnerAuth {ownerId, ownerKey, ownerSig, authOwnerId, authOwnerSig} =
@@ -1785,9 +1800,35 @@ instance Encoding AConnLinkData where
17851800
let cd = UserContactData {direct, owners, relays, userData}
17861801
pure $ ACLD SCMContact $ ContactLinkData vr cd
17871802

1803+
instance ConnectionModeI c => Encoding (UserConnLinkData c) where
1804+
smpEncode = \case
1805+
UserInvLinkData userData -> smpEncode (CMInvitation, userData)
1806+
UserContactLinkData UserContactData {direct, owners, relays, userData} ->
1807+
B.concat [smpEncode (CMContact, direct), smpEncodeList owners, smpEncodeList relays, smpEncode userData]
1808+
smpP = (\(AUCLD _ d) -> checkConnMode d) <$?> smpP
1809+
{-# INLINE smpP #-}
1810+
17881811
instance Encoding AUserConnLinkData where
1789-
smpEncode = undefined
1790-
smpP = undefined
1812+
smpEncode (AUCLD _ d) = smpEncode d
1813+
{-# INLINE smpEncode #-}
1814+
smpP =
1815+
smpP >>= \case
1816+
CMInvitation -> do
1817+
userData <- smpP <* A.takeByteString -- ignoring tail for forward compatibility with the future link data encoding
1818+
pure $ AUCLD SCMInvitation $ UserInvLinkData userData
1819+
CMContact -> do
1820+
direct <- smpP
1821+
owners <- smpListP
1822+
relays <- smpListP
1823+
userData <- smpP <* A.takeByteString -- ignoring tail for forward compatibility with the future link data encoding
1824+
let cd = UserContactData {direct, owners, relays, userData}
1825+
pure $ AUCLD SCMContact $ UserContactLinkData cd
1826+
1827+
instance StrEncoding AUserConnLinkData where
1828+
strEncode = smpEncode
1829+
{-# INLINE strEncode #-}
1830+
strP = smpP
1831+
{-# INLINE strP #-}
17911832

17921833
instance Encoding UserLinkData where
17931834
smpEncode (UserLinkData s) = if B.length s <= 254 then smpEncode s else smpEncode ('\255', Large s)
@@ -2009,7 +2050,7 @@ commandP binaryP =
20092050
strP
20102051
>>= \case
20112052
NEW_ -> s (NEW <$> strP_ <*> strP_ <*> pqIKP <*> (strP <|> pure SMP.SMSubscribe))
2012-
LSET_ -> undefined
2053+
LSET_ -> s (LSET <$> strP <*> optional (A.space *> strP))
20132054
JOIN_ -> s (JOIN <$> strP_ <*> strP_ <*> pqSupP <*> (strP_ <|> pure SMP.SMSubscribe) <*> binaryP)
20142055
LET_ -> s (LET <$> A.takeTill (== ' ') <* A.space <*> binaryP)
20152056
ACK_ -> s (ACK <$> A.decimal <*> optional (A.space *> binaryP))
@@ -2027,7 +2068,7 @@ commandP binaryP =
20272068
serializeCommand :: ACommand -> ByteString
20282069
serializeCommand = \case
20292070
NEW ntfs cMode pqIK subMode -> s (NEW_, ntfs, cMode, pqIK, subMode)
2030-
LSET {} -> undefined
2071+
LSET uld cd_ -> s (LSET_, uld) <> maybe "" (B.cons ' ' . s) cd_
20312072
JOIN ntfs cReq pqSup subMode cInfo -> s (JOIN_, ntfs, cReq, pqSup, subMode, Str $ serializeBinary cInfo)
20322073
LET confId cInfo -> B.unwords [s LET_, confId, serializeBinary cInfo]
20332074
ACK mId rcptInfo_ -> s (ACK_, mId) <> maybe "" (B.cons ' ' . serializeBinary) rcptInfo_

tests/AgentTests/EqInstances.hs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module AgentTests.EqInstances where
66

77
import Data.Type.Equality
8-
import Simplex.Messaging.Agent.Protocol (ConnLinkData (..), OwnerAuth (..), UserContactData (..), UserLinkData (..))
8+
import Simplex.Messaging.Agent.Protocol (ConnLinkData (..))
99
import Simplex.Messaging.Agent.Store
1010
import Simplex.Messaging.Client (ProxiedRelay (..))
1111

@@ -32,18 +32,6 @@ deriving instance Show (ConnLinkData c)
3232

3333
deriving instance Eq (ConnLinkData c)
3434

35-
deriving instance Show UserContactData
36-
37-
deriving instance Eq UserContactData
38-
39-
deriving instance Show UserLinkData
40-
41-
deriving instance Eq UserLinkData
42-
43-
deriving instance Show OwnerAuth
44-
45-
deriving instance Eq OwnerAuth
46-
4735
deriving instance Show ProxiedRelay
4836

4937
deriving instance Eq ProxiedRelay

tests/AgentTests/FunctionalAPITests.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ functionalAPITests ps = do
450450
it "should send multiple messages to the same connection" $ withSmpServer ps testSendMessagesB
451451
it "should send messages to the 2 connections" $ withSmpServer ps testSendMessagesB2
452452
describe "Async agent commands" $ do
453-
describe "connect using async agent commands" $
453+
fdescribe "connect using async agent commands" $
454454
testBasicMatrix2 ps testAsyncCommands
455455
it "should restore and complete async commands on restart" $
456456
testAsyncCommandsRestore ps
@@ -2583,6 +2583,9 @@ testAsyncCommands sqSecured alice bob baseId =
25832583
bobId <- createConnectionAsync alice 1 "1" True SCMInvitation IKPQOn SMSubscribe
25842584
("1", bobId', INV (ACR _ qInfo)) <- get alice
25852585
liftIO $ bobId' `shouldBe` bobId
2586+
setConnShortLinkAsync alice "1a" bobId SCMInvitation (UserInvLinkData $ UserLinkData "test") Nothing
2587+
("1a", bobId'', LINK (ACSL SCMInvitation _)) <- get alice
2588+
liftIO $ bobId'' `shouldBe` bobId
25862589
aliceId <- joinConnectionAsync bob 1 "2" True qInfo "bob's connInfo" PQSupportOn SMSubscribe
25872590
("2", aliceId', JOINED sqSecured') <- get bob
25882591
liftIO $ do

0 commit comments

Comments
 (0)