-- | A 'Cache' handler that operates in memory
module Calamity.Cache.InMemory
    ( runCacheInMemory
    , runCacheInMemory'
    , runCacheInMemoryNoMsg ) where

import           Calamity.Cache.Eff
import qualified Calamity.Internal.BoundedStore as BS
import qualified Calamity.Internal.SnowflakeMap as SM
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.Lens
import           Control.Monad.State.Strict

import           Data.Foldable
import qualified Data.HashMap.Lazy              as LH
import qualified Data.HashSet                   as LS
import           Data.IORef

import           GHC.Generics

import qualified Polysemy                       as P
import qualified Polysemy.AtomicState           as P

data Cache f = Cache
  { Cache f -> Maybe User
user              :: Maybe User
  , Cache f -> SnowflakeMap Guild
guilds            :: SM.SnowflakeMap Guild
  , Cache f -> SnowflakeMap DMChannel
dms               :: SM.SnowflakeMap DMChannel
  , Cache f -> HashMap (Snowflake GuildChannel) Guild
guildChannels     :: LH.HashMap (Snowflake GuildChannel) Guild
  , Cache f -> SnowflakeMap User
users             :: SM.SnowflakeMap User
  , Cache f -> HashSet (Snowflake Guild)
unavailableGuilds :: LS.HashSet (Snowflake Guild)
  , Cache f -> f (BoundedStore Message)
messages          :: f (BS.BoundedStore Message)
  }
  deriving ( (forall x. Cache f -> Rep (Cache f) x)
-> (forall x. Rep (Cache f) x -> Cache f) -> Generic (Cache f)
forall x. Rep (Cache f) x -> Cache f
forall x. Cache f -> Rep (Cache f) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (f :: * -> *) x. Rep (Cache f) x -> Cache f
forall (f :: * -> *) x. Cache f -> Rep (Cache f) x
$cto :: forall (f :: * -> *) x. Rep (Cache f) x -> Cache f
$cfrom :: forall (f :: * -> *) x. Cache f -> Rep (Cache f) x
Generic )

type CacheWithMsg = Cache Identity
type CacheNoMsg = Cache (Const ())

emptyCache :: CacheWithMsg
emptyCache :: CacheWithMsg
emptyCache = Maybe User
-> SnowflakeMap Guild
-> SnowflakeMap DMChannel
-> HashMap (Snowflake GuildChannel) Guild
-> SnowflakeMap User
-> HashSet (Snowflake Guild)
-> Identity (BoundedStore Message)
-> CacheWithMsg
forall (f :: * -> *).
Maybe User
-> SnowflakeMap Guild
-> SnowflakeMap DMChannel
-> HashMap (Snowflake GuildChannel) Guild
-> SnowflakeMap User
-> HashSet (Snowflake Guild)
-> f (BoundedStore Message)
-> Cache f
Cache Maybe User
forall a. Maybe a
Nothing SnowflakeMap Guild
forall a. SnowflakeMap a
SM.empty SnowflakeMap DMChannel
forall a. SnowflakeMap a
SM.empty HashMap (Snowflake GuildChannel) Guild
forall k v. HashMap k v
LH.empty SnowflakeMap User
forall a. SnowflakeMap a
SM.empty HashSet (Snowflake Guild)
forall a. HashSet a
LS.empty (BoundedStore Message -> Identity (BoundedStore Message)
forall a. a -> Identity a
Identity (BoundedStore Message -> Identity (BoundedStore Message))
-> BoundedStore Message -> Identity (BoundedStore Message)
forall a b. (a -> b) -> a -> b
$ Int -> BoundedStore Message
forall a. Int -> BoundedStore a
BS.empty 1000)

emptyCacheNoMsg :: CacheNoMsg
emptyCacheNoMsg :: CacheNoMsg
emptyCacheNoMsg = Maybe User
-> SnowflakeMap Guild
-> SnowflakeMap DMChannel
-> HashMap (Snowflake GuildChannel) Guild
-> SnowflakeMap User
-> HashSet (Snowflake Guild)
-> Const () (BoundedStore Message)
-> CacheNoMsg
forall (f :: * -> *).
Maybe User
-> SnowflakeMap Guild
-> SnowflakeMap DMChannel
-> HashMap (Snowflake GuildChannel) Guild
-> SnowflakeMap User
-> HashSet (Snowflake Guild)
-> f (BoundedStore Message)
-> Cache f
Cache Maybe User
forall a. Maybe a
Nothing SnowflakeMap Guild
forall a. SnowflakeMap a
SM.empty SnowflakeMap DMChannel
forall a. SnowflakeMap a
SM.empty HashMap (Snowflake GuildChannel) Guild
forall k v. HashMap k v
LH.empty SnowflakeMap User
forall a. SnowflakeMap a
SM.empty HashSet (Snowflake Guild)
forall a. HashSet a
LS.empty (() -> Const () (BoundedStore Message)
forall k a (b :: k). a -> Const a b
Const ())

