From 24e038f482c97e7ad75de2f496c14626df6420da Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Sun, 14 Jul 2024 15:04:33 +0300
Subject: [PATCH] feat: ChatRequestResponse event
Signed-off-by: Awiteb
---
crates/oxidetalis/src/database/blacklist.rs | 31 +++++++++-
.../src/database/out_chat_requests.rs | 29 +++++++--
crates/oxidetalis/src/websocket/errors.rs | 6 +-
.../oxidetalis/src/websocket/events/client.rs | 5 ++
.../oxidetalis/src/websocket/events/server.rs | 7 +++
.../src/websocket/handlers/chat_request.rs | 59 ++++++++++++++++++-
crates/oxidetalis/src/websocket/mod.rs | 3 +
7 files changed, 133 insertions(+), 7 deletions(-)
diff --git a/crates/oxidetalis/src/database/blacklist.rs b/crates/oxidetalis/src/database/blacklist.rs
index 8448843..9a4a712 100644
--- a/crates/oxidetalis/src/database/blacklist.rs
+++ b/crates/oxidetalis/src/database/blacklist.rs
@@ -16,11 +16,12 @@
//! Database extension to work with the balcklist table
+use chrono::Utc;
use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection;
-use crate::errors::ServerResult;
+use crate::{errors::ServerResult, websocket::errors::WsError};
/// Extension trait for the `DatabaseConnection` to work with the blacklist
/// table
@@ -32,6 +33,12 @@ pub trait BlackListExt {
blacklister: &UserModel,
target_public_key: &PublicKey,
) -> ServerResult;
+ /// Add the `target_public_key` to the blacklist of the `blacklister`
+ async fn add_to_blacklist(
+ &self,
+ blacklister: &UserModel,
+ target_public_key: &PublicKey,
+ ) -> ServerResult<()>;
}
impl BlackListExt for DatabaseConnection {
@@ -49,4 +56,26 @@ impl BlackListExt for DatabaseConnection {
.map(|u| u.is_some())
.map_err(Into::into)
}
+
+ async fn add_to_blacklist(
+ &self,
+ blacklister: &UserModel,
+ target_public_key: &PublicKey,
+ ) -> ServerResult<()> {
+ if self.is_blacklisted(blacklister, target_public_key).await? {
+ return Err(WsError::AlreadyOnTheblacklist.into());
+ }
+ if blacklister.public_key == target_public_key.to_string() {
+ return Err(WsError::CannotAddSelfToBlacklist.into());
+ }
+ BlacklistActiveModel {
+ user_id: Set(blacklister.id),
+ target: Set(target_public_key.to_string()),
+ blacklisted_at: Set(Utc::now()),
+ ..Default::default()
+ }
+ .save(self)
+ .await?;
+ Ok(())
+ }
}
diff --git a/crates/oxidetalis/src/database/out_chat_requests.rs b/crates/oxidetalis/src/database/out_chat_requests.rs
index b51274d..0994e4b 100644
--- a/crates/oxidetalis/src/database/out_chat_requests.rs
+++ b/crates/oxidetalis/src/database/out_chat_requests.rs
@@ -30,7 +30,7 @@ pub trait OutChatRequestsExt {
&self,
requester: &UserModel,
recipient: &PublicKey,
- ) -> ServerResult;
+ ) -> ServerResult