-- | Things that can be upgraded from snowflakes to their full data
module Calamity.Types.Upgradeable
    ( Upgradeable(..) ) where

import           Calamity.Cache.Eff
import           Calamity.Client.Types
import           Calamity.HTTP                  as H
import           Calamity.Internal.Utils
import           Calamity.Types.Model.Channel
import           Calamity.Types.Model.Guild
import           Calamity.Types.Model.User
import           Calamity.Types.Snowflake

import           Control.Applicative
import           Control.Lens

import           Data.Generics.Sum.Constructors

import qualified Polysemy                       as P
import qualified Polysemy.Fail                  as P
import qualified Polysemy.NonDet                as P

-- | A typeclass that represents snowflakes that can be upgraded to their
-- complete data, either through the cache or HTTP.
class Upgradeable a ids | a -> ids, ids -> a where
  -- | Upgrade a snowflake to it's complete data.
  --
  -- If it existed in the cache then it is returned from there, otherwise we
  -- fetch from HTTP and update the cache on success.
  upgrade :: BotC r => ids -> P.Sem r (Maybe a)

maybeToAlt :: Alternative f => Maybe a -> f a
maybeToAlt :: Maybe a -> f a
maybeToAlt (Just x :: a
x) = a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
maybeToAlt Nothing = f a
forall (f :: * -> *) a. Alternative f => f a
empty

instance Upgradeable User (Snowflake User) where
  upgrade :: Snowflake User -> Sem r (Maybe User)
upgrade uid :: Snowflake User
uid = Sem (NonDet : r) User -> Sem r (Maybe User)
forall (r :: [(* -> *) -> * -> *]) a.
Sem (NonDet : r) a -> Sem r (Maybe a)
P.runNonDetMaybe ((Snowflake User -> Sem (NonDet : r) (Maybe User)
forall (r :: [(* -> *) -> * -> *]).
MemberWithError CacheEff r =>
Snowflake User -> Sem r (Maybe User)
getUser Snowflake User
uid Sem (NonDet : r) (Maybe User)
-> (Maybe User -> Sem (NonDet : r) User) -> Sem (NonDet : r) User
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe User -> Sem (NonDet : r) User
forall (f :: * -> *) a. Alternative f => Maybe a -> f a
maybeToAlt) Sem (NonDet : r) User
-> Sem (NonDet : r) User -> Sem (NonDet : r) User
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Sem (NonDet : r) User
gethttp)
    where
      gethttp :: Sem (NonDet : r) User
gethttp = Sem (Fail : NonDet : r) User -> Sem (NonDet : r) User
forall (r :: [(* -> *) -> * -> *]) a.
Member NonDet r =>
Sem (Fail : r) a -> Sem r a
P.failToNonDet (Sem (Fail : NonDet : r) User -> Sem (NonDet : r) User)
-> Sem (Fail : NonDet : r) User -> Sem (NonDet : r) User
forall a b. (a -> b) -> a -> b
$ do
        Right u :: User
