feat: Create unique index for UserId
and Target
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
parent
9c1ba97791
commit
868ec66b68
2 changed files with 64 additions and 31 deletions
|
@ -139,22 +139,23 @@ impl UsersStatusExt for DatabaseConnection {
|
|||
whitelister: &UserModel,
|
||||
target_public_key: &PublicKey,
|
||||
) -> ServerResult<()> {
|
||||
if self.is_whitelisted(whitelister, target_public_key).await? {
|
||||
return Err(WsError::AlreadyOnTheWhitelist.into());
|
||||
}
|
||||
if whitelister.public_key == target_public_key.to_string() {
|
||||
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?
|
||||
.map(IntoActiveModel::into_active_model)
|
||||
{
|
||||
user.status = Set(AccessStatus::Whitelisted);
|
||||
user.updated_at = Set(Utc::now());
|
||||
user.update(self).await?;
|
||||
} else {
|
||||
UsersStatusActiveModel {
|
||||
} else if let Err(err) = (UsersStatusActiveModel {
|
||||
user_id: Set(whitelister.id),
|
||||
target: Set(target_public_key.to_string()),
|
||||
status: Set(AccessStatus::Whitelisted),
|
||||
|
@ -162,7 +163,14 @@ impl UsersStatusExt for DatabaseConnection {
|
|||
..Default::default()
|
||||
}
|
||||
.save(self)
|
||||
.await?;
|
||||
.await)
|
||||
{
|
||||
match err.sql_err() {
|
||||
Some(SqlErr::UniqueConstraintViolation(_)) => {
|
||||
return Err(WsError::AlreadyOnTheWhitelist.into());
|
||||
}
|
||||
_ => return Err(err.into()),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -173,22 +181,23 @@ impl UsersStatusExt for DatabaseConnection {
|
|||
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());
|
||||
}
|
||||
|
||||
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?
|
||||
.map(IntoActiveModel::into_active_model)
|
||||
{
|
||||
user.status = Set(AccessStatus::Blacklisted);
|
||||
user.updated_at = Set(Utc::now());
|
||||
user.update(self).await?;
|
||||
} else {
|
||||
UsersStatusActiveModel {
|
||||
} else if let Err(err) = (UsersStatusActiveModel {
|
||||
user_id: Set(blacklister.id),
|
||||
target: Set(target_public_key.to_string()),
|
||||
status: Set(AccessStatus::Blacklisted),
|
||||
|
@ -196,7 +205,14 @@ impl UsersStatusExt for DatabaseConnection {
|
|||
..Default::default()
|
||||
}
|
||||
.save(self)
|
||||
.await?;
|
||||
.await)
|
||||
{
|
||||
match err.sql_err() {
|
||||
Some(SqlErr::UniqueConstraintViolation(_)) => {
|
||||
return Err(WsError::AlreadyOnTheBlacklist.into());
|
||||
}
|
||||
_ => return Err(err.into()),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -278,9 +294,14 @@ async fn get_user_status(
|
|||
conn: &DatabaseConnection,
|
||||
user: &UserModel,
|
||||
target_public_key: &PublicKey,
|
||||
status: AccessStatus,
|
||||
) -> ServerResult<Option<UsersStatusModel>> {
|
||||
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)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
|
|
|
@ -40,6 +40,18 @@ impl MigrationTrait for Migration {
|
|||
.to_owned(),
|
||||
)
|
||||
.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
|
||||
.create_table(
|
||||
|
|
Loading…
Reference in a new issue