Skip to content

Commit c615ee7

Browse files
committed
chore: reduce meta complexity
1 parent 28ddaa3 commit c615ee7

5 files changed

Lines changed: 46 additions & 25 deletions

File tree

api/src/discord.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::time::Duration;
22
use cached::proc_macro::cached;
33
use http::StatusCode;
4-
use lambda_http::tracing::{error, info};
4+
use lambda_http::tracing::{error, info, warn};
55
use tokio::time::sleep;
66
use twilight_http::{Client, Error, Response};
77
use twilight_http::api_error::ApiError;
@@ -33,6 +33,7 @@ where
3333
match e.kind() {
3434
ErrorType::Response { error: ApiError::Ratelimited(ratelimited), ..} => {
3535
attempts += 1;
36+
warn!("Rate limited, retrying in {} seconds", ratelimited.retry_after);
3637
sleep(Duration::from_secs_f64(ratelimited.retry_after)).await;
3738
continue;
3839
}

api/src/meta/mod.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ops::{Add, Sub};
12
use std::sync::Arc;
23
use std::time::Duration;
34
use axum::{Extension, Json, Router};
@@ -7,15 +8,16 @@ use http::StatusCode;
78
use lambda_http::tracing::{error, info};
89
use serde::{Deserialize, Serialize};
910
use serde_json::{json, Value};
10-
use tokio::time::sleep;
11+
use tokio::time::{sleep, Instant};
1112
use tower_http::cors::CorsLayer;
1213
use twilight_http::Client;
1314
use twilight_model::guild::{Guild, Role};
1415
use twilight_model::id::Id;
1516
use twilight_model::id::marker::GuildMarker;
17+
use twilight_model::user::CurrentUserGuild;
1618
use twilight_model::util::ImageHash;
1719
use 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};
1921
use crate::utils::member_guilds;
2022

2123
pub 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)]
3353
struct 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

6582
async 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
}

ui/src/helpers/meta.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
import {GuildMeta} from "../stores/meta.js";
1+
import {PartialGuildMeta} from "../stores/meta.js";
22
import {isGuildAdmin} from "./discord.js";
33

44

55
export async function fetchGuildMetaMap(token) {
6-
return (await GuildMeta.fetchAll(token)).reduce((acc, guildMeta) => {
7-
guildMeta.roleMeta = guildMeta.roles.reduce((acc, role) => acc.set(role.id, role), new Map());
6+
return (await PartialGuildMeta.fetchAll(token)).reduce((acc, guildMeta) => {
7+
// guildMeta.roleMeta = guildMeta.roles.reduce((acc, role) => acc.set(role.id, role), new Map());
88
return acc.set(guildMeta.id, guildMeta);
99
}, new Map());
1010
}
1111

12+
export function filterByMember(guildMetaMap, currentUserGuildMetaMap) {
13+
let adminGuilds = new Map();
14+
for (let guildMeta of guildMetaMap.values()) {
15+
adminGuilds.set(guildMeta.id, guildMeta);
16+
}
17+
return adminGuilds
18+
}
19+
1220
export function filterByAdmin(guildMetaMap, currentUserGuildMetaMap) {
1321
let adminGuilds = new Map();
1422
for (let guildMeta of guildMetaMap.values()) {

ui/src/pages/verify/VerifyView.vue

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
<script setup>
22
3-
import MicrosoftIcon from "../../components/icons/MicrosoftIcon.vue";
4-
import GoogleIcon from "../../components/icons/GoogleIcon.vue";
53
import DiscordAuthButton from "../../components/auth/DiscordAuthButton.vue";
64
import {onMounted, ref, toRef} from "vue";
7-
import MicrosoftAuthButton from "../../components/verify/MicrosoftAuthButton.vue";
8-
import GoogleAuthButton from "../../components/verify/GoogleAuthButton.vue";
9-
import axios from "axios";
10-
import EmailAuthText from "../../components/verify/EmailAuthText.vue";
115
import {User} from "../../stores/user.js";
126
import LinkedAccountsTable from "../../components/verify/LinkedAccountsTable.vue";
137
import LinkAccountButton from "../../components/verify/LinkAccountButton.vue";

ui/src/stores/meta.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,20 @@ export class RoleMeta {
1515
}
1616
}
1717

18-
export class GuildMeta {
19-
constructor(id, name, icon, roles) {
18+
export class PartialGuildMeta {
19+
constructor(id, name, icon) {
2020
this.id = id;
2121
this.name = name;
2222
this.icon = icon;
23-
this.roles = roles;
2423
}
2524

2625
static fromJson(json) {
27-
return new GuildMeta(json['id'], json['name'], json['icon'], json['roles'].map(RoleMeta.fromJson));
26+
return new PartialGuildMeta(json['id'], json['name'], json['icon']);
2827
}
2928

3029
static async fetchAll(token) {
3130
let r = await axios.get(`${VITE_KB_API_URL}/meta/guilds`,
3231
{ headers: { 'Authorization': 'Discord ' + token } });
33-
return r.data.map(GuildMeta.fromJson);
32+
return r.data.map(PartialGuildMeta.fromJson);
3433
}
3534
}

0 commit comments

Comments
 (0)