twilight_cache_inmemory/event/
guild_scheduled_events.rs1use twilight_model::{
2 gateway::payload::incoming::{
3 GuildScheduledEventCreate, GuildScheduledEventDelete, GuildScheduledEventUpdate,
4 GuildScheduledEventUserAdd, GuildScheduledEventUserRemove,
5 },
6 guild::scheduled_event::GuildScheduledEvent,
7 id::{marker::GuildMarker, Id},
8};
9
10use crate::{
11 traits::CacheableGuildScheduledEvent, CacheableModels, InMemoryCache, ResourceType, UpdateCache,
12};
13
14impl<CacheModels: CacheableModels> InMemoryCache<CacheModels> {
15 pub(crate) fn cache_guild_scheduled_events(
16 &self,
17 guild_id: Id<GuildMarker>,
18 guild_scheduled_events: impl IntoIterator<Item = GuildScheduledEvent>,
19 ) {
20 for event in guild_scheduled_events {
21 self.cache_guild_scheduled_event(guild_id, event);
22 }
23 }
24
25 fn cache_guild_scheduled_event(
26 &self,
27 guild_id: Id<GuildMarker>,
28 guild_scheduled_event: GuildScheduledEvent,
29 ) {
30 self.guild_scheduled_events
31 .entry(guild_id)
32 .or_default()
33 .insert(guild_scheduled_event.id);
34
35 crate::upsert_guild_item(
36 &self.scheduled_events,
37 guild_id,
38 guild_scheduled_event.id,
39 CacheModels::GuildScheduledEvent::from(guild_scheduled_event),
40 );
41 }
42}
43
44impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for GuildScheduledEventCreate {
45 fn update(&self, cache: &InMemoryCache<CacheModels>) {
46 if !cache.wants(ResourceType::GUILD_SCHEDULED_EVENT) {
47 return;
48 }
49
50 cache.cache_guild_scheduled_event(self.guild_id, self.0.clone());
51 }
52}
53
54impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for GuildScheduledEventDelete {
55 fn update(&self, cache: &InMemoryCache<CacheModels>) {
56 if !cache.wants(ResourceType::GUILD_SCHEDULED_EVENT) {
57 return;
58 }
59
60 if cache.scheduled_events.remove(&self.id).is_some() {
61 if let Some(mut events) = cache.guild_scheduled_events.get_mut(&self.guild_id) {
62 events.remove(&self.id);
63 }
64 }
65 }
66}
67
68impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for GuildScheduledEventUpdate {
69 fn update(&self, cache: &InMemoryCache<CacheModels>) {
70 if !cache.wants(ResourceType::GUILD_SCHEDULED_EVENT) {
71 return;
72 }
73
74 cache.cache_guild_scheduled_event(self.guild_id, self.0.clone());
75 }
76}
77
78impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for GuildScheduledEventUserAdd {
79 fn update(&self, cache: &InMemoryCache<CacheModels>) {
80 cache
81 .scheduled_events
82 .entry(self.guild_scheduled_event_id)
83 .and_modify(|event| {
84 event
85 .value
86 .add_user(self.guild_id, self.guild_scheduled_event_id, self.user_id);
87 });
88 }
89}
90
91impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for GuildScheduledEventUserRemove {
92 fn update(&self, cache: &InMemoryCache<CacheModels>) {
93 cache
94 .scheduled_events
95 .entry(self.guild_scheduled_event_id)
96 .and_modify(|event| {
97 event
98 .value
99 .remove_user(self.guild_id, self.guild_scheduled_event_id, self.user_id);
100 });
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use crate::{test, DefaultInMemoryCache};
107 use twilight_model::{
108 gateway::payload::incoming::{
109 GuildScheduledEventCreate, GuildScheduledEventUserAdd, GuildScheduledEventUserRemove,
110 },
111 id::Id,
112 };
113
114 #[test]
115 fn guild_event_on_event() {
116 let cache = DefaultInMemoryCache::new();
117
118 let id = Id::new(1);
119 let guild_id = Id::new(2);
120 let user_id = Id::new(3);
121
122 cache.update(&GuildScheduledEventCreate(test::guild_scheduled_event(
123 id,
124 guild_id,
125 Some(41),
126 )));
127
128 assert_eq!(
129 1,
130 cache.guild_scheduled_events.get(&guild_id).unwrap().len()
131 );
132 assert_eq!(1, cache.scheduled_events.len());
133 assert_eq!(
134 41,
135 cache.scheduled_events.get(&id).unwrap().user_count.unwrap()
136 );
137
138 cache.update(&GuildScheduledEventUserAdd {
139 guild_id,
140 guild_scheduled_event_id: id,
141 user_id,
142 });
143
144 assert_eq!(
145 42,
146 cache.scheduled_events.get(&id).unwrap().user_count.unwrap()
147 );
148
149 cache.update(&GuildScheduledEventUserRemove {
150 guild_id,
151 guild_scheduled_event_id: id,
152 user_id,
153 });
154
155 assert_eq!(
156 41,
157 cache.scheduled_events.get(&id).unwrap().user_count.unwrap()
158 );
159 }
160}