chore: small improvements and fixed some warns

This commit is contained in:
əlemi 2024-03-26 00:49:07 +01:00
parent 6d078f7068
commit 4a99a3dc40
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 13 additions and 5 deletions

View file

@ -1,3 +1,4 @@
use openssl::pkey::{PKey, Private};
use reqwest::header::USER_AGENT; use reqwest::header::USER_AGENT;
use sea_orm::{DatabaseConnection, EntityTrait, IntoActiveModel}; use sea_orm::{DatabaseConnection, EntityTrait, IntoActiveModel};
@ -18,13 +19,13 @@ pub enum FetchError {
pub struct Fetcher { pub struct Fetcher {
db: DatabaseConnection, db: DatabaseConnection,
key: String, // TODO store pre-parsed _key: PKey<Private>, // TODO store pre-parsed
domain: String, // TODO merge directly with Context so we don't need to copy this domain: String, // TODO merge directly with Context so we don't need to copy this
} }
impl Fetcher { impl Fetcher {
pub fn new(db: DatabaseConnection, domain: String, key: String) -> Self { pub fn new(db: DatabaseConnection, domain: String, key: String) -> Self {
Fetcher { db, domain, key } Fetcher { db, domain, _key: PKey::private_key_from_pem(key.as_bytes()).unwrap() }
} }
pub async fn user(&self, id: &str) -> Result<model::user::Model, FetchError> { pub async fn user(&self, id: &str) -> Result<model::user::Model, FetchError> {

View file

@ -112,17 +112,17 @@ async fn fetch(db: &sea_orm::DatabaseConnection, uri: &str, save: bool) -> reqwe
match obj.base_type() { match obj.base_type() {
Some(BaseType::Object(ObjectType::Actor(_))) => { Some(BaseType::Object(ObjectType::Actor(_))) => {
model::user::Entity::insert( model::user::Entity::insert(
model::user::Model::new(obj).unwrap().into_active_model() model::user::Model::new(&obj).unwrap().into_active_model()
).exec(db).await.unwrap(); ).exec(db).await.unwrap();
}, },
Some(BaseType::Object(ObjectType::Activity(_))) => { Some(BaseType::Object(ObjectType::Activity(_))) => {
model::activity::Entity::insert( model::activity::Entity::insert(
model::activity::Model::new(obj).unwrap().into_active_model() model::activity::Model::new(&obj).unwrap().into_active_model()
).exec(db).await.unwrap(); ).exec(db).await.unwrap();
}, },
Some(BaseType::Object(ObjectType::Note)) => { Some(BaseType::Object(ObjectType::Note)) => {
model::object::Entity::insert( model::object::Entity::insert(
model::object::Model::new(obj).unwrap().into_active_model() model::object::Model::new(&obj).unwrap().into_active_model()
).exec(db).await.unwrap(); ).exec(db).await.unwrap();
}, },
Some(BaseType::Object(t)) => tracing::warn!("not implemented: {:?}", t), Some(BaseType::Object(t)) => tracing::warn!("not implemented: {:?}", t),

View file

@ -18,6 +18,13 @@ pub mod faker;
#[error("missing required field: '{0}'")] #[error("missing required field: '{0}'")]
pub struct FieldError(pub &'static str); pub struct FieldError(pub &'static str);
impl From<FieldError> for axum::http::StatusCode {
fn from(value: FieldError) -> Self {
tracing::error!("bad request: {value}");
axum::http::StatusCode::BAD_REQUEST
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize, sea_orm::FromJsonQueryResult)] #[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize, sea_orm::FromJsonQueryResult)]
pub struct Audience(pub Vec<String>); pub struct Audience(pub Vec<String>);