Skip to content

Commit 1007deb

Browse files
committed
refactor
1 parent 17fe6ed commit 1007deb

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/Simplex/Messaging/Notifications/Protocol.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ data DeviceToken
597597
| WPDeviceToken WPProvider WPTokenParams
598598
deriving (Eq, Ord, Show)
599599

600+
tokenPushProvider :: DeviceToken -> PushProvider
601+
tokenPushProvider = \case
602+
APNSDeviceToken pp _ -> PPAPNS pp
603+
WPDeviceToken pp _ -> PPWP pp
604+
600605
instance Encoding DeviceToken where
601606
smpEncode token = case token of
602607
APNSDeviceToken p t -> smpEncode (p, t)

src/Simplex/Messaging/Notifications/Server.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -630,26 +630,25 @@ showServer' = decodeLatin1 . strEncode . host
630630
ntfPush :: NtfPushServer -> M ()
631631
ntfPush s@NtfPushServer {pushQ} = forever $ do
632632
(srvHost_, tkn@NtfTknRec {ntfTknId, token = t, tknStatus}, ntf) <- atomically (readTBQueue pushQ)
633-
let (pp, _) = deviceTokenFields t
634-
liftIO $ logDebug $ "sending push notification to " <> T.pack (show pp)
633+
logDebug $ "sending push notification to " <> tshow (tokenPushProvider t)
635634
st <- asks store
636635
case ntf of
637636
PNVerification _ ->
638-
liftIO (deliverNotification st pp tkn ntf) >>= \case
637+
liftIO (deliverNotification st tkn ntf) >>= \case
639638
Right _ -> do
640639
void $ liftIO $ setTknStatusConfirmed st tkn
641640
incNtfStatT t ntfVrfDelivered
642641
Left _ -> incNtfStatT t ntfVrfFailed
643642
PNCheckMessages -> do
644-
liftIO (deliverNotification st pp tkn ntf) >>= \case
643+
liftIO (deliverNotification st tkn ntf) >>= \case
645644
Right _ -> do
646645
void $ liftIO $ updateTokenCronSentAt st ntfTknId . systemSeconds =<< getSystemTime
647646
incNtfStatT t ntfCronDelivered
648647
Left _ -> incNtfStatT t ntfCronFailed
649648
PNMessage {} -> checkActiveTkn tknStatus $ do
650649
stats <- asks serverStats
651650
liftIO $ updatePeriodStats (activeTokens stats) ntfTknId
652-
liftIO (deliverNotification st pp tkn ntf) >>= \case
651+
liftIO (deliverNotification st tkn ntf) >>= \case
653652
Left _ -> do
654653
incNtfStatT t ntfFailed
655654
liftIO $ mapM_ (`incServerStat` ntfFailedOwn stats) srvHost_
@@ -662,8 +661,8 @@ ntfPush s@NtfPushServer {pushQ} = forever $ do
662661
checkActiveTkn status action
663662
| status == NTActive = action
664663
| otherwise = liftIO $ logError "bad notification token status"
665-
deliverNotification :: NtfPostgresStore -> PushProvider -> NtfTknRec -> PushNotification -> IO (Either PushProviderError ())
666-
deliverNotification st pp tkn@NtfTknRec {ntfTknId} ntf = do
664+
deliverNotification :: NtfPostgresStore -> NtfTknRec -> PushNotification -> IO (Either PushProviderError ())
665+
deliverNotification st tkn@NtfTknRec {ntfTknId, token} ntf = do
667666
deliver <- getPushClient s pp
668667
runExceptT (deliver tkn ntf) >>= \case
669668
Right _ -> pure $ Right ()
@@ -679,6 +678,7 @@ ntfPush s@NtfPushServer {pushQ} = forever $ do
679678
PPInvalidPusher -> err e
680679
_ -> err e
681680
where
681+
pp = tokenPushProvider token
682682
retryDeliver :: IO (Either PushProviderError ())
683683
retryDeliver = do
684684
deliver <- newPushClient s pp

src/Simplex/Messaging/Notifications/Server/Env.hs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,34 +164,31 @@ newNtfPushServer qSize apnsConfig = do
164164

165165
newPushClient :: NtfPushServer -> PushProvider -> IO PushProviderClient
166166
newPushClient s pp = do
167-
case pp of
167+
c <- case pp of
168168
PPWP p -> newWPPushClient s p
169169
PPAPNS p -> newAPNSPushClient s p
170+
atomically $ TM.insert pp c $ pushClients s
171+
pure c
170172

171173
newAPNSPushClient :: NtfPushServer -> APNSProvider -> IO PushProviderClient
172174
newAPNSPushClient NtfPushServer {apnsConfig, pushClients} pp = do
173-
c <- case apnsProviderHost pp of
175+
case apnsProviderHost pp of
174176
Nothing -> pure $ \_ _ -> pure ()
175177
Just host -> apnsPushProviderClient <$> createAPNSPushClient host apnsConfig
176-
atomically $ TM.insert (PPAPNS pp) c pushClients
177-
pure c
178178

179179
newWPPushClient :: NtfPushServer -> WPProvider -> IO PushProviderClient
180180
newWPPushClient NtfPushServer {pushClients} pp = do
181181
logDebug "New WP Client requested"
182182
-- We use one http manager per push server (which may be used by different clients)
183-
manager <- wpHTTPManager
184-
let c = wpPushProviderClient manager
185-
atomically $ TM.insert (PPWP pp) c pushClients
186-
pure c
183+
wpPushProviderClient <$> wpHTTPManager
187184

188185
wpHTTPManager :: IO Manager
189186
wpHTTPManager = newManager tlsManagerSettings {
190187
-- Ideally, we should be able to override the domain resolution to
191188
-- disable requests to non-public IPs. The risk is very limited as
192189
-- we allow https only, and the body is encrypted. Disabling redirections
193190
-- avoids cross-protocol redir (https => http/unix)
194-
managerModifyRequest = \r -> pure r { redirectCount = 0 }
191+
managerModifyRequest = \r -> pure r {redirectCount = 0}
195192
}
196193

197194
getPushClient :: NtfPushServer -> PushProvider -> IO PushProviderClient

0 commit comments

Comments
 (0)