Added a feature to send pings to subscribers via HTTP requests

This commit is contained in:
Shiroyasha 2024-06-18 02:07:59 +03:00
parent 81ab2ba724
commit 4bd9a2ab2e
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
4 changed files with 54 additions and 9 deletions

View file

@ -14,12 +14,8 @@ use crate::{auth::Token, AppState};
pub fn router() -> Router<AppState> {
Router::new()
.route("/verify", get(verify))
// .route("/ping", post(ping))
// .route("/event", post(event))
// .route("/toast", post(toast))
// .route("/chat", post(chat))
// .route("/notice", post(notice))
.route("/raw", post(raw))
.route("/sub/raw", post(sub_raw))
}
#[derive(Deserialize)]
@ -80,4 +76,44 @@ async fn raw(
return (StatusCode::NOT_FOUND, "uuid doesnt defined".to_string()).into_response();
},
}
}
async fn sub_raw(
Token(token): Token,
Query(query): Query<UserUuid>,
State(state): State<AppState>,
body: String,
) -> Response {
debug!(body = body);
match token {
Some(t) => {
if !state.config.lock().await.verify_token(&t) {
return (StatusCode::UNAUTHORIZED, "wrong token".to_string()).into_response()
}
},
None => return (StatusCode::UNAUTHORIZED, "unauthorized".to_string()).into_response(),
}
let payload = match hex::decode(body) {
Ok(v) => v,
Err(_) => return (StatusCode::NOT_ACCEPTABLE, "not raw data".to_string()).into_response(),
};
debug!("{:?}", payload);
match query.uuid {
Some(uuid) => {
// for only one
let tx = match state.broadcasts.get(&uuid) {
Some(d) => d,
None => return (StatusCode::NOT_FOUND, "unknown uuid".to_string()).into_response(),
};
match tx.value().send(payload) {
Ok(_) => return (StatusCode::OK, "ok".to_string()).into_response(),
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "cant send".to_string()).into_response(),
};
},
None => {
return (StatusCode::NOT_FOUND, "uuid doesnt defined".to_string()).into_response();
},
}
}