-- | User endpoints
module Calamity.HTTP.User (
  UserRequest (..),
  ModifyUserData (..),
  GetCurrentUserGuildsOptions (..),
) where

import Calamity.HTTP.Internal.Request
import Calamity.HTTP.Internal.Route
import Calamity.Internal.AesonThings
import Calamity.Types.Model.Channel
import Calamity.Types.Model.Guild
import Calamity.Types.Model.User
import Calamity.Types.Snowflake

import Control.Lens hiding ((.=))

import Data.Aeson
import Data.Default.Class
import Data.Text (Text)

import GHC.Generics

import Network.HTTP.Req

data ModifyUserData = ModifyUserData
  { ModifyUserData -> Maybe Text
username :: Maybe Text
  , -- | The avatar field should be in discord's image data format: https://discord.com/developers/docs/reference#image-data
    ModifyUserData -> Maybe Text
avatar :: Maybe Text
  }
  deriving (Int -> ModifyUserData -> ShowS
[ModifyUserData] -> ShowS
ModifyUserData -> String
(Int -> ModifyUserData -> ShowS)
-> (ModifyUserData -> String)
-> ([ModifyUserData] -> ShowS)
-> Show ModifyUserData
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ModifyUserData] -> ShowS
$cshowList :: [ModifyUserData] -> ShowS
show :: ModifyUserData -> String
$cshow :: ModifyUserData -> String
showsPrec :: Int -> ModifyUserData -> ShowS
$cshowsPrec :: Int -> ModifyUserData -> ShowS
Show, (forall x. ModifyUserData -> Rep ModifyUserData x)
-> (forall x. Rep ModifyUserData x -> ModifyUserData)
-> Generic ModifyUserData
forall x. Rep ModifyUserData x -> ModifyUserData
forall x. ModifyUserData -> Rep ModifyUserData x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ModifyUserData x -> ModifyUserData
$cfrom :: forall x. ModifyUserData -> Rep ModifyUserData x
Generic, ModifyUserData
ModifyUserData -> Default ModifyUserData
forall a. a -> Default a
def :: ModifyUserData
$cdef :: ModifyUserData
Default)
  deriving ([ModifyUserData] -> Encoding
[ModifyUserData] -> Value
ModifyUserData -> Encoding
ModifyUserData -> Value
(ModifyUserData -> Value)
-> (ModifyUserData -> Encoding)
-> ([ModifyUserData] -> Value)
-> ([ModifyUserData] -> Encoding)
-> ToJSON ModifyUserData
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ModifyUserData] -> Encoding
$ctoEncodingList :: [ModifyUserData] -> Encoding
toJSONList :: [ModifyUserData] -> Value
$ctoJSONList :: [ModifyUserData] -> Value
toEncoding :: ModifyUserData -> Encoding
$ctoEncoding :: ModifyUserData -> Encoding
toJSON :: ModifyUserData -> Value
$ctoJSON :: ModifyUserData -> Value
ToJSON) via CalamityJSON ModifyUserData

data GetCurrentUserGuildsOptions = GetCurrentUserGuildsOptions
  { GetCurrentUserGuildsOptions -> Maybe (Snowflake Guild)
before :: Maybe (Snowflake Guild)
  , GetCurrentUserGuildsOptions -> Maybe (Snowflake Guild)
after :: Maybe (Snowflake Guild)
  , GetCurrentUserGuildsOptions -> Maybe Integer
limit :: Maybe Integer
  }
  deriving (Int -> GetCurrentUserGuildsOptions -> ShowS
[GetCurrentUserGuildsOptions] -> ShowS
GetCurrentUserGuildsOptions -> String
(Int -> GetCurrentUserGuildsOptions -> ShowS)
-> (GetCurrentUserGuildsOptions -> String)
-> ([GetCurrentUserGuildsOptions] -> ShowS)
-> Show GetCurrentUserGuildsOptions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetCurrentUserGuildsOptions] -> ShowS
$cshowList :: [GetCurrentUserGuildsOptions] -> ShowS
show :: GetCurrentUserGuildsOptions -> String
$cshow :: GetCurrentUserGuildsOptions -> String
showsPrec :: Int -> GetCurrentUserGuildsOptions -> ShowS
$cshowsPrec :: Int -> GetCurrentUserGuildsOptions -> ShowS
Show, (forall x.
 GetCurrentUserGuildsOptions -> Rep GetCurrentUserGuildsOptions x)
