refactor structures sent to frontend
parent
7f7a6472be
commit
cf2d273584
|
@ -217,7 +217,7 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) -> Vec<tokio::task::JoinHandl
|
|||
// TODO: randomly choose a intro to play
|
||||
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 {
|
||||
Ok(source) => source,
|
||||
Err(err) => {
|
||||
|
@ -246,7 +246,7 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) -> Vec<tokio::task::JoinHandl
|
|||
member.user.name,
|
||||
channel.guild_id.as_u64(),
|
||||
channel.name(),
|
||||
intro.0
|
||||
intro.index
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::sync::Arc;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
|
@ -8,13 +8,7 @@ use serde::Serialize;
|
|||
use serde_json::{json, Value};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::settings::{Intro, Settings, UserSettings};
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(crate) enum MeResponse<'a> {
|
||||
Settings(Vec<&'a UserSettings>),
|
||||
NoUserFound,
|
||||
}
|
||||
use crate::settings::{GuildSettings, Intro, IntroIndex, Settings, UserSettings};
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(crate) enum IntroResponse<'a> {
|
||||
|
@ -22,6 +16,30 @@ pub(crate) enum IntroResponse<'a> {
|
|||
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> {
|
||||
let settings = state.lock().await;
|
||||
|
||||
|
@ -44,17 +62,34 @@ pub(crate) async fn me(
|
|||
) -> Json<Value> {
|
||||
let settings = state.lock().await;
|
||||
|
||||
let user_settings = settings
|
||||
.guilds
|
||||
.values()
|
||||
.flat_map(|guild| guild.channels.values().flat_map(|channel| &channel.users))
|
||||
.filter(|(name, _)| **name == user)
|
||||
.map(|(_, settings)| settings)
|
||||
.collect::<Vec<_>>();
|
||||
let mut me = Me {
|
||||
username: user.clone(),
|
||||
guilds: Vec::new(),
|
||||
};
|
||||
|
||||
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))
|
||||
} else {
|
||||
Json(json!(MeResponse::Settings(user_settings)))
|
||||
Json(json!(MeResponse::Me(me)))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,10 @@ pub(crate) struct ChannelSettings {
|
|||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[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)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
|
Loading…
Reference in New Issue