feat: Chat request implementation #14
2 changed files with 98 additions and 0 deletions
77
crates/oxidetalis/src/websocket/handlers/chat_request.rs
Normal file
77
crates/oxidetalis/src/websocket/handlers/chat_request.rs
Normal file
|
@ -0,0 +1,77 @@
|
|||
// OxideTalis Messaging Protocol homeserver implementation
|
||||
// Copyright (C) 2024 OxideTalis Developers <otmp@4rs.nl>
|
||||
//
|
||||
// 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 <https://gnu.org/licenses/agpl-3.0>.
|
||||
|
||||
//! 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<Unsigned> {
|
||||
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.",
|
||||
awiteb marked this conversation as resolved
|
||||
);
|
||||
}
|
||||
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.")
|
||||
}
|
21
crates/oxidetalis/src/websocket/handlers/mod.rs
Normal file
21
crates/oxidetalis/src/websocket/handlers/mod.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
// OxideTalis Messaging Protocol homeserver implementation
|
||||
// Copyright (C) 2024 OxideTalis Developers <otmp@4rs.nl>
|
||||
//
|
||||
// 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 <https://gnu.org/licenses/agpl-3.0>.
|
||||
|
||||
//! Websocket event handlers.
|
||||
|
||||
mod chat_request;
|
||||
|
||||
pub use chat_request::*;
|
Loading…
Reference in a new issue
Maybe would be better to move this to
WsError
easier to manage errorsthere are also others below this
Right, this should be an error, not a message. I don't know how is this happened
I'll remove
Message
event it is actually useless. I don't know why I thought it was good.Well, I remembered when I added it, it was a pain to add a new error (before
ws_error
macro) so I thought it was good for simple messages.