-> (forall x.
    Rep GetCurrentUserGuildsOptions x -> GetCurrentUserGuildsOptions)
-> Generic GetCurrentUserGuildsOptions
forall x.
Rep GetCurrentUserGuildsOptions x -> GetCurrentUserGuildsOptions
forall x.
GetCurrentUserGuildsOptions -> Rep GetCurrentUserGuildsOptions x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep GetCurrentUserGuildsOptions x -> GetCurrentUserGuildsOptions
$cfrom :: forall x.
GetCurrentUserGuildsOptions -> Rep GetCurrentUserGuildsOptions x
Generic, GetCurrentUserGuildsOptions
GetCurrentUserGuildsOptions -> Default GetCurrentUserGuildsOptions
forall a. a -> Default a
def :: GetCurrentUserGuildsOptions
$cdef :: GetCurrentUserGuildsOptions
Default)

data UserRequest a where
  GetCurrentUser :: UserRequest User
  GetUser :: HasID User u => u -> UserRequest User
  ModifyCurrentUser :: ModifyUserData -> UserRequest User
  GetCurrentUserGuilds :: GetCurrentUserGuildsOptions -> UserRequest [Partial Guild]
  LeaveGuild :: HasID Guild g => g -> UserRequest ()
  CreateDM :: HasID User u => u -> UserRequest DMChannel

baseRoute :: RouteBuilder _
baseRoute :: RouteBuilder '[]
baseRoute = RouteBuilder '[]
mkRouteBuilder RouteBuilder '[] -> S -> ConsRes S '[]
forall a (reqs :: [(RequirementType, RouteRequirement)]).
RouteFragmentable a reqs =>
RouteBuilder reqs -> a -> ConsRes a reqs
// Text -> S
S Text
"users" RouteBuilder '[] -> S -> ConsRes S '[]
forall a (reqs :: [(RequirementType, RouteRequirement)]).
RouteFragmentable a reqs =>
RouteBuilder reqs -> a -> ConsRes a reqs
// Text -> S
S Text
"@me"

instance Request (UserRequest a) where
  type Result (UserRequest a) = a

  route :: UserRequest a -> Route
route UserRequest a
GetCurrentUser =
    RouteBuilder '[]
baseRoute
      RouteBuilder '[] -> (RouteBuilder '[] -> Route) -> Route
forall a b. a -> (a -> b) -> b
& RouteBuilder '[] -> Route
forall (reqs :: [(RequirementType, RouteRequirement)]).
EnsureFulfilled reqs =>
RouteBuilder reqs -> Route
buildRoute
  route (GetUser (forall a. HasID User a => a -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID @User -> Snowflake User
uid)) =
    RouteBuilder '[]
mkRouteBuilder RouteBuilder '[] -> S -> ConsRes S '[]
forall a (reqs :: [(RequirementType, RouteRequirement)]).
RouteFragmentable a reqs =>
RouteBuilder reqs -> a -> ConsRes a reqs
// Text -> S
S Text
"users" RouteBuilder '[] -> ID User -> ConsRes (ID User) '[]
forall a (reqs :: [(RequirementType, RouteRequirement)]).
RouteFragmentable a reqs =>
RouteBuilder reqs -> a -> ConsRes a reqs
// ID User
forall k (a :: k). ID a
ID @User
      RouteBuilder '[ '( 'IDRequirement User, 'Required)]
-> (RouteBuilder '[ '( 'IDRequirement User, 'Required)]
    -> RouteBuilder
         '[ '( 'IDRequirement User, 'Satisfied),
            '( 'IDRequirement User, 'Required)])
-> RouteBuilder
     '[ '( 'IDRequirement User, 'Satisfied),
        '( 'IDRequirement User, 'Required)]
forall a b. a -> (a -> b) -> b
& Snowflake User
-> RouteBuilder '[ '( 'IDRequirement User, 'Required)]
-> RouteBuilder
     '[ '( 'IDRequirement User, 'Satisfied),
        '( 'IDRequirement User, 'Required)]
