-- | Permission utilities
module Calamity.Utils.Permissions
    ( basePermissions
    , applyOverwrites
    , PermissionsIn(..)
    , PermissionsIn'(..) ) where

import           Calamity.Client.Types
import           Calamity.Types.Model.Channel.Guild
import           Calamity.Types.Model.Guild.Guild
import           Calamity.Types.Model.Guild.Member
import           Calamity.Types.Model.Guild.Overwrite
import           Calamity.Types.Model.Guild.Permissions
import           Calamity.Types.Model.User
import           Calamity.Types.Snowflake
import           Calamity.Types.Upgradeable

import           Control.Lens

import           Data.Maybe (catMaybes)
import           Data.Flags
import qualified Data.Vector.Unboxing                   as V
import           Data.Generics.Product.Fields

import qualified Polysemy                               as P
import Data.Foldable (Foldable(foldl'))

-- | Calculate a 'Member'\'s 'Permissions' in a 'Guild'
basePermissions :: Guild -> Member -> Permissions
basePermissions :: Guild -> Member -> Permissions
basePermissions Guild
g Member
m
  | (Guild
g Guild
-> Getting (Snowflake User) Guild (Snowflake User)
-> Snowflake User
forall s a. s -> Getting a s a -> a
^. IsLabel "ownerID" (Getting (Snowflake User) Guild (Snowflake User))
Getting (Snowflake User) Guild (Snowflake User)
#ownerID Snowflake User -> Snowflake User -> Bool
forall a. Eq a => a -> a -> Bool
== Member -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID Member
m) = Permissions
forall α. BoundedFlags α => α
allFlags
  | Bool
otherwise = let everyoneRole :: Maybe Role
everyoneRole  = Guild
g Guild -> Getting (Maybe Role) Guild (Maybe Role) -> Maybe Role
forall s a. s -> Getting a s a -> a
^. IsLabel
  "roles"
  ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
   -> Guild -> Const (Maybe Role) Guild)
(SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Guild -> Const (Maybe Role) Guild
#roles ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
 -> Guild -> Const (Maybe Role) Guild)
-> ((Maybe Role -> Const (Maybe Role) (Maybe Role))
    -> SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Getting (Maybe Role) Guild (Maybe Role)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Role)
-> Lens' (SnowflakeMap Role) (Maybe (IxValue (SnowflakeMap Role)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Snowflake Guild -> Snowflake Role
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Guild -> Snowflake Role)
-> Snowflake Guild -> Snowflake Role
forall a b. (a -> b) -> a -> b
$ Guild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID @Guild Guild
g)
                    permsEveryone :: Permissions
permsEveryone = Permissions -> (Role -> Permissions) -> Maybe Role -> Permissions
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Permissions
forall α. Flags α => α
noFlags (Role -> Getting Permissions Role Permissions -> Permissions
forall s a. s -> Getting a s a -> a
^. IsLabel "permissions" (Getting Permissions Role Permissions)
Getting Permissions Role Permissions
#permissions) Maybe Role
everyoneRole
                    roleIDs :: [Snowflake Role]
roleIDs       = Vector (Snowflake Role) -> [Snowflake Role]
forall a. Unboxable a => Vector a -> [a]
V.toList (Vector (Snowflake Role) -> [Snowflake Role])
-> Vector (Snowflake Role) -> [Snowflake Role]
forall a b. (a -> b) -> a -> b
$ Member
m Member
-> Getting
     (Vector (Snowflake Role)) Member (Vector (Snowflake Role))
-> Vector (Snowflake Role)
forall s a. s -> Getting a s a -> a
^. IsLabel
  "roles"
  (Getting
     (Vector (Snowflake Role)) Member (Vector (Snowflake Role)))
Getting (Vector (Snowflake Role)) Member (Vector (Snowflake Role))
#roles
                    rolePerms :: [Permissions]
rolePerms     = [Maybe Permissions] -> [Permissions]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Permissions] -> [Permissions])
-> [Maybe Permissions] -> [Permissions]
forall a b. (a -> b) -> a -> b
$ (Snowflake Role -> Maybe Permissions)
-> [Snowflake Role] -> [Maybe Permissions]
forall a b. (a -> b) -> [a] -> [b]
map (\Snowflake Role
rid -> Guild
g Guild
-> Getting (First Permissions) Guild Permissions
-> Maybe Permissions
forall s a. s -> Getting (First a) s a -> Maybe a
^? IsLabel
  "roles"
  ((SnowflakeMap Role
    -> Const (First Permissions) (SnowflakeMap Role))
   -> Guild -> Const (First Permissions) Guild)
(SnowflakeMap Role
 -> Const (First Permissions) (SnowflakeMap Role))
-> Guild -> Const (First Permissions) Guild
#roles ((SnowflakeMap Role
  -> Const (First Permissions) (SnowflakeMap Role))
 -> Guild -> Const (First Permissions) Guild)
