-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Servantify Cannon's internal API (#2081)
* Single notification pushes are strict in their HTTP body The websockets library does not support streaming (jaspervdj/websockets#119). So, there is no value in superficially transforming data to a stream that later won't be streamed. However, WebSocketsData enables us to be polymorphic in the message type, i.e. let the endpoints type decide what to use. This would make streaming easier to enable (if it's ever implemented by the library) and safes us from some nasty conversion code. * Forward the pushed notification as is This reflects the prior behavior of Cannon (that should not change). The type RawJson represents json content as plain text.
- Loading branch information
Showing
18 changed files
with
246 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Migrate the internal API of Cannon to Servant. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
-- This file is part of the Wire Server implementation. | ||
-- | ||
-- Copyright (C) 2022 Wire Swiss GmbH <[email protected]> | ||
-- | ||
-- This program is free software: you can redistribute it and/or modify it under | ||
-- the terms of the GNU Affero General Public License as published by the Free | ||
-- Software Foundation, either version 3 of the License, or (at your option) any | ||
-- later version. | ||
-- | ||
-- This program is distributed in the hope that it will be useful, but WITHOUT | ||
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
-- details. | ||
-- | ||
-- You should have received a copy of the GNU Affero General Public License along | ||
-- with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
module Wire.API.RawJson where | ||
|
||
import Imports | ||
import Servant | ||
|
||
-- | Wrap json content as plain 'LByteString' | ||
-- This type is intented to be used to receive json content as 'LByteString'. | ||
-- Warning: There is no validation of the json content. It may be any string. | ||
newtype RawJson = RawJson {rawJsonBytes :: LByteString} | ||
|
||
instance {-# OVERLAPPING #-} MimeUnrender JSON RawJson where | ||
mimeUnrender _ = pure . RawJson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DerivingVia #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
|
||
-- This file is part of the Wire Server implementation. | ||
-- | ||
-- Copyright (C) 2022 Wire Swiss GmbH <[email protected]> | ||
|
@@ -16,7 +20,8 @@ | |
-- with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
module Cannon.API.Internal | ||
( sitemap, | ||
( InternalAPI, | ||
internalServer, | ||
) | ||
where | ||
|
||
|
@@ -26,61 +31,82 @@ import Cannon.Types | |
import Cannon.WS | ||
import Control.Monad.Catch | ||
import Data.Aeson (encode) | ||
import qualified Data.ByteString.Lazy as L | ||
import Data.Id (ConnId, UserId) | ||
import Data.Swagger.Build.Api hiding (Response) | ||
import Data.Id hiding (client) | ||
import Gundeck.Types | ||
import Gundeck.Types.BulkPush | ||
import Imports hiding (head) | ||
import Network.HTTP.Types | ||
import Network.Wai | ||
import Network.Wai.Predicate | ||
import Network.Wai.Routing | ||
import Network.Wai.Utilities | ||
import Imports | ||
import Network.WebSockets | ||
import Servant | ||
import Servant.Conduit () | ||
import System.Logger.Class (msg, val) | ||
import qualified System.Logger.Class as LC | ||
import Wire.API.ErrorDescription | ||
import Wire.API.RawJson | ||
import Wire.API.Routes.MultiVerb | ||
import Wire.API.Routes.Named | ||
|
||
sitemap :: Routes ApiBuilder Cannon () | ||
sitemap = do | ||
get "/i/status" (continue (const $ return empty)) true | ||
head "/i/status" (continue (const $ return empty)) true | ||
|
||
post "/i/push/:user/:conn" (continue pushH) $ | ||
capture "user" .&. capture "conn" .&. request | ||
|
||
post "/i/bulkpush" (continue bulkpushH) $ | ||
request | ||
|
||
head "/i/presences/:uid/:conn" (continue checkPresenceH) $ | ||
param "uid" .&. param "conn" | ||
|
||
pushH :: UserId ::: ConnId ::: Request -> Cannon Response | ||
pushH (user ::: conn ::: req) = | ||
singlePush (readBody req) (PushTarget user conn) >>= \case | ||
PushStatusOk -> return empty | ||
PushStatusGone -> return $ errorRs status410 "general" "client gone" | ||
type InternalAPI = | ||
"i" | ||
:> ( Named | ||
"get-status" | ||
( "status" | ||
:> MultiVerb | ||
'GET | ||
'[PlainText] | ||
'[RespondEmpty 200 "Service is alive."] | ||
() | ||
) | ||
:<|> Named | ||
"push-notification" | ||
( "push" | ||
:> Capture "user" UserId | ||
:> Capture "conn" ConnId | ||
:> ReqBody '[JSON] RawJson | ||
:> MultiVerb | ||
'POST | ||
'[JSON] | ||
'[ ClientGone, | ||
RespondEmpty 200 "Successfully pushed." | ||
] | ||
(Maybe ()) | ||
) | ||
:<|> Named | ||
"bulk-push-notifications" | ||
( "bulkpush" | ||
:> ReqBody '[JSON] BulkPushRequest | ||
:> Post '[JSON] BulkPushResponse | ||
) | ||
:<|> Named | ||
"check-presence" | ||
( "presences" | ||
:> Capture "uid" UserId | ||
:> Capture "conn" ConnId | ||
:> MultiVerb | ||
'HEAD | ||
'[JSON] | ||
'[ PresenceNotRegistered, | ||
RespondEmpty 200 "Presence checked successfully." | ||
] | ||
(Maybe ()) | ||
) | ||
) | ||
|
||
-- | Parse the entire list of notifcations and targets, then call 'singlePush' on the each of them | ||
-- in order. | ||
bulkpushH :: Request -> Cannon Response | ||
bulkpushH req = json <$> (parseBody' (JsonRequest req) >>= bulkpush) | ||
internalServer :: ServerT InternalAPI Cannon | ||
internalServer = | ||
Named @"get-status" (pure ()) | ||
:<|> Named @"push-notification" pushHandler | ||
:<|> Named @"bulk-push-notifications" bulkPushHandler | ||
:<|> Named @"check-presence" checkPresenceHandler | ||
|
||
-- | The typed part of 'bulkpush'. | ||
bulkpush :: BulkPushRequest -> Cannon BulkPushResponse | ||
bulkpush (BulkPushRequest notifs) = | ||
BulkPushResponse . mconcat . zipWith compileResp notifs <$> (uncurry doNotif `mapM` notifs) | ||
where | ||
doNotif :: Notification -> [PushTarget] -> Cannon [PushStatus] | ||
doNotif (pure . encode -> notif) = mapConcurrentlyCannon (singlePush notif) | ||
compileResp :: | ||
(Notification, [PushTarget]) -> | ||
[PushStatus] -> | ||
[(NotificationId, PushTarget, PushStatus)] | ||
compileResp (notif, prcs) pss = zip3 (repeat (ntfId notif)) prcs pss | ||
pushHandler :: UserId -> ConnId -> RawJson -> Cannon (Maybe ()) | ||
pushHandler user conn body = | ||
singlePush (rawJsonBytes body) (PushTarget user conn) >>= \case | ||
PushStatusOk -> pure $ Just () | ||
PushStatusGone -> pure Nothing | ||
|
||
-- | Take a serialized 'Notification' string and send it to the 'PushTarget'. | ||
singlePush :: Cannon L.ByteString -> PushTarget -> Cannon PushStatus | ||
singlePush notification (PushTarget usrid conid) = do | ||
-- | Take notification @n@ and send it to the 'PushTarget'. | ||
singlePush :: (WebSocketsData a) => a -> PushTarget -> Cannon PushStatus | ||
singlePush n (PushTarget usrid conid) = do | ||
let k = mkKey usrid conid | ||
d <- clients | ||
LC.debug $ client (key2bytes k) . msg (val "push") | ||
|
@@ -91,15 +117,28 @@ singlePush notification (PushTarget usrid conid) = do | |
return PushStatusGone | ||
Just x -> do | ||
e <- wsenv | ||
b <- notification | ||
runWS e $ | ||
(sendMsg b k x >> return PushStatusOk) | ||
`catchAll` const (terminate k x >> return PushStatusGone) | ||
runWS e $ do | ||
catchAll | ||
(runWS e (sendMsg n k x) >> pure PushStatusOk) | ||
(const (terminate k x >> pure PushStatusGone)) | ||
|
||
bulkPushHandler :: BulkPushRequest -> Cannon BulkPushResponse | ||
bulkPushHandler (BulkPushRequest ns) = | ||
BulkPushResponse . mconcat . zipWith compileResp ns <$> (uncurry doNotify `Imports.mapM` ns) | ||
where | ||
doNotify :: Notification -> [PushTarget] -> Cannon [PushStatus] | ||
doNotify (encode -> notification) = | ||
mapConcurrentlyCannon (singlePush notification) | ||
compileResp :: | ||
(Notification, [PushTarget]) -> | ||
[PushStatus] -> | ||
[(NotificationId, PushTarget, PushStatus)] | ||
compileResp (notif, prcs) pss = zip3 (repeat (ntfId notif)) prcs pss | ||
|
||
checkPresenceH :: UserId ::: ConnId -> Cannon Response | ||
checkPresenceH (u ::: c) = do | ||
checkPresenceHandler :: UserId -> ConnId -> Cannon (Maybe ()) | ||
checkPresenceHandler u c = do | ||
e <- wsenv | ||
registered <- runWS e $ isRemoteRegistered u c | ||
if registered | ||
then return empty | ||
else return $ errorRs status404 "not-found" "presence not registered" | ||
then pure $ Just () | ||
else pure Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.