{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeApplications #-}

module Simplex.Messaging.Notifications.Client
  ( NtfClient,
    NtfClientError,
    defaultNTFClientConfig,
    ntfRegisterToken,
    ntfVerifyToken,
    ntfCheckToken,
    ntfReplaceToken,
    ntfDeleteToken,
    ntfSetCronInterval,
    ntfCreateSubscription,
    ntfCreateSubscriptions,
    ntfCheckSubscription,
    ntfCheckSubscriptions,
    ntfDeleteSubscription,
    sendNtfCommand,
    okNtfCommand,
  )
where

import Control.Monad.Except
import Control.Monad.Trans.Except
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.List.NonEmpty as L
import Data.Word (Word16)
import Simplex.Messaging.Client
import qualified Simplex.Messaging.Crypto as C
import Simplex.Messaging.Notifications.Protocol
import Simplex.Messaging.Notifications.Transport (NTFVersion, supportedClientNTFVRange, alpnSupportedNTFHandshakes)
import Simplex.Messaging.Protocol (ErrorType, pattern NoEntity)
import Simplex.Messaging.Transport (TLS, Transport (..))

type NtfClient = ProtocolClient NTFVersion ErrorType NtfResponse

type NtfClientError = ProtocolClientError ErrorType

defaultNTFClientConfig :: ProtocolClientConfig NTFVersion
defaultNTFClientConfig :: ProtocolClientConfig NTFVersion
defaultNTFClientConfig =
  (Maybe [ALPN]
-> Bool
-> VersionRange NTFVersion
-> ProtocolClientConfig NTFVersion
forall v.
Maybe [ALPN] -> Bool -> VersionRange v -> ProtocolClientConfig v
defaultClientConfig ([ALPN] -> Maybe [ALPN]
forall a. a -> Maybe a
Just [ALPN]
alpnSupportedNTFHandshakes) Bool
False VersionRange NTFVersion
supportedClientNTFVRange)
    {defaultTransport = ("443", transport @TLS)}
{-# INLINE defaultNTFClientConfig #-}

ntfRegisterToken :: NtfClient -> NetworkRequestMode -> C.APrivateAuthKey -> NewNtfEntity 'Token -> ExceptT NtfClientError IO (NtfTokenId, C.PublicKeyX25519)
ntfRegisterToken :: NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NewNtfEntity 'Token
-> ExceptT NtfClientError IO (NtfTokenId, PublicKeyX25519)
ntfRegisterToken NtfClient
c NetworkRequestMode
nm APrivateAuthKey
pKey NewNtfEntity 'Token
newTkn =
  NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand 'Token
-> ExceptT NtfClientError IO NtfResponse
forall (e :: NtfEntity).
NtfEntityI e =>
NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand e
-> ExceptT NtfClientError IO NtfResponse
sendNtfCommand NtfClient
c NetworkRequestMode
nm (APrivateAuthKey -> Maybe APrivateAuthKey
forall a. a -> Maybe a
Just APrivateAuthKey
pKey) NtfTokenId
NoEntity (NewNtfEntity 'Token -> NtfCommand 'Token
TNEW NewNtfEntity 'Token
newTkn) ExceptT NtfClientError IO NtfResponse
-> (NtfResponse
    -> ExceptT NtfClientError IO (NtfTokenId, PublicKeyX25519))
-> ExceptT NtfClientError IO (NtfTokenId, PublicKeyX25519)
forall a b.
ExceptT NtfClientError IO a
-> (a -> ExceptT NtfClientError IO b)
-> ExceptT NtfClientError IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    NRTknId NtfTokenId
tknId PublicKeyX25519
dhKey -> (NtfTokenId, PublicKeyX25519)
-> ExceptT NtfClientError IO (NtfTokenId, PublicKeyX25519)
forall a. a -> ExceptT NtfClientError IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NtfTokenId
tknId, PublicKeyX25519
dhKey)
    NtfResponse
r -> NtfClientError
-> ExceptT NtfClientError IO (NtfTokenId, PublicKeyX25519)
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (NtfClientError
 -> ExceptT NtfClientError IO (NtfTokenId, PublicKeyX25519))
-> NtfClientError
-> ExceptT NtfClientError IO (NtfTokenId, PublicKeyX25519)
forall a b. (a -> b) -> a -> b
$ NtfResponse -> NtfClientError
forall r err. Show r => r -> ProtocolClientError err
unexpectedResponse NtfResponse
r

ntfVerifyToken :: NtfClient -> NetworkRequestMode -> C.APrivateAuthKey -> NtfTokenId -> NtfRegCode -> ExceptT NtfClientError IO ()
ntfVerifyToken :: NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> NtfRegCode
-> ExceptT NtfClientError IO ()
ntfVerifyToken NtfClient
c NetworkRequestMode
nm APrivateAuthKey
pKey NtfTokenId
tknId NtfRegCode
code = NtfCommand 'Token
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
forall (e :: NtfEntity).
NtfEntityI e =>
NtfCommand e
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
okNtfCommand (NtfRegCode -> NtfCommand 'Token
TVFY NtfRegCode
code) NtfClient
c NetworkRequestMode
nm APrivateAuthKey
pKey NtfTokenId
tknId

ntfCheckToken :: NtfClient -> NetworkRequestMode -> C.APrivateAuthKey -> NtfTokenId -> ExceptT NtfClientError IO NtfTknStatus
ntfCheckToken :: NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO NtfTknStatus
ntfCheckToken NtfClient
c NetworkRequestMode
nm APrivateAuthKey
pKey NtfTokenId
tknId =
  NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand 'Token
-> ExceptT NtfClientError IO NtfResponse
forall (e :: NtfEntity).
NtfEntityI e =>
NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand e
-> ExceptT NtfClientError IO NtfResponse
sendNtfCommand NtfClient
c NetworkRequestMode
nm (APrivateAuthKey -> Maybe APrivateAuthKey
forall a. a -> Maybe a
Just APrivateAuthKey
pKey) NtfTokenId
tknId NtfCommand 'Token
TCHK ExceptT NtfClientError IO NtfResponse
-> (NtfResponse -> ExceptT NtfClientError IO NtfTknStatus)
-> ExceptT NtfClientError IO NtfTknStatus
forall a b.
ExceptT NtfClientError IO a
-> (a -> ExceptT NtfClientError IO b)
-> ExceptT NtfClientError IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    NRTkn NtfTknStatus
stat -> NtfTknStatus -> ExceptT NtfClientError IO NtfTknStatus
forall a. a -> ExceptT NtfClientError IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NtfTknStatus
stat
    NtfResponse
r -> NtfClientError -> ExceptT NtfClientError IO NtfTknStatus
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (NtfClientError -> ExceptT NtfClientError IO NtfTknStatus)
-> NtfClientError -> ExceptT NtfClientError IO NtfTknStatus
forall a b. (a -> b) -> a -> b
$ NtfResponse -> NtfClientError
forall r err. Show r => r -> ProtocolClientError err
unexpectedResponse NtfResponse
r

ntfReplaceToken :: NtfClient -> NetworkRequestMode -> C.APrivateAuthKey -> NtfTokenId -> DeviceToken -> ExceptT NtfClientError IO ()
ntfReplaceToken :: NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> DeviceToken
-> ExceptT NtfClientError IO ()
ntfReplaceToken NtfClient
c NetworkRequestMode
nm APrivateAuthKey
pKey NtfTokenId
tknId DeviceToken
token = NtfCommand 'Token
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
forall (e :: NtfEntity).
NtfEntityI e =>
NtfCommand e
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
okNtfCommand (DeviceToken -> NtfCommand 'Token
TRPL DeviceToken
token) NtfClient
c NetworkRequestMode
nm APrivateAuthKey
pKey NtfTokenId
tknId

ntfDeleteToken :: NtfClient -> NetworkRequestMode -> C.APrivateAuthKey -> NtfTokenId -> ExceptT NtfClientError IO ()
ntfDeleteToken :: NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
ntfDeleteToken = NtfCommand 'Token
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
forall (e :: NtfEntity).
NtfEntityI e =>
NtfCommand e
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
okNtfCommand NtfCommand 'Token
TDEL

-- set to 0 to disable
ntfSetCronInterval :: NtfClient -> NetworkRequestMode -> C.APrivateAuthKey -> NtfTokenId -> Word16 -> ExceptT NtfClientError IO ()
ntfSetCronInterval :: NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> Word16
-> ExceptT NtfClientError IO ()
ntfSetCronInterval NtfClient
c NetworkRequestMode
nm APrivateAuthKey
pKey NtfTokenId
tknId Word16
int = NtfCommand 'Token
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
forall (e :: NtfEntity).
NtfEntityI e =>
NtfCommand e
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
okNtfCommand (Word16 -> NtfCommand 'Token
TCRN Word16
int) NtfClient
c NetworkRequestMode
nm APrivateAuthKey
pKey NtfTokenId
tknId

ntfCreateSubscription :: NtfClient -> C.APrivateAuthKey -> NewNtfEntity 'Subscription -> ExceptT NtfClientError IO NtfSubscriptionId
ntfCreateSubscription :: NtfClient
-> APrivateAuthKey
-> NewNtfEntity 'Subscription
-> ExceptT NtfClientError IO NtfTokenId
ntfCreateSubscription NtfClient
c APrivateAuthKey
pKey NewNtfEntity 'Subscription
newSub =
  NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand 'Subscription
-> ExceptT NtfClientError IO NtfResponse
forall (e :: NtfEntity).
NtfEntityI e =>
NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand e
-> ExceptT NtfClientError IO NtfResponse
sendNtfCommand NtfClient
c NetworkRequestMode
NRMBackground (APrivateAuthKey -> Maybe APrivateAuthKey
forall a. a -> Maybe a
Just APrivateAuthKey
pKey) NtfTokenId
NoEntity (NewNtfEntity 'Subscription -> NtfCommand 'Subscription
SNEW NewNtfEntity 'Subscription
newSub) ExceptT NtfClientError IO NtfResponse
-> (NtfResponse -> ExceptT NtfClientError IO NtfTokenId)
-> ExceptT NtfClientError IO NtfTokenId
forall a b.
ExceptT NtfClientError IO a
-> (a -> ExceptT NtfClientError IO b)
-> ExceptT NtfClientError IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    NRSubId NtfTokenId
subId -> NtfTokenId -> ExceptT NtfClientError IO NtfTokenId
forall a. a -> ExceptT NtfClientError IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NtfTokenId
subId
    NtfResponse
r -> NtfClientError -> ExceptT NtfClientError IO NtfTokenId
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (NtfClientError -> ExceptT NtfClientError IO NtfTokenId)
-> NtfClientError -> ExceptT NtfClientError IO NtfTokenId
forall a b. (a -> b) -> a -> b
$ NtfResponse -> NtfClientError
forall r err. Show r => r -> ProtocolClientError err
unexpectedResponse NtfResponse
r

ntfCreateSubscriptions :: NtfClient -> C.APrivateAuthKey -> NonEmpty (NewNtfEntity 'Subscription) -> IO (NonEmpty (Either NtfClientError NtfSubscriptionId))
ntfCreateSubscriptions :: NtfClient
-> APrivateAuthKey
-> NonEmpty (NewNtfEntity 'Subscription)
-> IO (NonEmpty (Either NtfClientError NtfTokenId))
ntfCreateSubscriptions NtfClient
c APrivateAuthKey
pKey NonEmpty (NewNtfEntity 'Subscription)
newSubs = (Response ErrorType NtfResponse
 -> Either NtfClientError NtfTokenId)
-> NonEmpty (Response ErrorType NtfResponse)
-> NonEmpty (Either NtfClientError NtfTokenId)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
L.map Response ErrorType NtfResponse -> Either NtfClientError NtfTokenId
forall {err}.
Response err NtfResponse
-> Either (ProtocolClientError err) NtfTokenId
process (NonEmpty (Response ErrorType NtfResponse)
 -> NonEmpty (Either NtfClientError NtfTokenId))
-> IO (NonEmpty (Response ErrorType NtfResponse))
-> IO (NonEmpty (Either NtfClientError NtfTokenId))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NtfClient
-> NetworkRequestMode
-> NonEmpty (ClientCommand NtfResponse)
-> IO (NonEmpty (Response ErrorType NtfResponse))
forall v err msg.
Protocol v err msg =>
ProtocolClient v err msg
-> NetworkRequestMode
-> NonEmpty (ClientCommand msg)
-> IO (NonEmpty (Response err msg))
sendProtocolCommands NtfClient
c NetworkRequestMode
NRMBackground NonEmpty (ClientCommand NtfResponse)
NonEmpty (NtfTokenId, Maybe APrivateAuthKey, NtfCmd)
cs
  where
    cs :: NonEmpty (NtfTokenId, Maybe APrivateAuthKey, NtfCmd)
cs = (NewNtfEntity 'Subscription
 -> (NtfTokenId, Maybe APrivateAuthKey, NtfCmd))
-> NonEmpty (NewNtfEntity 'Subscription)
-> NonEmpty (NtfTokenId, Maybe APrivateAuthKey, NtfCmd)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
L.map (\NewNtfEntity 'Subscription
newSub -> (NtfTokenId
NoEntity, APrivateAuthKey -> Maybe APrivateAuthKey
forall a. a -> Maybe a
Just APrivateAuthKey
pKey, SNtfEntity 'Subscription -> NtfCommand 'Subscription -> NtfCmd
forall (e :: NtfEntity).
NtfEntityI e =>
SNtfEntity e -> NtfCommand e -> NtfCmd
NtfCmd SNtfEntity 'Subscription
SSubscription (NtfCommand 'Subscription -> NtfCmd)
-> NtfCommand 'Subscription -> NtfCmd
forall a b. (a -> b) -> a -> b
$ NewNtfEntity 'Subscription -> NtfCommand 'Subscription
SNEW NewNtfEntity 'Subscription
newSub)) NonEmpty (NewNtfEntity 'Subscription)
newSubs
    process :: Response err NtfResponse
-> Either (ProtocolClientError err) NtfTokenId
process (Response NtfTokenId
_ Either (ProtocolClientError err) NtfResponse
r) = case Either (ProtocolClientError err) NtfResponse
r of
      Right (NRSubId NtfTokenId
subId) -> NtfTokenId -> Either (ProtocolClientError err) NtfTokenId
forall a b. b -> Either a b
Right NtfTokenId
subId
      Right NtfResponse
r' -> ProtocolClientError err
-> Either (ProtocolClientError err) NtfTokenId
forall a b. a -> Either a b
Left (ProtocolClientError err
 -> Either (ProtocolClientError err) NtfTokenId)
-> ProtocolClientError err
-> Either (ProtocolClientError err) NtfTokenId
forall a b. (a -> b) -> a -> b
$ NtfResponse -> ProtocolClientError err
forall r err. Show r => r -> ProtocolClientError err
unexpectedResponse NtfResponse
r'
      Left ProtocolClientError err
e -> ProtocolClientError err
-> Either (ProtocolClientError err) NtfTokenId
forall a b. a -> Either a b
Left ProtocolClientError err
e

ntfCheckSubscription :: NtfClient -> C.APrivateAuthKey -> NtfSubscriptionId -> ExceptT NtfClientError IO NtfSubStatus
ntfCheckSubscription :: NtfClient
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO NtfSubStatus
ntfCheckSubscription NtfClient
c APrivateAuthKey
pKey NtfTokenId
subId =
  NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand 'Subscription
-> ExceptT NtfClientError IO NtfResponse
forall (e :: NtfEntity).
NtfEntityI e =>
NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand e
-> ExceptT NtfClientError IO NtfResponse
sendNtfCommand NtfClient
c NetworkRequestMode
NRMBackground (APrivateAuthKey -> Maybe APrivateAuthKey
forall a. a -> Maybe a
Just APrivateAuthKey
pKey) NtfTokenId
subId NtfCommand 'Subscription
SCHK ExceptT NtfClientError IO NtfResponse
-> (NtfResponse -> ExceptT NtfClientError IO NtfSubStatus)
-> ExceptT NtfClientError IO NtfSubStatus
forall a b.
ExceptT NtfClientError IO a
-> (a -> ExceptT NtfClientError IO b)
-> ExceptT NtfClientError IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    NRSub NtfSubStatus
stat -> NtfSubStatus -> ExceptT NtfClientError IO NtfSubStatus
forall a. a -> ExceptT NtfClientError IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NtfSubStatus
stat
    NtfResponse
r -> NtfClientError -> ExceptT NtfClientError IO NtfSubStatus
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (NtfClientError -> ExceptT NtfClientError IO NtfSubStatus)
-> NtfClientError -> ExceptT NtfClientError IO NtfSubStatus
forall a b. (a -> b) -> a -> b
$ NtfResponse -> NtfClientError
forall r err. Show r => r -> ProtocolClientError err
unexpectedResponse NtfResponse
r

ntfCheckSubscriptions :: NtfClient -> C.APrivateAuthKey -> NonEmpty NtfSubscriptionId -> IO (NonEmpty (Either NtfClientError NtfSubStatus))
ntfCheckSubscriptions :: NtfClient
-> APrivateAuthKey
-> NonEmpty NtfTokenId
-> IO (NonEmpty (Either NtfClientError NtfSubStatus))
ntfCheckSubscriptions NtfClient
c APrivateAuthKey
pKey NonEmpty NtfTokenId
subIds = (Response ErrorType NtfResponse
 -> Either NtfClientError NtfSubStatus)
-> NonEmpty (Response ErrorType NtfResponse)
-> NonEmpty (Either NtfClientError NtfSubStatus)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
L.map Response ErrorType NtfResponse
-> Either NtfClientError NtfSubStatus
forall {err}.
Response err NtfResponse
-> Either (ProtocolClientError err) NtfSubStatus
process (NonEmpty (Response ErrorType NtfResponse)
 -> NonEmpty (Either NtfClientError NtfSubStatus))
-> IO (NonEmpty (Response ErrorType NtfResponse))
-> IO (NonEmpty (Either NtfClientError NtfSubStatus))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NtfClient
-> NetworkRequestMode
-> NonEmpty (ClientCommand NtfResponse)
-> IO (NonEmpty (Response ErrorType NtfResponse))
forall v err msg.
Protocol v err msg =>
ProtocolClient v err msg
-> NetworkRequestMode
-> NonEmpty (ClientCommand msg)
-> IO (NonEmpty (Response err msg))
sendProtocolCommands NtfClient
c NetworkRequestMode
NRMBackground NonEmpty (ClientCommand NtfResponse)
NonEmpty (NtfTokenId, Maybe APrivateAuthKey, NtfCmd)
cs
  where
    cs :: NonEmpty (NtfTokenId, Maybe APrivateAuthKey, NtfCmd)
cs = (NtfTokenId -> (NtfTokenId, Maybe APrivateAuthKey, NtfCmd))
-> NonEmpty NtfTokenId
-> NonEmpty (NtfTokenId, Maybe APrivateAuthKey, NtfCmd)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
L.map (\NtfTokenId
subId -> (NtfTokenId
subId, APrivateAuthKey -> Maybe APrivateAuthKey
forall a. a -> Maybe a
Just APrivateAuthKey
pKey, SNtfEntity 'Subscription -> NtfCommand 'Subscription -> NtfCmd
forall (e :: NtfEntity).
NtfEntityI e =>
SNtfEntity e -> NtfCommand e -> NtfCmd
NtfCmd SNtfEntity 'Subscription
SSubscription NtfCommand 'Subscription
SCHK)) NonEmpty NtfTokenId
subIds
    process :: Response err NtfResponse
-> Either (ProtocolClientError err) NtfSubStatus
process (Response NtfTokenId
_ Either (ProtocolClientError err) NtfResponse
r) = case Either (ProtocolClientError err) NtfResponse
r of
      Right (NRSub NtfSubStatus
stat) -> NtfSubStatus -> Either (ProtocolClientError err) NtfSubStatus
forall a b. b -> Either a b
Right NtfSubStatus
stat
      Right NtfResponse
r' -> ProtocolClientError err
-> Either (ProtocolClientError err) NtfSubStatus
forall a b. a -> Either a b
Left (ProtocolClientError err
 -> Either (ProtocolClientError err) NtfSubStatus)
-> ProtocolClientError err
-> Either (ProtocolClientError err) NtfSubStatus
forall a b. (a -> b) -> a -> b
$ NtfResponse -> ProtocolClientError err
forall r err. Show r => r -> ProtocolClientError err
unexpectedResponse NtfResponse
r'
      Left ProtocolClientError err
e -> ProtocolClientError err
-> Either (ProtocolClientError err) NtfSubStatus
forall a b. a -> Either a b
Left ProtocolClientError err
e

ntfDeleteSubscription :: NtfClient -> C.APrivateAuthKey -> NtfSubscriptionId -> ExceptT NtfClientError IO ()
ntfDeleteSubscription :: NtfClient
-> APrivateAuthKey -> NtfTokenId -> ExceptT NtfClientError IO ()
ntfDeleteSubscription NtfClient
c = NtfCommand 'Subscription
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
forall (e :: NtfEntity).
NtfEntityI e =>
NtfCommand e
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
okNtfCommand NtfCommand 'Subscription
SDEL NtfClient
c NetworkRequestMode
NRMBackground

-- | Send notification server command
sendNtfCommand :: NtfEntityI e => NtfClient -> NetworkRequestMode -> Maybe C.APrivateAuthKey -> NtfEntityId -> NtfCommand e -> ExceptT NtfClientError IO NtfResponse
sendNtfCommand :: forall (e :: NtfEntity).
NtfEntityI e =>
NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand e
-> ExceptT NtfClientError IO NtfResponse
sendNtfCommand NtfClient
c NetworkRequestMode
nm Maybe APrivateAuthKey
pKey NtfTokenId
entId NtfCommand e
cmd = NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> ProtoCommand NtfResponse
-> ExceptT NtfClientError IO NtfResponse
forall v err msg.
Protocol v err msg =>
ProtocolClient v err msg
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> ProtoCommand msg
-> ExceptT (ProtocolClientError err) IO msg
sendProtocolCommand NtfClient
c NetworkRequestMode
nm Maybe APrivateAuthKey
pKey NtfTokenId
entId (SNtfEntity e -> NtfCommand e -> NtfCmd
forall (e :: NtfEntity).
NtfEntityI e =>
SNtfEntity e -> NtfCommand e -> NtfCmd
NtfCmd SNtfEntity e
forall (e :: NtfEntity). NtfEntityI e => SNtfEntity e
sNtfEntity NtfCommand e
cmd)

okNtfCommand :: NtfEntityI e => NtfCommand e -> NtfClient -> NetworkRequestMode -> C.APrivateAuthKey -> NtfEntityId -> ExceptT NtfClientError IO ()
okNtfCommand :: forall (e :: NtfEntity).
NtfEntityI e =>
NtfCommand e
-> NtfClient
-> NetworkRequestMode
-> APrivateAuthKey
-> NtfTokenId
-> ExceptT NtfClientError IO ()
okNtfCommand NtfCommand e
cmd NtfClient
c NetworkRequestMode
nm APrivateAuthKey
pKey NtfTokenId
entId =
  NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand e
-> ExceptT NtfClientError IO NtfResponse
forall (e :: NtfEntity).
NtfEntityI e =>
NtfClient
-> NetworkRequestMode
-> Maybe APrivateAuthKey
-> NtfTokenId
-> NtfCommand e
-> ExceptT NtfClientError IO NtfResponse
sendNtfCommand NtfClient
c NetworkRequestMode
nm (APrivateAuthKey -> Maybe APrivateAuthKey
forall a. a -> Maybe a
Just APrivateAuthKey
pKey) NtfTokenId
entId NtfCommand e
cmd ExceptT NtfClientError IO NtfResponse
-> (NtfResponse -> ExceptT NtfClientError IO ())
-> ExceptT NtfClientError IO ()
forall a b.
ExceptT NtfClientError IO a
-> (a -> ExceptT NtfClientError IO b)
-> ExceptT NtfClientError IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    NtfResponse
NROk -> () -> ExceptT NtfClientError IO ()
forall a. a -> ExceptT NtfClientError IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    NtfResponse
r -> NtfClientError -> ExceptT NtfClientError IO ()
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (NtfClientError -> ExceptT NtfClientError IO ())
-> NtfClientError -> ExceptT NtfClientError IO ()
forall a b. (a -> b) -> a -> b
$ NtfResponse -> NtfClientError
forall r err. Show r => r -> ProtocolClientError err
unexpectedResponse NtfResponse
r