forall t (reqs :: [(RequirementType, RouteRequirement)]).
Typeable t =>
Snowflake t
-> RouteBuilder reqs
-> RouteBuilder ('( 'IDRequirement t, 'Satisfied) : reqs)
giveID Snowflake User
uid
      RouteBuilder
  '[ '( 'IDRequirement User, 'Satisfied),
     '( 'IDRequirement User, 'Required)]
-> (RouteBuilder
      '[ '( 'IDRequirement User, 'Satisfied),
         '( 'IDRequirement User, 'Required)]
    -> Route)
-> Route
forall a b. a -> (a -> b) -> b
& RouteBuilder
  '[ '( 'IDRequirement User, 'Satisfied),
     '( 'IDRequirement User, 'Required)]
-> Route
forall (reqs :: [(RequirementType, RouteRequirement)]).
EnsureFulfilled reqs =>
RouteBuilder reqs -> Route
buildRoute
  route (ModifyCurrentUser ModifyUserData
_) =
    RouteBuilder '[]
baseRoute
      RouteBuilder '[] -> (RouteBuilder '[] -> Route) -> Route
forall a b. a -> (a -> b) -> b
& RouteBuilder '[] -> Route
forall (reqs :: [(RequirementType, RouteRequirement)]).
EnsureFulfilled reqs =>
RouteBuilder reqs -> Route
buildRoute
  route (GetCurrentUserGuilds GetCurrentUserGuildsOptions
_) =
    RouteBuilder '[]
baseRoute RouteBuilder '[] -> S -> ConsRes S '[]
forall a (reqs :: [(RequirementType, RouteRequirement)]).
RouteFragmentable a reqs =>
RouteBuilder reqs -> a -> ConsRes a reqs
// Text -> S
S Text
"guilds"
      RouteBuilder '[] -> (RouteBuilder '[] -> Route) -> Route
forall a b. a -> (a -> b) -> b
& RouteBuilder '[] -> Route
forall (reqs :: [(RequirementType, RouteRequirement)]).
EnsureFulfilled reqs =>
RouteBuilder reqs -> Route
buildRoute
  route (LeaveGuild (forall a. HasID Guild a => a -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID @Guild -> Snowflake Guild
gid)) =
    RouteBuilder '[]
baseRoute RouteBuilder '[] -> S -> ConsRes S '[]
forall a (reqs :: [(RequirementType, RouteRequirement)]).
RouteFragmentable a reqs =>
RouteBuilder reqs -> a -> ConsRes a reqs
// Text -> S
S Text
"guilds" RouteBuilder '[] -> ID Guild -> ConsRes (ID Guild) '[]
forall a (reqs :: [(RequirementType, RouteRequirement)]).
RouteFragmentable a reqs =>
RouteBuilder reqs -> a -> ConsRes a reqs
// ID Guild
forall k (a :: k). ID a
ID @Guild
      RouteBuilder '[ '( 'IDRequirement Guild, 'Required)]
-> (RouteBuilder '[ '( 'IDRequirement Guild, 'Required)]
    -> RouteBuilder
         '[ '( 'IDRequirement Guild, 'Satisfied),
            '( 'IDRequirement Guild, 'Required)])
-> RouteBuilder
     '[ '( 'IDRequirement Guild, 'Satisfied),
        '( 'IDRequirement Guild, 'Required)]
forall a b. a -> (a -> b) -> b
& Snowflake Guild
-> RouteBuilder '[ '( 'IDRequirement Guild, 'Required)]
-> RouteBuilder
     '[ '( 'IDRequirement Guild, 'Satisfied),
        '( 'IDRequirement Guild, 'Required)]
forall t (reqs :: [(RequirementType, RouteRequirement)]).
Typeable t =>
Snowflake t
-> RouteBuilder reqs
-> RouteBuilder ('( 'IDRequirement t, 'Satisfied) : reqs)
giveID Snowflake Guild
gid
      RouteBuilder
  '[ '( 'IDRequirement Guild, 'Satisfied),
     '( 'IDRequirement Guild, 'Required)]
-> (RouteBuilder
      '[ '( 'IDRequirement Guild, 'Satisfied),
         '( 'IDRequirement Guild, 'Required)]
    -> Route)
-> Route
forall a b. a -> (a -> b) -> b
& RouteBuilder
  '[ '( 'IDRequirement Guild, 'Satisfied),
     '( 'IDRequirement Guild, 'Required)]
-> Route
forall (reqs :: [(RequirementType, RouteRequirement)]).
EnsureFulfilled reqs =>
RouteBuilder reqs -> Route
buildRoute
  route (CreateDM u
_) =
    RouteBuilder '[]
baseRoute RouteBuilder '[] -> S -> ConsRes S '[]
forall a (reqs :: [(RequirementType, RouteRequirement)]).
RouteFragmentable a reqs =>
RouteBuilder reqs -> a -> ConsRes a reqs
// Text -> S
S Text
"channels"
      RouteBuilder '[] -> (RouteBuilder '[] -> Route) -> Route