u <- UserRequest User -> Sem (Fail : NonDet : r) (Either RestError User)
forall a (r :: [(* -> *) -> * -> *]).
(Request a, BotC r, FromJSON (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (UserRequest User
 -> Sem (Fail : NonDet : r) (Either RestError User))
-> UserRequest User
-> Sem (Fail : NonDet : r) (Either RestError User)
forall a b. (a -> b) -> a -> b
$ Snowflake User -> UserRequest User
forall u. HasID User u => u -> UserRequest User
H.GetUser Snowflake User
uid
        User -> Sem (Fail : NonDet : r) ()
forall (r :: [(* -> *) -> * -> *]).
MemberWithError CacheEff r =>
User -> Sem r ()
setUser User
u
        User -> Sem (Fail : NonDet : r) User
forall (f :: * -> *) a. Applicative f => a -> f a
pure User
u

instance Upgradeable Member (Snowflake Guild, Snowflake Member) where
  upgrade :: (Snowflake Guild, Snowflake Member) -> Sem r (Maybe Member)
upgrade (gid :: Snowflake Guild
gid, mid :: Snowflake Member
mid) = Sem (NonDet : r) Member -> Sem r (Maybe Member)
forall (r :: [(* -> *) -> * -> *]) a.
Sem (NonDet : r) a -> Sem r (Maybe a)
P.runNonDetMaybe (Sem (NonDet : r) Member
getcache Sem (NonDet : r) Member
-> Sem (NonDet : r) Member -> Sem (NonDet : r) Member
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Sem (NonDet : r) Member
gethttp)
    where
      getcache :: Sem (NonDet : r) Member
getcache = Sem (Fail : NonDet : r) Member -> Sem (NonDet : r) Member
forall (r :: [(* -> *) -> * -> *]) a.
Member NonDet r =>
Sem (Fail : r) a -> Sem r a
P.failToNonDet (Sem (Fail : NonDet : r) Member -> Sem (NonDet : r) Member)
-> Sem (Fail : NonDet : r) Member -> Sem (NonDet : r) Member
forall a b. (a -> b) -> a -> b
$ do
        Just g :: Guild
g <- Snowflake Guild -> Sem (Fail : NonDet : r) (Maybe Guild)
forall (r :: [(* -> *) -> * -> *]).
MemberWithError CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
gid
        Just m :: Member
m <- Maybe Member -> Sem (Fail : NonDet : r) (Maybe Member)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Guild
g Guild
-> Getting (Maybe Member) Guild (Maybe Member) -> Maybe Member
forall s a. s -> Getting a s a -> a
^. IsLabel
  "members"
  ((SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
   -> Guild -> Const (Maybe Member) Guild)
(SnowflakeMap Member -> Const (Maybe Member) (SnowflakeMap Member))
-> Guild -> Const (Maybe Member) Guild
#members ((SnowflakeMap Member
  -> Const (Maybe Member) (SnowflakeMap Member))
 -> Guild -> Const (Maybe Member) Guild)
-> ((Maybe Member -> Const (Maybe Member) (Maybe Member))
    -> SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
-> Getting (Maybe Member) Guild (Maybe Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (SnowflakeMap Member)
Snowflake Member
mid)
        Member -> Sem (Fail : NonDet : r) Member
forall (f :: * -> *) a. Applicative f => a -> f a
pure Member
m
      gethttp :: Sem (NonDet : r) Member
gethttp = Sem (Fail : NonDet : r) Member -> Sem (NonDet : r) Member
forall (r :: [(* -> *) -> * -> *]) a.
Member NonDet r =>
Sem (Fail : r) a -> Sem r a
P.failToNonDet (Sem (Fail : NonDet : r) Member -> Sem (NonDet : r) Member)
-> Sem (Fail : NonDet : r) Member -> Sem (NonDet : r) Member
forall a b. (a -> b) -> a -> b
$ do
        Right m :: Member
m <- GuildRequest Member
-> Sem (Fail : NonDet : r) (Either RestError Member)
forall a (r :: [(* -> *) -> * -> *]).
(Request a, BotC r, FromJSON (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (GuildRequest Member
 -> Sem (Fail : NonDet : r) (Either RestError Member))
-> GuildRequest Member
-> Sem (Fail : NonDet : r) (Either RestError Member)
forall a b. (a -> b) -> a -> b
$ Snowflake Guild -> Snowflake User -> GuildRequest Member
forall g u.
(HasID Guild g, HasID User u) =>
g -> u -> GuildRequest Member
H.GetGuildMember Snowflake Guild
gid (Snowflake Member -> Snowflake User
forall a b. Snowflake a -> Snowflake b
coerceSnowflake @_ @User Snowflake Member
mid)
        -- getcache could have failed becuase the member wasn't cached
        Snowflake Guild -> (Guild -> Guild) -> Sem (Fail : NonDet : r) ()
forall (r :: [(* -> *) -> * -> *]).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
gid (IsLabel
  "members"
  ((SnowflakeMap Member -> Identity (SnowflakeMap Member))
   -> Guild -> Identity Guild)
(SnowflakeMap Member -> Identity (SnowflakeMap Member))
-> Guild -> Identity Guild
#members ((SnowflakeMap Member -> Identity (SnowflakeMap Member))
 -> Guild -> Identity Guild)
-> ((Maybe Member -> Identity (Maybe Member))
    -> SnowflakeMap Member -> Identity (SnowflakeMap Member))
-> (Maybe Member -> Identity (Maybe Member))
-> Guild
-> Identity Guild
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (SnowflakeMap Member)
Snowflake Member
mid ((Maybe Member -> Identity (Maybe Member))
 -> Guild -> Identity Guild)
-> Member -> Guild -> Guild
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Member
m)
        Member -> Sem (Fail : NonDet : r) Member
forall (f :: * -> *) a. Applicative f => a -> f a
pure Member
m

instance Upgradeable Guild (Snowflake Guild) where
  upgrade :: Snowflake Guild -> Sem r (Maybe Guild)
upgrade gid :: Snowflake Guild
gid = Sem (NonDet : r) Guild -> Sem r (Maybe Guild)
forall (r :: [(* -> *) -> * -> *]) a.
Sem (NonDet : r) a -> Sem r (Maybe a)
P.runNonDetMaybe ((Snowflake Guild -> Sem (NonDet : r) (Maybe Guild)
forall (r :: [(* -> *) -> * -> *]).
MemberWithError CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
gid Sem (NonDet : r) (Maybe Guild)
-> (Maybe Guild -> Sem (NonDet : r) Guild)
-> Sem (NonDet : r) Guild
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe Guild -> Sem (NonDet : r) Guild
forall (f :: * -> *) a. Alternative f => Maybe a -> f a
maybeToAlt) Sem (NonDet : r) Guild
-> Sem (NonDet : r) Guild -> Sem (NonDet : r) Guild
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Sem (NonDet : r) Guild
gethttp)
    where
      gethttp :: Sem (NonDet : r) Guild
gethttp = Sem (Fail : NonDet : r) Guild -> Sem (NonDet : r) Guild
forall (r :: [(* -> *) -> * -> *]) a.
Member NonDet r =>
Sem (Fail : r) a -> Sem r a
P.failToNonDet (Sem (Fail : NonDet : r) Guild -> Sem (NonDet : r) Guild)
-> Sem (Fail : NonDet : r) Guild -> Sem (NonDet : r) Guild
forall a b. (a -> b) -> a -> b
$ do
        Right g :: Guild
g <- GuildRequest Guild
-> Sem (Fail : NonDet : r) (Either RestError Guild)
forall a (r :: [(* -> *) -> * -> *]).
(Request a, BotC r, FromJSON (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (GuildRequest Guild
 -> Sem (Fail : NonDet : r) (Either RestError Guild))
-> GuildRequest Guild
-> Sem (Fail : NonDet : r) (Either RestError Guild)
forall a b. (a -> b) -> a -> b
$ Snowflake Guild -> GuildRequest Guild
forall g. HasID Guild g => g -> GuildRequest Guild
H.GetGuild Snowflake Guild
gid
        Guild -> Sem (Fail : NonDet : r) Guild
forall (f :: * -> *) a. Applicative f => a -> f a
pure Guild
g

insertChannel :: BotC r => Channel -> P.Sem r ()
insertChannel :: Channel -> Sem r ()
insertChannel (DMChannel' dm :: DMChannel
dm) = DMChannel -> Sem r ()
forall (r :: [(* -> *) -> * -> *]).
MemberWithError CacheEff r =>
DMChannel -> Sem r ()
setDM DMChannel
dm
insertChannel (GuildChannel' ch :: GuildChannel
ch) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: [(* -> *) -> * -> *]).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
ch) (IsLabel
  "channels"
  ((SnowflakeMap GuildChannel
    -> Identity (SnowflakeMap GuildChannel))
   -> Guild -> Identity Guild)
(SnowflakeMap GuildChannel -> Identity (SnowflakeMap GuildChannel))
-> Guild -> Identity Guild
#channels ((SnowflakeMap GuildChannel
  -> Identity (SnowflakeMap GuildChannel))
 -> Guild -> Identity Guild)
-> ((Maybe GuildChannel -> Identity (Maybe GuildChannel))
    -> SnowflakeMap GuildChannel
    -> Identity (SnowflakeMap GuildChannel))
-> (Maybe GuildChannel -> Identity (Maybe GuildChannel))
-> Guild
-> Identity Guild
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap GuildChannel)
-> Lens'
     (SnowflakeMap GuildChannel)
     (Maybe (IxValue (SnowflakeMap GuildChannel)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (GuildChannel -> Snowflake GuildChannel
forall b a. HasID b a => a -> Snowflake b
getID @GuildChannel GuildChannel
ch) ((Maybe GuildChannel -> Identity (Maybe GuildChannel))
 -> Guild -> Identity Guild)
-> GuildChannel -> Guild -> Guild
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ GuildChannel
ch)
insertChannel _ = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

instance Upgradeable Channel (Snowflake Channel) where
  upgrade :: Snowflake Channel -> Sem r (Maybe Channel)
upgrade cid :: Snowflake Channel
cid = Sem (NonDet : r) Channel -> Sem r (Maybe Channel)
forall (r :: [(* -> *) -> * -> *]) a.
Sem (NonDet : r) a -> Sem r (Maybe a)
P.runNonDetMaybe (Sem (NonDet : r) Channel
getcacheDM Sem (NonDet : r) Channel
-> Sem (NonDet : r) Channel -> Sem (NonDet : r) Channel
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Sem (NonDet : r) Channel
getcacheGuild Sem (NonDet : r) Channel
-> Sem (NonDet : r) Channel -> Sem (NonDet : r) Channel
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Sem (NonDet : r) Channel
gethttp)
    where
      getcacheDM :: Sem (NonDet : r) Channel
getcacheDM = DMChannel -> Channel
DMChannel' (DMChannel -> Channel)
-> Sem (NonDet : r) (Maybe DMChannel)
-> Sem (NonDet : r) (Maybe Channel)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> Snowflake DMChannel -> Sem (NonDet : r) (Maybe DMChannel)
forall (r :: [(* -> *) -> * -> *]).
MemberWithError CacheEff r =>
Snowflake DMChannel -> Sem r (Maybe DMChannel)
getDM (Snowflake Channel -> Snowflake DMChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake Snowflake Channel
cid) Sem (NonDet : r) (Maybe Channel)
-> (Maybe Channel -> Sem (NonDet : r) Channel)
-> Sem (NonDet : r) Channel
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe Channel -> Sem (NonDet : r) Channel
forall (f :: * -> *) a. Alternative f => Maybe a -> f a
maybeToAlt
      getcacheGuild :: Sem (NonDet : r) Channel
getcacheGuild = GuildChannel -> Channel
GuildChannel' (GuildChannel -> Channel)
-> Sem (NonDet : r) (Maybe GuildChannel)
-> Sem (NonDet : r) (Maybe Channel)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> Snowflake GuildChannel -> Sem (NonDet : r) (Maybe GuildChannel)
forall (r :: [(* -> *) -> * -> *]).
MemberWithError CacheEff r =>
Snowflake GuildChannel -> Sem r (Maybe GuildChannel)
getGuildChannel (Snowflake Channel -> Snowflake GuildChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake Snowflake Channel
cid) Sem (NonDet : r) (Maybe Channel)
-> (Maybe Channel -> Sem (NonDet : r) Channel)
-> Sem (NonDet : r) Channel
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe Channel -> Sem (NonDet : r) Channel
forall (f :: * -> *) a. Alternative f => Maybe a -> f a
maybeToAlt
      gethttp :: Sem (NonDet : r) Channel
gethttp = Sem (Fail : NonDet : r) Channel -> Sem (NonDet : r) Channel
forall (r :: [(* -> *) -> * -> *]) a.
Member NonDet r =>
Sem (Fail : r) a -> Sem r a
P.failToNonDet (Sem (Fail : NonDet : r) Channel -> Sem (NonDet : r) Channel)
-> Sem (Fail : NonDet : r) Channel -> Sem (NonDet : r) Channel
forall a b. (a -> b) -> a -> b
$ do
        Right c :: Channel
c <- ChannelRequest Channel
-> Sem (Fail : NonDet : r) (Either RestError Channel)
forall a (r :: [(* -> *) -> * -> *]).
(Request a, BotC r, FromJSON (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (ChannelRequest Channel
 -> Sem (Fail : NonDet : r) (Either RestError Channel))
-> ChannelRequest Channel
-> Sem (Fail : NonDet : r) (Either RestError Channel)
forall a b. (a -> b) -> a -> b
$ Snowflake Channel -> ChannelRequest Channel
forall c. HasID Channel c => c -> ChannelRequest Channel
H.GetChannel Snowflake Channel
cid
        Channel -> Sem (Fail : NonDet : r) ()
forall (r :: [(* -> *) -> * -> *]). BotC r => Channel -> Sem r ()
insertChannel Channel
c
        Channel -> Sem (Fail : NonDet : r) Channel
forall (f :: * -> *) a. Applicative f => a -> f a
pure Channel
c

instance Upgradeable GuildChannel (Snowflake GuildChannel) where
  upgrade :: Snowflake GuildChannel -> Sem r (Maybe GuildChannel)
upgrade cid :: Snowflake GuildChannel
cid = Sem (NonDet : r) GuildChannel -> Sem r (Maybe GuildChannel)
forall (r :: [(* -> *) -> * -> *]) a.
Sem (NonDet : r) a -> Sem r (Maybe a)
P.runNonDetMaybe (Sem (NonDet : r) GuildChannel
getcache Sem (NonDet : r) GuildChannel
-> Sem (NonDet : r) GuildChannel -> Sem (NonDet : r) GuildChannel
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Sem (NonDet : r) GuildChannel
gethttp)
    where
      getcache :: Sem (NonDet : r) GuildChannel
getcache = Snowflake GuildChannel -> Sem (NonDet : r) (Maybe GuildChannel)
forall (r :: [(* -> *) -> * -> *]).
MemberWithError CacheEff r =>
Snowflake GuildChannel -> Sem r (Maybe GuildChannel)
getGuildChannel (Snowflake GuildChannel -> Snowflake GuildChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake Snowflake GuildChannel
cid) Sem (NonDet : r) (Maybe GuildChannel)
-> (Maybe GuildChannel -> Sem (NonDet : r) GuildChannel)
-> Sem (NonDet : r) GuildChannel
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe GuildChannel -> Sem (NonDet : r) GuildChannel
forall (f :: * -> *) a. Alternative f => Maybe a -> f a
maybeToAlt
      gethttp :: Sem (NonDet : r) GuildChannel
gethttp = Sem (Fail : NonDet : r) GuildChannel
-> Sem (NonDet : r) GuildChannel
forall (r :: [(* -> *) -> * -> *]) a.
Member NonDet r =>
Sem (Fail : r) a -> Sem r a
P.failToNonDet (Sem (Fail : NonDet : r) GuildChannel
 -> Sem (NonDet : r) GuildChannel)
-> Sem (Fail : NonDet : r) GuildChannel
-> Sem (NonDet : r) GuildChannel
forall a b. (a -> b) -> a -> b
$ do
        Right c :: Channel
c <- ChannelRequest Channel
-> Sem (Fail : NonDet : r) (Either RestError Channel)
forall a (r :: [(* -> *) -> * -> *]).
(Request a, BotC r, FromJSON (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (ChannelRequest Channel
 -> Sem (Fail : NonDet : r) (Either RestError Channel))
-> ChannelRequest Channel
-> Sem (Fail : NonDet : r) (Either RestError Channel)
forall a b. (a -> b) -> a -> b
$ Snowflake Channel -> ChannelRequest Channel
forall c. HasID Channel c => c -> ChannelRequest Channel
H.GetChannel (Snowflake GuildChannel -> Snowflake Channel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake @_ @Channel Snowflake GuildChannel
cid)
        Channel -> Sem (Fail : NonDet : r) ()
forall (r :: [(* -> *) -> * -> *]). BotC r => Channel -> Sem r ()
insertChannel Channel
c
        Maybe GuildChannel -> Sem (Fail : NonDet : r) GuildChannel
forall (f :: * -> *) a. Alternative f => Maybe a -> f a
maybeToAlt (Channel
c Channel
-> Getting (First GuildChannel) Channel GuildChannel
-> Maybe GuildChannel
forall s a. s -> Getting (First a) s a -> Maybe a
^? forall s t a b.
AsConstructor "GuildChannel'" s t a b =>
Prism s t a b
forall (ctor :: Symbol) s t a b.
AsConstructor ctor s t a b =>
Prism s t a b
_Ctor @"GuildChannel'")

instance Upgradeable Emoji (Snowflake Guild, Snowflake Emoji) where
  upgrade :: (Snowflake Guild, Snowflake Emoji) -> Sem r (Maybe Emoji)
upgrade (gid :: Snowflake Guild
gid, eid :: Snowflake Emoji
eid) = Sem (NonDet : r) Emoji -> Sem r (Maybe Emoji)
forall (r :: [(* -> *) -> * -> *]) a.
Sem (NonDet : r) a -> Sem r (Maybe a)
P.runNonDetMaybe (Sem (NonDet : r) Emoji
getcache Sem (NonDet : r) Emoji
-> Sem (NonDet : r) Emoji -> Sem (NonDet : r) Emoji
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Sem (NonDet : r) Emoji
gethttp)
    where
      getcache :: Sem (NonDet : r) Emoji
getcache = Sem (Fail : NonDet : r) Emoji -> Sem (NonDet : r) Emoji
forall (r :: [(* -> *) -> * -> *]) a.
Member NonDet r =>
Sem (Fail : r) a -> Sem r a
P.failToNonDet (Sem (Fail : NonDet : r) Emoji -> Sem (NonDet : r) Emoji)
-> Sem (Fail : NonDet : r) Emoji -> Sem (NonDet : r) Emoji
forall a b. (a -> b) -> a -> b
$ do
        Just g :: Guild
g <- Snowflake Guild -> Sem (Fail : NonDet : r) (Maybe Guild)
forall (r :: [(* -> *) -> * -> *]).
MemberWithError CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
gid
        Just m :: Emoji
m <- Maybe Emoji -> Sem (Fail : NonDet : r) (Maybe Emoji)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Guild
g Guild -> Getting (Maybe Emoji) Guild (Maybe Emoji) -> Maybe Emoji
forall s a. s -> Getting a s a -> a
^. IsLabel
  "emojis"
  ((SnowflakeMap Emoji -> Const (Maybe Emoji) (SnowflakeMap Emoji))
   -> Guild -> Const (Maybe Emoji) Guild)
(SnowflakeMap Emoji -> Const (Maybe Emoji) (SnowflakeMap Emoji))
-> Guild -> Const (Maybe Emoji) Guild
#emojis ((SnowflakeMap Emoji -> Const (Maybe Emoji) (SnowflakeMap Emoji))
 -> Guild -> Const (Maybe Emoji) Guild)
-> ((Maybe Emoji -> Const (Maybe Emoji) (Maybe Emoji))
    -> SnowflakeMap Emoji -> Const (Maybe Emoji) (SnowflakeMap Emoji))
-> Getting (Maybe Emoji) Guild (Maybe Emoji)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Emoji)
-> Lens'
     (SnowflakeMap Emoji) (Maybe (IxValue (SnowflakeMap Emoji)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (SnowflakeMap Emoji)
Snowflake Emoji
eid)
        Emoji -> Sem (Fail : NonDet : r) Emoji
forall (f :: * -> *) a. Applicative f => a -> f a
pure Emoji
m
      gethttp :: Sem (NonDet : r) Emoji
gethttp = Sem (Fail : NonDet : r) Emoji -> Sem (NonDet : r) Emoji
forall (r :: [(* -> *) -> * -> *]) a.
Member NonDet r =>
Sem (Fail : r) a -> Sem r a
P.failToNonDet (Sem (Fail : NonDet : r) Emoji -> Sem (NonDet : r) Emoji)
-> Sem (Fail : NonDet : r) Emoji -> Sem (NonDet : r) Emoji
forall a b. (a -> b) -> a -> b
$ do
        Right e :: Emoji
e <- EmojiRequest Emoji
-> Sem (Fail : NonDet : r) (Either RestError Emoji)
forall a (r :: [(* -> *) -> * -> *]).
(Request a, BotC r, FromJSON (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (EmojiRequest Emoji
 -> Sem (Fail : NonDet : r) (Either RestError Emoji))
-> EmojiRequest Emoji
-> Sem (Fail : NonDet : r) (Either RestError Emoji)
forall a b. (a -> b) -> a -> b
$ Snowflake Guild -> Snowflake Emoji -> EmojiRequest Emoji
forall g e.
(HasID Guild g, HasID Emoji e) =>
g -> e -> EmojiRequest Emoji
H.GetGuildEmoji Snowflake Guild
gid Snowflake Emoji
eid
        Snowflake Guild -> (Guild -> Guild) -> Sem (Fail : NonDet : r) ()
forall (r :: [(* -> *) -> * -> *]).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
gid (IsLabel
  "emojis"
  ((SnowflakeMap Emoji -> Identity (SnowflakeMap Emoji))
   -> Guild -> Identity Guild)
(SnowflakeMap Emoji -> Identity (SnowflakeMap Emoji))
-> Guild -> Identity Guild
#emojis ((SnowflakeMap Emoji -> Identity (SnowflakeMap Emoji))
 -> Guild -> Identity Guild)
-> ((Maybe Emoji -> Identity (Maybe Emoji))
    -> SnowflakeMap Emoji -> Identity (SnowflakeMap Emoji))
-> (Maybe Emoji -> Identity (Maybe Emoji))
-> Guild
-> Identity Guild
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Emoji)
-> Lens'
     (SnowflakeMap Emoji) (Maybe (IxValue (SnowflakeMap Emoji)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (SnowflakeMap Emoji)
Snowflake Emoji
eid ((Maybe Emoji -> Identity (Maybe Emoji))
 -> Guild -> Identity Guild)
-> Emoji -> Guild -> Guild
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Emoji
e)
        Emoji -> Sem (Fail : NonDet : r) Emoji
forall (f :: * -> *) a. Applicative f => a -> f a
pure Emoji
e