support for deleting intros

pull/5/head
Patrick Cleavelin 2023-03-14 21:43:21 -05:00
parent 9bedffa616
commit 8da6247805
3 changed files with 49 additions and 3 deletions

View File

@ -40,6 +40,7 @@ impl Permissions {
pub enum Permission { pub enum Permission {
None, None,
UploadSounds, UploadSounds,
DeleteSounds,
} }
impl Permission { impl Permission {

View File

@ -8,7 +8,7 @@ mod routes;
pub mod settings; pub mod settings;
use axum::http::{HeaderValue, Method}; use axum::http::{HeaderValue, Method};
use axum::routing::{get, post}; use axum::routing::{delete, get, post};
use axum::Router; use axum::Router;
use futures::StreamExt; use futures::StreamExt;
use settings::ApiState; use settings::ApiState;
@ -135,9 +135,10 @@ fn spawn_api(settings: Arc<Mutex<Settings>>) {
let api = Router::new() let api = Router::new()
.route("/health", get(routes::health)) .route("/health", get(routes::health))
.route("/me", get(routes::me)) .route("/me", get(routes::me))
.route("/intros/:guild", get(routes::intros))
.route("/intros/:guild/add", get(routes::add_guild_intro)) .route("/intros/:guild/add", get(routes::add_guild_intro))
.route("/intros/:guild/upload", post(routes::upload_guild_intro)) .route("/intros/:guild/upload", post(routes::upload_guild_intro))
.route("/intros/:guild", get(routes::intros)) .route("/intros/:guild/delete", delete(routes::delete_guild_intro))
.route( .route(
"/intros/:guild/:channel/:intro", "/intros/:guild/:channel/:intro",
post(routes::add_intro_to_user), post(routes::add_intro_to_user),
@ -152,7 +153,7 @@ fn spawn_api(settings: Arc<Mutex<Settings>>) {
// TODO: move this to env variable // TODO: move this to env variable
.allow_origin([origin.parse().unwrap()]) .allow_origin([origin.parse().unwrap()])
.allow_headers(Any) .allow_headers(Any)
.allow_methods([Method::GET, Method::POST]), .allow_methods([Method::GET, Method::POST, Method::DELETE]),
) )
.with_state(Arc::new(state)); .with_state(Arc::new(state));
let addr = SocketAddr::from(([0, 0, 0, 0], 8100)); let addr = SocketAddr::from(([0, 0, 0, 0], 8100));

View File

@ -53,6 +53,9 @@ pub(crate) struct MeChannel<'a> {
pub(crate) intros: &'a Vec<IntroIndex>, pub(crate) intros: &'a Vec<IntroIndex>,
} }
#[derive(Deserialize)]
pub(crate) struct DeleteIntroRequest(Vec<String>);
pub(crate) async fn health() -> &'static str { pub(crate) async fn health() -> &'static str {
"Hello!" "Hello!"
} }
@ -434,3 +437,44 @@ pub(crate) async fn add_guild_intro(
Ok(()) Ok(())
} }
pub(crate) async fn delete_guild_intro(
State(state): State<Arc<ApiState>>,
Path(guild): Path<u64>,
headers: HeaderMap,
Json(body): Json<DeleteIntroRequest>,
) -> Result<(), Error> {
let mut settings = state.settings.lock().await;
// TODO: make this an impl on HeaderMap
let Some(token) = headers.get("token").and_then(|v| v.to_str().ok()) else { return Err(Error::NoUserFound); };
{
let Some(guild) = settings.guilds.get(&guild) else { return Err(Error::NoGuildFound); };
let auth_user = match settings.auth_users.get(token) {
Some(user) => user,
None => return Err(Error::NoUserFound),
};
let Some(guild_user) = guild.users.get(&auth_user.name) else { return Err(Error::NoUserFound) };
if !guild_user.permissions.can(auth::Permission::DeleteSounds) {
return Err(Error::InvalidPermission);
}
}
let Some(guild) = settings.guilds.get_mut(&guild) else { return Err(Error::NoGuildFound); };
// Remove intro from any users
for channel in guild.channels.iter_mut() {
for user in channel.1.users.iter_mut() {
user.1
.intros
.retain(|user_intro| !body.0.iter().any(|intro| &user_intro.index == intro));
}
}
for intro in &body.0 {
guild.intros.remove(intro);
}
Ok(())
}