Skip to content

Commit 86a2491

Browse files
committed
delete /push/tokens/:pid
1 parent 292d7a1 commit 86a2491

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

libs/wire-api/src/Wire/API/Error/Gundeck.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ data GundeckError
2525
| AddTokenErrorInvalid
2626
| AddTokenErrorTooLong
2727
| AddTokenErrorMetadataTooLong
28+
| TokenNotFound
2829

2930
instance KnownError (MapError e) => IsSwaggerError (e :: GundeckError) where
3031
addToSwagger = addStaticErrorToSwagger @(MapError e)
@@ -38,3 +39,5 @@ type instance MapError 'AddTokenErrorInvalid = 'StaticError 404 "invalid-token"
3839
type instance MapError 'AddTokenErrorTooLong = 'StaticError 413 "token-too-long" "Push token length must be < 8192 for GCM or 400 for APNS"
3940

4041
type instance MapError 'AddTokenErrorMetadataTooLong = 'StaticError 413 "metadata-too-long" "Tried to add token to endpoint resulting in metadata length > 2048"
42+
43+
type instance MapError 'TokenNotFound = 'StaticError 404 "not-found" "Push token not found"

libs/wire-api/src/Wire/API/Push/V2/Token.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Wire.API.Push.V2.Token
3838
AddTokenError (..),
3939
AddTokenSuccess (..),
4040
AddTokenResponses,
41+
DeleteTokenResponses,
4142

4243
-- * Swagger
4344
modelPushToken,
@@ -205,7 +206,7 @@ instance ToSchema AppName where
205206
makeLenses ''PushToken
206207

207208
--------------------------------------------------------------------------------
208-
-- API types
209+
-- Add token types
209210

210211
type AddTokenErrorResponses =
211212
'[ ErrorResponse 'E.AddTokenErrorNoBudget,
@@ -244,3 +245,11 @@ instance AsHeaders '[Token] PushToken AddTokenSuccess where
244245
instance (res ~ AddTokenResponses) => AsUnion res (Either AddTokenError AddTokenSuccess) where
245246
toUnion = eitherToUnion (toUnion @AddTokenErrorResponses) (Z . I)
246247
fromUnion = eitherFromUnion (fromUnion @AddTokenErrorResponses) (unI . unZ)
248+
249+
--------------------------------------------------------------------------------
250+
-- Delete token types
251+
252+
type DeleteTokenResponses =
253+
'[ ErrorResponse 'E.TokenNotFound,
254+
RespondEmpty 204 "Push token unregistered"
255+
]

libs/wire-api/src/Wire/API/Routes/Public/Gundeck.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ type GundeckAPI =
3838
:> ReqBody '[JSON] PushToken
3939
:> MultiVerb 'POST '[JSON] AddTokenResponses (Either AddTokenError AddTokenSuccess)
4040
)
41+
:<|> Named
42+
"delete-push-token"
43+
( Summary "Unregister a native push token"
44+
:> ZUser
45+
:> "push"
46+
:> "tokens"
47+
:> Capture' '[Description "The push token to delete"] "pid" Token
48+
:> MultiVerb 'DELETE '[JSON] DeleteTokenResponses (Maybe ())
49+
)
4150

4251
swaggerDoc :: Swagger.Swagger
4352
swaggerDoc = toSwagger (Proxy @GundeckAPI)

services/gundeck/src/Gundeck/API/Public.hs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import Network.Wai.Predicate hiding (setStatus)
4040
import Network.Wai.Routing hiding (route)
4141
import Network.Wai.Utilities as Wai
4242
import Network.Wai.Utilities.Swagger
43-
import Servant (HasServer (..))
43+
import Servant (HasServer (..), (:<|>) (..))
4444
import Wire.API.Notification (NotificationId)
4545
import qualified Wire.API.Notification as Public
4646
import qualified Wire.API.Push.Token as Public
@@ -52,7 +52,9 @@ import qualified Wire.API.Swagger as Public.Swagger
5252
-- Servant API
5353

5454
servantSitemap :: ServerT GundeckAPI Gundeck
55-
servantSitemap = Named @"register-push-token" addToken
55+
servantSitemap =
56+
Named @"register-push-token" addToken
57+
:<|> Named @"delete-push-token" deleteToken
5658

5759
--------------------------------------------------------------------------------
5860
-- Wai Routes API
@@ -61,17 +63,6 @@ sitemap :: Routes ApiBuilder Gundeck ()
6163
sitemap = do
6264
-- Push API -----------------------------------------------------------
6365

