From 8cc6aef67baf0ae6ae941543f521ea006d0a376b Mon Sep 17 00:00:00 2001 From: Awiteb Date: Fri, 12 Jul 2024 23:15:19 +0300 Subject: [PATCH] feat: ChatRequest event handler Signed-off-by: Awiteb --- .../src/websocket/handlers/chat_request.rs | 77 +++++++++++++++++++ .../oxidetalis/src/websocket/handlers/mod.rs | 21 +++++ 2 files changed, 98 insertions(+) create mode 100644 crates/oxidetalis/src/websocket/handlers/chat_request.rs create mode 100644 crates/oxidetalis/src/websocket/handlers/mod.rs diff --git a/crates/oxidetalis/src/websocket/handlers/chat_request.rs b/crates/oxidetalis/src/websocket/handlers/chat_request.rs new file mode 100644 index 0000000..0f43230 --- /dev/null +++ b/crates/oxidetalis/src/websocket/handlers/chat_request.rs @@ -0,0 +1,77 @@ +// OxideTalis Messaging Protocol homeserver implementation +// Copyright (C) 2024 OxideTalis Developers +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +//! Handler for incoming and outgoing chat requests. + +use std::str::FromStr; + +use oxidetalis_core::types::PublicKey; +use oxidetalis_entities::prelude::*; +use sea_orm::DatabaseConnection; + +use crate::database::InChatRequestsExt; +use crate::extensions::OnlineUsersExt; +use crate::{ + database::{BlackListExt, OutChatRequestsExt, UserTableExt, WhiteListExt}, + try_ws, + websocket::{errors::WsError, ServerEvent, Unsigned, ONLINE_USERS}, +}; + +/// Handle a chat request from a user. +pub async fn handle_chat_request( + db: &DatabaseConnection, + from: Option<&UserModel>, + to_public_key: &PublicKey, +) -> ServerEvent { + let Some(from_user) = from else { + return WsError::RegistredUserEvent.into(); + }; + let Some(to_user) = try_ws!(db.get_user_by_pubk(to_public_key).await) else { + return WsError::UserNotFound.into(); + }; + if from_user.id == to_user.id { + return 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) { + return ServerEvent::message("You have already sent a chat request to this user."); + } + + 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.", + ); + } + + try_ws!(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); + } + ServerEvent::message("Chat request sent successfully.") +} diff --git a/crates/oxidetalis/src/websocket/handlers/mod.rs b/crates/oxidetalis/src/websocket/handlers/mod.rs new file mode 100644 index 0000000..5f632a3 --- /dev/null +++ b/crates/oxidetalis/src/websocket/handlers/mod.rs @@ -0,0 +1,21 @@ +// OxideTalis Messaging Protocol homeserver implementation +// Copyright (C) 2024 OxideTalis Developers +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +//! Websocket event handlers. + +mod chat_request; + +pub use chat_request::*;