twilight_cache_inmemory/event/
mod.rs

1pub mod channel;
2pub mod emoji;
3pub mod guild;
4pub mod guild_scheduled_events;
5pub mod integration;
6pub mod interaction;
7pub mod member;
8pub mod message;
9pub mod presence;
10pub mod reaction;
11pub mod role;
12pub mod stage_instance;
13pub mod sticker;
14pub mod thread;
15pub mod voice_state;
16
17use std::{borrow::Cow, collections::HashSet};
18
19use crate::{config::ResourceType, CacheableModels, InMemoryCache, UpdateCache};
20use twilight_model::{
21    gateway::payload::incoming::{Ready, UnavailableGuild, UserUpdate},
22    id::{marker::GuildMarker, Id},
23    user::{CurrentUser, User},
24};
25
26impl<CacheModels: CacheableModels> InMemoryCache<CacheModels> {
27    fn cache_current_user(&self, current_user: CurrentUser) {
28        self.current_user
29            .lock()
30            .expect("current user poisoned")
31            .replace(CacheModels::CurrentUser::from(current_user));
32    }
33
34    pub(crate) fn cache_user(&self, user: Cow<'_, User>, guild_id: Option<Id<GuildMarker>>) {
35        if let Some(cached_user) = self.users.get_mut(&user.id) {
36            if cached_user.value() == user.as_ref() {
37                if let Some(guild_id) = guild_id {
38                    self.user_guilds
39                        .entry(user.id)
40                        .or_default()
41                        .insert(guild_id);
42                }
43
44                return;
45            }
46        }
47
48        let user = user.into_owned();
49        let user_id = user.id;
50
51        self.users.insert(user_id, CacheModels::User::from(user));
52
53        if let Some(guild_id) = guild_id {
54            let mut guild_id_set = HashSet::new();
55            guild_id_set.insert(guild_id);
56            self.user_guilds.insert(user_id, guild_id_set);
57        }
58    }
59
60    fn unavailable_guild(&self, guild_id: Id<GuildMarker>) {
61        self.unavailable_guilds.insert(guild_id);
62        self.delete_guild(guild_id, true);
63    }
64}
65
66impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for Ready {
67    fn update(&self, cache: &InMemoryCache<CacheModels>) {
68        if cache.wants(ResourceType::USER_CURRENT) {
69            cache.cache_current_user(self.user.clone());
70        }
71
72        if cache.wants(ResourceType::GUILD) {
73            for guild in &self.guilds {
74                cache.unavailable_guild(guild.id);
75            }
76        }
77    }
78}
79
80impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for UnavailableGuild {
81    fn update(&self, cache: &InMemoryCache<CacheModels>) {
82        if cache.wants(ResourceType::GUILD) {
83            cache.unavailable_guild(self.id);
84        }
85    }
86}
87
88impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for UserUpdate {
89    fn update(&self, cache: &InMemoryCache<CacheModels>) {
90        if !cache.wants(ResourceType::USER_CURRENT) {
91            return;
92        }
93
94        cache.cache_current_user(self.0.clone());
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use crate::{test, DefaultInMemoryCache};
101
102    /// Test retrieval of the current user, notably that it doesn't simply
103    /// panic or do anything funny. This is the only synchronous mutex that we
104    /// might have trouble with across await points if we're not careful.
105    #[test]
106    fn current_user_retrieval() {
107        let cache = DefaultInMemoryCache::new();
108        assert!(cache.current_user().is_none());
109        cache.cache_current_user(test::current_user(1));
110        assert!(cache.current_user().is_some());
111    }
112}