emptyCache' :: Int -> CacheWithMsg
emptyCache' :: Int -> CacheWithMsg
emptyCache' msgLimit :: Int
msgLimit = Maybe User
-> SnowflakeMap Guild
-> SnowflakeMap DMChannel
-> HashMap (Snowflake GuildChannel) Guild
-> SnowflakeMap User
-> HashSet (Snowflake Guild)
-> Identity (BoundedStore Message)
-> CacheWithMsg
forall (f :: * -> *).
Maybe User
-> SnowflakeMap Guild
-> SnowflakeMap DMChannel
-> HashMap (Snowflake GuildChannel) Guild
-> SnowflakeMap User
-> HashSet (Snowflake Guild)
-> f (BoundedStore Message)
-> Cache f
Cache Maybe User
forall a. Maybe a
Nothing SnowflakeMap Guild
forall a. SnowflakeMap a
SM.empty SnowflakeMap DMChannel
forall a. SnowflakeMap a
SM.empty HashMap (Snowflake GuildChannel) Guild
forall k v. HashMap k v
LH.empty SnowflakeMap User
forall a. SnowflakeMap a
SM.empty HashSet (Snowflake Guild)
forall a. HashSet a
LS.empty (BoundedStore Message -> Identity (BoundedStore Message)
forall a. a -> Identity a
Identity (BoundedStore Message -> Identity (BoundedStore Message))
-> BoundedStore Message -> Identity (BoundedStore Message)
forall a b. (a -> b) -> a -> b
$ Int -> BoundedStore Message
forall a. Int -> BoundedStore a
BS.empty Int
msgLimit)

-- | Run the cache in memory with a default message cache size of 1000
runCacheInMemory :: P.Member (P.Embed IO) r => P.Sem (CacheEff ': r) a -> P.Sem r a
runCacheInMemory :: Sem (CacheEff : r) a -> Sem r a
runCacheInMemory m :: Sem (CacheEff : r) a
m = do
  IORef CacheWithMsg
var <- IO (IORef CacheWithMsg) -> Sem r (IORef CacheWithMsg)
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO (IORef CacheWithMsg) -> Sem r (IORef CacheWithMsg))
-> IO (IORef CacheWithMsg) -> Sem r (IORef CacheWithMsg)
forall a b. (a -> b) -> a -> b
$ CacheWithMsg -> IO (IORef CacheWithMsg)
forall a. a -> IO (IORef a)
newIORef CacheWithMsg
emptyCache
  IORef CacheWithMsg
-> Sem (AtomicState CacheWithMsg : r) a -> Sem r a
forall s (r :: [(* -> *) -> * -> *]) a.
Member (Embed IO) r =>
IORef s -> Sem (AtomicState s : r) a -> Sem r a
P.runAtomicStateIORef IORef CacheWithMsg
var (Sem (AtomicState CacheWithMsg : r) a -> Sem r a)
-> Sem (AtomicState CacheWithMsg : r) a -> Sem r a
forall a b. (a -> b) -> a -> b
$ (forall (m :: * -> *) x.
 CacheEff m x -> Sem (AtomicState CacheWithMsg : r) x)