-> ((Permissions -> Const (First Permissions) Permissions)
    -> SnowflakeMap Role
    -> Const (First Permissions) (SnowflakeMap Role))
-> Getting (First Permissions) Guild Permissions
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Role)
-> Traversal' (SnowflakeMap Role) (IxValue (SnowflakeMap Role))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (SnowflakeMap Role)
Snowflake Role
rid ((Role -> Const (First Permissions) Role)
 -> SnowflakeMap Role
 -> Const (First Permissions) (SnowflakeMap Role))
-> ((Permissions -> Const (First Permissions) Permissions)
    -> Role -> Const (First Permissions) Role)
-> (Permissions -> Const (First Permissions) Permissions)
-> SnowflakeMap Role
-> Const (First Permissions) (SnowflakeMap Role)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IsLabel
  "permissions"
  ((Permissions -> Const (First Permissions) Permissions)
   -> Role -> Const (First Permissions) Role)
(Permissions -> Const (First Permissions) Permissions)
-> Role -> Const (First Permissions) Role
#permissions) [Snowflake Role]
roleIDs
                    perms :: Permissions
perms         = (Permissions -> Permissions -> Permissions)
-> Permissions -> [Permissions] -> Permissions
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Permissions -> Permissions -> Permissions
forall α. Flags α => α -> α -> α
andFlags Permissions
forall α. Flags α => α
noFlags (Permissions
permsEveryonePermissions -> [Permissions] -> [Permissions]
forall a. a -> [a] -> [a]
:[Permissions]
rolePerms)
                in if Permissions
perms Permissions -> Permissions -> Bool
forall α. Flags α => α -> α -> Bool
.<=. Permissions
administrator
                   then Permissions
forall α. BoundedFlags α => α
allFlags
                   else Permissions
perms

-- | Apply any 'Overwrite's for a 'GuildChannel' onto some 'Permissions'
applyOverwrites :: GuildChannel -> Member -> Permissions -> Permissions
applyOverwrites :: GuildChannel -> Member -> Permissions -> Permissions
applyOverwrites GuildChannel
c Member
m Permissions
p
  | Permissions
p Permissions -> Permissions -> Bool
forall α. Flags α => α -> α -> Bool
.<=. Permissions
administrator = Permissions
forall α. BoundedFlags α => α
allFlags
  | Bool
otherwise =
    let everyoneOverwrite :: Maybe Overwrite
everyoneOverwrite = GuildChannel
c GuildChannel
-> Getting (Maybe Overwrite) GuildChannel (Maybe Overwrite)
-> Maybe Overwrite
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' "permissionOverwrites" s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @"permissionOverwrites" ((SnowflakeMap Overwrite
  -> Const (Maybe Overwrite) (SnowflakeMap Overwrite))
 -> GuildChannel -> Const (Maybe Overwrite) GuildChannel)
-> ((Maybe Overwrite -> Const (Maybe Overwrite) (Maybe Overwrite))
    -> SnowflakeMap Overwrite
    -> Const (Maybe Overwrite) (SnowflakeMap Overwrite))
-> Getting (Maybe Overwrite) GuildChannel (Maybe Overwrite)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Overwrite)
-> Lens'
     (SnowflakeMap Overwrite) (Maybe (IxValue (SnowflakeMap Overwrite)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Snowflake Guild -> Snowflake Overwrite
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Guild -> Snowflake Overwrite)
-> Snowflake Guild -> Snowflake Overwrite
forall a b. (a -> b) -> a -> b
$ GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID @Guild GuildChannel
c)
        everyoneAllow :: Permissions
