{-# LANGUAGE TemplateHaskell #-}
module Calamity.Types.Model.User (
User (..),
UserBanner (..),
Partial (PartialUser),
StatusType (..),
) where
import Calamity.Internal.IntColour
import Calamity.Types.CDNAsset (CDNAsset (..))
import Calamity.Types.Model.Avatar
import {-# SOURCE #-} Calamity.Types.Model.Guild.Member
import Calamity.Types.Partial
import Calamity.Types.Snowflake
import Calamity.Utils.CDNUrl (assetHashFile, cdnURL)
import Data.Aeson ((.:), (.:?))
import qualified Data.Aeson as Aeson
import Data.Colour (Colour)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Read (decimal)
import Data.Word
import Network.HTTP.Req ((/:), (/~))
import Optics.TH
import qualified TextShow
import TextShow.TH
data UserBanner = UserBanner
{ UserBanner -> Snowflake User
userID :: Snowflake User
, UserBanner -> Text
hash :: T.Text
}
deriving (Int -> UserBanner -> ShowS
[UserBanner] -> ShowS
UserBanner -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UserBanner] -> ShowS
$cshowList :: [UserBanner] -> ShowS
show :: UserBanner -> String
$cshow :: UserBanner -> String
showsPrec :: Int -> UserBanner -> ShowS
$cshowsPrec :: Int -> UserBanner -> ShowS
Show, UserBanner -> UserBanner -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UserBanner -> UserBanner -> Bool
$c/= :: UserBanner -> UserBanner -> Bool
== :: UserBanner -> UserBanner -> Bool
$c== :: UserBanner -> UserBanner -> Bool
Eq)
instance CDNAsset UserBanner where
assetURL :: UserBanner -> Url 'Https
assetURL UserBanner {Text
hash :: Text
$sel:hash:UserBanner :: UserBanner -> Text
hash, Snowflake User
userID :: Snowflake User
$sel:userID:UserBanner :: UserBanner -> Snowflake User
userID} =
Url 'Https
cdnURL forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"banners" forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ Snowflake User
userID forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text -> Text
assetHashFile Text
hash
data User = User
{ User -> Snowflake User
id :: Snowflake User
, User -> Text
username :: Text
, User -> Text
discriminator :: Text
, User -> Maybe Bool
bot :: Maybe Bool
, User -> Avatar
avatar :: Avatar
, User -> Maybe Bool
mfaEnabled :: Maybe Bool
, User -> Maybe UserBanner
banner :: Maybe UserBanner
, User -> Maybe (Colour Double)
accentColour :: Maybe (Colour Double)
, User -> Maybe Text
locale :: Maybe Text
, User -> Maybe Bool
verified :: Maybe Bool
, User -> Maybe Text
email :: Maybe Text
, User -> Maybe Word64
flags :: Maybe Word64
, User -> Maybe Word64
premiumType :: Maybe Word64
}
deriving (Int -> User -> ShowS
[User] -> ShowS
User -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [User] -> ShowS
$cshowList :: [User] -> ShowS
show :: User -> String
$cshow :: User -> String
showsPrec :: Int -> User -> ShowS
$cshowsPrec :: Int -> User -> ShowS
Show, User -> User -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: User -> User -> Bool
$c/= :: User -> User -> Bool
== :: User -> User -> Bool
$c== :: User -> User -> Bool
Eq)
deriving (Int -> User -> Builder
Int -> User -> Text
Int -> User -> Text
[User] -> Builder
[User] -> Text
[User] -> Text
User -> Builder
User -> Text
User -> Text
forall a.
(Int -> a -> Builder)
-> (a -> Builder)
-> ([a] -> Builder)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> TextShow a
showtlList :: [User] -> Text
$cshowtlList :: [User] -> Text
showtl :: User -> Text
$cshowtl :: User -> Text
showtlPrec :: Int -> User -> Text
$cshowtlPrec :: Int -> User -> Text
showtList :: [User] -> Text
$cshowtList :: [User] -> Text
showt :: User -> Text
$cshowt :: User -> Text
showtPrec :: Int -> User -> Text
$cshowtPrec :: Int -> User -> Text
showbList :: [User] -> Builder
$cshowbList :: [User] -> Builder
showb :: User -> Builder
$cshowb :: User -> Builder
showbPrec :: Int -> User -> Builder
$cshowbPrec :: Int -> User -> Builder
TextShow.TextShow) via TextShow.FromStringShow User
deriving (HasID User) via HasIDField "id" User
deriving (HasID Member) via HasIDFieldCoerce' "id" User
instance Aeson.FromJSON User where
parseJSON :: Value -> Parser User
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"User" forall a b. (a -> b) -> a -> b
$ \Object
v -> do
Snowflake User
uid <- Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
Maybe Text
avatarHash <- Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"avatar"
Text
discrim <- Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"discriminator"
Int
discrim' <- case forall a. Integral a => Reader a
decimal Text
discrim of
Right (Int
n, Text
_) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
n
Left String
e -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
e
let avatar :: Avatar
avatar = Maybe Text -> Snowflake User -> Int -> Avatar
Avatar Maybe Text
avatarHash Snowflake User
uid Int
discrim'
Maybe UserBanner
banner <- (Snowflake User -> Text -> UserBanner
UserBanner Snowflake User
uid forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"banner"
Snowflake User
-> Text
-> Text
-> Maybe Bool
-> Avatar
-> Maybe Bool
-> Maybe UserBanner
-> Maybe (Colour Double)
-> Maybe Text
-> Maybe Bool
-> Maybe Text
-> Maybe Word64
-> Maybe Word64
-> User
User
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Applicative f => a -> f a
pure Snowflake User
uid
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"username"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"discriminator"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"bot"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => a -> f a
pure Avatar
avatar
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"mfa_enabled"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe UserBanner
banner
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap IntColour -> Colour Double
fromIntColour forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"accent_color")
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"locale"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"verified"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"email"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"flags"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"premium_type"
newtype instance Partial User = PartialUser
{ Partial User -> Snowflake User
id :: Snowflake User
}
deriving (Int -> Partial User -> ShowS
[Partial User] -> ShowS
Partial User -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Partial User] -> ShowS
$cshowList :: [Partial User] -> ShowS
show :: Partial User -> String
$cshow :: Partial User -> String
showsPrec :: Int -> Partial User -> ShowS
$cshowsPrec :: Int -> Partial User -> ShowS
Show, Partial User -> Partial User -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Partial User -> Partial User -> Bool
$c/= :: Partial User -> Partial User -> Bool
== :: Partial User -> Partial User -> Bool
$c== :: Partial User -> Partial User -> Bool
Eq)
deriving (HasID User) via HasIDField "id" (Partial User)
instance Aeson.FromJSON (Partial User) where
parseJSON :: Value -> Parser (Partial User)
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Partial User" forall a b. (a -> b) -> a -> b
$ \Object
v ->
Snowflake User -> Partial User
PartialUser forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
data StatusType
= Idle
| DND
| Online
| Offline
| Invisible
deriving (StatusType -> StatusType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StatusType -> StatusType -> Bool
$c/= :: StatusType -> StatusType -> Bool
== :: StatusType -> StatusType -> Bool
$c== :: StatusType -> StatusType -> Bool
Eq, Int -> StatusType -> ShowS
[StatusType] -> ShowS
StatusType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StatusType] -> ShowS
$cshowList :: [StatusType] -> ShowS
show :: StatusType -> String
$cshow :: StatusType -> String
showsPrec :: Int -> StatusType -> ShowS
$cshowsPrec :: Int -> StatusType -> ShowS
Show, Int -> StatusType
StatusType -> Int
StatusType -> [StatusType]
StatusType -> StatusType
StatusType -> StatusType -> [StatusType]
StatusType -> StatusType -> StatusType -> [StatusType]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: StatusType -> StatusType -> StatusType -> [StatusType]
$cenumFromThenTo :: StatusType -> StatusType -> StatusType -> [StatusType]
enumFromTo :: StatusType -> StatusType -> [StatusType]
$cenumFromTo :: StatusType -> StatusType -> [StatusType]
enumFromThen :: StatusType -> StatusType -> [StatusType]
$cenumFromThen :: StatusType -> StatusType -> [StatusType]
enumFrom :: StatusType -> [StatusType]
$cenumFrom :: StatusType -> [StatusType]
fromEnum :: StatusType -> Int
$cfromEnum :: StatusType -> Int
toEnum :: Int -> StatusType
$ctoEnum :: Int -> StatusType
pred :: StatusType -> StatusType
$cpred :: StatusType -> StatusType
succ :: StatusType -> StatusType
$csucc :: StatusType -> StatusType
Enum)
instance Aeson.FromJSON StatusType where
parseJSON :: Value -> Parser StatusType
parseJSON = forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"StatusType" forall a b. (a -> b) -> a -> b
$ \case
Text
"idle" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure StatusType
Idle
Text
"dnd" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure StatusType
DND
Text
"online" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure StatusType
Online
Text
"offline" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure StatusType
Offline
Text
"invisible" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure StatusType
Invisible
Text
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown status type"
instance Aeson.ToJSON StatusType where
toJSON :: StatusType -> Value
toJSON =
forall a. ToJSON a => a -> Value
Aeson.toJSON @Text forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
StatusType
Idle -> Text
"idle"
StatusType
DND -> Text
"dnd"
StatusType
Online -> Text
"online"
StatusType
Offline -> Text
"offline"
StatusType
Invisible -> Text
"invisible"
$(deriveTextShow 'PartialUser)
$(deriveTextShow ''StatusType)
$(makeFieldLabelsNoPrefix ''User)
$(makeFieldLabelsNoPrefix 'PartialUser)
$(makeFieldLabelsNoPrefix ''StatusType)