-> Sem (CacheEff : r) a -> Sem (AtomicState CacheWithMsg : r) a
forall (e1 :: (* -> *) -> * -> *) (e2 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
FirstOrder e1 "reinterpret" =>
(forall (m :: * -> *) x. e1 m x -> Sem (e2 : r) x)
-> Sem (e1 : r) a -> Sem (e2 : r) a
P.reinterpret forall k (t :: * -> *) (r :: [(* -> *) -> * -> *]) (m :: k) a.
(MessageMod (Cache t), Member (AtomicState (Cache t)) r) =>
CacheEff m a -> Sem r a
forall (m :: * -> *) x.
CacheEff m x -> Sem (AtomicState CacheWithMsg : r) x
runCache' Sem (CacheEff : r) a
m

-- | Run the cache in memory with no messages being cached
runCacheInMemoryNoMsg :: P.Member (P.Embed IO) r => P.Sem (CacheEff ': r) a -> P.Sem r a
runCacheInMemoryNoMsg :: Sem (CacheEff : r) a -> Sem r a
runCacheInMemoryNoMsg m :: Sem (CacheEff : r) a
m = do
  IORef CacheNoMsg
var <- IO (IORef CacheNoMsg) -> Sem r (IORef CacheNoMsg)
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO (IORef CacheNoMsg) -> Sem r (IORef CacheNoMsg))
-> IO (IORef CacheNoMsg) -> Sem r (IORef CacheNoMsg)
forall a b. (a -> b) -> a -> b
$ CacheNoMsg -> IO (IORef CacheNoMsg)
forall a. a -> IO (IORef a)
newIORef CacheNoMsg
emptyCacheNoMsg
  IORef CacheNoMsg -> Sem (AtomicState CacheNoMsg : r) a -> Sem r a
forall s (r :: [(* -> *) -> * -> *]) a.
Member (Embed IO) r =>
IORef s -> Sem (AtomicState s : r) a -> Sem r a
P.runAtomicStateIORef IORef CacheNoMsg
var (Sem (AtomicState CacheNoMsg : r) a -> Sem r a)
-> Sem (AtomicState CacheNoMsg : r) a -> Sem r a
forall a b. (a -> b) -> a -> b
$ (forall (m :: * -> *) x.
 CacheEff m x -> Sem (AtomicState CacheNoMsg : r) x)
-> Sem (CacheEff : r) a -> Sem (AtomicState CacheNoMsg : r) a
forall (e1 :: (* -> *) -> * -> *) (e2 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
FirstOrder e1 "reinterpret" =>
(forall (m :: * -> *) x. e1 m x -> Sem (e2 : r) x)
-> Sem (e1 : r) a -> Sem (e2 : r) a
P.reinterpret forall k (t :: * -> *) (r :: [(* -> *) -> * -> *]) (m :: k) a.
(MessageMod (Cache t), Member (AtomicState (Cache t)) r) =>
CacheEff m a -> Sem r a
forall (m :: * -> *) x.
CacheEff m x -> Sem (AtomicState CacheNoMsg : r) x
runCache' Sem (CacheEff : r) a
m

-- | Run the cache in memory with a configurable message cache limit
runCacheInMemory' :: P.Member (P.Embed IO) r => Int -> P.Sem (CacheEff ': r) a -> P.Sem r a
runCacheInMemory' :: Int -> Sem (CacheEff : r) a -> Sem r a
runCacheInMemory' msgLimit :: Int
msgLimit m :: Sem (CacheEff : r) a
m = do
  IORef CacheWithMsg
var <- IO (IORef CacheWithMsg) -> Sem r (IORef CacheWithMsg)
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO (IORef CacheWithMsg) -> Sem r (IORef CacheWithMsg))
-> IO (IORef CacheWithMsg) -> Sem r (IORef CacheWithMsg)
forall a b. (a -> b) -> a -> b
$ CacheWithMsg -> IO (IORef CacheWithMsg)
forall a. a -> IO (IORef a)
newIORef (Int -> CacheWithMsg
emptyCache' Int
msgLimit)
  IORef CacheWithMsg
-> Sem (AtomicState CacheWithMsg : r) a -> Sem r a
forall s (r :: [(* -> *) -> * -> *]) a.
Member (Embed IO) r =>
IORef s -> Sem (AtomicState s : r) a -> Sem r a
P.runAtomicStateIORef IORef CacheWithMsg
var (Sem (AtomicState CacheWithMsg : r) a -> Sem r a)
-> Sem (AtomicState CacheWithMsg : r) a -> Sem r a
forall a b. (a -> b) -> a -> b
$ (forall (m :: * -> *) x.
 CacheEff m x -> Sem (AtomicState CacheWithMsg : r) x)
-> Sem (CacheEff : r) a -> Sem (AtomicState CacheWithMsg : r) a
forall (e1 :: (* -> *) -> * -> *) (e2 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
FirstOrder e1 "reinterpret" =>
(forall (m :: * -> *) x. e1 m x -> Sem (e2 : r) x)
-> Sem (e1 : r) a -> Sem (e2 : r) a
P.reinterpret forall k (t :: * -> *) (r :: [(* -> *) -> * -> *]) (m :: k) a.
(MessageMod (Cache t), Member (AtomicState (Cache t)) r) =>
CacheEff m a -> Sem r a
forall (m :: * -> *) x.
CacheEff m x -> Sem (AtomicState CacheWithMsg : r) x
runCache' Sem (CacheEff : r) a
m

runCache' :: (MessageMod (Cache t), P.Member (P.AtomicState (Cache t)) r) => CacheEff m a -> P.Sem r a
runCache' :: CacheEff m a -> Sem r a
runCache' act :: CacheEff m a
act = (Cache t -> (Cache t, a)) -> Sem r a
forall s a (r :: [(* -> *) -> * -> *]).
Member (AtomicState s) r =>
(s -> (s, a)) -> Sem r a
P.atomicState' (((a, Cache t) -> (Cache t, a)
forall a b. (a, b) -> (b, a)
swap ((a, Cache t) -> (Cache t, a))
-> (Cache t -> (a, Cache t)) -> Cache t -> (Cache t, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Cache t -> (a, Cache t)) -> Cache t -> (Cache t, a))
-> (State (Cache t) a -> Cache t -> (a, Cache t))
-> State (Cache t) a
-> Cache t
-> (Cache t, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State (Cache t) a -> Cache t -> (a, Cache t)
forall s a. State s a -> s -> (a, s)
runState (State (Cache t) a -> Cache t -> (Cache t, a))
-> State (Cache t) a -> Cache t -> (Cache t, a)
forall a b. (a -> b) -> a -> b
$ CacheEff m a -> State (Cache t) a
forall k (t :: * -> *) (m :: k) a.
MessageMod (Cache t) =>
CacheEff m a -> State (Cache t) a
runCache CacheEff m a
act)

