{-# LANGUAGE RecordWildCards #-}
--------------------------------------------------------------------------------
-- |
-- Module : Database.EventStore.Internal.Subscription.Packages
-- Copyright : (C) 2015 Yorick Laupa
-- License : (see the file LICENSE)
--
-- Maintainer : Yorick Laupa <yo.eight@gmail.com>
-- Stability : provisional
-- Portability : non-portable
--
--------------------------------------------------------------------------------
module Database.EventStore.Internal.Subscription.Packages where

--------------------------------------------------------------------------------
import Data.ProtocolBuffers
import Data.Serialize
import Data.UUID

--------------------------------------------------------------------------------
import Database.EventStore.Internal.Command
import Database.EventStore.Internal.Prelude
import Database.EventStore.Internal.Settings
import Database.EventStore.Internal.Subscription.Message
import Database.EventStore.Internal.Types

--------------------------------------------------------------------------------
-- | Creates Ack 'Package'.
createAckPackage :: Maybe Credentials -> UUID -> Text -> [UUID] -> Package
createAckPackage :: Maybe Credentials -> UUID -> Text -> [UUID] -> Package
createAckPackage Maybe Credentials
cred UUID
corr Text
sid [UUID]
eids =
    Package
    { packageCmd :: Command
packageCmd         = Command
persistentSubscriptionAckEventsCmd
    , packageCorrelation :: UUID
packageCorrelation = UUID
corr
    , packageData :: ByteString
packageData        = Put -> ByteString
runPut forall a b. (a -> b) -> a -> b
$ forall a. Encode a => a -> Put
encodeMessage PersistentSubscriptionAckEvents
msg
    , packageCred :: Maybe Credentials
packageCred        = Maybe Credentials
cred
    }
  where
    bytes :: [ByteString]
bytes = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall lazy strict. LazySequence lazy strict => lazy -> strict
toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. UUID -> ByteString
toByteString) [UUID]
eids
    msg :: PersistentSubscriptionAckEvents
msg   = Text -> [ByteString] -> PersistentSubscriptionAckEvents
persistentSubscriptionAckEvents Text
sid [ByteString]
bytes

--------------------------------------------------------------------------------
-- | Create Nak 'Package'.
createNakPackage :: Maybe Credentials
                 -> UUID
                 -> Text
                 -> NakAction
                 -> Maybe Text
                 -> [UUID]
                 -> Package
createNakPackage :: Maybe Credentials
-> UUID -> Text -> NakAction -> Maybe Text -> [UUID] -> Package
createNakPackage Maybe Credentials
cred UUID
corr Text
sid NakAction
act Maybe Text
txt [UUID]
eids =
    Package
    { packageCmd :: Command
packageCmd         = Command
persistentSubscriptionNakEventsCmd
    , packageCorrelation :: UUID
packageCorrelation = UUID
corr
    , packageData :: ByteString
packageData        = Put -> ByteString
runPut forall a b. (a -> b) -> a -> b
$ forall a. Encode a => a -> Put
encodeMessage PersistentSubscriptionNakEvents
msg
    , packageCred :: Maybe Credentials
packageCred        = Maybe Credentials
cred
    }
  where
    bytes :: [ByteString]
bytes = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall lazy strict. LazySequence lazy strict => lazy -> strict
toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. UUID -> ByteString
toByteString) [UUID]
eids
    msg :: PersistentSubscriptionNakEvents
msg   = Text
-> [ByteString]
-> Maybe Text
-> NakAction
-> PersistentSubscriptionNakEvents
persistentSubscriptionNakEvents Text
sid [ByteString]
bytes Maybe Text
txt NakAction
act

--------------------------------------------------------------------------------
-- | Create an unsubscribe 'Package'.
createUnsubscribePackage :: UUID -> Package
createUnsubscribePackage :: UUID -> Package
createUnsubscribePackage UUID
uuid =
    Package
    { packageCmd :: Command
packageCmd         = Command
unsubscribeFromStreamCmd
    , packageCorrelation :: UUID
packageCorrelation = UUID
uuid
    , packageData :: ByteString
packageData        = Put -> ByteString
runPut forall a b. (a -> b) -> a -> b
$ forall a. Encode a => a -> Put
encodeMessage UnsubscribeFromStream
UnsubscribeFromStream
    , packageCred :: Maybe Credentials
packageCred        = forall a. Maybe a
Nothing
    }

--------------------------------------------------------------------------------
createAuthPackage :: Credentials -> UUID -> Package
createAuthPackage :: Credentials -> UUID -> Package
createAuthPackage Credentials
cred UUID
uuid =
  Package
  { packageCmd :: Command
packageCmd         = Command
authenticateCmd
  , packageCorrelation :: UUID
packageCorrelation = UUID
uuid
  , packageData :: ByteString
packageData        = ByteString
""
  , packageCred :: Maybe Credentials
packageCred        = forall a. a -> Maybe a
Just Credentials
cred
  }