twilight_cache_inmemory/event/
integration.rs

1use crate::{CacheableModels, InMemoryCache, UpdateCache, config::ResourceType};
2use twilight_model::{
3    gateway::payload::incoming::{IntegrationCreate, IntegrationDelete, IntegrationUpdate},
4    guild::GuildIntegration,
5    id::{
6        Id,
7        marker::{GuildMarker, IntegrationMarker},
8    },
9};
10
11impl<CacheModels: CacheableModels> InMemoryCache<CacheModels> {
12    fn cache_integration(&self, guild_id: Id<GuildMarker>, integration: GuildIntegration) {
13        self.guild_integrations
14            .entry(guild_id)
15            .or_default()
16            .insert(integration.id);
17
18        crate::upsert_guild_item(
19            &self.integrations,
20            guild_id,
21            (guild_id, integration.id),
22            CacheModels::GuildIntegration::from(integration),
23        );
24    }
25
26    fn delete_integration(&self, guild_id: Id<GuildMarker>, integration_id: Id<IntegrationMarker>) {
27        if self
28            .integrations
29            .remove(&(guild_id, integration_id))
30            .is_some()
31            && let Some(mut integrations) = self.guild_integrations.get_mut(&guild_id)
32        {
33            integrations.remove(&integration_id);
34        }
35    }
36}
37
38impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for IntegrationCreate {
39    fn update(&self, cache: &InMemoryCache<CacheModels>) {
40        if !cache.wants(ResourceType::INTEGRATION) {
41            return;
42        }
43
44        if let Some(guild_id) = self.guild_id {
45            crate::upsert_guild_item(
46                &cache.integrations,
47                guild_id,
48                (guild_id, self.id),
49                CacheModels::GuildIntegration::from(self.0.clone()),
50            );
51        }
52    }
53}
54
55impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for IntegrationDelete {
56    fn update(&self, cache: &InMemoryCache<CacheModels>) {
57        if !cache.wants(ResourceType::INTEGRATION) {
58            return;
59        }
60
61        cache.delete_integration(self.guild_id, self.id);
62    }
63}
64
65impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for IntegrationUpdate {
66    fn update(&self, cache: &InMemoryCache<CacheModels>) {
67        if !cache.wants(ResourceType::INTEGRATION) {
68            return;
69        }
70
71        if let Some(guild_id) = self.guild_id {
72            cache.cache_integration(guild_id, self.0.clone());
73        }
74    }
75}