diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index a8e225fad..b4cbf60c3 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -26,7 +26,7 @@ import Simplex.Messaging.Notifications.Server.Env import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Notifications.Transport -import Simplex.Messaging.Protocol (ErrorType (..), SignedTransmission, Transmission, encodeTransmission, tGet, tPut, SMPServer) +import Simplex.Messaging.Protocol (ErrorType (..), SMPServer, SignedTransmission, Transmission, encodeTransmission, tGet, tPut) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server import qualified Simplex.Messaging.TMap as TM @@ -86,7 +86,6 @@ ntfSubscriber NtfSubscriber {newSubQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ -- if not there, create env + start process (runNtfSubscriber) -- return env atomically $ newSMPSubscriber 0 -- todo get from config - runSMPSubscriber :: NtfSubscriber -> m () runSMPSubscriber = forever $ pure () @@ -202,21 +201,18 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do | otherwise -> VRFailed _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed NtfCmd SSubscription c@(SNEW sub@(NewNtfSub tknId _)) -> do - -- require that token is active, e.g. return Nothing from store if not - (tkn_, sub_) <- atomically $ findNtfSubscription st sub - case tkn_ of - Just NtfTknData {tknVerifyKey} -> - pure $ - if verifyCmdSignature sig_ signed tknVerifyKey - then case sub_ of - Just s@NtfSubData {tokenId} - | tknId == tokenId -> verifiedSubCmd s c - | otherwise -> VRFailed - _ -> VRVerified (NtfReqNew corrId (ANE SSubscription sub)) - else VRFailed - _ -> pure $ maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed + r_ <- atomically $ findNtfSubscription st sub + pure $ case r_ of + Just (NtfTknData {tknVerifyKey}, sub_) -> + if verifyCmdSignature sig_ signed tknVerifyKey + then case sub_ of + Just s@NtfSubData {tokenId} + | tknId == tokenId -> verifiedSubCmd s c + | otherwise -> VRFailed + _ -> VRVerified (NtfReqNew corrId (ANE SSubscription sub)) + else VRFailed + _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed NtfCmd SSubscription c -> do - -- require that token is active, e.g. return Nothing from store if not r_ <- atomically $ getNtfSubscription st entId pure $ case r_ of Just (s, NtfTknData {tknVerifyKey}) diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index d0b2e9d7d..9defbbd0c 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -20,7 +20,7 @@ import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Protocol (NotifierId, ProtocolServer) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (whenM, ($>>=)) +import Simplex.Messaging.Util (whenM, ($>>=), (<$$>)) data NtfStore = NtfStore { tokens :: TMap NtfTokenId NtfTknData, @@ -127,15 +127,25 @@ deleteNtfToken st tknId = do regKey = C.toPubKey C.pubKeyBytes getNtfSubscription :: NtfStore -> NtfSubscriptionId -> STM (Maybe (NtfSubData, NtfTknData)) -getNtfSubscription st subId = pure Nothing -- TM.lookup subId (subscriptions st) +getNtfSubscription st subId = + TM.lookup subId (subscriptions st) + $>>= \sub@NtfSubData {tokenId} -> + (sub,) <$$> getActiveNtfToken st tokenId -findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe NtfTknData, Maybe NtfSubData) -findNtfSubscription st (NewNtfSub tknId smpQueue) = pure (Nothing, Nothing) +findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe (NtfTknData, Maybe NtfSubData)) +findNtfSubscription st (NewNtfSub tknId SMPQueueNtf {smpServer, notifierId}) = do + getActiveNtfToken st tknId >>= mapM (\tkn -> (tkn,) <$> getSub) + where + getSub :: STM (Maybe NtfSubData) + getSub = + TM.lookup (tknId, smpServer, notifierId) (subscriptionLookup st) + $>>= (`TM.lookup` subscriptions st) --- findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe NtfSubData) --- findNtfSubscription st (NewNtfSub tknId smpQueue) = --- TM.lookup (tknId, smpQueue) (subscriptionLookup st) --- $>>= (`TM.lookup` subscriptions st) +getActiveNtfToken :: NtfStore -> NtfTokenId -> STM (Maybe NtfTknData) +getActiveNtfToken st tknId = + getNtfToken st tknId $>>= \tkn@NtfTknData {tknStatus} -> do + tStatus <- readTVar tknStatus + pure $ if tStatus == NTActive then Just tkn else Nothing mkNtfSubData :: NewNtfEntity 'Subscription -> STM NtfSubData mkNtfSubData (NewNtfSub tokenId smpQueue) = do