twilight_cache_inmemory/event/
integration.rs1use crate::{config::ResourceType, CacheableModels, InMemoryCache, UpdateCache};
2use twilight_model::{
3 gateway::payload::incoming::{IntegrationCreate, IntegrationDelete, IntegrationUpdate},
4 guild::GuildIntegration,
5 id::{
6 marker::{GuildMarker, IntegrationMarker},
7 Id,
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 {
32 if let Some(mut integrations) = self.guild_integrations.get_mut(&guild_id) {
33 integrations.remove(&integration_id);
34 }
35 }
36 }
37}
38
39impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for IntegrationCreate {
40 fn update(&self, cache: &InMemoryCache<CacheModels>) {
41 if !cache.wants(ResourceType::INTEGRATION) {
42 return;
43 }
44
45 if let Some(guild_id) = self.guild_id {
46 crate::upsert_guild_item(
47 &cache.integrations,
48 guild_id,
49 (guild_id, self.id),
50 CacheModels::GuildIntegration::from(self.0.clone()),
51 );
52 }
53 }
54}
55
56impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for IntegrationDelete {
57 fn update(&self, cache: &InMemoryCache<CacheModels>) {
58 if !cache.wants(ResourceType::INTEGRATION) {
59 return;
60 }
61
62 cache.delete_integration(self.guild_id, self.id);
63 }
64}
65
66impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for IntegrationUpdate {
67 fn update(&self, cache: &InMemoryCache<CacheModels>) {
68 if !cache.wants(ResourceType::INTEGRATION) {
69 return;
70 }
71
72 if let Some(guild_id) = self.guild_id {
73 cache.cache_integration(guild_id, self.0.clone());
74 }
75 }
76}