change: Change public key type to binary

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-23 17:05:35 +03:00
parent 35a7354de1
commit ab4e388ff8
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
8 changed files with 31 additions and 17 deletions

View file

@ -57,7 +57,7 @@ impl OutChatRequestsExt for DatabaseConnection {
) -> ServerResult<Option<OutChatRequestsModel>> { ) -> ServerResult<Option<OutChatRequestsModel>> {
requester requester
.find_related(OutChatRequestsEntity) .find_related(OutChatRequestsEntity)
.filter(OutChatRequestsColumn::Recipient.eq(recipient.to_string())) .filter(OutChatRequestsColumn::Recipient.eq(recipient))
.one(self) .one(self)
.await .await
.map_err(Into::into) .map_err(Into::into)

View file

@ -63,7 +63,7 @@ impl UserTableExt for DatabaseConnection {
#[logcall] #[logcall]
async fn get_user_by_pubk(&self, public_key: &PublicKey) -> ServerResult<Option<UserModel>> { async fn get_user_by_pubk(&self, public_key: &PublicKey) -> ServerResult<Option<UserModel>> {
UserEntity::find() UserEntity::find()
.filter(UserColumn::PublicKey.eq(public_key.to_string())) .filter(UserColumn::PublicKey.eq(public_key))
.one(self) .one(self)
.await .await
.map_err(Into::into) .map_err(Into::into)

View file

@ -297,7 +297,7 @@ async fn get_user_status(
user.find_related(UsersStatusEntity) user.find_related(UsersStatusEntity)
.filter( .filter(
UsersStatusColumn::Target UsersStatusColumn::Target
.eq(target_public_key.to_string()) .eq(target_public_key)
.and(UsersStatusColumn::Status.eq(status)), .and(UsersStatusColumn::Status.eq(status)),
) )
.one(conn) .one(conn)

View file

@ -22,10 +22,8 @@
//! Implemented SeaORM support for core types, enabling the use of these types //! Implemented SeaORM support for core types, enabling the use of these types
//! as column types in SeaORM //! as column types in SeaORM
use std::str::FromStr;
use sea_orm::{ use sea_orm::{
sea_query::{ArrayType, ValueType, ValueTypeErr}, sea_query::{ArrayType, BlobSize, ValueType, ValueTypeErr},
ColumnType, ColumnType,
DbErr, DbErr,
QueryResult, QueryResult,
@ -38,22 +36,38 @@ use super::PublicKey;
impl From<PublicKey> for Value { impl From<PublicKey> for Value {
fn from(public_key: PublicKey) -> Self { 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 { impl TryGetable for PublicKey {
fn try_get_by<I: sea_orm::ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> { fn try_get_by<I: sea_orm::ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> {
<String as TryGetable>::try_get_by(res, idx).and_then(|v| { let db_err = |err: &str| TryGetError::DbErr(DbErr::Type(err.to_owned()));
PublicKey::from_str(&v).map_err(|err| TryGetError::DbErr(DbErr::Type(err.to_string())))
<Vec<u8> as TryGetable>::try_get_by(res, idx).and_then(|v| {
v.try_into()
.map_err(|_| db_err("Invalid binary length"))
.and_then(|bytes| {
<PublicKey as TryFrom<[u8; 33]>>::try_from(bytes)
.map_err(|_| db_err("Invalid Public Key"))
})
}) })
} }
} }
impl ValueType for PublicKey { impl ValueType for PublicKey {
fn try_from(v: Value) -> Result<Self, ValueTypeErr> { fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
<String as ValueType>::try_from(v) <Vec<u8> as ValueType>::try_from(v).and_then(|v| {
.and_then(|v| PublicKey::from_str(&v).map_err(|_| ValueTypeErr)) v.try_into().map_err(|_| ValueTypeErr).and_then(|bytes| {
<PublicKey as TryFrom<[u8; 33]>>::try_from(bytes).map_err(|_| ValueTypeErr)
})
})
} }
fn type_name() -> String { fn type_name() -> String {
@ -61,10 +75,10 @@ impl ValueType for PublicKey {
} }
fn array_type() -> ArrayType { fn array_type() -> ArrayType {
ArrayType::String ArrayType::Bytes
} }
fn column_type() -> ColumnType { fn column_type() -> ColumnType {
ColumnType::String(None) ColumnType::Binary(BlobSize::Blob(None))
} }
} }

View file

@ -57,7 +57,7 @@ impl MigrationTrait for Migration {
.on_update(ForeignKeyAction::NoAction) .on_update(ForeignKeyAction::NoAction)
.on_delete(ForeignKeyAction::Cascade), .on_delete(ForeignKeyAction::Cascade),
) )
.col(ColumnDef::new(InChatRequests::Sender).string().not_null()) .col(ColumnDef::new(InChatRequests::Sender).binary().not_null())
.col( .col(
ColumnDef::new(InChatRequests::InOn) ColumnDef::new(InChatRequests::InOn)
.timestamp_with_time_zone() .timestamp_with_time_zone()

View file

@ -59,7 +59,7 @@ impl MigrationTrait for Migration {
) )
.col( .col(
ColumnDef::new(OutChatRequests::Recipient) ColumnDef::new(OutChatRequests::Recipient)
.string() .binary()
.not_null(), .not_null(),
) )
.col( .col(

View file

@ -65,7 +65,7 @@ impl MigrationTrait for Migration {
.on_update(ForeignKeyAction::NoAction) .on_update(ForeignKeyAction::NoAction)
.on_delete(ForeignKeyAction::Cascade), .on_delete(ForeignKeyAction::Cascade),
) )
.col(ColumnDef::new(UsersStatus::Target).string().not_null()) .col(ColumnDef::new(UsersStatus::Target).binary().not_null())
.col( .col(
ColumnDef::new(UsersStatus::Status) ColumnDef::new(UsersStatus::Status)
.enumeration( .enumeration(

View file

@ -43,7 +43,7 @@ impl MigrationTrait for Migration {
) )
.col( .col(
ColumnDef::new(Users::PublicKey) ColumnDef::new(Users::PublicKey)
.string() .binary()
.not_null() .not_null()
.unique_key(), .unique_key(),
) )