1+ use std:: ops:: { Add , Sub } ;
12use std:: sync:: Arc ;
23use std:: time:: Duration ;
34use axum:: { Extension , Json , Router } ;
@@ -7,15 +8,16 @@ use http::StatusCode;
78use lambda_http:: tracing:: { error, info} ;
89use serde:: { Deserialize , Serialize } ;
910use serde_json:: { json, Value } ;
10- use tokio:: time:: sleep;
11+ use tokio:: time:: { sleep, Instant } ;
1112use tower_http:: cors:: CorsLayer ;
1213use twilight_http:: Client ;
1314use twilight_model:: guild:: { Guild , Role } ;
1415use twilight_model:: id:: Id ;
1516use twilight_model:: id:: marker:: GuildMarker ;
17+ use twilight_model:: user:: CurrentUserGuild ;
1618use twilight_model:: util:: ImageHash ;
1719use crate :: AppState ;
18- use crate :: discord:: { get_current_user_guilds_no_cache , get_current_user_guilds_prime_cache, get_guild, get_guild_prime_cache} ;
20+ use crate :: discord:: { get_current_user_guilds_prime_cache, get_guild, get_guild_prime_cache} ;
1921use crate :: utils:: member_guilds;
2022
2123pub fn router ( ) -> Router < AppState > {
@@ -29,6 +31,24 @@ pub fn setup(discord_bot: Arc<Client>) {
2931 tokio:: spawn ( refresh_meta_cache ( discord_bot) ) ;
3032}
3133
34+
35+ #[ derive( Debug , Serialize , Deserialize ) ]
36+ struct PartialGuildMeta {
37+ id : Id < GuildMarker > ,
38+ name : String ,
39+ icon : Option < ImageHash > ,
40+ }
41+ impl From < CurrentUserGuild > for PartialGuildMeta {
42+ fn from ( guild : CurrentUserGuild ) -> Self {
43+ PartialGuildMeta {
44+ id : guild. id ,
45+ name : guild. name ,
46+ icon : guild. icon ,
47+ }
48+ }
49+ }
50+
51+
3252#[ derive( Debug , Serialize , Deserialize ) ]
3353struct GuildMeta {
3454 id : Id < GuildMarker > ,
@@ -52,19 +72,17 @@ async fn get_guilds_meta(
5272 Extension ( discord_user) : Extension < Arc < Client > > ,
5373 State ( app_state) : State < AppState > ,
5474) -> Result < Json < Value > , StatusCode > {
55- let member_guilds = member_guilds ( & discord_user, & app_state. discord_bot ) . await ?;
56-
57- let mut meta = vec ! [ ] ;
58- for member_guild in & member_guilds {
59- meta. push ( GuildMeta :: from ( get_guild ( member_guild. id , & app_state. discord_bot ) . await ?) )
60- }
75+ Ok ( Json ( json ! (
76+ member_guilds( & discord_user, & app_state. discord_bot) . await ?
77+ . into_iter( ) . map( PartialGuildMeta :: from) . collect:: <Vec <PartialGuildMeta >>( )
78+ ) ) )
6179
62- Ok ( Json ( json ! ( meta) ) )
6380}
6481
6582async fn refresh_meta_cache ( discord_bot : Arc < Client > ) {
6683 loop {
6784 info ! ( "Refreshing meta cache" ) ;
85+ let time = Instant :: now ( ) ;
6886 match get_current_user_guilds_prime_cache ( & * discord_bot) . await {
6987 Ok ( guilds) => {
7088 info ! ( "Refreshing meta cache: {}" , guilds. len( ) ) ;
@@ -76,6 +94,7 @@ async fn refresh_meta_cache(discord_bot: Arc<Client>) {
7694 error ! ( "refresh_meta_cache error: {:#?}" , e) ;
7795 }
7896 }
79- sleep ( Duration :: from_secs ( 50 ) ) . await ;
97+ info ! ( "{:?} {:?} Waiting {} seconds" , time, Instant :: now( ) , ( time. sub( Instant :: now( ) ) . add( Duration :: from_secs( 50 ) ) ) . as_secs( ) ) ;
98+ sleep ( ( time. sub ( Instant :: now ( ) ) ) . add ( Duration :: from_secs ( 50 ) ) ) . await ;
8099 }
81100}
0 commit comments