everyoneAllow     = Permissions
-> (Overwrite -> Permissions) -> Maybe Overwrite -> Permissions
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Permissions
forall α. Flags α => α
noFlags (Overwrite
-> Getting Permissions Overwrite Permissions -> Permissions
forall s a. s -> Getting a s a -> a
^. IsLabel "allow" (Getting Permissions Overwrite Permissions)
Getting Permissions Overwrite Permissions
#allow) Maybe Overwrite
everyoneOverwrite
        everyoneDeny :: Permissions
everyoneDeny      = Permissions
-> (Overwrite -> Permissions) -> Maybe Overwrite -> Permissions
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Permissions
forall α. Flags α => α
noFlags (Overwrite
-> Getting Permissions Overwrite Permissions -> Permissions
forall s a. s -> Getting a s a -> a
^. IsLabel "deny" (Getting Permissions Overwrite Permissions)
Getting Permissions Overwrite Permissions
#deny) Maybe Overwrite
everyoneOverwrite
        p' :: Permissions
p'                = Permissions
p Permissions -> Permissions -> Permissions
forall α. Flags α => α -> α -> α
.-. Permissions
everyoneDeny Permissions -> Permissions -> Permissions
forall α. Flags α => α -> α -> α
.+. Permissions
everyoneAllow
        roleOverwriteIDs :: [Snowflake Overwrite]
roleOverwriteIDs  = (Snowflake Role -> Snowflake Overwrite)
-> [Snowflake Role] -> [Snowflake Overwrite]
forall a b. (a -> b) -> [a] -> [b]
map (Snowflake Role -> Snowflake Overwrite
forall a b. Snowflake a -> Snowflake b
coerceSnowflake @_ @Overwrite) ([Snowflake Role] -> [Snowflake Overwrite])
-> (Vector (Snowflake Role) -> [Snowflake Role])
-> Vector (Snowflake Role)
-> [Snowflake Overwrite]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Snowflake Role) -> [Snowflake Role]
forall a. Unboxable a => Vector a -> [a]
V.toList (Vector (Snowflake Role) -> [Snowflake Overwrite])
-> Vector (Snowflake Role) -> [Snowflake Overwrite]
forall a b. (a -> b) -> a -> b
$ Member
m Member
-> Getting
     (Vector (Snowflake Role)) Member (Vector (Snowflake Role))
-> Vector (Snowflake Role)
forall s a. s -> Getting a s a -> a
^. IsLabel
  "roles"
  (Getting
     (Vector (Snowflake Role)) Member (Vector (Snowflake Role)))
Getting (Vector (Snowflake Role)) Member (Vector (Snowflake Role))
#roles
        roleOverwrites :: [Overwrite]
roleOverwrites    = [Maybe Overwrite] -> [Overwrite]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Overwrite] -> [Overwrite])
-> [Maybe Overwrite] -> [Overwrite]
forall a b. (a -> b) -> a -> b
$ (Snowflake Overwrite -> Maybe Overwrite)
-> [Snowflake Overwrite] -> [Maybe Overwrite]
forall a b. (a -> b) -> [a] -> [b]
map (\Snowflake Overwrite
oid -> GuildChannel
c GuildChannel
-> Getting (First Overwrite) GuildChannel Overwrite
-> Maybe Overwrite
forall s a. s -> Getting (First a) s a -> Maybe a
^? forall s a. HasField' "permissionOverwrites" s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @"permissionOverwrites" ((SnowflakeMap Overwrite
  -> Const (First Overwrite) (SnowflakeMap Overwrite))
 -> GuildChannel -> Const (First Overwrite) GuildChannel)
-> ((Overwrite -> Const (First Overwrite) Overwrite)
    -> SnowflakeMap Overwrite
    -> Const (First Overwrite) (SnowflakeMap Overwrite))
-> Getting (First Overwrite) GuildChannel Overwrite
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Overwrite)
-> Traversal'
     (SnowflakeMap Overwrite) (IxValue (SnowflakeMap Overwrite))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (SnowflakeMap Overwrite)
