chore: Impl AsRef for ServerEvent

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-09 19:23:49 +03:00
parent 2bac5be8c8
commit 33b44cb256
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
3 changed files with 21 additions and 10 deletions

View file

@ -19,7 +19,7 @@ use std::sync::Arc;
use chrono::Utc;
use oxidetalis_config::Config;
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
use salvo::{websocket::Message, Depot};
use salvo::Depot;
use sea_orm::DatabaseConnection;
use uuid::Uuid;
@ -87,9 +87,10 @@ impl OnlineUsersExt for OnlineUsers {
let now = Utc::now();
self.write().await.par_iter_mut().for_each(|(_, u)| {
u.pinged_at = now;
let _ = u.sender.unbounded_send(Ok(Message::from(
&ServerEvent::ping().sign(&u.shared_secret),
)));
let _ = u.sender.unbounded_send(Ok(ServerEvent::ping()
.sign(&u.shared_secret)
.as_ref()
.into()));
});
}

View file

@ -101,6 +101,12 @@ impl ServerEvent<Unsigned> {
}
}
impl<S> AsRef<Self> for ServerEvent<S> {
fn as_ref(&self) -> &Self {
self
}
}
impl From<&ServerEvent<Signed>> for Message {
fn from(value: &ServerEvent<Signed>) -> Self {
Message::text(serde_json::to_string(value).expect("This can't fail"))

View file

@ -132,18 +132,22 @@ async fn handle_socket(
match handle_ws_msg(msg, &nonce_cache, &user_shared_secret).await {
Ok(event) => {
if let Some(server_event) = handle_events(event, &conn_id).await {
if let Err(err) = sender.unbounded_send(Ok(Message::from(
&server_event.sign(&user_shared_secret),
))) {
if let Err(err) = sender.unbounded_send(Ok(server_event
.sign(&user_shared_secret)
.as_ref()
.into()))
{
log::error!("Websocket Error: {err}");
break;
}
};
}
Err(err) => {
if let Err(err) = sender.unbounded_send(Ok(Message::from(
&ServerEvent::from(err).sign(&user_shared_secret),
))) {
if let Err(err) = sender.unbounded_send(Ok(ServerEvent::from(err)
.sign(&user_shared_secret)
.as_ref()
.into()))
{
log::error!("Websocket Error: {err}");
break;
};