class MessageMod t where
  setMessage' :: Message -> State t ()
  getMessage' :: Snowflake Message -> State t (Maybe Message)
  getMessages' :: State t [Message]
  delMessage' :: Snowflake Message -> State t ()

instance MessageMod CacheWithMsg where
  setMessage' :: Message -> State CacheWithMsg ()
setMessage' m :: Message
m = IsLabel
  "messages"
  ((Identity (BoundedStore Message)
    -> Identity (Identity (BoundedStore Message)))
   -> CacheWithMsg -> Identity CacheWithMsg)
(Identity (BoundedStore Message)
 -> Identity (Identity (BoundedStore Message)))
-> CacheWithMsg -> Identity CacheWithMsg
#messages ((Identity (BoundedStore Message)
  -> Identity (Identity (BoundedStore Message)))
 -> CacheWithMsg -> Identity CacheWithMsg)
-> ((BoundedStore Message -> Identity (BoundedStore Message))
    -> Identity (BoundedStore Message)
    -> Identity (Identity (BoundedStore Message)))
-> (BoundedStore Message -> Identity (BoundedStore Message))
-> CacheWithMsg
-> Identity CacheWithMsg
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BoundedStore Message -> Identity (BoundedStore Message))
-> Identity (BoundedStore Message)
-> Identity (Identity (BoundedStore Message))
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped ((BoundedStore Message -> Identity (BoundedStore Message))
 -> CacheWithMsg -> Identity CacheWithMsg)
-> (BoundedStore Message -> BoundedStore Message)
-> State CacheWithMsg ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Message -> BoundedStore Message -> BoundedStore Message
forall a. HasID' a => a -> BoundedStore a -> BoundedStore a
BS.addItem Message
m
  getMessage' :: Snowflake Message -> State CacheWithMsg (Maybe Message)
getMessage' mid :: Snowflake Message
mid = Getting (Maybe Message) CacheWithMsg (Maybe Message)
-> State CacheWithMsg (Maybe Message)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (IsLabel
  "messages"
  ((Identity (BoundedStore Message)
    -> Const (Maybe Message) (Identity (BoundedStore Message)))
   -> CacheWithMsg -> Const (Maybe Message) CacheWithMsg)
(Identity (BoundedStore Message)
 -> Const (Maybe Message) (Identity (BoundedStore Message)))
-> CacheWithMsg -> Const (Maybe Message) CacheWithMsg
#messages ((Identity (BoundedStore Message)
  -> Const (Maybe Message) (Identity (BoundedStore Message)))
 -> CacheWithMsg -> Const (Maybe Message) CacheWithMsg)
-> ((Maybe Message -> Const (Maybe Message) (Maybe Message))
    -> Identity (BoundedStore Message)
    -> Const (Maybe Message) (Identity (BoundedStore Message)))
-> Getting (Maybe Message) CacheWithMsg (Maybe Message)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BoundedStore Message
 -> Const (Maybe Message) (BoundedStore Message))
-> Identity (BoundedStore Message)
-> Const (Maybe Message) (Identity (BoundedStore Message))
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped ((BoundedStore Message
  -> Const (Maybe Message) (BoundedStore Message))
 -> Identity (BoundedStore Message)
 -> Const (Maybe Message) (Identity (BoundedStore Message)))
-> ((Maybe Message -> Const (Maybe Message) (Maybe Message))
    -> BoundedStore Message
    -> Const (Maybe Message) (BoundedStore Message))
-> (Maybe Message -> Const (Maybe Message) (Maybe Message))
-> Identity (BoundedStore Message)
-> Const (Maybe Message) (Identity (BoundedStore Message))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (BoundedStore Message)
-> Lens'
     (BoundedStore Message) (Maybe (IxValue (BoundedStore Message)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (BoundedStore Message)
Snowflake Message
mid)
  getMessages' :: State CacheWithMsg [Message]
getMessages' = BoundedStore Message -> [Message]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (BoundedStore Message -> [Message])
-> StateT CacheWithMsg Identity (BoundedStore Message)
-> State CacheWithMsg [Message]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Getting (BoundedStore Message) CacheWithMsg (BoundedStore Message)
-> StateT CacheWithMsg Identity (BoundedStore Message)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (IsLabel
  "messages"
  ((Identity (BoundedStore Message)
    -> Const (BoundedStore Message) (Identity (BoundedStore Message)))
   -> CacheWithMsg -> Const (BoundedStore Message) CacheWithMsg)
(Identity (BoundedStore Message)
 -> Const (BoundedStore Message) (Identity (BoundedStore Message)))
-> CacheWithMsg -> Const (BoundedStore Message) CacheWithMsg
#messages ((Identity (BoundedStore Message)
  -> Const (BoundedStore Message) (Identity (BoundedStore Message)))
 -> CacheWithMsg -> Const (BoundedStore Message) CacheWithMsg)
