feat: Chat request implementation #14

Manually merged
awiteb merged 55 commits from awiteb/chat-request-and-response into master 2024-07-18 14:21:39 +02:00 AGit
2 changed files with 59 additions and 2 deletions
Showing only changes of commit 1857365aac - Show all commits

View file

@ -23,6 +23,7 @@ use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*; use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
use super::WhiteListExt;
use crate::{errors::ServerResult, websocket::errors::WsError}; use crate::{errors::ServerResult, websocket::errors::WsError};
/// Extension trait for the `DatabaseConnection` to work with the blacklist /// Extension trait for the `DatabaseConnection` to work with the blacklist
@ -35,13 +36,22 @@ pub trait BlackListExt {
blacklister: &UserModel, blacklister: &UserModel,
target_public_key: &PublicKey, target_public_key: &PublicKey,
) -> ServerResult<bool>; ) -> ServerResult<bool>;
/// Add the `target_public_key` to the blacklist of the `blacklister`
/// Add the `target_public_key` to the blacklist of the `blacklister` and
/// remove it from the whitelist table (if it's there)
async fn add_to_blacklist( async fn add_to_blacklist(
&self, &self,
blacklister: &UserModel, blacklister: &UserModel,
target_public_key: &PublicKey, target_public_key: &PublicKey,
) -> ServerResult<()>; ) -> ServerResult<()>;
/// Remove the target from blacklist table
async fn remove_from_blacklist(
&self,
blacklister: &UserModel,
target_public_key: &PublicKey,
) -> ServerResult<()>;
/// Returns the blacklist of the user /// Returns the blacklist of the user
async fn user_blacklist( async fn user_blacklist(
&self, &self,
@ -78,6 +88,9 @@ impl BlackListExt for DatabaseConnection {
if blacklister.public_key == target_public_key.to_string() { if blacklister.public_key == target_public_key.to_string() {
return Err(WsError::CannotAddSelfToBlacklist.into()); return Err(WsError::CannotAddSelfToBlacklist.into());
} }
self.remove_from_whitelist(blacklister, target_public_key)
.await?;
BlacklistActiveModel { BlacklistActiveModel {
user_id: Set(blacklister.id), user_id: Set(blacklister.id),
target: Set(target_public_key.to_string()), target: Set(target_public_key.to_string()),
@ -89,6 +102,22 @@ impl BlackListExt for DatabaseConnection {
Ok(()) Ok(())
} }
async fn remove_from_blacklist(
&self,
blacklister: &UserModel,
target_public_key: &PublicKey,
) -> ServerResult<()> {
if let Some(target_user) = blacklister
.find_related(BlacklistEntity)
.filter(BlacklistColumn::Target.eq(target_public_key.to_string()))
.one(self)
.await?
{
target_user.delete(self).await?;
}
Ok(())
}
async fn user_blacklist( async fn user_blacklist(
&self, &self,
blacklister: &UserModel, blacklister: &UserModel,

View file

@ -23,6 +23,7 @@ use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*; use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
use super::BlackListExt;
use crate::{errors::ServerResult, websocket::errors::WsError}; use crate::{errors::ServerResult, websocket::errors::WsError};
/// Extension trait for the `DatabaseConnection` to work with the whitelist /// Extension trait for the `DatabaseConnection` to work with the whitelist
@ -36,13 +37,21 @@ pub trait WhiteListExt {
target_public_key: &PublicKey, target_public_key: &PublicKey,
) -> ServerResult<bool>; ) -> ServerResult<bool>;
/// Add the `target_public_key` to the whitelist of the `whitelister` /// Add the `target_public_key` to the whitelist of the `whitelister` and
/// remove it from the blacklist table (if it's there)
async fn add_to_whitelist( async fn add_to_whitelist(
&self, &self,
whitelister: &UserModel, whitelister: &UserModel,
target_public_key: &PublicKey, target_public_key: &PublicKey,
) -> ServerResult<()>; ) -> ServerResult<()>;
/// Remove the target from whitelist table
async fn remove_from_whitelist(
&self,
whitelister: &UserModel,
target_public_key: &PublicKey,
) -> ServerResult<()>;
/// Returns the whitelist of the user /// Returns the whitelist of the user
async fn user_whitelist( async fn user_whitelist(
&self, &self,
@ -78,6 +87,9 @@ impl WhiteListExt for DatabaseConnection {
if whitelister.public_key == target_public_key.to_string() { if whitelister.public_key == target_public_key.to_string() {
return Err(WsError::CannotAddSelfToWhitelist.into()); return Err(WsError::CannotAddSelfToWhitelist.into());
} }
self.remove_from_blacklist(whitelister, target_public_key)
.await?;
WhitelistActiveModel { WhitelistActiveModel {
user_id: Set(whitelister.id), user_id: Set(whitelister.id),
target: Set(target_public_key.to_string()), target: Set(target_public_key.to_string()),
@ -89,6 +101,22 @@ impl WhiteListExt for DatabaseConnection {
Ok(()) Ok(())
} }
async fn remove_from_whitelist(
&self,
whitelister: &UserModel,
target_public_key: &PublicKey,
) -> ServerResult<()> {
if let Some(target_user) = whitelister
.find_related(WhitelistEntity)
.filter(WhitelistColumn::Target.eq(target_public_key.to_string()))
.one(self)
.await?
{
target_user.delete(self).await?;
}
Ok(())
}
async fn user_whitelist( async fn user_whitelist(
&self, &self,
whitelister: &UserModel, whitelister: &UserModel,