parent
0c0aaa7048
commit
e4cc833ccc
4 changed files with 143 additions and 1 deletions
|
|
@ -15,6 +15,7 @@ use crate::{
|
|||
models::{Guild, SoundboardSound},
|
||||
voice,
|
||||
};
|
||||
use tracing::info;
|
||||
|
||||
pub fn routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
|
|
@ -43,6 +44,10 @@ pub fn routes() -> Router<AppState> {
|
|||
"/guilds/{guild_id}/sounds",
|
||||
get(list_sounds).post(upload_sound),
|
||||
)
|
||||
.route(
|
||||
"/guilds/{guild_id}/sounds/{sound_id}",
|
||||
post(delete_sound_post).delete(delete_sound),
|
||||
)
|
||||
.route("/channels", post(create_channel))
|
||||
.route(
|
||||
"/channels/{channel_id}/messages",
|
||||
|
|
@ -759,6 +764,57 @@ async fn upload_sound(
|
|||
Ok(Json(sound))
|
||||
}
|
||||
|
||||
async fn delete_sound_post(
|
||||
state: State<AppState>,
|
||||
user: AuthUser,
|
||||
path: Path<(Uuid, Uuid)>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
delete_sound(state, user, path).await
|
||||
}
|
||||
|
||||
async fn delete_sound(
|
||||
State(state): State<AppState>,
|
||||
user: AuthUser,
|
||||
Path((guild_id, sound_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
ensure_guild_member(&state, guild_id, user.id).await?;
|
||||
|
||||
let sound = db::get_sound_by_id(&state.db, sound_id)
|
||||
.await
|
||||
.map_err(|e| ApiError::internal(&e.to_string()))?
|
||||
.ok_or_else(|| ApiError {
|
||||
status: StatusCode::NOT_FOUND,
|
||||
message: "sound not found".to_string(),
|
||||
})?;
|
||||
|
||||
if sound.guild_id != guild_id {
|
||||
return Err(ApiError::bad_request("sound does not belong to this guild"));
|
||||
}
|
||||
|
||||
let is_owner = db::is_guild_owner(&state.db, guild_id, user.id)
|
||||
.await
|
||||
.map_err(|e| ApiError::internal(&e.to_string()))?;
|
||||
|
||||
if sound.created_by_user_id != user.id && !is_owner {
|
||||
return Err(ApiError {
|
||||
status: StatusCode::FORBIDDEN,
|
||||
message: "you do not have permission to delete this sound".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
// Delete file from disk
|
||||
let relative_path = sound.file_path.trim_start_matches('/');
|
||||
if let Err(e) = tokio::fs::remove_file(relative_path).await {
|
||||
info!("failed to delete sound file {}: {}", relative_path, e);
|
||||
}
|
||||
|
||||
db::delete_sound(&state.db, sound_id)
|
||||
.await
|
||||
.map_err(|e| ApiError::internal(&format!("failed to delete sound record: {e}")))?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn ensure_guild_member(
|
||||
state: &AppState,
|
||||
guild_id: Uuid,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue