{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}

-- | Client for Slack's @users.conversations@ endpoint.
-- <https://api.slack.com/methods/users.conversations>
module Web.Slack.UsersConversations
  ( UsersConversationsRequest (..),
    UsersConversationsResponse (..),
    usersConversations,
    usersConversationsAll,
  )
where

import Servant.API
import Servant.Client hiding (Response)
import Servant.Client.Core hiding (Response)
import Web.FormUrlEncoded (ToForm (..), genericToForm)
import Web.Slack.AesonUtils
import Web.Slack.Common (Cursor, TeamId, UserId)
import Web.Slack.Conversation (Conversation, ConversationType)
import Web.Slack.Internal
import Web.Slack.Pager (LoadPage, PagedRequest (..), PagedResponse (..), Response, ResponseMetadata, fetchAllBy)
import Web.Slack.Prelude

newtype ConversationTypesList = ConversationTypesList {ConversationTypesList -> NonEmpty ConversationType
unConversationTypesList :: NonEmpty ConversationType}
  deriving stock (Int -> ConversationTypesList -> ShowS
[ConversationTypesList] -> ShowS
ConversationTypesList -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConversationTypesList] -> ShowS
$cshowList :: [ConversationTypesList] -> ShowS
show :: ConversationTypesList -> String
$cshow :: ConversationTypesList -> String
showsPrec :: Int -> ConversationTypesList -> ShowS
$cshowsPrec :: Int -> ConversationTypesList -> ShowS
Show, ConversationTypesList -> ConversationTypesList -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConversationTypesList -> ConversationTypesList -> Bool
$c/= :: ConversationTypesList -> ConversationTypesList -> Bool
== :: ConversationTypesList -> ConversationTypesList -> Bool
$c== :: ConversationTypesList -> ConversationTypesList -> Bool
Eq)
  deriving newtype (ConversationTypesList -> ()
forall a. (a -> ()) -> NFData a
rnf :: ConversationTypesList -> ()
$crnf :: ConversationTypesList -> ()
NFData)

instance ToHttpApiData ConversationTypesList where
  toQueryParam :: ConversationTypesList -> Text
toQueryParam (ConversationTypesList NonEmpty ConversationType
list) = forall mono.
(MonoFoldable mono, Monoid (Element mono)) =>
Element mono -> mono -> Element mono
intercalate Text
"," forall a b. (a -> b) -> a -> b
$ forall a. ToHttpApiData a => a -> Text
toUrlPiece forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty ConversationType
list

-- | <https://api.slack.com/methods/users.conversations>
data UsersConversationsRequest = UsersConversationsRequest
  { UsersConversationsRequest -> Maybe Cursor
cursor :: Maybe Cursor
  , UsersConversationsRequest -> Maybe Bool
excludeArchived :: Maybe Bool
  , UsersConversationsRequest -> Maybe Int
limit :: Maybe Int
  , UsersConversationsRequest -> Maybe TeamId
teamId :: Maybe TeamId
  , UsersConversationsRequest -> Maybe ConversationTypesList
types :: Maybe ConversationTypesList
  -- ^ Specify some specific conversation types. Defaults on Slack's end to
  -- @public_channel@, aka 'PublicChannelType'.
  , UsersConversationsRequest -> Maybe UserId
user :: Maybe UserId
  -- ^ Look at the conversations of a specified user rather than the calling
  -- user
  }
  deriving stock (Int -> UsersConversationsRequest -> ShowS
[UsersConversationsRequest] -> ShowS
UsersConversationsRequest -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UsersConversationsRequest] -> ShowS
$cshowList :: [UsersConversationsRequest] -> ShowS
show :: UsersConversationsRequest -> String
$cshow :: UsersConversationsRequest -> String
showsPrec :: Int -> UsersConversationsRequest -> ShowS
$cshowsPrec :: Int -> UsersConversationsRequest -> ShowS
Show, UsersConversationsRequest -> UsersConversationsRequest -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UsersConversationsRequest -> UsersConversationsRequest -> Bool
$c/= :: UsersConversationsRequest -> UsersConversationsRequest -> Bool
== :: UsersConversationsRequest -> UsersConversationsRequest -> Bool
$c== :: UsersConversationsRequest -> UsersConversationsRequest -> Bool
Eq, forall x.
Rep UsersConversationsRequest x -> UsersConversationsRequest
forall x.
UsersConversationsRequest -> Rep UsersConversationsRequest x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep UsersConversationsRequest x -> UsersConversationsRequest
$cfrom :: forall x.
UsersConversationsRequest -> Rep UsersConversationsRequest x
Generic)
  deriving anyclass (UsersConversationsRequest
forall a. a -> Default a
def :: UsersConversationsRequest
$cdef :: UsersConversationsRequest
Default)

instance ToForm UsersConversationsRequest where
  toForm :: UsersConversationsRequest -> Form
