{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
module Network.Pusher
(
Settings (..),
defaultSettings,
Token (..),
Address (..),
Pusher,
newPusher,
newPusherWithConnManager,
trigger,
triggerBatch,
Event (..),
channels,
channel,
users,
authenticatePresence,
authenticatePrivate,
PusherError (..),
parseWebhookPayload,
WebhookEv (..),
WebhookPayload (..),
Webhooks (..),
parseAppKeyHdr,
parseAuthSignatureHdr,
parseWebhooksBody,
verifyWebhooksBody,
parseWebhookPayloadWith,
)
where
import Control.Monad.IO.Class
( MonadIO,
liftIO,
)
import qualified Data.Aeson as A
import qualified Data.ByteString as B
import qualified Data.Text as T
import Network.Pusher.Data
( Address (..),
Event (..),
Pusher (..),
Settings (..),
Token (..),
defaultSettings,
newPusher,
newPusherWithConnManager,
)
import Network.Pusher.Error (PusherError (..))
import qualified Network.Pusher.Internal as Pusher
import qualified Network.Pusher.Internal.Auth as Auth
import qualified Network.Pusher.Internal.HTTP as HTTP
import Network.Pusher.Internal.Util (getSystemTimeSeconds)
import Network.Pusher.Protocol
( ChannelInfoQuery,
ChannelsInfo,
ChannelsInfoQuery,
FullChannelInfo,
Users,
)
import Network.Pusher.Webhook
( WebhookEv (..),
WebhookPayload (..),
Webhooks (..),
parseAppKeyHdr,
parseAuthSignatureHdr,
parseWebhookPayloadWith,
parseWebhooksBody,
verifyWebhooksBody,
)
trigger ::
MonadIO m =>
Pusher ->
[T.Text] ->
T.Text ->
T.Text ->
Maybe T.Text ->
m (Either PusherError ())
trigger :: Pusher
-> [Text]
-> Text
-> Text
-> Maybe Text
-> m (Either PusherError ())
trigger Pusher
pusher [Text]
chans Text
event Text
dat Maybe Text
socketId = do
(RequestParams
requestParams, Value
requestBody) <-
Pusher
-> [Text]
-> Text
-> Text
-> Maybe Text
-> Word64
-> (RequestParams, Value)
Pusher.mkTriggerRequest Pusher
pusher [Text]
chans Text
event Text
dat Maybe Text
socketId
(Word64 -> (RequestParams, Value))
-> m Word64 -> m (RequestParams, Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Word64
forall (m :: * -> *). MonadIO m => m Word64
getSystemTimeSeconds
IO (Either PusherError ()) -> m (Either PusherError ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either PusherError ()) -> m (Either PusherError ()))
-> IO (Either PusherError ()) -> m (Either PusherError ())
forall a b. (a -> b) -> a -> b
$ Manager -> RequestParams -> Value -> IO (Either PusherError ())
forall a.
ToJSON a =>
Manager -> RequestParams -> a -> IO (Either PusherError ())
HTTP.post (Pusher -> Manager
pConnectionManager Pusher
pusher) RequestParams
requestParams Value
requestBody
triggerBatch ::
MonadIO m =>
Pusher ->
[Event] ->
m (Either PusherError ())
triggerBatch :: Pusher -> [Event] -> m (Either PusherError ())
triggerBatch Pusher
pusher [Event]
events = do
(RequestParams
requestParams, Value
requestBody) <-
Pusher -> [Event] -> Word64 -> (RequestParams, Value)
Pusher.mkTriggerBatchRequest Pusher
pusher [Event]
events (Word64 -> (RequestParams, Value))
-> m Word64 -> m (RequestParams, Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Word64
forall (m :: * -> *). MonadIO m => m Word64
getSystemTimeSeconds
IO (Either PusherError ()) -> m (Either PusherError ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either PusherError ()) -> m (Either PusherError ()))
-> IO (Either PusherError ()) -> m (Either PusherError ())
forall a b. (a -> b) -> a -> b
$ Manager -> RequestParams -> Value -> IO (Either PusherError ())
forall a.
ToJSON a =>
Manager -> RequestParams -> a -> IO (Either PusherError ())
HTTP.post (Pusher -> Manager
pConnectionManager Pusher
pusher) RequestParams
requestParams Value
requestBody
channels ::
MonadIO m =>
Pusher ->
T.Text ->
ChannelsInfoQuery ->
m (Either PusherError ChannelsInfo)
channels :: Pusher
-> Text -> ChannelsInfoQuery -> m (Either PusherError ChannelsInfo)
channels Pusher
pusher Text
prefixFilter ChannelsInfoQuery
attributes = do
RequestParams
requestParams <-
Pusher -> Text -> ChannelsInfoQuery -> Word64 -> RequestParams
Pusher.mkChannelsRequest Pusher
pusher Text
prefixFilter ChannelsInfoQuery
attributes
(Word64 -> RequestParams) -> m Word64 -> m RequestParams
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Word64
forall (m :: * -> *). MonadIO m => m Word64
getSystemTimeSeconds
IO (Either PusherError ChannelsInfo)
-> m (Either PusherError ChannelsInfo)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either PusherError ChannelsInfo)
-> m (Either PusherError ChannelsInfo))
-> IO (Either PusherError ChannelsInfo)
-> m (Either PusherError ChannelsInfo)
forall a b. (a -> b) -> a -> b
$ Manager -> RequestParams -> IO (Either PusherError ChannelsInfo)
forall a.
FromJSON a =>
Manager -> RequestParams -> IO (Either PusherError a)
HTTP.get (Pusher -> Manager
pConnectionManager Pusher
pusher) RequestParams
requestParams
channel ::
MonadIO m =>
Pusher ->
B.ByteString ->
ChannelInfoQuery ->
m (Either PusherError FullChannelInfo)
channel :: Pusher
-> ByteString
-> ChannelInfoQuery
-> m (Either PusherError FullChannelInfo)
channel Pusher
pusher ByteString
chan ChannelInfoQuery
attributes = do
RequestParams
requestParams <-
Pusher -> ByteString -> ChannelInfoQuery -> Word64 -> RequestParams
Pusher.mkChannelRequest Pusher
pusher ByteString
chan ChannelInfoQuery
attributes (Word64 -> RequestParams) -> m Word64 -> m RequestParams
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Word64
forall (m :: * -> *). MonadIO m => m Word64
getSystemTimeSeconds
IO (Either PusherError FullChannelInfo)
-> m (Either PusherError FullChannelInfo)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either PusherError FullChannelInfo)
-> m (Either PusherError FullChannelInfo))
-> IO (Either PusherError FullChannelInfo)
-> m (Either PusherError FullChannelInfo)
forall a b. (a -> b) -> a -> b
$ Manager -> RequestParams -> IO (Either PusherError FullChannelInfo)
forall a.
FromJSON a =>
Manager -> RequestParams -> IO (Either PusherError a)
HTTP.get (Pusher -> Manager
pConnectionManager Pusher
pusher) RequestParams
requestParams
users :: MonadIO m => Pusher -> B.ByteString -> m (Either PusherError Users)
users :: Pusher -> ByteString -> m (Either PusherError Users)
users Pusher
pusher ByteString
chan = do
RequestParams
requestParams <- Pusher -> ByteString -> Word64 -> RequestParams
Pusher.mkUsersRequest Pusher
pusher ByteString
chan (Word64 -> RequestParams) -> m Word64 -> m RequestParams
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Word64
forall (m :: * -> *). MonadIO m => m Word64
getSystemTimeSeconds
IO (Either PusherError Users) -> m (Either PusherError Users)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either PusherError Users) -> m (Either PusherError Users))
-> IO (Either PusherError Users) -> m (Either PusherError Users)
forall a b. (a -> b) -> a -> b
$ Manager -> RequestParams -> IO (Either PusherError Users)
forall a.
FromJSON a =>
Manager -> RequestParams -> IO (Either PusherError a)
HTTP.get (Pusher -> Manager
pConnectionManager Pusher
pusher) RequestParams
requestParams
authenticatePrivate :: Pusher -> T.Text -> T.Text -> B.ByteString
authenticatePrivate :: Pusher -> Text -> Text -> ByteString
authenticatePrivate Pusher
pusher = Token -> Text -> Text -> ByteString
Auth.authenticatePrivate (Pusher -> Token
pToken Pusher
pusher)
authenticatePresence ::
A.ToJSON a => Pusher -> T.Text -> T.Text -> a -> B.ByteString
authenticatePresence :: Pusher -> Text -> Text -> a -> ByteString
authenticatePresence Pusher
pusher = Token -> Text -> Text -> a -> ByteString
forall a. ToJSON a => Token -> Text -> Text -> a -> ByteString
Auth.authenticatePresence (Pusher -> Token
pToken Pusher
pusher)
parseWebhookPayload ::
Pusher ->
[(B.ByteString, B.ByteString)] ->
B.ByteString ->
Maybe WebhookPayload
parseWebhookPayload :: Pusher
-> [(ByteString, ByteString)] -> ByteString -> Maybe WebhookPayload
parseWebhookPayload Pusher
pusher =
let token :: Token
token = Pusher -> Token
pToken Pusher
pusher
ourAppKey :: ByteString
ourAppKey = Token -> ByteString
tokenKey Token
token
ourAppSecret :: ByteString
ourAppSecret = Token -> ByteString
tokenSecret Token
token
lookupKeysSecret :: ByteString -> Maybe ByteString
lookupKeysSecret ByteString
whAppKey =
if ByteString
whAppKey ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
ourAppKey then ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
ourAppSecret else Maybe ByteString
forall a. Maybe a
Nothing
in (ByteString -> Maybe ByteString)
-> [(ByteString, ByteString)] -> ByteString -> Maybe WebhookPayload
parseWebhookPayloadWith ByteString -> Maybe ByteString
lookupKeysSecret