11use std:: collections:: VecDeque ;
22
3+ use extract_map:: entry:: Entry ;
4+
35use super :: { BaseGuildChannel , Cache , CacheUpdate , GenericChannelId , GuildThread } ;
46use crate :: internal:: prelude:: * ;
57use crate :: model:: channel:: { GuildChannel , Message } ;
@@ -18,6 +20,9 @@ use crate::model::event::{
1820 GuildRoleCreateEvent ,
1921 GuildRoleDeleteEvent ,
2022 GuildRoleUpdateEvent ,
23+ GuildScheduledEventCreateEvent ,
24+ GuildScheduledEventDeleteEvent ,
25+ GuildScheduledEventUpdateEvent ,
2126 GuildStickersUpdateEvent ,
2227 GuildUpdateEvent ,
2328 MessageCreateEvent ,
@@ -26,13 +31,21 @@ use crate::model::event::{
2631 ReadyEvent ,
2732 ThreadCreateEvent ,
2833 ThreadDeleteEvent ,
34+ ThreadListSyncEvent ,
2935 ThreadUpdateEvent ,
3036 UserUpdateEvent ,
3137 VoiceChannelStatusUpdateEvent ,
3238 VoiceStateUpdateEvent ,
3339} ;
3440use crate :: model:: gateway:: Presence ;
35- use crate :: model:: guild:: { Guild , GuildMemberFlags , Member , MemberGeneratedFlags , Role } ;
41+ use crate :: model:: guild:: {
42+ Guild ,
43+ GuildMemberFlags ,
44+ Member ,
45+ MemberGeneratedFlags ,
46+ Role ,
47+ ScheduledEvent ,
48+ } ;
3649use crate :: model:: user:: { CurrentUser , OnlineStatus } ;
3750use crate :: model:: voice:: VoiceState ;
3851
@@ -72,9 +85,9 @@ impl CacheUpdate for ChannelUpdateEvent {
7285}
7386
7487impl CacheUpdate for ChannelPinsUpdateEvent {
75- type Output = ( ) ;
88+ type Output = std :: convert :: Infallible ;
7689
77- fn update ( & mut self , cache : & Cache ) -> Option < ( ) > {
90+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
7891 if let Some ( guild_id) = self . guild_id {
7992 if let Some ( mut guild) = cache. guilds . get_mut ( & guild_id) {
8093 let ( channel_id, thread_id) = self . channel_id . split ( ) ;
@@ -94,9 +107,9 @@ impl CacheUpdate for ChannelPinsUpdateEvent {
94107}
95108
96109impl CacheUpdate for GuildCreateEvent {
97- type Output = ( ) ;
110+ type Output = std :: convert :: Infallible ;
98111
99- fn update ( & mut self , cache : & Cache ) -> Option < ( ) > {
112+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
100113 cache. unavailable_guilds . remove ( & self . guild . id ) ;
101114 let guild = self . guild . clone ( ) ;
102115
@@ -132,9 +145,9 @@ impl CacheUpdate for GuildDeleteEvent {
132145}
133146
134147impl CacheUpdate for GuildEmojisUpdateEvent {
135- type Output = ( ) ;
148+ type Output = std :: convert :: Infallible ;
136149
137- fn update ( & mut self , cache : & Cache ) -> Option < ( ) > {
150+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
138151 if let Some ( mut guild) = cache. guilds . get_mut ( & self . guild_id ) {
139152 guild. emojis . clone_from ( & self . emojis ) ;
140153 }
@@ -144,9 +157,9 @@ impl CacheUpdate for GuildEmojisUpdateEvent {
144157}
145158
146159impl CacheUpdate for GuildMemberAddEvent {
147- type Output = ( ) ;
160+ type Output = std :: convert :: Infallible ;
148161
149- fn update ( & mut self , cache : & Cache ) -> Option < ( ) > {
162+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
150163 if let Some ( mut guild) = cache. guilds . get_mut ( & self . member . guild_id ) {
151164 guild. member_count += 1 ;
152165 guild. members . insert ( self . member . clone ( ) ) ;
@@ -225,9 +238,9 @@ impl CacheUpdate for GuildMemberUpdateEvent {
225238}
226239
227240impl CacheUpdate for GuildMembersChunkEvent {
228- type Output = ( ) ;
241+ type Output = std :: convert :: Infallible ;
229242
230- fn update ( & mut self , cache : & Cache ) -> Option < ( ) > {
243+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
231244 if let Some ( mut g) = cache. guilds . get_mut ( & self . guild_id ) {
232245 g. members . extend ( self . members . clone ( ) ) ;
233246 }
@@ -237,9 +250,9 @@ impl CacheUpdate for GuildMembersChunkEvent {
237250}
238251
239252impl CacheUpdate for GuildRoleCreateEvent {
240- type Output = ( ) ;
253+ type Output = std :: convert :: Infallible ;
241254
242- fn update ( & mut self , cache : & Cache ) -> Option < ( ) > {
255+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
243256 cache. guilds . get_mut ( & self . role . guild_id ) . map ( |mut g| g. roles . insert ( self . role . clone ( ) ) ) ;
244257 None
245258 }
@@ -268,9 +281,9 @@ impl CacheUpdate for GuildRoleUpdateEvent {
268281}
269282
270283impl CacheUpdate for GuildStickersUpdateEvent {
271- type Output = ( ) ;
284+ type Output = std :: convert :: Infallible ;
272285
273- fn update ( & mut self , cache : & Cache ) -> Option < ( ) > {
286+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
274287 if let Some ( mut guild) = cache. guilds . get_mut ( & self . guild_id ) {
275288 guild. stickers . clone_from ( & self . stickers ) ;
276289 }
@@ -280,9 +293,9 @@ impl CacheUpdate for GuildStickersUpdateEvent {
280293}
281294
282295impl CacheUpdate for GuildUpdateEvent {
283- type Output = ( ) ;
296+ type Output = std :: convert :: Infallible ;
284297
285- fn update ( & mut self , cache : & Cache ) -> Option < ( ) > {
298+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
286299 if let Some ( mut guild) = cache. guilds . get_mut ( & self . guild . id ) {
287300 guild. afk_metadata . clone_from ( & self . guild . afk_metadata ) ;
288301 guild. banner . clone_from ( & self . guild . banner ) ;
@@ -438,9 +451,9 @@ impl CacheUpdate for PresenceUpdateEvent {
438451}
439452
440453impl CacheUpdate for ReadyEvent {
441- type Output = ( ) ;
454+ type Output = std :: convert :: Infallible ;
442455
443- fn update ( & mut self , cache : & Cache ) -> Option < ( ) > {
456+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
444457 for unavailable in & self . ready . guilds {
445458 cache. guilds . remove ( & unavailable. id ) ;
446459 cache. unavailable_guilds . insert ( unavailable. id , ( ) ) ;
@@ -492,6 +505,51 @@ impl CacheUpdate for ThreadDeleteEvent {
492505 }
493506}
494507
508+ impl CacheUpdate for ThreadListSyncEvent {
509+ type Output = std:: convert:: Infallible ;
510+
511+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
512+ let mut guild = cache. guilds . get_mut ( & self . guild_id ) ?;
513+ let Some ( channel_ids) = & self . channel_ids else {
514+ // channel_ids is none, this is a full sync, easy path
515+ guild. threads . clone_from ( & self . threads ) ;
516+ return None ;
517+ } ;
518+
519+ // Add new threads and update existing threads.
520+ for new_thread in & self . threads {
521+ match guild. threads . entry ( & new_thread. id ) {
522+ Entry :: Occupied ( mut entry) => {
523+ entry. get_mut ( ) . clone_from ( new_thread) ;
524+ } ,
525+ Entry :: Vacant ( entry) => {
526+ entry. insert ( new_thread. clone ( ) ) ;
527+ } ,
528+ }
529+ }
530+
531+ // Remove threads which are not provided in the sync and are in the provided channels.
532+ let mut removed_threads = Vec :: new ( ) ;
533+ for & channel_id in channel_ids {
534+ for thread in & guild. threads {
535+ if thread. parent_id != channel_id {
536+ continue ;
537+ }
538+
539+ if !self . threads . contains_key ( & thread. id ) {
540+ removed_threads. push ( thread. id ) ;
541+ }
542+ }
543+ }
544+
545+ for to_remove in removed_threads {
546+ guild. threads . remove ( & to_remove) ;
547+ }
548+
549+ None
550+ }
551+ }
552+
495553impl CacheUpdate for UserUpdateEvent {
496554 type Output = CurrentUser ;
497555
@@ -541,3 +599,34 @@ impl CacheUpdate for VoiceChannelStatusUpdateEvent {
541599 old
542600 }
543601}
602+
603+ fn update_guild_event ( cache : & Cache , event : & ScheduledEvent ) -> Option < ScheduledEvent > {
604+ let mut guild = cache. guilds . get_mut ( & event. guild_id ) ?;
605+ guild. scheduled_events . insert ( event. clone ( ) )
606+ }
607+
608+ impl CacheUpdate for GuildScheduledEventCreateEvent {
609+ type Output = std:: convert:: Infallible ;
610+
611+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
612+ update_guild_event ( cache, & self . event ) ;
613+ None
614+ }
615+ }
616+
617+ impl CacheUpdate for GuildScheduledEventUpdateEvent {
618+ type Output = ScheduledEvent ;
619+
620+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
621+ update_guild_event ( cache, & self . event )
622+ }
623+ }
624+
625+ impl CacheUpdate for GuildScheduledEventDeleteEvent {
626+ type Output = ScheduledEvent ;
627+
628+ fn update ( & mut self , cache : & Cache ) -> Option < Self :: Output > {
629+ let mut guild = cache. guilds . get_mut ( & self . event . guild_id ) ?;
630+ guild. scheduled_events . remove ( & self . event . id )
631+ }
632+ }
0 commit comments