module Galley.Effects.BackendNotificationQueueAccess where

import Data.Qualified
import Imports
import Network.AMQP qualified as Q
import Polysemy
import Polysemy.Error
import Wire.API.Federation.BackendNotifications
import Wire.API.Federation.Component
import Wire.API.Federation.Error

data BackendNotificationQueueAccess m a where
  EnqueueNotification ::
    (KnownComponent c) =>
    Q.DeliveryMode ->
    Remote x ->
    FedQueueClient c a ->
    BackendNotificationQueueAccess m (Either FederationError a)
  EnqueueNotificationsConcurrently ::
    (KnownComponent c, Foldable f, Functor f) =>
    Q.DeliveryMode ->
    f (Remote x) ->
    (Remote [x] -> FedQueueClient c a) ->
    BackendNotificationQueueAccess m (Either FederationError [Remote a])
  EnqueueNotificationsConcurrentlyBuckets ::
    (KnownComponent c, Foldable f, Functor f) =>
    Q.DeliveryMode ->
    f (Remote x) ->
    (Remote x -> FedQueueClient c a) ->
    BackendNotificationQueueAccess m (Either FederationError [Remote a])

enqueueNotification ::
  ( KnownComponent c,
    Member (Error FederationError) r,
    Member BackendNotificationQueueAccess r
  ) =>
  Q.DeliveryMode ->
  Remote x ->
  FedQueueClient c a ->
  Sem r a
enqueueNotification :: forall (c :: Component) (r :: EffectRow) x a.
(KnownComponent c, Member (Error FederationError) r,
 Member BackendNotificationQueueAccess r) =>
DeliveryMode -> Remote x -> FedQueueClient c a -> Sem r a
enqueueNotification DeliveryMode
m Remote x
r FedQueueClient c a
q = BackendNotificationQueueAccess (Sem r) (Either FederationError a)
-> Sem r (Either FederationError a)
forall (e :: Effect) (r :: EffectRow) a.
Member e r =>
e (Sem r) a -> Sem r a
send (DeliveryMode
-> Remote x
-> FedQueueClient c a
-> BackendNotificationQueueAccess
     (Sem r) (Either FederationError a)
forall {k} (c :: Component) f x (m :: k).
KnownComponent c =>
DeliveryMode
-> Remote f
-> FedQueueClient c x
-> BackendNotificationQueueAccess m (Either FederationError x)
EnqueueNotification DeliveryMode
m Remote x
r FedQueueClient c a
q) Sem r (Either FederationError a)
-> (Either FederationError a -> Sem r a) -> Sem r a
forall a b. Sem r a -> (a -> Sem r b) -> Sem r b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (FederationError -> Sem r a)
-> (a -> Sem r a) -> Either FederationError a -> Sem r a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either FederationError -> Sem r a
forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw a -> Sem r a
forall a. a -> Sem r a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

enqueueNotificationsConcurrently ::
  ( KnownComponent c,
    Foldable f,
    Functor f,
    Member (Error FederationError) r,
    Member BackendNotificationQueueAccess r
  ) =>
  Q.DeliveryMode ->
  f (Remote x) ->
  (Remote [x] -> FedQueueClient c a) ->
  Sem r [Remote a]
enqueueNotificationsConcurrently :: forall (c :: Component) (f :: * -> *) (r :: EffectRow) x a.
(KnownComponent c, Foldable f, Functor f,
 Member (Error FederationError) r,
 Member BackendNotificationQueueAccess r) =>
DeliveryMode
-> f (Remote x)
-> (Remote [x] -> FedQueueClient c a)
-> Sem r [Remote a]
enqueueNotificationsConcurrently DeliveryMode
m f (Remote x)
r Remote [x] -> FedQueueClient c a
q =
  BackendNotificationQueueAccess
  (Sem r) (Either FederationError [Remote a])
-> Sem r (Either FederationError [Remote a])
forall (e :: Effect) (r :: EffectRow) a.
Member e r =>
e (Sem r) a -> Sem r a
send (DeliveryMode
-> f (Remote x)
-> (Remote [x] -> FedQueueClient c a)
-> BackendNotificationQueueAccess
     (Sem r) (Either FederationError [Remote a])
forall {k} (c :: Component) (f :: * -> *) x a (m :: k).
(KnownComponent c, Foldable f, Functor f) =>
DeliveryMode
-> f (Remote x)
-> (Remote [x] -> FedQueueClient c a)
-> BackendNotificationQueueAccess
     m (Either FederationError [Remote a])
EnqueueNotificationsConcurrently DeliveryMode
m f (Remote x)
r Remote [x] -> FedQueueClient c a
q)
    Sem r (Either FederationError [Remote a])
-> (Either FederationError [Remote a] -> Sem r [Remote a])
-> Sem r [Remote a]
forall a b. Sem r a -> (a -> Sem r b) -> Sem r b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (FederationError -> Sem r [Remote a])
-> ([Remote a] -> Sem r [Remote a])
-> Either FederationError [Remote a]
-> Sem r [Remote a]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either FederationError -> Sem r [Remote a]
forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw [Remote a] -> Sem r [Remote a]
forall a. a -> Sem r a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

enqueueNotificationsConcurrentlyBuckets ::
  ( KnownComponent c,
    Foldable f,
    Functor f,
    Member (Error FederationError) r,
    Member BackendNotificationQueueAccess r
  ) =>
  Q.DeliveryMode ->
  f (Remote x) ->
  (Remote x -> FedQueueClient c a) ->
  Sem r [Remote a]
enqueueNotificationsConcurrentlyBuckets :: forall (c :: Component) (f :: * -> *) (r :: EffectRow) x a.
(KnownComponent c, Foldable f, Functor f,
 Member (Error FederationError) r,
 Member BackendNotificationQueueAccess r) =>
DeliveryMode
-> f (Remote x)
-> (Remote x -> FedQueueClient c a)
-> Sem r [Remote a]
enqueueNotificationsConcurrentlyBuckets DeliveryMode
m f (Remote x)
r Remote x -> FedQueueClient c a
q =
  BackendNotificationQueueAccess
  (Sem r) (Either FederationError [Remote a])
-> Sem r (Either FederationError [Remote a])
forall (e :: Effect) (r :: EffectRow) a.
Member e r =>
e (Sem r) a -> Sem r a
send (DeliveryMode
-> f (Remote x)
-> (Remote x -> FedQueueClient c a)
-> BackendNotificationQueueAccess
     (Sem r) (Either FederationError [Remote a])
forall {k} (c :: Component) (f :: * -> *) x a (m :: k).
(KnownComponent c, Foldable f, Functor f) =>
DeliveryMode
-> f (Remote x)
-> (Remote x -> FedQueueClient c a)
-> BackendNotificationQueueAccess
     m (Either FederationError [Remote a])
EnqueueNotificationsConcurrentlyBuckets DeliveryMode
m f (Remote x)
r Remote x -> FedQueueClient c a
q) Sem r (Either FederationError [Remote a])
-> (Either FederationError [Remote a] -> Sem r [Remote a])
-> Sem r [Remote a]
forall a b. Sem r a -> (a -> Sem r b) -> Sem r b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (FederationError -> Sem r [Remote a])
-> ([Remote a] -> Sem r [Remote a])
-> Either FederationError [Remote a]
-> Sem r [Remote a]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either FederationError -> Sem r [Remote a]
forall e (r :: EffectRow) a. Member (Error e) r => e -> Sem r a
throw [Remote a] -> Sem r [Remote a]
forall a. a -> Sem r a
forall (f :: * -> *) a. Applicative f => a -> f a
pure