refactor structures sent to frontend

pull/5/head
Patrick Cleavelin 2023-02-28 19:18:52 -06:00
parent 7f7a6472be
commit cf2d273584
3 changed files with 58 additions and 20 deletions

View File

@ -217,7 +217,7 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) -> Vec<tokio::task::JoinHandl
// TODO: randomly choose a intro to play // TODO: randomly choose a intro to play
let Some(intro) = user.intros.first() else { continue; }; let Some(intro) = user.intros.first() else { continue; };
let source = match guild_settings.intros.get(intro.0) { let source = match guild_settings.intros.get(intro.index) {
Some(Intro::Online(intro)) => match songbird::ytdl(&intro.url).await { Some(Intro::Online(intro)) => match songbird::ytdl(&intro.url).await {
Ok(source) => source, Ok(source) => source,
Err(err) => { Err(err) => {
@ -246,7 +246,7 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) -> Vec<tokio::task::JoinHandl
member.user.name, member.user.name,
channel.guild_id.as_u64(), channel.guild_id.as_u64(),
channel.name(), channel.name(),
intro.0 intro.index
); );
continue; continue;
} }

View File

@ -1,4 +1,4 @@
use std::sync::Arc; use std::{collections::HashMap, sync::Arc};
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
@ -8,13 +8,7 @@ use serde::Serialize;
use serde_json::{json, Value}; use serde_json::{json, Value};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use crate::settings::{Intro, Settings, UserSettings}; use crate::settings::{GuildSettings, Intro, IntroIndex, Settings, UserSettings};
#[derive(Serialize)]
pub(crate) enum MeResponse<'a> {
Settings(Vec<&'a UserSettings>),
NoUserFound,
}
#[derive(Serialize)] #[derive(Serialize)]
pub(crate) enum IntroResponse<'a> { pub(crate) enum IntroResponse<'a> {
@ -22,6 +16,30 @@ pub(crate) enum IntroResponse<'a> {
NoGuildFound, NoGuildFound,
} }
#[derive(Serialize)]
pub(crate) enum MeResponse<'a> {
Me(Me<'a>),
NoUserFound,
}
#[derive(Serialize)]
pub(crate) struct Me<'a> {
pub(crate) username: String,
pub(crate) guilds: Vec<MeGuild<'a>>,
}
#[derive(Serialize)]
pub(crate) struct MeGuild<'a> {
pub(crate) name: String,
pub(crate) channels: Vec<MeChannel<'a>>,
}
#[derive(Serialize)]
pub(crate) struct MeChannel<'a> {
pub(crate) name: String,
pub(crate) intros: &'a Vec<IntroIndex>,
}
pub(crate) async fn health(State(state): State<Arc<Mutex<Settings>>>) -> Json<Value> { pub(crate) async fn health(State(state): State<Arc<Mutex<Settings>>>) -> Json<Value> {
let settings = state.lock().await; let settings = state.lock().await;
@ -44,17 +62,34 @@ pub(crate) async fn me(
) -> Json<Value> { ) -> Json<Value> {
let settings = state.lock().await; let settings = state.lock().await;
let user_settings = settings let mut me = Me {
.guilds username: user.clone(),
.values() guilds: Vec::new(),
.flat_map(|guild| guild.channels.values().flat_map(|channel| &channel.users)) };
.filter(|(name, _)| **name == user)
.map(|(_, settings)| settings)
.collect::<Vec<_>>();
if user_settings.is_empty() { for g in &settings.guilds {
let mut guild = MeGuild {
name: g.0.to_string(),
channels: Vec::new(),
};
for channel in &g.1.channels {
let user_settings = channel.1.users.iter().find(|u| *u.0 == user);
let Some(user) = user_settings else { continue; };
guild.channels.push(MeChannel {
name: channel.0.to_owned(),
intros: &user.1.intros,
});
}
me.guilds.push(guild);
}
if me.guilds.is_empty() {
Json(json!(MeResponse::NoUserFound)) Json(json!(MeResponse::NoUserFound))
} else { } else {
Json(json!(MeResponse::Settings(user_settings))) Json(json!(MeResponse::Me(me)))
} }
} }

View File

@ -54,7 +54,10 @@ pub(crate) struct ChannelSettings {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct IntroIndex(pub usize, pub i32); pub(crate) struct IntroIndex {
pub(crate) index: usize,
pub(crate) volume: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]