-> ((BoundedStore Message
     -> Const (BoundedStore Message) (BoundedStore Message))
    -> Identity (BoundedStore Message)
    -> Const (BoundedStore Message) (Identity (BoundedStore Message)))
-> Getting
     (BoundedStore Message) CacheWithMsg (BoundedStore Message)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BoundedStore Message
 -> Const (BoundedStore Message) (BoundedStore Message))
-> Identity (BoundedStore Message)
-> Const (BoundedStore Message) (Identity (BoundedStore Message))
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped)
  delMessage' :: Snowflake Message -> State CacheWithMsg ()
delMessage' mid :: Snowflake Message
mid = IsLabel
  "messages"
  ((Identity (BoundedStore Message)
    -> Identity (Identity (BoundedStore Message)))
   -> CacheWithMsg -> Identity CacheWithMsg)
(Identity (BoundedStore Message)
 -> Identity (Identity (BoundedStore Message)))
-> CacheWithMsg -> Identity CacheWithMsg
#messages ((Identity (BoundedStore Message)
  -> Identity (Identity (BoundedStore Message)))
 -> CacheWithMsg -> Identity CacheWithMsg)
-> ((BoundedStore Message -> Identity (BoundedStore Message))
    -> Identity (BoundedStore Message)
    -> Identity (Identity (BoundedStore Message)))
-> (BoundedStore Message -> Identity (BoundedStore Message))
-> CacheWithMsg
-> Identity CacheWithMsg
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BoundedStore Message -> Identity (BoundedStore Message))
-> Identity (BoundedStore Message)
-> Identity (Identity (BoundedStore Message))
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped ((BoundedStore Message -> Identity (BoundedStore Message))
 -> CacheWithMsg -> Identity CacheWithMsg)
-> (BoundedStore Message -> BoundedStore Message)
-> State CacheWithMsg ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Index (BoundedStore Message)
-> BoundedStore Message -> BoundedStore Message
forall m. At m => Index m -> m -> m
sans Index (BoundedStore Message)
Snowflake Message
mid

instance MessageMod CacheNoMsg where
  setMessage' :: Message -> State CacheNoMsg ()
setMessage' _ = () -> State CacheNoMsg ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  getMessage' :: Snowflake Message -> State CacheNoMsg (Maybe Message)
getMessage' _ = Maybe Message -> State CacheNoMsg (Maybe Message)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Message
forall a. Maybe a
Nothing
  getMessages' :: State CacheNoMsg [Message]
getMessages' = [Message] -> State CacheNoMsg [Message]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
  delMessage' :: Snowflake Message -> State CacheNoMsg ()
delMessage' _ = () -> State CacheNoMsg ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

runCache :: MessageMod (Cache t) => CacheEff m a -> State (Cache t) a

runCache :: CacheEff m a -> State (Cache t) a
runCache (SetBotUser u :: User
u) = IsLabel
  "user" (ASetter (Cache t) (Cache t) (Maybe User) (Maybe User))
ASetter (Cache t) (Cache t) (Maybe User) (Maybe User)
#user ASetter (Cache t) (Cache t) (Maybe User) (Maybe User)
-> User -> StateT (Cache t) Identity ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a (Maybe b) -> b -> m ()
?= User
u
runCache GetBotUser     = Getting a (Cache t) a -> State (Cache t) a
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use IsLabel "user" (Getting a (Cache t) a)
Getting a (Cache t) a
#user

runCache (SetGuild g :: Guild
g)   = do
  #guilds %= SM.insert g
  #guildChannels %= LH.filter (\v -> getID @Guild v /= getID @Guild g)
  #guildChannels %= LH.union (LH.fromList $ map (,g) (SM.keys (g ^. #channels)))
runCache (GetGuild gid :: Snowflake Guild
gid) = Getting a (Cache t) a -> State (Cache t) a
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (IsLabel
  "guilds"
  ((SnowflakeMap Guild -> Const (Maybe Guild) (SnowflakeMap Guild))
   -> Cache t -> Const a (Cache t))
(SnowflakeMap Guild -> Const (Maybe Guild) (SnowflakeMap Guild))
-> Cache t -> Const a (Cache t)
#guilds ((SnowflakeMap Guild -> Const (Maybe Guild) (SnowflakeMap Guild))
 -> Cache t -> Const a (Cache t))
-> ((a -> Const a a)
    -> SnowflakeMap Guild -> Const (Maybe Guild) (SnowflakeMap Guild))
