From 79ef0d244816cc0f3d0c31cb5b0732acc2259823 Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Mon, 22 Jul 2024 22:50:36 +0300
Subject: [PATCH] refactor: Update public key column type from `String` to
`PublicKey`
Signed-off-by: Awiteb
---
.../src/database/in_chat_requests.rs | 2 +-
.../src/database/out_chat_requests.rs | 2 +-
crates/oxidetalis/src/database/user.rs | 2 +-
crates/oxidetalis/src/database/user_status.rs | 8 +++----
crates/oxidetalis/src/schemas/user.rs | 4 ++--
.../src/websocket/handlers/chat_request.rs | 22 ++++++-------------
.../src/incoming_chat_requests.rs | 3 ++-
.../src/outgoing_chat_requests.rs | 3 ++-
crates/oxidetalis_entities/src/users.rs | 3 ++-
.../oxidetalis_entities/src/users_status.rs | 4 ++--
10 files changed, 24 insertions(+), 29 deletions(-)
diff --git a/crates/oxidetalis/src/database/in_chat_requests.rs b/crates/oxidetalis/src/database/in_chat_requests.rs
index 061fb84..10c150a 100644
--- a/crates/oxidetalis/src/database/in_chat_requests.rs
+++ b/crates/oxidetalis/src/database/in_chat_requests.rs
@@ -42,7 +42,7 @@ impl InChatRequestsExt for DatabaseConnection {
) -> ServerResult<()> {
InChatRequestsEntity::insert(InChatRequestsActiveModel {
recipient_id: Set(recipient.id),
- sender: Set(sender.to_string()),
+ sender: Set(*sender),
in_on: Set(Utc::now()),
..Default::default()
})
diff --git a/crates/oxidetalis/src/database/out_chat_requests.rs b/crates/oxidetalis/src/database/out_chat_requests.rs
index ed34d8a..0274eda 100644
--- a/crates/oxidetalis/src/database/out_chat_requests.rs
+++ b/crates/oxidetalis/src/database/out_chat_requests.rs
@@ -71,7 +71,7 @@ impl OutChatRequestsExt for DatabaseConnection {
) -> ServerResult<()> {
if let Err(err) = (OutChatRequestsActiveModel {
sender_id: Set(requester.id),
- recipient: Set(recipient.to_string()),
+ recipient: Set(*recipient),
out_on: Set(Utc::now()),
..Default::default()
}
diff --git a/crates/oxidetalis/src/database/user.rs b/crates/oxidetalis/src/database/user.rs
index 79fc109..90065e8 100644
--- a/crates/oxidetalis/src/database/user.rs
+++ b/crates/oxidetalis/src/database/user.rs
@@ -45,7 +45,7 @@ impl UserTableExt for DatabaseConnection {
#[logcall]
async fn register_user(&self, public_key: &PublicKey, is_admin: bool) -> ServerResult<()> {
if let Err(err) = (UserActiveModel {
- public_key: Set(public_key.to_string()),
+ public_key: Set(*public_key),
is_admin: Set(is_admin),
..Default::default()
})
diff --git a/crates/oxidetalis/src/database/user_status.rs b/crates/oxidetalis/src/database/user_status.rs
index 4daf506..43d1b36 100644
--- a/crates/oxidetalis/src/database/user_status.rs
+++ b/crates/oxidetalis/src/database/user_status.rs
@@ -138,7 +138,7 @@ impl UsersStatusExt for DatabaseConnection {
whitelister: &UserModel,
target_public_key: &PublicKey,
) -> ServerResult<()> {
- if whitelister.public_key == target_public_key.to_string() {
+ if &whitelister.public_key == target_public_key {
return Err(WsError::CannotAddSelfToWhitelist.into());
}
@@ -156,7 +156,7 @@ impl UsersStatusExt for DatabaseConnection {
user.update(self).await?;
} else if let Err(err) = (UsersStatusActiveModel {
user_id: Set(whitelister.id),
- target: Set(target_public_key.to_string()),
+ target: Set(*target_public_key),
status: Set(AccessStatus::Whitelisted),
updated_at: Set(Utc::now()),
..Default::default()
@@ -181,7 +181,7 @@ impl UsersStatusExt for DatabaseConnection {
blacklister: &UserModel,
target_public_key: &PublicKey,
) -> ServerResult<()> {
- if blacklister.public_key == target_public_key.to_string() {
+ if &blacklister.public_key == target_public_key {
return Err(WsError::CannotAddSelfToBlacklist.into());
}
@@ -199,7 +199,7 @@ impl UsersStatusExt for DatabaseConnection {
user.update(self).await?;
} else if let Err(err) = (UsersStatusActiveModel {
user_id: Set(blacklister.id),
- target: Set(target_public_key.to_string()),
+ target: Set(*target_public_key),
status: Set(AccessStatus::Blacklisted),
updated_at: Set(Utc::now()),
..Default::default()
diff --git a/crates/oxidetalis/src/schemas/user.rs b/crates/oxidetalis/src/schemas/user.rs
index feec7f2..237c201 100644
--- a/crates/oxidetalis/src/schemas/user.rs
+++ b/crates/oxidetalis/src/schemas/user.rs
@@ -54,7 +54,7 @@ impl Default for WhiteListedUser {
impl From for WhiteListedUser {
fn from(user: UsersStatusModel) -> Self {
Self {
- public_key: PublicKey::from_str(&user.target).expect("Is valid public key"),
+ public_key: user.target,
whitelisted_at: user.updated_at,
}
}
@@ -72,7 +72,7 @@ impl Default for BlackListedUser {
impl From for BlackListedUser {
fn from(user: UsersStatusModel) -> Self {
Self {
- public_key: PublicKey::from_str(&user.target).expect("Is valid public key"),
+ public_key: user.target,
blacklisted_at: user.updated_at,
}
}
diff --git a/crates/oxidetalis/src/websocket/handlers/chat_request.rs b/crates/oxidetalis/src/websocket/handlers/chat_request.rs
index e18712f..222137a 100644
--- a/crates/oxidetalis/src/websocket/handlers/chat_request.rs
+++ b/crates/oxidetalis/src/websocket/handlers/chat_request.rs
@@ -16,8 +16,6 @@
//! Handler for incoming and outgoing chat requests.
-use std::str::FromStr;
-
use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection;
@@ -47,14 +45,12 @@ pub async fn handle_chat_request(
if from_user.id == to_user.id {
return Some(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!(Some db.get_chat_request_to(from_user, to_public_key).await).is_some() {
return Some(WsError::AlreadySendChatRequest.into());
}
- if try_ws!(Some db.is_blacklisted(&to_user, &from_public_key).await) {
+ if try_ws!(Some db.is_blacklisted(&to_user, &from_user.public_key).await) {
return Some(WsError::RecipientBlacklist.into());
}
@@ -64,17 +60,17 @@ pub async fn handle_chat_request(
return Some(WsError::InternalServerError.into());
}
- if try_ws!(Some db.is_whitelisted(&to_user, &from_public_key).await) {
+ if try_ws!(Some db.is_whitelisted(&to_user, &from_user.public_key).await) {
return Some(WsError::AlreadyInRecipientWhitelist.into());
}
try_ws!(Some 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))
+ .send(&conn_id, ServerEvent::chat_request(&from_user.public_key))
.await;
} else {
- try_ws!(Some db.save_in_chat_request(&from_public_key, &to_user).await);
+ try_ws!(Some db.save_in_chat_request(&from_user.public_key, &to_user).await);
}
None
}
@@ -96,12 +92,8 @@ pub async fn handle_chat_response(
return Some(WsError::CannotRespondToOwnChatRequest.into());
}
- // FIXME: When change the entity public key to a PublicKey type, change this
- let recipient_public_key =
- PublicKey::from_str(&recipient_user.public_key).expect("Is valid public key");
-
if try_ws!(Some
- db.get_chat_request_to(&sender_user, &recipient_public_key)
+ db.get_chat_request_to(&sender_user, &recipient_user.public_key)
.await
)
.is_none()
@@ -118,7 +110,7 @@ pub async fn handle_chat_response(
};
try_ws!(Some
- db.remove_out_chat_request(&sender_user, &recipient_public_key)
+ db.remove_out_chat_request(&sender_user, &recipient_user.public_key)
.await
);
@@ -126,7 +118,7 @@ pub async fn handle_chat_response(
ONLINE_USERS
.send(
&conn_id,
- ServerEvent::chat_request_response(recipient_public_key, accepted),
+ ServerEvent::chat_request_response(recipient_user.public_key, accepted),
)
.await;
} else {
diff --git a/crates/oxidetalis_entities/src/incoming_chat_requests.rs b/crates/oxidetalis_entities/src/incoming_chat_requests.rs
index 9c55f74..a34d7c7 100644
--- a/crates/oxidetalis_entities/src/incoming_chat_requests.rs
+++ b/crates/oxidetalis_entities/src/incoming_chat_requests.rs
@@ -22,6 +22,7 @@
//! Entity for `in_chat_requests` table
use chrono::Utc;
+use oxidetalis_core::types::PublicKey as CorePublicKey;
use sea_orm::entity::prelude::*;
use crate::prelude::*;
@@ -33,7 +34,7 @@ pub struct Model {
pub id: UserId,
pub recipient_id: UserId,
/// Public key of the sender
- pub sender: String,
+ pub sender: CorePublicKey,
/// The timestamp of the request, when it was received
pub in_on: chrono::DateTime,
}
diff --git a/crates/oxidetalis_entities/src/outgoing_chat_requests.rs b/crates/oxidetalis_entities/src/outgoing_chat_requests.rs
index 95ba3f1..0f5a614 100644
--- a/crates/oxidetalis_entities/src/outgoing_chat_requests.rs
+++ b/crates/oxidetalis_entities/src/outgoing_chat_requests.rs
@@ -22,6 +22,7 @@
//! Entity for `out_chat_requests` table
use chrono::Utc;
+use oxidetalis_core::types::PublicKey as CorePublicKey;
use sea_orm::entity::prelude::*;
use crate::prelude::*;
@@ -33,7 +34,7 @@ pub struct Model {
pub id: UserId,
pub sender_id: UserId,
/// Public key of the recipient
- pub recipient: String,
+ pub recipient: CorePublicKey,
/// The timestamp of the request, when it was sent
pub out_on: chrono::DateTime,
}
diff --git a/crates/oxidetalis_entities/src/users.rs b/crates/oxidetalis_entities/src/users.rs
index 1a7692b..df6dd82 100644
--- a/crates/oxidetalis_entities/src/users.rs
+++ b/crates/oxidetalis_entities/src/users.rs
@@ -21,6 +21,7 @@
//! Entity for `users` table
+use oxidetalis_core::types::PublicKey as CorePublicKey;
use sea_orm::entity::prelude::*;
use crate::prelude::*;
@@ -30,7 +31,7 @@ use crate::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: UserId,
- pub public_key: String,
+ pub public_key: CorePublicKey,
pub is_admin: bool,
}
diff --git a/crates/oxidetalis_entities/src/users_status.rs b/crates/oxidetalis_entities/src/users_status.rs
index 23eab9c..7651bc1 100644
--- a/crates/oxidetalis_entities/src/users_status.rs
+++ b/crates/oxidetalis_entities/src/users_status.rs
@@ -22,6 +22,7 @@
//! Entity for `users_status` table
use chrono::Utc;
+use oxidetalis_core::types::PublicKey as CorePublicKey;
use sea_orm::entity::prelude::*;
use crate::prelude::*;
@@ -41,8 +42,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub id: UserId,
pub user_id: UserId,
- /// Public key of the target
- pub target: String,
+ pub target: CorePublicKey,
pub status: AccessStatus,
pub updated_at: chrono::DateTime,
}