{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TemplateHaskell #-}

----------------------------------------------------------------------

----------------------------------------------------------------------

-- |
-- Module: Web.Slack.Common
-- Description:
module Web.Slack.Common
  ( Color (..),
    UserId (..),
    ConversationId (..),
    TeamId (..),
    Cursor (..),
    SlackTimestamp (..),
    mkSlackTimestamp,
    timestampFromText,
    Message (..),
    MessageType (..),
    SlackClientError (..),
    SlackMessageText (..),
  )
where

-- FIXME: Web.Slack.Prelude

-- aeson

-- base

-- deepseq
import Control.DeepSeq (NFData)
import Control.Exception
import Data.Aeson
import Data.Aeson.TH
-- servant-client

-- slack-web

-- text
import Data.Text (Text)
import GHC.Generics (Generic)
import Servant.Client
import Web.Slack.Types
import Web.Slack.Util
import Prelude

#if !MIN_VERSION_servant(0,16,0)
type ClientError = ServantError
#endif

data MessageType = MessageTypeMessage
  deriving stock (MessageType -> MessageType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MessageType -> MessageType -> Bool
$c/= :: MessageType -> MessageType -> Bool
== :: MessageType -> MessageType -> Bool
$c== :: MessageType -> MessageType -> Bool
Eq, Int -> MessageType -> ShowS
[MessageType] -> ShowS
MessageType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MessageType] -> ShowS
$cshowList :: [MessageType] -> ShowS
show :: MessageType -> String
$cshow :: MessageType -> String
showsPrec :: Int -> MessageType -> ShowS
$cshowsPrec :: Int -> MessageType -> ShowS
Show, forall x. Rep MessageType x -> MessageType
forall x. MessageType -> Rep MessageType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep MessageType x -> MessageType
$cfrom :: forall x. MessageType -> Rep MessageType x
Generic)

instance NFData MessageType

instance FromJSON MessageType where
  parseJSON :: Value -> Parser MessageType
parseJSON Value
"message" = forall (f :: * -> *) a. Applicative f => a -> f a
pure MessageType
MessageTypeMessage
  parseJSON Value
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid MessageType"

instance ToJSON MessageType where
  toJSON :: MessageType -> Value
toJSON MessageType
_ = Text -> Value
String Text
"message"

data Message = Message
  { Message -> MessageType
messageType :: MessageType
  , Message -> Maybe UserId
messageUser :: Maybe UserId
  -- ^ not present for bot messages at least
  , Message -> SlackMessageText
messageText :: SlackMessageText
  -- ^ the message text is in a markdown-like slack-specific format.
  -- Use 'Web.Slack.MessageParser.messageToHtml' to convert it to HTML.
  , Message -> SlackTimestamp
messageTs :: SlackTimestamp
  }
  deriving stock (Message -> Message -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Message -> Message -> Bool
$c/= :: Message -> Message -> Bool
== :: Message -> Message -> Bool
$c== :: Message -> Message -> Bool
Eq, forall x. Rep Message x -> Message
forall x. Message -> Rep Message x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Message x -> Message
$cfrom :: forall x. Message -> Rep Message x
Generic, Int -> Message -> ShowS
[Message] -> ShowS
Message -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Message] -> ShowS
$cshowList :: [Message] -> ShowS
show :: Message -> String
$cshow :: Message -> String
showsPrec :: Int -> Message -> ShowS
$cshowsPrec :: Int -> Message -> ShowS
Show)

instance NFData Message

$(deriveJSON (jsonOpts "message") ''Message)

-- |
-- Errors that can be triggered by a slack request.
data SlackClientError
  = -- | errors from the network connection
    ServantError ClientError
  | -- | errors returned by the slack API
    SlackError Text
  deriving stock (SlackClientError -> SlackClientError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SlackClientError -> SlackClientError -> Bool
$c/= :: SlackClientError -> SlackClientError -> Bool
== :: SlackClientError -> SlackClientError -> Bool
$c== :: SlackClientError -> SlackClientError -> Bool
Eq, forall x. Rep SlackClientError x -> SlackClientError
forall x. SlackClientError -> Rep SlackClientError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SlackClientError x -> SlackClientError
$cfrom :: forall x. SlackClientError -> Rep SlackClientError x
Generic, Int -> SlackClientError -> ShowS
[SlackClientError] -> ShowS
SlackClientError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SlackClientError] -> ShowS
$cshowList :: [SlackClientError] -> ShowS
show :: SlackClientError -> String
$cshow :: SlackClientError -> String
showsPrec :: Int -> SlackClientError -> ShowS
$cshowsPrec :: Int -> SlackClientError -> ShowS
Show)

instance NFData SlackClientError

instance Exception SlackClientError