Snowflake Overwrite
oid) [Snowflake Overwrite]
roleOverwriteIDs
        roleAllow :: Permissions
roleAllow         = (Permissions -> Permissions -> Permissions)
-> Permissions -> [Permissions] -> Permissions
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Permissions -> Permissions -> Permissions
forall α. Flags α => α -> α -> α
andFlags Permissions
forall α. Flags α => α
noFlags ([Overwrite]
roleOverwrites [Overwrite]
-> Getting (Endo [Permissions]) [Overwrite] Permissions
-> [Permissions]
forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. (Overwrite -> Const (Endo [Permissions]) Overwrite)
-> [Overwrite] -> Const (Endo [Permissions]) [Overwrite]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((Overwrite -> Const (Endo [Permissions]) Overwrite)
 -> [Overwrite] -> Const (Endo [Permissions]) [Overwrite])
-> ((Permissions -> Const (Endo [Permissions]) Permissions)
    -> Overwrite -> Const (Endo [Permissions]) Overwrite)
-> Getting (Endo [Permissions]) [Overwrite] Permissions
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IsLabel
  "allow"
  ((Permissions -> Const (Endo [Permissions]) Permissions)
   -> Overwrite -> Const (Endo [Permissions]) Overwrite)
(Permissions -> Const (Endo [Permissions]) Permissions)
-> Overwrite -> Const (Endo [Permissions]) Overwrite
#allow)
        roleDeny :: Permissions
roleDeny          = (Permissions -> Permissions -> Permissions)
-> Permissions -> [Permissions] -> Permissions
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Permissions -> Permissions -> Permissions
forall α. Flags α => α -> α -> α
andFlags Permissions
forall α. Flags α => α
noFlags ([Overwrite]
roleOverwrites [Overwrite]
-> Getting (Endo [Permissions]) [Overwrite] Permissions
-> [Permissions]
forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. (Overwrite -> Const (Endo [Permissions]) Overwrite)
-> [Overwrite] -> Const (Endo [Permissions]) [Overwrite]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((Overwrite -> Const (Endo [Permissions]) Overwrite)
 -> [Overwrite] -> Const (Endo [Permissions]) [Overwrite])
-> ((Permissions -> Const (Endo [Permissions]) Permissions)
    -> Overwrite -> Const (Endo [Permissions]) Overwrite)
-> Getting (Endo [Permissions]) [Overwrite] Permissions
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IsLabel
  "deny"
  ((Permissions -> Const (Endo [Permissions]) Permissions)
   -> Overwrite -> Const (Endo [Permissions]) Overwrite)
(Permissions -> Const (Endo [Permissions]) Permissions)
-> Overwrite -> Const (Endo [Permissions]) Overwrite
#deny)
        p'' :: Permissions
p''               = Permissions
p' Permissions -> Permissions -> Permissions
forall α. Flags α => α -> α -> α
.-. Permissions
roleDeny Permissions -> Permissions -> Permissions
forall α. Flags α => α -> α -> α
.+. Permissions
roleAllow
        memberOverwrite :: Maybe Overwrite
memberOverwrite   = GuildChannel
c GuildChannel
-> Getting (Maybe Overwrite) GuildChannel (Maybe Overwrite)
-> Maybe Overwrite
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' "permissionOverwrites" s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @"permissionOverwrites" ((SnowflakeMap Overwrite
  -> Const (Maybe Overwrite) (SnowflakeMap Overwrite))
 -> GuildChannel -> Const (Maybe Overwrite) GuildChannel)
-> ((Maybe Overwrite -> Const (Maybe Overwrite) (Maybe Overwrite))
    -> SnowflakeMap Overwrite
    -> Const (Maybe Overwrite) (SnowflakeMap Overwrite))