-> Getting a (Cache t) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Guild)
-> Lens'
     (SnowflakeMap Guild) (Maybe (IxValue (SnowflakeMap Guild)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (SnowflakeMap Guild)
Snowflake Guild
gid)
runCache (GetGuildChannel cid :: Snowflake GuildChannel
cid) = Getting (Maybe Guild) (Cache t) (Maybe Guild)
-> StateT (Cache t) Identity (Maybe Guild)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (IsLabel
  "guildChannels"
  ((HashMap (Snowflake GuildChannel) Guild
    -> Const (Maybe Guild) (HashMap (Snowflake GuildChannel) Guild))
   -> Cache t -> Const (Maybe Guild) (Cache t))
(HashMap (Snowflake GuildChannel) Guild
 -> Const (Maybe Guild) (HashMap (Snowflake GuildChannel) Guild))
-> Cache t -> Const (Maybe Guild) (Cache t)
#guildChannels ((HashMap (Snowflake GuildChannel) Guild
  -> Const (Maybe Guild) (HashMap (Snowflake GuildChannel) Guild))
 -> Cache t -> Const (Maybe Guild) (Cache t))
-> ((Maybe Guild -> Const (Maybe Guild) (Maybe Guild))
    -> HashMap (Snowflake GuildChannel) Guild
    -> Const (Maybe Guild) (HashMap (Snowflake GuildChannel) Guild))