toForm = forall a.
(Generic a, GToForm a (Rep a)) =>
FormOptions -> a -> Form
genericToForm FormOptions
snakeCaseFormOptions

instance PagedRequest UsersConversationsRequest where
  setCursor :: Maybe Cursor
-> UsersConversationsRequest -> UsersConversationsRequest
setCursor Maybe Cursor
c UsersConversationsRequest
req = UsersConversationsRequest
req {cursor :: Maybe Cursor
cursor = Maybe Cursor
c}

data UsersConversationsResponse = UsersConversationsResponse
  { UsersConversationsResponse -> [Conversation]
channels :: [Conversation]
  , UsersConversationsResponse -> Maybe ResponseMetadata
responseMetadata :: Maybe ResponseMetadata
  }
  deriving stock (Int -> UsersConversationsResponse -> ShowS
[UsersConversationsResponse] -> ShowS
UsersConversationsResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UsersConversationsResponse] -> ShowS
$cshowList :: [UsersConversationsResponse] -> ShowS
show :: UsersConversationsResponse -> String
$cshow :: UsersConversationsResponse -> String
showsPrec :: Int -> UsersConversationsResponse -> ShowS
$cshowsPrec :: Int -> UsersConversationsResponse -> ShowS
Show, UsersConversationsResponse -> UsersConversationsResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UsersConversationsResponse -> UsersConversationsResponse -> Bool
$c/= :: UsersConversationsResponse -> UsersConversationsResponse -> Bool
== :: UsersConversationsResponse -> UsersConversationsResponse -> Bool
$c== :: UsersConversationsResponse -> UsersConversationsResponse -> Bool
Eq)

instance PagedResponse UsersConversationsResponse where
  type ResponseObject UsersConversationsResponse = Conversation
  getResponseData :: UsersConversationsResponse
-> [ResponseObject UsersConversationsResponse]
getResponseData UsersConversationsResponse {[Conversation]
channels :: [Conversation]
channels :: UsersConversationsResponse -> [Conversation]
channels} = [Conversation]
channels
  getResponseMetadata :: UsersConversationsResponse -> Maybe ResponseMetadata
getResponseMetadata UsersConversationsResponse {Maybe ResponseMetadata
responseMetadata :: Maybe ResponseMetadata
responseMetadata :: UsersConversationsResponse -> Maybe ResponseMetadata
responseMetadata} = Maybe ResponseMetadata
responseMetadata

$(deriveFromJSON snakeCaseOptions ''UsersConversationsResponse)

type Api =
  "users.conversations"
    :> AuthProtect "token"
    :> ReqBody '[FormUrlEncoded] UsersConversationsRequest
    :> Post '[JSON] (ResponseJSON UsersConversationsResponse)

-- | Client for Slack's @users.conversations@ endpoint.
-- <https://api.slack.com/methods/users.conversations>
usersConversations :: SlackConfig -> UsersConversationsRequest -> IO (Response UsersConversationsResponse)
usersConversations :: SlackConfig
-> UsersConversationsRequest
-> IO (Response UsersConversationsResponse)
usersConversations SlackConfig
slackConfig UsersConversationsRequest
req = do
  let authR :: AuthenticatedRequest (AuthProtect "token")
authR = SlackConfig -> AuthenticatedRequest (AuthProtect "token")
mkSlackAuthenticateReq SlackConfig
slackConfig
  forall a. ClientM (ResponseJSON a) -> Manager -> IO (Response a)
run (AuthenticatedRequest (AuthProtect "token")
-> UsersConversationsRequest
-> ClientM (ResponseJSON UsersConversationsResponse)
usersConversations_ AuthenticatedRequest (AuthProtect "token")
authR UsersConversationsRequest
req) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. SlackConfig -> Manager
slackConfigManager forall a b. (a -> b) -> a -> b
$ SlackConfig
slackConfig

usersConversations_ ::
  AuthenticatedRequest (AuthProtect "token") ->
  UsersConversationsRequest ->
  ClientM (ResponseJSON UsersConversationsResponse)
usersConversations_ :: AuthenticatedRequest (AuthProtect "token")
-> UsersConversationsRequest
-> ClientM (ResponseJSON UsersConversationsResponse)
usersConversations_ = forall api.
HasClient ClientM api =>
Proxy api -> Client ClientM api
client (forall {k} (t :: k). Proxy t
Proxy @Api)

-- | Fetch all user conversations.
usersConversationsAll :: SlackConfig -> UsersConversationsRequest -> IO (LoadPage IO Conversation)
usersConversationsAll :: SlackConfig
-> UsersConversationsRequest -> IO (LoadPage IO Conversation)
usersConversationsAll = forall (m :: * -> *) req resp.
(MonadIO m, PagedRequest req, PagedResponse resp) =>
(req -> m (Response resp))
-> req -> m (LoadPage m (ResponseObject resp))
fetchAllBy forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. SlackConfig
-> UsersConversationsRequest
-> IO (Response UsersConversationsResponse)
usersConversations