refactor: Update public key column type from String
to PublicKey
#29
8 changed files with 31 additions and 17 deletions
|
@ -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)
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
awiteb marked this conversation as resolved
|
|||||||
|
|
||||||
|
impl From<&PublicKey> for Value {
|
||||||
|
fn from(public_key: &PublicKey) -> Self {
|
||||||
|
public_key.as_bytes().as_slice().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
awiteb marked this conversation as resolved
Amjad50
commented
replace
replace `map` by `and_then` and you won't need `and_then(|res| res)`
```rust
<String as TryGetable>::try_get_by(res, idx).and_then(|v| {
PublicKey::from_str(&v).map_err(|err| TryGetError::DbErr(DbErr::Type(err.to_string())))
})
```
|
|||||||
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"))
|
||||||
|
})
|
||||||
awiteb marked this conversation as resolved
Amjad50
commented
same as above, same as above, `and_then` instead of `map`
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -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(),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue
Wouldn't it be better to use
Bytes
type? easier to convert as its just copying bytes and making sure its the length expected and maybe more efficientYes I think so, it is actually a good idea.