-> Getting (Maybe Guild) (Cache t) (Maybe Guild)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (HashMap (Snowflake GuildChannel) Guild)
-> Lens'
     (HashMap (Snowflake GuildChannel) Guild)
     (Maybe (IxValue (HashMap (Snowflake GuildChannel) Guild)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (HashMap (Snowflake GuildChannel) Guild)
Snowflake GuildChannel
cid) StateT (Cache t) Identity (Maybe Guild)
-> (Maybe Guild -> Maybe GuildChannel)
-> StateT (Cache t) Identity (Maybe GuildChannel)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (Maybe Guild -> (Guild -> Maybe GuildChannel) -> Maybe GuildChannel
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Guild
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
-> Maybe GuildChannel
forall s a. s -> Getting a s a -> a
^. IsLabel
  "channels"
  ((SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
   -> Guild -> Const (Maybe GuildChannel) Guild)
(SnowflakeMap GuildChannel
 -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Guild -> Const (Maybe GuildChannel) Guild
#channels ((SnowflakeMap GuildChannel
  -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
 -> Guild -> Const (Maybe GuildChannel) Guild)
-> ((Maybe GuildChannel
     -> Const (Maybe GuildChannel) (Maybe GuildChannel))
    -> SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
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 Index (SnowflakeMap GuildChannel)
Snowflake GuildChannel
cid))
runCache GetGuilds      = SnowflakeMap Guild -> [Guild]
forall a. SnowflakeMap a -> [a]
SM.elems (SnowflakeMap Guild -> [Guild])
-> StateT (Cache t) Identity (SnowflakeMap Guild)
-> StateT (Cache t) Identity [Guild]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Getting (SnowflakeMap Guild) (Cache t) (SnowflakeMap Guild)
-> StateT (Cache t) Identity (SnowflakeMap Guild)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use IsLabel
  "guilds"
  (Getting (SnowflakeMap Guild) (Cache t) (SnowflakeMap Guild))
Getting (SnowflakeMap Guild) (Cache t) (SnowflakeMap Guild)
#guilds
runCache (DelGuild gid :: Snowflake Guild
gid) = do
  #guilds %= sans gid
  #guildChannels %= LH.filter (\v -> getID @Guild v /= gid)

runCache (SetDM dm :: DMChannel
dm)  = IsLabel
  "dms"
  (ASetter
     (Cache t)
     (Cache t)
     (SnowflakeMap DMChannel)
     (SnowflakeMap DMChannel))
ASetter
  (Cache t)
  (Cache t)
  (SnowflakeMap DMChannel)
  (SnowflakeMap DMChannel)
#dms ASetter
  (Cache t)
  (Cache t)
  (SnowflakeMap DMChannel)
  (SnowflakeMap DMChannel)
-> (SnowflakeMap DMChannel -> SnowflakeMap DMChannel)
-> StateT (Cache t) Identity ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= DMChannel -> SnowflakeMap DMChannel -> SnowflakeMap DMChannel
forall a. HasID' a => a -> SnowflakeMap a -> SnowflakeMap a
SM.insert DMChannel
dm
runCache (GetDM did :: Snowflake DMChannel
did) = Getting a (Cache t) a -> State (Cache t) a
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (IsLabel
  "dms"
  ((SnowflakeMap DMChannel
    -> Const (Maybe DMChannel) (SnowflakeMap DMChannel))
   -> Cache t -> Const a (Cache t))
(SnowflakeMap DMChannel
 -> Const (Maybe DMChannel) (SnowflakeMap DMChannel))
-> Cache t -> Const a (Cache t)
#dms ((SnowflakeMap DMChannel
  -> Const (Maybe DMChannel) (SnowflakeMap DMChannel))
 -> Cache t -> Const a (Cache t))
-> ((a -> Const a a)
    -> SnowflakeMap DMChannel
    -> Const (Maybe DMChannel) (SnowflakeMap DMChannel))
-> Getting a (Cache t) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap DMChannel)
-> Lens'
     (SnowflakeMap DMChannel) (Maybe (IxValue (SnowflakeMap DMChannel)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (SnowflakeMap DMChannel)
Snowflake DMChannel
did)
runCache GetDMs      = SnowflakeMap DMChannel -> [DMChannel]
forall a. SnowflakeMap a -> [a]
SM.elems (SnowflakeMap DMChannel -> [DMChannel])
-> StateT (Cache t) Identity (SnowflakeMap DMChannel)
-> StateT (Cache t) Identity [DMChannel]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Getting (SnowflakeMap DMChannel) (Cache t) (SnowflakeMap DMChannel)
-> StateT (Cache t) Identity (SnowflakeMap DMChannel)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use IsLabel
  "dms"
  (Getting
     (SnowflakeMap DMChannel) (Cache t) (SnowflakeMap DMChannel))
Getting (SnowflakeMap DMChannel) (Cache t) (SnowflakeMap DMChannel)
#dms
runCache (DelDM did :: Snowflake DMChannel
did) = IsLabel
  "dms"
  (ASetter
     (Cache t)
     (Cache t)
     (SnowflakeMap DMChannel)
     (SnowflakeMap DMChannel))
ASetter
  (Cache t)
  (Cache t)
  (SnowflakeMap DMChannel)
  (SnowflakeMap DMChannel)
#dms ASetter
  (Cache t)
  (Cache t)
  (SnowflakeMap DMChannel)
  (SnowflakeMap DMChannel)
-> (SnowflakeMap DMChannel -> SnowflakeMap DMChannel)
-> StateT (Cache t) Identity ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Index (SnowflakeMap DMChannel)
-> SnowflakeMap DMChannel -> SnowflakeMap DMChannel
forall m. At m => Index m -> m -> m
sans Index (SnowflakeMap DMChannel)
Snowflake DMChannel
did

runCache (SetUser u :: User
u)   = IsLabel
  "users"
  (ASetter
     (Cache t) (Cache t) (SnowflakeMap User) (SnowflakeMap User))
ASetter (Cache t) (Cache t) (SnowflakeMap User) (SnowflakeMap User)
#users ASetter (Cache t) (Cache t) (SnowflakeMap User) (SnowflakeMap User)
-> (SnowflakeMap User -> SnowflakeMap User)
-> StateT (Cache t) Identity ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= User -> SnowflakeMap User -> SnowflakeMap User
forall a. HasID' a => a -> SnowflakeMap a -> SnowflakeMap a
SM.insert User
u
runCache (GetUser uid :: Snowflake User
uid) = Getting a (Cache t) a -> State (Cache t) a
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (IsLabel
  "users"
  ((SnowflakeMap User -> Const (Maybe User) (SnowflakeMap User))
   -> Cache t -> Const a (Cache t))
(SnowflakeMap User -> Const (Maybe User) (SnowflakeMap User))
-> Cache t -> Const a (Cache t)
#users ((SnowflakeMap User -> Const (Maybe User) (SnowflakeMap User))
 -> Cache t -> Const a (Cache t))
-> ((a -> Const a a)
    -> SnowflakeMap User -> Const (Maybe User) (SnowflakeMap User))
-> Getting a (Cache t) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap User)
-> Lens' (SnowflakeMap User) (Maybe (IxValue (SnowflakeMap User)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (SnowflakeMap User)
Snowflake User
uid)
runCache GetUsers      = SnowflakeMap User -> [User]
forall a. SnowflakeMap a -> [a]
SM.elems (SnowflakeMap User -> [User])
-> StateT (Cache t) Identity (SnowflakeMap User)
-> StateT (Cache t) Identity [User]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Getting (SnowflakeMap User) (Cache t) (SnowflakeMap User)
-> StateT (Cache t) Identity (SnowflakeMap User)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use IsLabel
  "users" (Getting (SnowflakeMap User) (Cache t) (SnowflakeMap User))
Getting (SnowflakeMap User) (Cache t) (SnowflakeMap User)
#users
runCache (DelUser uid :: Snowflake User
uid) = IsLabel
  "users"
  (ASetter
     (Cache t) (Cache t) (SnowflakeMap User) (SnowflakeMap User))