64-
delete "/push/tokens/:pid" (continue deleteTokenH) $
65-
header "Z-User"
66-
.&. param "pid"
67-
.&. accept "application" "json"
68-
document "DELETE" "unregisterPushToken" $ do
69-
summary "Unregister a native push token"
70-
parameter Path "pid" bytes' $
71-
description "The push token to delete"
72-
response 204 "Push token unregistered" end
73-
response 404 "Push token does not exist" end
74-
7566
get "/push/tokens" (continue listTokensH) $
7667
header "Z-User"
7768
.&. accept "application" "json"
@@ -154,9 +145,8 @@ addToken uid cid newtok =
154145
Push.AddTokenTooLong -> Left Public.AddTokenErrorTooLong
155146
Push.AddTokenMetadataTooLong -> Left Public.AddTokenErrorMetadataTooLong
156147

157-
deleteTokenH :: UserId ::: Public.Token ::: JSON -> Gundeck Response
158-
deleteTokenH (uid ::: tok ::: _) =
159-
setStatus status204 empty <$ Push.deleteToken uid tok
148+
deleteToken :: UserId -> Public.Token -> Gundeck (Maybe ())
149+
deleteToken = Push.deleteToken
160150

161151
listTokensH :: UserId ::: JSON -> Gundeck Response
162152
listTokensH (uid ::: _) =

services/gundeck/src/Gundeck/Push.hs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,10 @@ addToken :: UserId -> ConnId -> PushToken -> Gundeck AddTokenResponse
414414
addToken uid cid newtok = mpaRunWithBudget 1 AddTokenNoBudget $ do
415415
(cur, old) <- foldl' (matching newtok) (Nothing, []) <$> Data.lookup uid Data.LocalQuorum
416416
Log.info $
417-
"user" .= UUID.toASCIIBytes (toUUID uid)
418-
~~ "token" .= Text.take 16 (tokenText (newtok ^. token))
417+
"user"
418+
.= UUID.toASCIIBytes (toUUID uid)
419+
~~ "token"
420+
.= Text.take 16 (tokenText (newtok ^. token))
419421
~~ msg (val "Registering push token")
420422
continue newtok cur
421423
>>= either
@@ -466,7 +468,8 @@ addToken uid cid newtok = mpaRunWithBudget 1 AddTokenNoBudget $ do
466468
pure (Left AddTokenNotFound)
467469
Left (Aws.InvalidToken _) -> do
468470
Log.info $
469-
"token" .= tokenText tok
471+
"token"
472+
.= tokenText tok
470473
~~ msg (val "Invalid push token.")
471474
pure (Left AddTokenInvalid)
472475
Left (Aws.TokenTooLong l) -> do
@@ -537,17 +540,22 @@ updateEndpoint uid t arn e = do
537540
equalTransport = t ^. tokenTransport == arn ^. snsTopic . endpointTransport
538541
equalApp = t ^. tokenApp == arn ^. snsTopic . endpointAppName
539542
logMessage a r tk m =
540-
"user" .= UUID.toASCIIBytes (toUUID a)
541-
~~ "token" .= Text.take 16 (tokenText tk)
542-
~~ "arn" .= toText r
543+
"user"
544+
.= UUID.toASCIIBytes (toUUID a)
545+
~~ "token"
546+
.= Text.take 16 (tokenText tk)
547+
~~ "arn"
548+
.= toText r
543549
~~ msg (val m)
544550

545-
deleteToken :: UserId -> Token -> Gundeck ()
551+
deleteToken :: UserId -> Token -> Gundeck (Maybe ())
546552
deleteToken uid tok = do
547-
as <- filter (\x -> x ^. addrToken == tok) <$> Data.lookup uid Data.LocalQuorum
548-
when (null as) $
549-
throwM (mkError status404 "not-found" "Push token not found")
550-
Native.deleteTokens as Nothing
553+
Data.lookup uid Data.LocalQuorum
554+
>>= ( \case
555+
[] -> pure Nothing
556+
xs -> Native.deleteTokens xs Nothing $> Just ()
557+
)
558+
. filter (\x -> x ^. addrToken == tok)
551559

552560
listTokens :: UserId -> Gundeck PushTokenList
553561
listTokens uid = PushTokenList . map (^. addrPushToken) <$> Data.lookup uid Data.LocalQuorum

0 commit comments

Comments
 (0)