From ab4e388ff837863c20ec949f4bc047bc054e83f2 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Tue, 23 Jul 2024 17:05:35 +0300 Subject: [PATCH] change: Change public key type to binary Signed-off-by: Awiteb --- .../src/database/out_chat_requests.rs | 2 +- crates/oxidetalis/src/database/user.rs | 2 +- crates/oxidetalis/src/database/user_status.rs | 2 +- .../oxidetalis_core/src/types/impl_sea_orm.rs | 34 +++++++++++++------ .../create_incoming_chat_requests_table.rs | 2 +- .../create_outgoing_chat_requests_table.rs | 2 +- .../src/create_users_status.rs | 2 +- .../src/create_users_table.rs | 2 +- 8 files changed, 31 insertions(+), 17 deletions(-) diff --git a/crates/oxidetalis/src/database/out_chat_requests.rs b/crates/oxidetalis/src/database/out_chat_requests.rs index 0274eda..586be54 100644 --- a/crates/oxidetalis/src/database/out_chat_requests.rs +++ b/crates/oxidetalis/src/database/out_chat_requests.rs @@ -57,7 +57,7 @@ impl OutChatRequestsExt for DatabaseConnection { ) -> ServerResult> { requester .find_related(OutChatRequestsEntity) - .filter(OutChatRequestsColumn::Recipient.eq(recipient.to_string())) + .filter(OutChatRequestsColumn::Recipient.eq(recipient)) .one(self) .await .map_err(Into::into) diff --git a/crates/oxidetalis/src/database/user.rs b/crates/oxidetalis/src/database/user.rs index 90065e8..d43301d 100644 --- a/crates/oxidetalis/src/database/user.rs +++ b/crates/oxidetalis/src/database/user.rs @@ -63,7 +63,7 @@ impl UserTableExt for DatabaseConnection { #[logcall] async fn get_user_by_pubk(&self, public_key: &PublicKey) -> ServerResult> { UserEntity::find() - .filter(UserColumn::PublicKey.eq(public_key.to_string())) + .filter(UserColumn::PublicKey.eq(public_key)) .one(self) .await .map_err(Into::into) diff --git a/crates/oxidetalis/src/database/user_status.rs b/crates/oxidetalis/src/database/user_status.rs index 43d1b36..76643ed 100644 --- a/crates/oxidetalis/src/database/user_status.rs +++ b/crates/oxidetalis/src/database/user_status.rs @@ -297,7 +297,7 @@ async fn get_user_status( user.find_related(UsersStatusEntity) .filter( UsersStatusColumn::Target - .eq(target_public_key.to_string()) + .eq(target_public_key) .and(UsersStatusColumn::Status.eq(status)), ) .one(conn) diff --git a/crates/oxidetalis_core/src/types/impl_sea_orm.rs b/crates/oxidetalis_core/src/types/impl_sea_orm.rs index 8d6f7c5..f5f5b6f 100644 --- a/crates/oxidetalis_core/src/types/impl_sea_orm.rs +++ b/crates/oxidetalis_core/src/types/impl_sea_orm.rs @@ -22,10 +22,8 @@ //! Implemented SeaORM support for core types, enabling the use of these types //! as column types in SeaORM -use std::str::FromStr; - use sea_orm::{ - sea_query::{ArrayType, ValueType, ValueTypeErr}, + sea_query::{ArrayType, BlobSize, ValueType, ValueTypeErr}, ColumnType, DbErr, QueryResult, @@ -38,22 +36,38 @@ use super::PublicKey; impl From for Value { fn from(public_key: PublicKey) -> Self { - Self::String(Some(Box::new(public_key.to_string()))) + public_key.as_bytes().as_slice().into() + } +} + +impl From<&PublicKey> for Value { + fn from(public_key: &PublicKey) -> Self { + public_key.as_bytes().as_slice().into() } } impl TryGetable for PublicKey { fn try_get_by(res: &QueryResult, idx: I) -> Result { - ::try_get_by(res, idx).and_then(|v| { - PublicKey::from_str(&v).map_err(|err| TryGetError::DbErr(DbErr::Type(err.to_string()))) + let db_err = |err: &str| TryGetError::DbErr(DbErr::Type(err.to_owned())); + + as TryGetable>::try_get_by(res, idx).and_then(|v| { + v.try_into() + .map_err(|_| db_err("Invalid binary length")) + .and_then(|bytes| { + >::try_from(bytes) + .map_err(|_| db_err("Invalid Public Key")) + }) }) } } impl ValueType for PublicKey { fn try_from(v: Value) -> Result { - ::try_from(v) - .and_then(|v| PublicKey::from_str(&v).map_err(|_| ValueTypeErr)) + as ValueType>::try_from(v).and_then(|v| { + v.try_into().map_err(|_| ValueTypeErr).and_then(|bytes| { + >::try_from(bytes).map_err(|_| ValueTypeErr) + }) + }) } fn type_name() -> String { @@ -61,10 +75,10 @@ impl ValueType for PublicKey { } fn array_type() -> ArrayType { - ArrayType::String + ArrayType::Bytes } fn column_type() -> ColumnType { - ColumnType::String(None) + ColumnType::Binary(BlobSize::Blob(None)) } } diff --git a/crates/oxidetalis_migrations/src/create_incoming_chat_requests_table.rs b/crates/oxidetalis_migrations/src/create_incoming_chat_requests_table.rs index 1af430c..3cd6fed 100644 --- a/crates/oxidetalis_migrations/src/create_incoming_chat_requests_table.rs +++ b/crates/oxidetalis_migrations/src/create_incoming_chat_requests_table.rs @@ -57,7 +57,7 @@ impl MigrationTrait for Migration { .on_update(ForeignKeyAction::NoAction) .on_delete(ForeignKeyAction::Cascade), ) - .col(ColumnDef::new(InChatRequests::Sender).string().not_null()) + .col(ColumnDef::new(InChatRequests::Sender).binary().not_null()) .col( ColumnDef::new(InChatRequests::InOn) .timestamp_with_time_zone() diff --git a/crates/oxidetalis_migrations/src/create_outgoing_chat_requests_table.rs b/crates/oxidetalis_migrations/src/create_outgoing_chat_requests_table.rs index 6130d87..daa1092 100644 --- a/crates/oxidetalis_migrations/src/create_outgoing_chat_requests_table.rs +++ b/crates/oxidetalis_migrations/src/create_outgoing_chat_requests_table.rs @@ -59,7 +59,7 @@ impl MigrationTrait for Migration { ) .col( ColumnDef::new(OutChatRequests::Recipient) - .string() + .binary() .not_null(), ) .col( diff --git a/crates/oxidetalis_migrations/src/create_users_status.rs b/crates/oxidetalis_migrations/src/create_users_status.rs index beba81b..aae3804 100644 --- a/crates/oxidetalis_migrations/src/create_users_status.rs +++ b/crates/oxidetalis_migrations/src/create_users_status.rs @@ -65,7 +65,7 @@ impl MigrationTrait for Migration { .on_update(ForeignKeyAction::NoAction) .on_delete(ForeignKeyAction::Cascade), ) - .col(ColumnDef::new(UsersStatus::Target).string().not_null()) + .col(ColumnDef::new(UsersStatus::Target).binary().not_null()) .col( ColumnDef::new(UsersStatus::Status) .enumeration( diff --git a/crates/oxidetalis_migrations/src/create_users_table.rs b/crates/oxidetalis_migrations/src/create_users_table.rs index d8f2956..9a52372 100644 --- a/crates/oxidetalis_migrations/src/create_users_table.rs +++ b/crates/oxidetalis_migrations/src/create_users_table.rs @@ -43,7 +43,7 @@ impl MigrationTrait for Migration { ) .col( ColumnDef::new(Users::PublicKey) - .string() + .binary() .not_null() .unique_key(), )