mirror of
https://github.com/shiroyashik/sculptor.git
synced 2025-12-06 13:01:12 +03:00
Now when an avatar gets deleted it sends an Event
This commit is contained in:
parent
a1f9eba502
commit
64d1111522
1 changed files with 16 additions and 11 deletions
|
|
@ -1,14 +1,17 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow_http::{http_error_ret, response::Result};
|
use anyhow_http::{http_error_ret, response::Result};
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Bytes,
|
body::Bytes,
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
Json,
|
Json,
|
||||||
};
|
};
|
||||||
|
use dashmap::DashMap;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs,
|
fs,
|
||||||
io::{self, AsyncReadExt, BufWriter},
|
io::{self, AsyncReadExt, BufWriter}, sync::broadcast::Sender,
|
||||||
};
|
};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
|
@ -129,16 +132,7 @@ pub async fn upload_avatar(
|
||||||
pub async fn equip_avatar(Token(token): Token, State(state): State<AppState>) -> String {
|
pub async fn equip_avatar(Token(token): Token, State(state): State<AppState>) -> String {
|
||||||
debug!("[API] S2C : Equip");
|
debug!("[API] S2C : Equip");
|
||||||
let uuid = state.user_manager.get(&token.unwrap()).unwrap().uuid;
|
let uuid = state.user_manager.get(&token.unwrap()).unwrap().uuid;
|
||||||
if state
|
send_event(&state.broadcasts, &uuid);
|
||||||
.broadcasts
|
|
||||||
.get(&uuid)
|
|
||||||
.unwrap()
|
|
||||||
.send(S2CMessage::Event(uuid).to_vec())
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
debug!("[WebSocket] Failed to send Event! Maybe there is no one to send")
|
|
||||||
// TODO: Put into Handler
|
|
||||||
};
|
|
||||||
"ok".to_string()
|
"ok".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -155,7 +149,18 @@ pub async fn delete_avatar(Token(token): Token, State(state): State<AppState>) -
|
||||||
);
|
);
|
||||||
let avatar_file = format!("avatars/{}.moon", user_info.uuid);
|
let avatar_file = format!("avatars/{}.moon", user_info.uuid);
|
||||||
fs::remove_file(avatar_file).await?;
|
fs::remove_file(avatar_file).await?;
|
||||||
|
send_event(&state.broadcasts, &user_info.uuid);
|
||||||
}
|
}
|
||||||
// let avatar_file = format!("avatars/{}.moon",user_info.uuid);
|
// let avatar_file = format!("avatars/{}.moon",user_info.uuid);
|
||||||
Ok("ok".to_string())
|
Ok("ok".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn send_event(broadcasts: &Arc<DashMap<Uuid, Sender<Vec<u8>>>>, uuid: &Uuid) {
|
||||||
|
if let Some(broadcast) = broadcasts.get(&uuid) {
|
||||||
|
if broadcast.send(S2CMessage::Event(*uuid).to_vec()).is_err() {
|
||||||
|
debug!("[WebSocket] Failed to send Event! There is no one to send. UUID: {uuid}")
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
debug!("[WebSocket] Failed to send Event! Can't find UUID: {uuid}")
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue