refactor: Update public key column type from String to PublicKey #29

Manually merged
awiteb merged 6 commits from awiteb/refactor-entities-public-key into master 2024-07-24 00:20:10 +02:00 AGit
10 changed files with 24 additions and 29 deletions
Showing only changes of commit 79ef0d2448 - Show all commits

View file

@ -42,7 +42,7 @@ impl InChatRequestsExt for DatabaseConnection {
) -> ServerResult<()> { ) -> ServerResult<()> {
InChatRequestsEntity::insert(InChatRequestsActiveModel { InChatRequestsEntity::insert(InChatRequestsActiveModel {
recipient_id: Set(recipient.id), recipient_id: Set(recipient.id),
sender: Set(sender.to_string()), sender: Set(*sender),
in_on: Set(Utc::now()), in_on: Set(Utc::now()),
..Default::default() ..Default::default()
}) })

View file

@ -71,7 +71,7 @@ impl OutChatRequestsExt for DatabaseConnection {
) -> ServerResult<()> { ) -> ServerResult<()> {
if let Err(err) = (OutChatRequestsActiveModel { if let Err(err) = (OutChatRequestsActiveModel {
sender_id: Set(requester.id), sender_id: Set(requester.id),
recipient: Set(recipient.to_string()), recipient: Set(*recipient),
out_on: Set(Utc::now()), out_on: Set(Utc::now()),
..Default::default() ..Default::default()
} }

View file

@ -45,7 +45,7 @@ impl UserTableExt for DatabaseConnection {
#[logcall] #[logcall]
async fn register_user(&self, public_key: &PublicKey, is_admin: bool) -> ServerResult<()> { async fn register_user(&self, public_key: &PublicKey, is_admin: bool) -> ServerResult<()> {
if let Err(err) = (UserActiveModel { if let Err(err) = (UserActiveModel {
public_key: Set(public_key.to_string()), public_key: Set(*public_key),
is_admin: Set(is_admin), is_admin: Set(is_admin),
..Default::default() ..Default::default()
}) })

View file

@ -138,7 +138,7 @@ impl UsersStatusExt for DatabaseConnection {
whitelister: &UserModel, whitelister: &UserModel,
target_public_key: &PublicKey, target_public_key: &PublicKey,
) -> ServerResult<()> { ) -> ServerResult<()> {
if whitelister.public_key == target_public_key.to_string() { if &whitelister.public_key == target_public_key {
return Err(WsError::CannotAddSelfToWhitelist.into()); return Err(WsError::CannotAddSelfToWhitelist.into());
} }
@ -156,7 +156,7 @@ impl UsersStatusExt for DatabaseConnection {
user.update(self).await?; user.update(self).await?;
} else if let Err(err) = (UsersStatusActiveModel { } else if let Err(err) = (UsersStatusActiveModel {
user_id: Set(whitelister.id), user_id: Set(whitelister.id),
target: Set(target_public_key.to_string()), target: Set(*target_public_key),
status: Set(AccessStatus::Whitelisted), status: Set(AccessStatus::Whitelisted),
updated_at: Set(Utc::now()), updated_at: Set(Utc::now()),
..Default::default() ..Default::default()
@ -181,7 +181,7 @@ impl UsersStatusExt for DatabaseConnection {
blacklister: &UserModel, blacklister: &UserModel,
target_public_key: &PublicKey, target_public_key: &PublicKey,
) -> ServerResult<()> { ) -> ServerResult<()> {
if blacklister.public_key == target_public_key.to_string() { if &blacklister.public_key == target_public_key {
return Err(WsError::CannotAddSelfToBlacklist.into()); return Err(WsError::CannotAddSelfToBlacklist.into());
} }
@ -199,7 +199,7 @@ impl UsersStatusExt for DatabaseConnection {
user.update(self).await?; user.update(self).await?;
} else if let Err(err) = (UsersStatusActiveModel { } else if let Err(err) = (UsersStatusActiveModel {
user_id: Set(blacklister.id), user_id: Set(blacklister.id),
target: Set(target_public_key.to_string()), target: Set(*target_public_key),
status: Set(AccessStatus::Blacklisted), status: Set(AccessStatus::Blacklisted),
updated_at: Set(Utc::now()), updated_at: Set(Utc::now()),
..Default::default() ..Default::default()

View file

@ -54,7 +54,7 @@ impl Default for WhiteListedUser {
impl From<UsersStatusModel> for WhiteListedUser { impl From<UsersStatusModel> for WhiteListedUser {
fn from(user: UsersStatusModel) -> Self { fn from(user: UsersStatusModel) -> Self {
Self { Self {
public_key: PublicKey::from_str(&user.target).expect("Is valid public key"), public_key: user.target,
whitelisted_at: user.updated_at, whitelisted_at: user.updated_at,
} }
} }
@ -72,7 +72,7 @@ impl Default for BlackListedUser {
impl From<UsersStatusModel> for BlackListedUser { impl From<UsersStatusModel> for BlackListedUser {
fn from(user: UsersStatusModel) -> Self { fn from(user: UsersStatusModel) -> Self {
Self { Self {
public_key: PublicKey::from_str(&user.target).expect("Is valid public key"), public_key: user.target,
blacklisted_at: user.updated_at, blacklisted_at: user.updated_at,
} }
} }

View file

@ -16,8 +16,6 @@
//! Handler for incoming and outgoing chat requests. //! Handler for incoming and outgoing chat requests.
use std::str::FromStr;
use oxidetalis_core::types::PublicKey; use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*; use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
@ -47,14 +45,12 @@ pub async fn handle_chat_request(
if from_user.id == to_user.id { if from_user.id == to_user.id {
return Some(WsError::CannotSendChatRequestToSelf.into()); 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() { if try_ws!(Some db.get_chat_request_to(from_user, to_public_key).await).is_some() {
return Some(WsError::AlreadySendChatRequest.into()); 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()); return Some(WsError::RecipientBlacklist.into());
} }
@ -64,17 +60,17 @@ pub async fn handle_chat_request(
return Some(WsError::InternalServerError.into()); 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()); return Some(WsError::AlreadyInRecipientWhitelist.into());
} }
try_ws!(Some db.save_out_chat_request(from_user, to_public_key).await); 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 { if let Some(conn_id) = ONLINE_USERS.is_online(to_public_key).await {
ONLINE_USERS ONLINE_USERS
.send(&conn_id, ServerEvent::chat_request(&from_public_key)) .send(&conn_id, ServerEvent::chat_request(&from_user.public_key))
.await; .await;
} else { } 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 None
} }
@ -96,12 +92,8 @@ pub async fn handle_chat_response(
return Some(WsError::CannotRespondToOwnChatRequest.into()); 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 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 .await
) )
.is_none() .is_none()
@ -118,7 +110,7 @@ pub async fn handle_chat_response(
}; };
try_ws!(Some 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 .await
); );
@ -126,7 +118,7 @@ pub async fn handle_chat_response(
ONLINE_USERS ONLINE_USERS
.send( .send(
&conn_id, &conn_id,
ServerEvent::chat_request_response(recipient_public_key, accepted), ServerEvent::chat_request_response(recipient_user.public_key, accepted),
) )
.await; .await;
} else { } else {

View file

@ -22,6 +22,7 @@
//! Entity for `in_chat_requests` table //! Entity for `in_chat_requests` table
use chrono::Utc; use chrono::Utc;
use oxidetalis_core::types::PublicKey as CorePublicKey;
Amjad50 marked this conversation as resolved
Review

Why is it here CorePublicKey? while in oxidetails crate is just PublicKey, I think it might be confusing as we would think its 2 types.

Why is it here `CorePublicKey`? while in `oxidetails` crate is just `PublicKey`, I think it might be confusing as we would think its 2 types.
Review

while in oxidetails crate is just PublicKey

Because oxidetalis only work with one PublicKey from the core, while
the core work with two PublicKey, the first one from k256 crate and
the second one it its own.

> while in `oxidetails` crate is just `PublicKey` Because `oxidetalis` only work with one `PublicKey` from the core, while the core work with two `PublicKey`, the first one from `k256` crate and the second one it its own.
Review

Ah right, didn't notice that

Ah right, didn't notice that
Review

Amjad50: Why is it here CorePublicKey? while in oxidetails crate is just PublicKey

You're right, I was mean if we use it in the core not entities.

awiteb: while the core work with two PublicKey

I didn't realize that was in entities.

> Amjad50: Why is it here CorePublicKey? while in oxidetails crate is just PublicKey You're right, I was mean if we use it in the core not entities. > awiteb: while the core work with two PublicKey I didn't realize that was in entities.
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use crate::prelude::*; use crate::prelude::*;
@ -33,7 +34,7 @@ pub struct Model {
pub id: UserId, pub id: UserId,
pub recipient_id: UserId, pub recipient_id: UserId,
/// Public key of the sender /// Public key of the sender
pub sender: String, pub sender: CorePublicKey,
/// The timestamp of the request, when it was received /// The timestamp of the request, when it was received
pub in_on: chrono::DateTime<Utc>, pub in_on: chrono::DateTime<Utc>,
} }

View file

@ -22,6 +22,7 @@
//! Entity for `out_chat_requests` table //! Entity for `out_chat_requests` table
use chrono::Utc; use chrono::Utc;
use oxidetalis_core::types::PublicKey as CorePublicKey;
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use crate::prelude::*; use crate::prelude::*;
@ -33,7 +34,7 @@ pub struct Model {
pub id: UserId, pub id: UserId,
pub sender_id: UserId, pub sender_id: UserId,
/// Public key of the recipient /// Public key of the recipient
pub recipient: String, pub recipient: CorePublicKey,
/// The timestamp of the request, when it was sent /// The timestamp of the request, when it was sent
pub out_on: chrono::DateTime<Utc>, pub out_on: chrono::DateTime<Utc>,
} }

View file

@ -21,6 +21,7 @@
//! Entity for `users` table //! Entity for `users` table
use oxidetalis_core::types::PublicKey as CorePublicKey;
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use crate::prelude::*; use crate::prelude::*;
@ -30,7 +31,7 @@ use crate::prelude::*;
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: UserId, pub id: UserId,
pub public_key: String, pub public_key: CorePublicKey,
pub is_admin: bool, pub is_admin: bool,
} }

View file

@ -22,6 +22,7 @@
//! Entity for `users_status` table //! Entity for `users_status` table
use chrono::Utc; use chrono::Utc;
use oxidetalis_core::types::PublicKey as CorePublicKey;
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use crate::prelude::*; use crate::prelude::*;
@ -41,8 +42,7 @@ pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: UserId, pub id: UserId,
pub user_id: UserId, pub user_id: UserId,
/// Public key of the target pub target: CorePublicKey,
pub target: String,
pub status: AccessStatus, pub status: AccessStatus,
pub updated_at: chrono::DateTime<Utc>, pub updated_at: chrono::DateTime<Utc>,
} }