{-# LANGUAGE CPP #-}
module Web.Slack.Classy
( SlackConfig (..),
mkSlackConfig,
apiTest,
authTest,
chatPostMessage,
conversationsList,
conversationsHistory,
conversationsHistoryAll,
conversationsReplies,
repliesFetchAll,
getUserDesc,
usersList,
userLookupByEmail,
authenticateReq,
Response,
LoadPage,
HasManager (..),
HasToken (..),
)
where
import Control.Arrow ((&&&))
import Control.Monad.Reader
import Data.Map qualified as Map
import Data.Maybe
import Data.Text (Text)
import Network.HTTP.Client (Manager)
import Web.Slack (SlackConfig (..), authenticateReq, mkSlackConfig)
import Web.Slack qualified as NonClassy
import Web.Slack.Api qualified as Api
import Web.Slack.Auth qualified as Auth
import Web.Slack.Chat qualified as Chat
import Web.Slack.Common qualified as Common
import Web.Slack.Conversation qualified as Conversation
import Web.Slack.Pager
import Web.Slack.User qualified as User
import Prelude
#if !MIN_VERSION_servant(0,13,0)
mkClientEnv :: Manager -> BaseUrl -> ClientEnv
mkClientEnv = ClientEnv
#endif
class HasManager a where
getManager :: a -> Manager
class HasToken a where
getToken :: a -> Text
instance HasManager SlackConfig where
getManager :: SlackConfig -> Manager
getManager = SlackConfig -> Manager
slackConfigManager
instance HasToken SlackConfig where
getToken :: SlackConfig -> Text
getToken = SlackConfig -> Text
slackConfigToken
apiTest ::
(MonadReader env m, HasManager env, MonadIO m) =>
Api.TestReq ->
m (Response Api.TestRsp)
apiTest :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, MonadIO m) =>
TestReq -> m (Response TestRsp)
apiTest = forall env (m :: * -> *) a.
(MonadReader env m, MonadIO m) =>
(env -> IO a) -> m a
liftToReader forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip (Manager -> TestReq -> IO (Response TestRsp)
NonClassy.apiTest forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HasManager a => a -> Manager
getManager)
authTest ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
m (Response Auth.TestRsp)
authTest :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
m (Response TestRsp)
authTest = forall env (m :: * -> *) a.
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
(SlackConfig -> IO a) -> m a
liftNonClassy SlackConfig -> IO (Response TestRsp)
NonClassy.authTest
conversationsList ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
Conversation.ListReq ->
m (Response Conversation.ListRsp)
conversationsList :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
ListReq -> m (Response ListRsp)
conversationsList = forall env (m :: * -> *) a.
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
(SlackConfig -> IO a) -> m a
liftNonClassy forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip SlackConfig -> ListReq -> IO (Response ListRsp)
NonClassy.conversationsList
conversationsHistory ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
Conversation.HistoryReq ->
m (Response Conversation.HistoryRsp)
conversationsHistory :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
HistoryReq -> m (Response HistoryRsp)
conversationsHistory = forall env (m :: * -> *) a.
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
(SlackConfig -> IO a) -> m a
liftNonClassy forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip SlackConfig -> HistoryReq -> IO (Response HistoryRsp)
NonClassy.conversationsHistory
conversationsReplies ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
Conversation.RepliesReq ->
m (Response Conversation.HistoryRsp)
conversationsReplies :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
RepliesReq -> m (Response HistoryRsp)
conversationsReplies = forall env (m :: * -> *) a.
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
(SlackConfig -> IO a) -> m a
liftNonClassy forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip SlackConfig -> RepliesReq -> IO (Response HistoryRsp)
NonClassy.conversationsReplies
chatPostMessage ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
Chat.PostMsgReq ->
m (Response Chat.PostMsgRsp)
chatPostMessage :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
PostMsgReq -> m (Response PostMsgRsp)
chatPostMessage = forall env (m :: * -> *) a.
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
(SlackConfig -> IO a) -> m a
liftNonClassy forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip SlackConfig -> PostMsgReq -> IO (Response PostMsgRsp)
NonClassy.chatPostMessage
usersList ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
m (Response User.ListRsp)
usersList :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
m (Response ListRsp)
usersList = forall env (m :: * -> *) a.
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
(SlackConfig -> IO a) -> m a
liftNonClassy SlackConfig -> IO (Response ListRsp)
NonClassy.usersList
userLookupByEmail ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
User.Email ->
m (Response User.UserRsp)
userLookupByEmail :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
Email -> m (Response UserRsp)
userLookupByEmail = forall env (m :: * -> *) a.
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
(SlackConfig -> IO a) -> m a
liftNonClassy forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip SlackConfig -> Email -> IO (Response UserRsp)
NonClassy.userLookupByEmail
getUserDesc ::
(Common.UserId -> Text) ->
User.ListRsp ->
(Common.UserId -> Text)
getUserDesc :: (UserId -> Text) -> ListRsp -> UserId -> Text
getUserDesc UserId -> Text
unknownUserFn ListRsp
users =
let userMap :: Map UserId Text
userMap = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall a b. (a -> b) -> a -> b
$ (User -> UserId
User.userId forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& User -> Text
User.userName) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ListRsp -> [User]
User.listRspMembers ListRsp
users
in \UserId
userId -> forall a. a -> Maybe a -> a
fromMaybe (UserId -> Text
unknownUserFn UserId
userId) forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup UserId
userId Map UserId Text
userMap
conversationsHistoryAll ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
Conversation.HistoryReq ->
m (LoadPage m Common.Message)
conversationsHistoryAll :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
HistoryReq -> m (LoadPage m Message)
conversationsHistoryAll = forall (m :: * -> *) req resp.
(MonadIO m, PagedRequest req, PagedResponse resp) =>
(req -> m (Response resp))
-> req -> m (LoadPage m (ResponseObject resp))
fetchAllBy forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
HistoryReq -> m (Response HistoryRsp)
conversationsHistory
repliesFetchAll ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
Conversation.RepliesReq ->
m (LoadPage m Common.Message)
repliesFetchAll :: forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
RepliesReq -> m (LoadPage m Message)
repliesFetchAll = forall (m :: * -> *) req resp.
(MonadIO m, PagedRequest req, PagedResponse resp) =>
(req -> m (Response resp))
-> req -> m (LoadPage m (ResponseObject resp))
fetchAllBy forall env (m :: * -> *).
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
RepliesReq -> m (Response HistoryRsp)
conversationsReplies
liftNonClassy ::
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
(SlackConfig -> IO a) ->
m a
liftNonClassy :: forall env (m :: * -> *) a.
(MonadReader env m, HasManager env, HasToken env, MonadIO m) =>
(SlackConfig -> IO a) -> m a
liftNonClassy SlackConfig -> IO a
f =
forall env (m :: * -> *) a.
(MonadReader env m, MonadIO m) =>
(env -> IO a) -> m a
liftToReader forall a b. (a -> b) -> a -> b
$ \env
env -> SlackConfig -> IO a
f forall a b. (a -> b) -> a -> b
$ Manager -> Text -> SlackConfig
SlackConfig (forall a. HasManager a => a -> Manager
getManager env
env) (forall a. HasToken a => a -> Text
getToken env
env)
liftToReader ::
(MonadReader env m, MonadIO m) =>
(env -> IO a) ->
m a
liftToReader :: forall env (m :: * -> *) a.
(MonadReader env m, MonadIO m) =>
(env -> IO a) -> m a
liftToReader env -> IO a
f = do
env
env <- forall r (m :: * -> *). MonadReader r m => m r
ask
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ env -> IO a
f env
env