{-# LANGUAGE TemplateHaskell #-}
module Calamity.Types.Model.Channel.Webhook (Webhook (..)) where
import {-# SOURCE #-} Calamity.Types.Model.Channel
import {-# SOURCE #-} Calamity.Types.Model.Guild.Guild
import Calamity.Types.Model.User
import Calamity.Types.Snowflake
import Data.Aeson ((.:), (.:?))
import Data.Aeson qualified as Aeson
import Data.Text (Text)
import Optics.TH
import TextShow.TH
data Webhook = Webhook
{ Webhook -> Snowflake Webhook
id :: Snowflake Webhook
, Webhook -> Integer
type_ :: Integer
, Webhook -> Maybe (Snowflake Guild)
guildID :: Maybe (Snowflake Guild)
, Webhook -> Maybe (Snowflake Channel)
channelID :: Maybe (Snowflake Channel)
, Webhook -> Maybe (Snowflake User)
user :: Maybe (Snowflake User)
, Webhook -> Text
name :: Text
, Webhook -> Text
avatar :: Text
, Webhook -> Maybe Text
token :: Maybe Text
}
deriving (Webhook -> Webhook -> Bool
(Webhook -> Webhook -> Bool)
-> (Webhook -> Webhook -> Bool) -> Eq Webhook
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Webhook -> Webhook -> Bool
== :: Webhook -> Webhook -> Bool
$c/= :: Webhook -> Webhook -> Bool
/= :: Webhook -> Webhook -> Bool
Eq, Int -> Webhook -> ShowS
[Webhook] -> ShowS
Webhook -> String
(Int -> Webhook -> ShowS)
-> (Webhook -> String) -> ([Webhook] -> ShowS) -> Show Webhook
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Webhook -> ShowS
showsPrec :: Int -> Webhook -> ShowS
$cshow :: Webhook -> String
show :: Webhook -> String
$cshowList :: [Webhook] -> ShowS
showList :: [Webhook] -> ShowS
Show)
deriving (HasID Webhook) via HasIDField "id" Webhook
instance Aeson.FromJSON Webhook where
parseJSON :: Value -> Parser Webhook
parseJSON = String -> (Object -> Parser Webhook) -> Value -> Parser Webhook
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Webhook" ((Object -> Parser Webhook) -> Value -> Parser Webhook)
-> (Object -> Parser Webhook) -> Value -> Parser Webhook
forall a b. (a -> b) -> a -> b
$ \Object
v -> do
Maybe Object
user <- Object
v Object -> Key -> Parser (Maybe Object)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"user"
Maybe (Snowflake User)
userID <- (Object -> Parser (Snowflake User))
-> Maybe Object -> Parser (Maybe (Snowflake User))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse (Object -> Key -> Parser (Snowflake User)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id") Maybe Object
user
Snowflake Webhook
-> Integer
-> Maybe (Snowflake Guild)
-> Maybe (Snowflake Channel)
-> Maybe (Snowflake User)
-> Text
-> Text
-> Maybe Text
-> Webhook
Webhook
(Snowflake Webhook
-> Integer
-> Maybe (Snowflake Guild)
-> Maybe (Snowflake Channel)
-> Maybe (Snowflake User)
-> Text
-> Text
-> Maybe Text
-> Webhook)
-> Parser (Snowflake Webhook)
-> Parser
(Integer
-> Maybe (Snowflake Guild)
-> Maybe (Snowflake Channel)
-> Maybe (Snowflake User)
-> Text
-> Text
-> Maybe Text
-> Webhook)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser (Snowflake Webhook)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
Parser
(Integer
-> Maybe (Snowflake Guild)
-> Maybe (Snowflake Channel)
-> Maybe (Snowflake User)
-> Text
-> Text
-> Maybe Text
-> Webhook)
-> Parser Integer
-> Parser
(Maybe (Snowflake Guild)
-> Maybe (Snowflake Channel)
-> Maybe (Snowflake User)
-> Text
-> Text
-> Maybe Text
-> Webhook)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Integer
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type"
Parser
(Maybe (Snowflake Guild)
-> Maybe (Snowflake Channel)
-> Maybe (Snowflake User)
-> Text
-> Text
-> Maybe Text
-> Webhook)
-> Parser (Maybe (Snowflake Guild))
-> Parser
(Maybe (Snowflake Channel)
-> Maybe (Snowflake User) -> Text -> Text -> Maybe Text -> Webhook)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe (Snowflake Guild))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"guild_id"
Parser
(Maybe (Snowflake Channel)
-> Maybe (Snowflake User) -> Text -> Text -> Maybe Text -> Webhook)
-> Parser (Maybe (Snowflake Channel))
-> Parser
(Maybe (Snowflake User) -> Text -> Text -> Maybe Text -> Webhook)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe (Snowflake Channel))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"channel_id"
Parser
(Maybe (Snowflake User) -> Text -> Text -> Maybe Text -> Webhook)
-> Parser (Maybe (Snowflake User))
-> Parser (Text -> Text -> Maybe Text -> Webhook)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe (Snowflake User) -> Parser (Maybe (Snowflake User))
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Snowflake User)
userID
Parser (Text -> Text -> Maybe Text -> Webhook)
-> Parser Text -> Parser (Text -> Maybe Text -> Webhook)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
Parser (Text -> Maybe Text -> Webhook)
-> Parser Text -> Parser (Maybe Text -> Webhook)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"avatar"
Parser (Maybe Text -> Webhook)
-> Parser (Maybe Text) -> Parser Webhook
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"token"
$(deriveTextShow ''Webhook)
$(makeFieldLabelsNoPrefix ''Webhook)