forall a b. a -> (a -> b) -> b
& RouteBuilder '[] -> Route
forall (reqs :: [(RequirementType, RouteRequirement)]).
EnsureFulfilled reqs =>
RouteBuilder reqs -> Route
buildRoute

  action :: UserRequest a -> Url 'Https -> Option 'Https -> Req LbsResponse
action UserRequest a
GetCurrentUser = Url 'Https -> Option 'Https -> Req LbsResponse
getWith
  action (GetUser u
_) = Url 'Https -> Option 'Https -> Req LbsResponse
getWith
  action (ModifyCurrentUser ModifyUserData
o) = ReqBodyJson ModifyUserData
-> Url 'Https -> Option 'Https -> Req LbsResponse
forall a.
HttpBody a =>
a -> Url 'Https -> Option 'Https -> Req LbsResponse
patchWith' (ReqBodyJson ModifyUserData
 -> Url 'Https -> Option 'Https -> Req LbsResponse)
-> ReqBodyJson ModifyUserData
-> Url 'Https
-> Option 'Https
-> Req LbsResponse
forall a b. (a -> b) -> a -> b
$ ModifyUserData -> ReqBodyJson ModifyUserData
forall a. a -> ReqBodyJson a
ReqBodyJson ModifyUserData
o
  action (GetCurrentUserGuilds GetCurrentUserGuildsOptions{Maybe (Snowflake Guild)
before :: Maybe (Snowflake Guild)
$sel:before:GetCurrentUserGuildsOptions :: GetCurrentUserGuildsOptions -> Maybe (Snowflake Guild)
before, Maybe (Snowflake Guild)
after :: Maybe (Snowflake Guild)
$sel:after:GetCurrentUserGuildsOptions :: GetCurrentUserGuildsOptions -> Maybe (Snowflake Guild)
after, Maybe Integer
limit :: Maybe Integer
$sel:limit:GetCurrentUserGuildsOptions :: GetCurrentUserGuildsOptions -> Maybe Integer
limit}) =
    Option 'Https -> Url 'Https -> Option 'Https -> Req LbsResponse
getWithP
      ( Text
"before" Text -> Maybe Word64 -> Option 'Https
forall a. ToHttpApiData a => Text -> Maybe a -> Option 'Https
=:? (Snowflake Guild -> Word64
forall t. Snowflake t -> Word64
fromSnowflake (Snowflake Guild -> Word64)
-> Maybe (Snowflake Guild) -> Maybe Word64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Snowflake Guild)
before) Option 'Https -> Option 'Https -> Option 'Https
forall a. Semigroup a => a -> a -> a
<> Text
"after" Text -> Maybe Word64 -> Option 'Https
forall a. ToHttpApiData a => Text -> Maybe a -> Option 'Https
=:? (Snowflake Guild -> Word64
forall t. Snowflake t -> Word64
fromSnowflake (Snowflake Guild -> Word64)
-> Maybe (Snowflake Guild) -> Maybe Word64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Snowflake Guild)
after)
          Option 'Https -> Option 'Https -> Option 'Https
forall a. Semigroup a => a -> a -> a
<> Text
"limit" Text -> Maybe Integer -> Option 'Https
forall a. ToHttpApiData a => Text -> Maybe a -> Option 'Https
=:? Maybe Integer
limit
      )
  action (LeaveGuild g
_) = Url 'Https -> Option 'Https -> Req LbsResponse
deleteWith
  action (CreateDM (forall a. HasID User a => a -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID @User -> Snowflake User
uid)) = ReqBodyJson Value -> Url 'Https -> Option 'Https -> Req LbsResponse
forall a.
HttpBody a =>
a -> Url 'Https -> Option 'Https -> Req LbsResponse
postWith' (ReqBodyJson Value
 -> Url 'Https -> Option 'Https -> Req LbsResponse)
-> ReqBodyJson Value
-> Url 'Https
-> Option 'Https
-> Req LbsResponse
forall a b. (a -> b) -> a -> b
$ Value -> ReqBodyJson Value
forall a. a -> ReqBodyJson a
ReqBodyJson ([Pair] -> Value
object [Key
"recipient_id" Key -> Snowflake User -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Snowflake User
uid])