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 64 additions and 31 deletions
Showing only changes of commit 868ec66b68 - Show all commits

View file

@ -139,22 +139,23 @@ impl UsersStatusExt for DatabaseConnection {
whitelister: &UserModel, whitelister: &UserModel,
target_public_key: &PublicKey, target_public_key: &PublicKey,
) -> ServerResult<()> { ) -> ServerResult<()> {
if self.is_whitelisted(whitelister, target_public_key).await? {
return Err(WsError::AlreadyOnTheWhitelist.into());
}
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());
} }
if let Some(mut user) = get_user_status(self, whitelister, target_public_key) if let Some(mut user) = get_user_status(
self,
whitelister,
target_public_key,
AccessStatus::Blacklisted,
)
.await? .await?
.map(IntoActiveModel::into_active_model) .map(IntoActiveModel::into_active_model)
{ {
user.status = Set(AccessStatus::Whitelisted); user.status = Set(AccessStatus::Whitelisted);
user.updated_at = Set(Utc::now()); user.updated_at = Set(Utc::now());
user.update(self).await?; user.update(self).await?;
} else { } else if let Err(err) = (UsersStatusActiveModel {
UsersStatusActiveModel {
user_id: Set(whitelister.id), user_id: Set(whitelister.id),
target: Set(target_public_key.to_string()), target: Set(target_public_key.to_string()),
status: Set(AccessStatus::Whitelisted), status: Set(AccessStatus::Whitelisted),
@ -162,7 +163,14 @@ impl UsersStatusExt for DatabaseConnection {
..Default::default() ..Default::default()
} }
.save(self) .save(self)
.await?; .await)
{
match err.sql_err() {
Some(SqlErr::UniqueConstraintViolation(_)) => {
return Err(WsError::AlreadyOnTheWhitelist.into());
}
_ => return Err(err.into()),
}
} }
Ok(()) Ok(())
@ -173,22 +181,23 @@ impl UsersStatusExt for DatabaseConnection {
blacklister: &UserModel, blacklister: &UserModel,
target_public_key: &PublicKey, target_public_key: &PublicKey,
) -> ServerResult<()> { ) -> ServerResult<()> {
if self.is_blacklisted(blacklister, target_public_key).await? {
return Err(WsError::AlreadyOnTheBlacklist.into());
}
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());
} }
if let Some(mut user) = get_user_status(self, blacklister, target_public_key) if let Some(mut user) = get_user_status(
self,
blacklister,
target_public_key,
AccessStatus::Whitelisted,
)
.await? .await?
.map(IntoActiveModel::into_active_model) .map(IntoActiveModel::into_active_model)
{ {
user.status = Set(AccessStatus::Blacklisted); user.status = Set(AccessStatus::Blacklisted);
user.updated_at = Set(Utc::now()); user.updated_at = Set(Utc::now());
user.update(self).await?; user.update(self).await?;
} else { } else if let Err(err) = (UsersStatusActiveModel {
UsersStatusActiveModel {
user_id: Set(blacklister.id), user_id: Set(blacklister.id),
target: Set(target_public_key.to_string()), target: Set(target_public_key.to_string()),
status: Set(AccessStatus::Blacklisted), status: Set(AccessStatus::Blacklisted),
@ -196,7 +205,14 @@ impl UsersStatusExt for DatabaseConnection {
..Default::default() ..Default::default()
} }
.save(self) .save(self)
.await?; .await)
{
match err.sql_err() {
Some(SqlErr::UniqueConstraintViolation(_)) => {
return Err(WsError::AlreadyOnTheBlacklist.into());
}
_ => return Err(err.into()),
}
} }
Ok(()) Ok(())
@ -278,9 +294,14 @@ async fn get_user_status(
conn: &DatabaseConnection, conn: &DatabaseConnection,
user: &UserModel, user: &UserModel,
target_public_key: &PublicKey, target_public_key: &PublicKey,
status: AccessStatus,
) -> ServerResult<Option<UsersStatusModel>> { ) -> ServerResult<Option<UsersStatusModel>> {
user.find_related(UsersStatusEntity) user.find_related(UsersStatusEntity)
.filter(UsersStatusColumn::Target.eq(target_public_key.to_string())) .filter(
UsersStatusColumn::Target
.eq(target_public_key.to_string())
.and(UsersStatusColumn::Status.eq(status)),
)
.one(conn) .one(conn)
.await .await
.map_err(Into::into) .map_err(Into::into)

View file

@ -40,6 +40,18 @@ impl MigrationTrait for Migration {
.to_owned(), .to_owned(),
) )
.await?; .await?;
manager
.create_index(
Index::create()
.if_not_exists()
.name("sep_status")
.table(UsersStatus::Table)
.col(UsersStatus::UserId)
.col(UsersStatus::Target)
.unique()
.to_owned(),
)
.await?;
manager manager
.create_table( .create_table(