-> Getting (Maybe Overwrite) GuildChannel (Maybe Overwrite)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Overwrite)
-> Lens'
     (SnowflakeMap Overwrite) (Maybe (IxValue (SnowflakeMap Overwrite)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Snowflake Member -> Snowflake Overwrite
forall a b. Snowflake a -> Snowflake b
coerceSnowflake @_ @Overwrite (Snowflake Member -> Snowflake Overwrite)
-> Snowflake Member -> Snowflake Overwrite
forall a b. (a -> b) -> a -> b
$ Member -> Snowflake Member
forall b a. HasID b a => a -> Snowflake b
getID @Member Member
m)
        memberAllow :: Permissions
memberAllow       = Permissions
-> (Overwrite -> Permissions) -> Maybe Overwrite -> Permissions
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Permissions
forall α. Flags α => α
noFlags (Overwrite
-> Getting Permissions Overwrite Permissions -> Permissions
forall s a. s -> Getting a s a -> a
^. IsLabel "allow" (Getting Permissions Overwrite Permissions)
Getting Permissions Overwrite Permissions
#allow) Maybe Overwrite
memberOverwrite
        memberDeny :: Permissions
memberDeny        = Permissions
-> (Overwrite -> Permissions) -> Maybe Overwrite -> Permissions
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Permissions
forall α. Flags α => α
noFlags (Overwrite
-> Getting Permissions Overwrite Permissions -> Permissions
forall s a. s -> Getting a s a -> a
^. IsLabel "deny" (Getting Permissions Overwrite Permissions)
Getting Permissions Overwrite Permissions
#deny) Maybe Overwrite
memberOverwrite
        p''' :: Permissions
p'''              = Permissions
p'' Permissions -> Permissions -> Permissions
forall α. Flags α => α -> α -> α
.-. Permissions
memberDeny Permissions -> Permissions -> Permissions
forall α. Flags α => α -> α -> α
.+. Permissions
memberAllow
    in Permissions
p'''

-- | Things that 'Member's have 'Permissions' in
class PermissionsIn a where
  -- | Calculate a 'Member'\'s 'Permissions' in something
  --
  -- If permissions could not be calculated because something couldn't be found
  -- in the cache, this will return an empty set of permissions. Use
  -- 'permissionsIn'' if you want to handle cases where something might not exist
  -- in cache.
  permissionsIn :: a -> Member -> Permissions

-- | A 'Member'\'s 'Permissions' in a channel are their roles and overwrites
instance PermissionsIn (Guild, GuildChannel) where
  permissionsIn :: (Guild, GuildChannel) -> Member -> Permissions
permissionsIn (Guild
g, GuildChannel
c) Member
m = GuildChannel -> Member -> Permissions -> Permissions
applyOverwrites GuildChannel
c Member
m (Permissions -> Permissions) -> Permissions -> Permissions
forall a b. (a -> b) -> a -> b
$ Guild -> Member -> Permissions
basePermissions Guild
g Member
m

-- | A 'Member'\'s 'Permissions' in a guild are just their roles
instance PermissionsIn Guild where
  permissionsIn :: Guild -> Member -> Permissions
permissionsIn Guild
g Member
m = Guild -> Member -> Permissions
basePermissions Guild
g Member
m

-- | A variant of 'PermissionsIn' that will use the cache/http.
class PermissionsIn' a where
  -- | Calculate the permissions of something that has a 'User' id
  permissionsIn' :: (BotC r, HasID User u) => a -> u -> P.Sem r Permissions

-- | A 'User''s 'Permissions' in a channel are their roles and overwrites
--
-- This will fetch the guild from the cache or http as needed
instance PermissionsIn' GuildChannel where
  permissionsIn' :: GuildChannel -> u -> Sem r Permissions
permissionsIn' GuildChannel
c (forall a. HasID User a => a -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID @User -> Snowflake User
uid) = do
    Maybe Member
m <- (Snowflake Guild, Snowflake Member) -> Sem r (Maybe Member)
forall a ids (r :: EffectRow).
(Upgradeable a ids, BotC r) =>
ids -> Sem r (Maybe a)
upgrade (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID @Guild GuildChannel
c, Snowflake User -> Snowflake Member
forall a b. Snowflake a -> Snowflake b
coerceSnowflake @_ @Member Snowflake User
uid)
    Maybe Guild
g <- Snowflake Guild -> Sem r (Maybe Guild)
forall a ids (r :: EffectRow).
(Upgradeable a ids, BotC r) =>
ids -> Sem r (Maybe a)
upgrade (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID @Guild GuildChannel
c)
    case (Maybe Member
m, Maybe Guild
g) of
      (Just Member
m, Just Guild
g') -> Permissions -> Sem r Permissions
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Permissions -> Sem r Permissions)
-> Permissions -> Sem r Permissions
forall a b. (a -> b) -> a -> b
$ (Guild, GuildChannel) -> Member -> Permissions
forall a. PermissionsIn a => a -> Member -> Permissions
permissionsIn (Guild
g', GuildChannel
c) Member
m
      (Maybe Member, Maybe Guild)
_cantFind         -> Permissions -> Sem r Permissions
forall (f :: * -> *) a. Applicative f => a -> f a
pure Permissions
forall α. Flags α => α
noFlags

-- | A 'Member'\'s 'Permissions' in a guild are just their roles
instance PermissionsIn' Guild where
  permissionsIn' :: Guild -> u -> Sem r Permissions
permissionsIn' Guild
g (forall a. HasID User a => a -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID @User -> Snowflake User
uid) = do
    Maybe Member
m <- (Snowflake Guild, Snowflake Member) -> Sem r (Maybe Member)
forall a ids (r :: EffectRow).
(Upgradeable a ids, BotC r) =>
ids -> Sem r (Maybe a)
upgrade (Guild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID @Guild Guild
g, Snowflake User -> Snowflake Member
forall a b. Snowflake a -> Snowflake b
coerceSnowflake @_ @Member Snowflake User
uid)
    case Maybe Member
m of
      Just Member
m' -> Permissions -> Sem r Permissions
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Permissions -> Sem r Permissions)
-> Permissions -> Sem r Permissions
forall a b. (a -> b) -> a -> b
$ Guild -> Member -> Permissions
forall a. PermissionsIn a => a -> Member -> Permissions
permissionsIn Guild
g Member
m'
      Maybe Member
Nothing -> Permissions -> Sem r Permissions
forall (f :: * -> *) a. Applicative f => a -> f a
pure Permissions
forall α. Flags α => α
noFlags

-- | A 'Member'\'s 'Permissions' in a channel are their roles and overwrites
--
-- This will fetch the guild and channel from the cache or http as needed
instance PermissionsIn' (Snowflake GuildChannel) where
  permissionsIn' :: Snowflake GuildChannel -> u -> Sem r Permissions
permissionsIn' Snowflake GuildChannel
cid u
u = do
    Maybe GuildChannel
c <- Snowflake GuildChannel -> Sem r (Maybe GuildChannel)
forall a ids (r :: EffectRow).
(Upgradeable a ids, BotC r) =>
ids -> Sem r (Maybe a)
upgrade Snowflake GuildChannel
cid
    case Maybe GuildChannel
c of
      Just GuildChannel
c'  -> GuildChannel -> u -> Sem r Permissions
forall a (r :: EffectRow) u.
(PermissionsIn' a, BotC r, HasID User u) =>
a -> u -> Sem r Permissions
permissionsIn' GuildChannel
c' u
u
      Maybe GuildChannel
Nothing  -> Permissions -> Sem r Permissions
forall (f :: * -> *) a. Applicative f => a -> f a
pure Permissions
forall α. Flags α => α
noFlags

-- | A 'Member'\'s 'Permissions' in a guild are just their roles
--
-- This will fetch the guild from the cache or http as needed
instance PermissionsIn' (Snowflake Guild) where
  permissionsIn' :: Snowflake Guild -> u -> Sem r Permissions
permissionsIn' Snowflake Guild
gid u
u = do
    Maybe Guild
g <- Snowflake Guild -> Sem r (Maybe Guild)
forall a ids (r :: EffectRow).
(Upgradeable a ids, BotC r) =>
ids -> Sem r (Maybe a)
upgrade Snowflake Guild
gid
    case Maybe Guild
g of
      Just Guild
g' -> Guild -> u -> Sem r Permissions
forall a (r :: EffectRow) u.
(PermissionsIn' a, BotC r, HasID User u) =>
a -> u -> Sem r Permissions
permissionsIn' Guild
g' u
u
      Maybe Guild
Nothing -> Permissions -> Sem r Permissions
forall (f :: * -> *) a. Applicative f => a -> f a
pure Permissions
forall α. Flags α => α
noFlags