From 09f90b060f1ab56dd9387a5c8a9d3f814ade7015 Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Wed, 17 Jul 2024 21:16:36 +0300
Subject: [PATCH] chore: Returns `Option` of `ServerEvent` in chat request
handlers
Signed-off-by: Awiteb
---
crates/oxidetalis/src/macros.rs | 8 ++-
.../src/websocket/handlers/chat_request.rs | 57 +++++++++----------
crates/oxidetalis/src/websocket/mod.rs | 6 +-
3 files changed, 34 insertions(+), 37 deletions(-)
diff --git a/crates/oxidetalis/src/macros.rs b/crates/oxidetalis/src/macros.rs
index 2943e59..f2d95e2 100644
--- a/crates/oxidetalis/src/macros.rs
+++ b/crates/oxidetalis/src/macros.rs
@@ -35,13 +35,15 @@
/// [`Err`]: std::result::Result::Err
#[macro_export]
macro_rules! try_ws {
- ($result_expr:expr) => {
+ (Some $result_expr:expr) => {
match $result_expr {
Ok(val) => val,
Err(err) => {
log::error!("{err}");
- return $crate::websocket::ServerEvent::<$crate::websocket::Unsigned>::from(
- $crate::websocket::errors::WsError::from(err),
+ return Some(
+ $crate::websocket::ServerEvent::<$crate::websocket::Unsigned>::from(
+ $crate::websocket::errors::WsError::from(err),
+ ),
);
}
}
diff --git a/crates/oxidetalis/src/websocket/handlers/chat_request.rs b/crates/oxidetalis/src/websocket/handlers/chat_request.rs
index c3c31e3..b41580c 100644
--- a/crates/oxidetalis/src/websocket/handlers/chat_request.rs
+++ b/crates/oxidetalis/src/websocket/handlers/chat_request.rs
@@ -35,45 +35,42 @@ pub async fn handle_chat_request(
db: &DatabaseConnection,
from: Option<&UserModel>,
to_public_key: &PublicKey,
-) -> ServerEvent {
+) -> Option> {
let Some(from_user) = from else {
- return WsError::RegistredUserEvent.into();
+ return Some(WsError::RegistredUserEvent.into());
};
- let Some(to_user) = try_ws!(db.get_user_by_pubk(to_public_key).await) else {
- return WsError::UserNotFound.into();
+ let Some(to_user) = try_ws!(Some db.get_user_by_pubk(to_public_key).await) else {
+ return Some(WsError::UserNotFound.into());
};
if from_user.id == to_user.id {
- return WsError::CannotSendChatRequestToSelf.into();
+ return Some(WsError::CannotSendChatRequestToSelf.into());
}
// FIXME: When change the entity public key to a PublicKey type, change this
let from_public_key = PublicKey::from_str(&from_user.public_key).expect("Is valid public key");
- if try_ws!(db.is_blacklisted(&to_user, &from_public_key).await) {
- return ServerEvent::message(
- "You are unable to send a chat request because you are on the recipient's blacklist.",
- );
- }
- if try_ws!(db.have_chat_request_to(from_user, to_public_key).await).is_some() {
- return ServerEvent::message("You have already sent a chat request to this user.");
+ if try_ws!(Some db.have_chat_request_to(from_user, to_public_key).await).is_some() {
+ return Some(WsError::AlreadySendChatRequest.into());
}
- try_ws!(db.add_to_whitelist(from_user, to_public_key).await);
-
- if try_ws!(db.is_whitelisted(&to_user, &from_public_key).await) {
- return ServerEvent::message(
- "You are already on the recipient's whitelist, so you can now chat with them.",
- );
+ if try_ws!(Some db.is_blacklisted(&to_user, &from_public_key).await) {
+ return Some(WsError::RecipientBlacklist.into());
}
- try_ws!(db.save_out_chat_request(from_user, to_public_key).await);
+ try_ws!(Some db.add_to_whitelist(from_user, to_public_key).await);
+
+ if try_ws!(Some db.is_whitelisted(&to_user, &from_public_key).await) {
+ return Some(WsError::AlreadyInRecipientWhitelist.into());
+ }
+
+ try_ws!(Some db.save_out_chat_request(from_user, to_public_key).await);
if let Some(conn_id) = ONLINE_USERS.is_online(to_public_key).await {
ONLINE_USERS
.send(&conn_id, ServerEvent::chat_request(&from_public_key))
.await;
} else {
- try_ws!(db.save_in_chat_request(&from_public_key, &to_user).await);
+ try_ws!(Some db.save_in_chat_request(&from_public_key, &to_user).await);
}
- ServerEvent::message("Chat request sent successfully.")
+ None
}
pub async fn handle_chat_response(
@@ -81,28 +78,28 @@ pub async fn handle_chat_response(
recipient: Option<&UserModel>,
sender_public_key: &PublicKey,
accepted: bool,
-) -> ServerEvent {
+) -> Option> {
let Some(recipient_user) = recipient else {
- return WsError::RegistredUserEvent.into();
+ return Some(WsError::RegistredUserEvent.into());
};
- let Some(sender_user) = try_ws!(db.get_user_by_pubk(sender_public_key).await) else {
- return WsError::UserNotFound.into();
+ let Some(sender_user) = try_ws!(Some db.get_user_by_pubk(sender_public_key).await) else {
+ return Some(WsError::UserNotFound.into());
};
if recipient_user.id == sender_user.id {
- return WsError::CannotRespondToOwnChatRequest.into();
+ return Some(WsError::CannotRespondToOwnChatRequest.into());
}
// FIXME: When change the entity public key to a PublicKey type, change this
let recipient_public_key =
PublicKey::from_str(&recipient_user.public_key).expect("Is valid public key");
- if try_ws!(
+ if try_ws!(Some
db.have_chat_request_to(&sender_user, &recipient_public_key)
.await
)
.is_none()
{
- return WsError::NoChatRequestFromRecipient.into();
+ return Some(WsError::NoChatRequestFromRecipient.into());
}
if let Some(conn_id) = ONLINE_USERS.is_online(sender_public_key).await {
@@ -125,10 +122,10 @@ pub async fn handle_chat_response(
db.add_to_blacklist(recipient_user, sender_public_key).await
};
- try_ws!(
+ try_ws!(Some
db.remove_out_chat_request(&sender_user, &recipient_public_key)
.await
);
- ServerEvent::message("Chat request response sent successfully.")
+ None
}
diff --git a/crates/oxidetalis/src/websocket/mod.rs b/crates/oxidetalis/src/websocket/mod.rs
index 8702700..cd5d4ec 100644
--- a/crates/oxidetalis/src/websocket/mod.rs
+++ b/crates/oxidetalis/src/websocket/mod.rs
@@ -216,11 +216,9 @@ async fn handle_events(
ONLINE_USERS.update_pong(conn_id).await;
None
}
- ClientEventType::ChatRequest { to } => {
- Some(handlers::handle_chat_request(db, user, to).await)
- }
+ ClientEventType::ChatRequest { to } => handlers::handle_chat_request(db, user, to).await,
ClientEventType::ChatRequestResponse { to, accepted } => {
- Some(handlers::handle_chat_response(db, user, to, *accepted).await)
+ handlers::handle_chat_response(db, user, to, *accepted).await
}
}
}