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::{CacheableModels, InMemoryCache, UpdateCache, config::ResourceType};
20use twilight_model::{
21    gateway::payload::incoming::{Ready, UnavailableGuild, UserUpdate},
22    id::{Id, marker::GuildMarker},
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            && cached_user.value() == user.as_ref()
37            && let Some(guild_id) = guild_id
38        {
39            self.user_guilds
40                .entry(user.id)
41                .or_default()
42                .insert(guild_id);
43
44            return;
45        }
46
47        let user = user.into_owned();
48        let user_id = user.id;
49
50        self.users.insert(user_id, CacheModels::User::from(user));
51
52        if let Some(guild_id) = guild_id {
53            let mut guild_id_set = HashSet::new();
54            guild_id_set.insert(guild_id);
55            self.user_guilds.insert(user_id, guild_id_set);
56        }
57    }
58
59    fn unavailable_guild(&self, guild_id: Id<GuildMarker>) {
60        self.unavailable_guilds.insert(guild_id);
61        self.delete_guild(guild_id, true);
62    }
63}
64
65impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for Ready {
66    fn update(&self, cache: &InMemoryCache<CacheModels>) {
67        if cache.wants(ResourceType::USER_CURRENT) {
68            cache.cache_current_user(self.user.clone());
69        }
70
71        if cache.wants(ResourceType::GUILD) {
72            for guild in &self.guilds {
73                cache.unavailable_guild(guild.id);
74            }
75        }
76    }
77}
78
79impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for UnavailableGuild {
80    fn update(&self, cache: &InMemoryCache<CacheModels>) {
81        if cache.wants(ResourceType::GUILD) {
82            cache.unavailable_guild(self.id);
83        }
84    }
85}
86
87impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for UserUpdate {
88    fn update(&self, cache: &InMemoryCache<CacheModels>) {
89        if !cache.wants(ResourceType::USER_CURRENT) {
90            return;
91        }
92
93        cache.cache_current_user(self.0.clone());
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use crate::{DefaultInMemoryCache, test};
100
101    /// Test retrieval of the current user, notably that it doesn't simply
102    /// panic or do anything funny. This is the only synchronous mutex that we
103    /// might have trouble with across await points if we're not careful.
104    #[test]
105    fn current_user_retrieval() {
106        let cache = DefaultInMemoryCache::new();
107        assert!(cache.current_user().is_none());
108        cache.cache_current_user(test::current_user(1));
109        assert!(cache.current_user().is_some());
110    }
111}