twilight_cache_inmemory/event/
stage_instance.rs1use crate::{config::ResourceType, CacheableModels, InMemoryCache, UpdateCache};
2use twilight_model::{
3 channel::StageInstance,
4 gateway::payload::incoming::{StageInstanceCreate, StageInstanceDelete, StageInstanceUpdate},
5 id::{
6 marker::{GuildMarker, StageMarker},
7 Id,
8 },
9};
10
11impl<CacheModels: CacheableModels> InMemoryCache<CacheModels> {
12 pub(crate) fn cache_stage_instances(
13 &self,
14 guild_id: Id<GuildMarker>,
15 stage_instances: impl IntoIterator<Item = StageInstance>,
16 ) {
17 for stage_instance in stage_instances {
18 self.cache_stage_instance(guild_id, stage_instance);
19 }
20 }
21
22 fn cache_stage_instance(&self, guild_id: Id<GuildMarker>, stage_instance: StageInstance) {
23 self.guild_stage_instances
24 .entry(guild_id)
25 .or_default()
26 .insert(stage_instance.id);
27
28 crate::upsert_guild_item(
29 &self.stage_instances,
30 guild_id,
31 stage_instance.id,
32 CacheModels::StageInstance::from(stage_instance),
33 );
34 }
35
36 fn delete_stage_instance(&self, stage_id: Id<StageMarker>) {
37 if let Some((_, data)) = self.stage_instances.remove(&stage_id) {
38 let guild_id = data.guild_id;
39
40 if let Some(mut stage_instances) = self.guild_stage_instances.get_mut(&guild_id) {
41 stage_instances.remove(&stage_id);
42 }
43 }
44 }
45}
46
47impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for StageInstanceCreate {
48 fn update(&self, cache: &InMemoryCache<CacheModels>) {
49 if !cache.wants(ResourceType::STAGE_INSTANCE) {
50 return;
51 }
52
53 cache.cache_stage_instance(self.guild_id, self.0.clone());
54 }
55}
56
57impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for StageInstanceDelete {
58 fn update(&self, cache: &InMemoryCache<CacheModels>) {
59 if !cache.wants(ResourceType::STAGE_INSTANCE) {
60 return;
61 }
62
63 cache.delete_stage_instance(self.id);
64 }
65}
66
67impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for StageInstanceUpdate {
68 fn update(&self, cache: &InMemoryCache<CacheModels>) {
69 if !cache.wants(ResourceType::STAGE_INSTANCE) {
70 return;
71 }
72
73 cache.cache_stage_instance(self.guild_id, self.0.clone());
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use crate::DefaultInMemoryCache;
80 use twilight_model::{
81 channel::{stage_instance::PrivacyLevel, StageInstance},
82 gateway::payload::incoming::{
83 StageInstanceCreate, StageInstanceDelete, StageInstanceUpdate,
84 },
85 id::Id,
86 };
87
88 #[test]
89 fn stage_channels() {
90 let cache = DefaultInMemoryCache::new();
91
92 let stage_instance = StageInstance {
93 channel_id: Id::new(1),
94 guild_id: Id::new(2),
95 guild_scheduled_event_id: Some(Id::new(3)),
96 id: Id::new(4),
97 privacy_level: PrivacyLevel::GuildOnly,
98 topic: "topic".into(),
99 };
100
101 cache.update(&StageInstanceCreate(stage_instance.clone()));
102
103 {
104 let cached_instances = cache
105 .guild_stage_instances(stage_instance.guild_id)
106 .unwrap();
107 assert_eq!(1, cached_instances.len());
108 }
109
110 {
111 let cached_instance = cache.stage_instance(stage_instance.id).unwrap();
112 assert_eq!(
113 stage_instance.guild_scheduled_event_id,
114 cached_instance.guild_scheduled_event_id
115 );
116 assert_eq!(stage_instance.topic, cached_instance.topic);
117 }
118
119 let new_stage_instance = StageInstance {
120 topic: "a new topic".into(),
121 ..stage_instance
122 };
123
124 cache.update(&StageInstanceUpdate(new_stage_instance.clone()));
125
126 {
127 let cached_instance = cache.stage_instance(stage_instance.id).unwrap();
128 assert_ne!(stage_instance.topic, cached_instance.topic);
129 assert_eq!(new_stage_instance.topic, "a new topic");
130 }
131
132 cache.update(&StageInstanceDelete(new_stage_instance));
133
134 {
135 let cached_instances = cache
136 .guild_stage_instances(stage_instance.guild_id)
137 .unwrap();
138 assert_eq!(0, cached_instances.len());
139 }
140
141 {
142 let cached_instance = cache.stage_instance(stage_instance.id);
143 assert!(cached_instance.is_none());
144 }
145 }
146}