Skip to content

Commit aca3cec

Browse files
committed
Use content of push notif with web push
1 parent ea40a66 commit aca3cec

File tree

1 file changed

+19
-5
lines changed
  • src/Simplex/Messaging/Notifications/Server/Push

1 file changed

+19
-5
lines changed

src/Simplex/Messaging/Notifications/Server/Push/WebPush.hs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,25 @@
1313
module Simplex.Messaging.Notifications.Server.Push.WebPush where
1414

1515
import Network.HTTP.Client
16-
import Simplex.Messaging.Notifications.Protocol (DeviceToken (WPDeviceToken), WPEndpoint (..))
16+
import Simplex.Messaging.Notifications.Protocol (DeviceToken (WPDeviceToken), WPEndpoint (..), encodePNMessages, PNMessageData)
1717
import Simplex.Messaging.Notifications.Server.Store.Types
1818
import Simplex.Messaging.Notifications.Server.Push
1919
import Control.Monad.Except
2020
import Control.Logger.Simple (logDebug)
2121
import Simplex.Messaging.Util (tshow)
2222
import qualified Data.ByteString.Char8 as B
23-
import Data.ByteString.Char8 (ByteString)
2423
import Control.Monad.IO.Class (liftIO)
2524
import Control.Exception ( fromException, SomeException, try )
2625
import qualified Network.HTTP.Types as N
26+
import qualified Data.Aeson as J
27+
import Data.Aeson ((.=))
28+
import qualified Data.ByteString.Lazy as BL
29+
import Data.List.NonEmpty (NonEmpty)
30+
import qualified Data.Text.Encoding as T
31+
import qualified Data.Text as T
2732

2833
wpPushProviderClient :: Manager -> PushProviderClient
29-
wpPushProviderClient mg tkn _ = do
34+
wpPushProviderClient mg tkn pn = do
3035
e <- B.unpack <$> endpoint tkn
3136
r <- liftPPWPError $ parseUrlThrow e
3237
logDebug $ "Request to " <> tshow r.host
@@ -39,18 +44,27 @@ wpPushProviderClient mg tkn _ = do
3944
let req = r {
4045
method = "POST"
4146
, requestHeaders
42-
, requestBody = "ping"
47+
, requestBody = RequestBodyLBS $ encodePN pn
4348
, redirectCount = 0
4449
}
4550
_ <- liftPPWPError $ httpNoBody req mg
4651
pure ()
4752
where
48-
endpoint :: NtfTknRec -> ExceptT PushProviderError IO ByteString
53+
endpoint :: NtfTknRec -> ExceptT PushProviderError IO B.ByteString
4954
endpoint NtfTknRec {token} = do
5055
case token of
5156
WPDeviceToken WPEndpoint{ endpoint = e } -> pure e
5257
_ -> fail "Wrong device token"
5358

59+
encodePN :: PushNotification -> BL.ByteString
60+
encodePN pn = J.encode $ case pn of
61+
PNVerification code -> J.object [ "verification" .= code ]
62+
PNMessage d -> J.object [ "message" .= encodeData d ]
63+
PNCheckMessages -> J.object [ "checkMessages" .= True ]
64+
where
65+
encodeData :: NonEmpty PNMessageData -> String
66+
encodeData a = T.unpack . T.decodeUtf8 $ encodePNMessages a
67+
5468
liftPPWPError :: IO a -> ExceptT PushProviderError IO a
5569
liftPPWPError = liftPPWPError' toPPWPError
5670

0 commit comments

Comments
 (0)