ASetter (Cache t) (Cache t) (SnowflakeMap User) (SnowflakeMap User)
#users ASetter (Cache t) (Cache t) (SnowflakeMap User) (SnowflakeMap User)
-> (SnowflakeMap User -> SnowflakeMap User)
-> StateT (Cache t) Identity ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Index (SnowflakeMap User) -> SnowflakeMap User -> SnowflakeMap User
forall m. At m => Index m -> m -> m
sans Index (SnowflakeMap User)
Snowflake User
uid

runCache (SetUnavailableGuild gid :: Snowflake Guild
gid) = IsLabel
  "unavailableGuilds"
  (ASetter
     (Cache t)
     (Cache t)
     (HashSet (Snowflake Guild))
     (HashSet (Snowflake Guild)))
ASetter
  (Cache t)
  (Cache t)
  (HashSet (Snowflake Guild))
  (HashSet (Snowflake Guild))
#unavailableGuilds ASetter
  (Cache t)
  (Cache t)
  (HashSet (Snowflake Guild))
  (HashSet (Snowflake Guild))
-> (HashSet (Snowflake Guild) -> HashSet (Snowflake Guild))
-> StateT (Cache t) Identity ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Snowflake Guild
-> HashSet (Snowflake Guild) -> HashSet (Snowflake Guild)
forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
LS.insert Snowflake Guild
gid
runCache (IsUnavailableGuild gid :: Snowflake Guild
gid)  = Getting Bool (Cache t) Bool -> StateT (Cache t) Identity Bool
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (IsLabel
  "unavailableGuilds"
  ((HashSet (Snowflake Guild)
    -> Const Bool (HashSet (Snowflake Guild)))
   -> Cache t -> Const Bool (Cache t))
(HashSet (Snowflake Guild)
 -> Const Bool (HashSet (Snowflake Guild)))
-> Cache t -> Const Bool (Cache t)
#unavailableGuilds ((HashSet (Snowflake Guild)
  -> Const Bool (HashSet (Snowflake Guild)))
 -> Cache t -> Const Bool (Cache t))
-> ((Bool -> Const Bool Bool)
    -> HashSet (Snowflake Guild)
    -> Const Bool (HashSet (Snowflake Guild)))
-> Getting Bool (Cache t) Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (HashSet (Snowflake Guild))
-> Lens' (HashSet (Snowflake Guild)) Bool
forall m. Contains m => Index m -> Lens' m Bool
contains Index (HashSet (Snowflake Guild))
Snowflake Guild
gid)
runCache GetUnavailableGuilds      = HashSet (Snowflake Guild) -> [Snowflake Guild]
forall a. HashSet a -> [a]
LS.toList (HashSet (Snowflake Guild) -> [Snowflake Guild])
-> StateT (Cache t) Identity (HashSet (Snowflake Guild))
-> StateT (Cache t) Identity [Snowflake Guild]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Getting
  (HashSet (Snowflake Guild)) (Cache t) (HashSet (Snowflake Guild))
-> StateT (Cache t) Identity (HashSet (Snowflake Guild))
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use IsLabel
  "unavailableGuilds"
  (Getting
     (HashSet (Snowflake Guild)) (Cache t) (HashSet (Snowflake Guild)))
Getting
  (HashSet (Snowflake Guild)) (Cache t) (HashSet (Snowflake Guild))
#unavailableGuilds
runCache (DelUnavailableGuild gid :: Snowflake Guild
gid) = IsLabel
  "unavailableGuilds"
  (ASetter
     (Cache t)
     (Cache t)
     (HashSet (Snowflake Guild))
     (HashSet (Snowflake Guild)))
ASetter
  (Cache t)
  (Cache t)
  (HashSet (Snowflake Guild))
  (HashSet (Snowflake Guild))
#unavailableGuilds ASetter
  (Cache t)
  (Cache t)
  (HashSet (Snowflake Guild))
  (HashSet (Snowflake Guild))
-> (HashSet (Snowflake Guild) -> HashSet (Snowflake Guild))
-> StateT (Cache t) Identity ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Index (HashSet (Snowflake Guild))
-> HashSet (Snowflake Guild) -> HashSet (Snowflake Guild)
forall m. At m => Index m -> m -> m
sans Index (HashSet (Snowflake Guild))
Snowflake Guild
gid

runCache (SetMessage m :: Message
m)   = Message -> StateT (Cache t) Identity ()
forall t. MessageMod t => Message -> State t ()
setMessage' Message
m
runCache (GetMessage mid :: Snowflake Message
mid) = Snowflake Message -> State (Cache t) (Maybe Message)
forall t.
MessageMod t =>
Snowflake Message -> State t (Maybe Message)
getMessage' Snowflake Message
mid
runCache GetMessages      = State (Cache t) a
forall t. MessageMod t => State t [Message]
getMessages'
runCache (DelMessage mid :: Snowflake Message
mid) = Snowflake Message -> StateT (Cache t) Identity ()
forall t. MessageMod t => Snowflake Message -> State t ()
delMessage' Snowflake Message
mid