From f1bcd13cf931c5579d800c77404288a76f109bf0 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Sat, 6 Apr 2024 20:20:29 +0200 Subject: [PATCH] progress --- Cargo.lock | 1 - crates/kitsune-activitypub/Cargo.toml | 1 - .../kitsune-activitypub/src/fetcher/actor.rs | 3 +-- crates/kitsune-activitypub/src/fetcher/mod.rs | 6 +++--- crates/kitsune-activitypub/src/lib.rs | 15 ++++++--------- .../kitsune-activitypub/src/mapping/object.rs | 17 ++++++++++++----- crates/kitsune-error/src/axum.rs | 1 + crates/kitsune-error/src/lib.rs | 18 ++++++++++++++++++ crates/kitsune-s3/src/lib.rs | 10 +++++----- crates/kitsune-service/src/attachment.rs | 4 ++-- crates/kitsune-service/src/post/mod.rs | 11 ++++------- crates/kitsune-service/src/user.rs | 10 +++------- 12 files changed, 55 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d2e2614f7..97e543ee5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3636,7 +3636,6 @@ dependencies = [ "base64-simd", "diesel", "diesel-async", - "eyre", "futures-util", "headers", "http 1.1.0", diff --git a/crates/kitsune-activitypub/Cargo.toml b/crates/kitsune-activitypub/Cargo.toml index 549c02cf0..68daf8475 100644 --- a/crates/kitsune-activitypub/Cargo.toml +++ b/crates/kitsune-activitypub/Cargo.toml @@ -11,7 +11,6 @@ autometrics = { version = "1.0.1", default-features = false } base64-simd = "0.8.0" diesel = "2.1.5" diesel-async = "0.4.1" -eyre = "0.6.12" futures-util = "0.3.30" headers = "0.4.0" http = "1.1.0" diff --git a/crates/kitsune-activitypub/src/fetcher/actor.rs b/crates/kitsune-activitypub/src/fetcher/actor.rs index cc4c8fbbe..07cd1e50d 100644 --- a/crates/kitsune-activitypub/src/fetcher/actor.rs +++ b/crates/kitsune-activitypub/src/fetcher/actor.rs @@ -63,8 +63,7 @@ impl Fetcher { match self .resolver .resolve_account(&actor.preferred_username, domain) - .await - .map_err(Error::Resolver)? + .await? { Some(resource) if resource.uri == actor.id => { actor.preferred_username = resource.username; diff --git a/crates/kitsune-activitypub/src/fetcher/mod.rs b/crates/kitsune-activitypub/src/fetcher/mod.rs index c44b8ac8d..9645b3b49 100644 --- a/crates/kitsune-activitypub/src/fetcher/mod.rs +++ b/crates/kitsune-activitypub/src/fetcher/mod.rs @@ -123,15 +123,15 @@ impl FetcherTrait for Fetcher { Arc::new(self.resolver.clone()) } - async fn fetch_account(&self, opts: AccountFetchOptions<'_>) -> eyre::Result> { + async fn fetch_account(&self, opts: AccountFetchOptions<'_>) -> Result> { Ok(self.fetch_actor(opts).await?) } - async fn fetch_emoji(&self, url: &str) -> eyre::Result> { + async fn fetch_emoji(&self, url: &str) -> Result> { Ok(self.fetch_emoji(url).await?) } - async fn fetch_post(&self, opts: PostFetchOptions<'_>) -> eyre::Result> { + async fn fetch_post(&self, opts: PostFetchOptions<'_>) -> Result> { Ok(self.fetch_object(opts.url, opts.call_depth).await?) } } diff --git a/crates/kitsune-activitypub/src/lib.rs b/crates/kitsune-activitypub/src/lib.rs index de946f164..a39cc1fbf 100644 --- a/crates/kitsune-activitypub/src/lib.rs +++ b/crates/kitsune-activitypub/src/lib.rs @@ -22,7 +22,7 @@ use kitsune_db::{ with_transaction, PgPool, }; use kitsune_embed::Client as EmbedClient; -use kitsune_error::{Error, Result}; +use kitsune_error::{kitsune_error, Error, Result}; use kitsune_language::Language; use kitsune_search::{AnySearchBackend, SearchBackend}; use kitsune_type::ap::{object::MediaAttachment, Object, Tag, TagType}; @@ -103,8 +103,7 @@ async fn handle_custom_emojis( emoji_text: emoji_tag.name.clone(), }) .try_collect::>() - .await - .map_err(Error::FetchEmoji)?; + .await?; diesel::insert_into(posts_custom_emojis::table) .values(emojis) @@ -205,15 +204,14 @@ async fn preprocess_object( if Uri::try_from(&object.attributed_to)?.authority() != Uri::try_from(&object.id)?.authority() { - return Err(Error::InvalidDocument); + return Err(kitsune_error!("invalid document")); } let Some(author) = fetcher .fetch_account(object.attributed_to.as_str().into()) - .await - .map_err(Error::FetchAccount)? + .await? else { - return Err(Error::NotFound); + return Err(kitsune_error!("account not found")); }; CowBox::boxed(author) @@ -228,8 +226,7 @@ async fn preprocess_object( .call_depth(call_depth + 1) .build(), ) - .await - .map_err(Error::FetchPost)? + .await? .map(|post| post.id) } else { None diff --git a/crates/kitsune-activitypub/src/mapping/object.rs b/crates/kitsune-activitypub/src/mapping/object.rs index 5951892b7..e2239ec89 100644 --- a/crates/kitsune-activitypub/src/mapping/object.rs +++ b/crates/kitsune-activitypub/src/mapping/object.rs @@ -13,7 +13,7 @@ use kitsune_db::{ schema::{accounts, custom_emojis, media_attachments, posts, posts_custom_emojis}, with_connection, }; -use kitsune_error::{Error, Result}; +use kitsune_error::{bail, kitsune_error, Error, ErrorType, Result}; use kitsune_type::ap::{ actor::{Actor, PublicKey}, ap_context, @@ -35,12 +35,19 @@ impl IntoObject for DbMediaAttachment { type Output = MediaAttachment; async fn into_object(self, state: State<'_>) -> Result { - let mime = Mime::from_str(&self.content_type).map_err(|_| Error::UnsupportedMediaType)?; + let mime = Mime::from_str(&self.content_type).map_err( + |_| kitsune_error!(type = ErrorType::UnsupportedMediaType, "unsupported media type"), + )?; + let r#type = match mime.type_() { mime::AUDIO => MediaAttachmentType::Audio, mime::IMAGE => MediaAttachmentType::Image, mime::VIDEO => MediaAttachmentType::Video, - _ => return Err(Error::UnsupportedMediaType), + _ => { + return Err( + kitsune_error!(type = ErrorType::UnsupportedMediaType, "unsupported media type"), + ) + } }; let url = state.service.attachment.get_url(self.id).await?; @@ -98,7 +105,7 @@ impl IntoObject for Post { // Therefore it's also not an object // We just return en error here if self.reposted_post_id.is_some() { - return Err(Error::NotFound); + bail!("post not found"); } let (account, in_reply_to, mentions, emojis, attachment_stream) = @@ -255,7 +262,7 @@ impl IntoObject for CustomEmoji { // Let's pretend we're not home and do not answer let name = match self.domain { None => Ok(format!(":{}:", self.shortcode)), - Some(_) => Err(Error::NotFound), + Some(_) => Err(kitsune_error!("custom emoji not found")), }?; let icon = with_connection!(state.db_pool, |db_conn| { diff --git a/crates/kitsune-error/src/axum.rs b/crates/kitsune-error/src/axum.rs index 4f504e14a..c6a2eaf23 100644 --- a/crates/kitsune-error/src/axum.rs +++ b/crates/kitsune-error/src/axum.rs @@ -22,6 +22,7 @@ impl IntoResponse for Error { ErrorType::Forbidden(maybe_body) => to_response(StatusCode::FORBIDDEN, maybe_body), ErrorType::NotFound => StatusCode::NOT_FOUND.into_response(), ErrorType::Unauthorized => StatusCode::UNAUTHORIZED.into_response(), + ErrorType::UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE.into_response(), ErrorType::Other(maybe_body) => { to_response(StatusCode::INTERNAL_SERVER_ERROR, maybe_body) } diff --git a/crates/kitsune-error/src/lib.rs b/crates/kitsune-error/src/lib.rs index 91c4e850f..14b1230dd 100644 --- a/crates/kitsune-error/src/lib.rs +++ b/crates/kitsune-error/src/lib.rs @@ -11,12 +11,30 @@ mod ext; pub type BoxError = Box; pub type Result = std::result::Result; +#[macro_export] +macro_rules! bail { + ($(type = $type:expr,)? $msg:expr) => { + return Err($crate::kitsune_error!($(type = $type,)? $msg)); + }; +} + +#[macro_export] +macro_rules! kitsune_error { + (type = $type:expr, $msg:expr) => { + $crate::Error::msg($msg).with_error_type($type) + }; + ($msg:expr) => { + $crate::kitsune_error!(type = $crate::ErrorType::Other(None), $msg) + }; +} + #[derive(Clone, Debug)] pub enum ErrorType { BadRequest(Option), Forbidden(Option), NotFound, Unauthorized, + UnsupportedMediaType, Other(Option), } diff --git a/crates/kitsune-s3/src/lib.rs b/crates/kitsune-s3/src/lib.rs index 7c025e81b..0d333ed04 100644 --- a/crates/kitsune-s3/src/lib.rs +++ b/crates/kitsune-s3/src/lib.rs @@ -4,7 +4,7 @@ use http::{ header::{CONTENT_LENGTH, ETAG}, Request, }; -use kitsune_error::{Error, Result}; +use kitsune_error::{bail, Error, Result}; use kitsune_http_client::{Body, Client as HttpClient, Response}; use rusty_s3::{actions::CreateMultipartUpload, Bucket, Credentials, S3Action}; use serde::Serialize; @@ -47,7 +47,7 @@ async fn execute_request(client: &HttpClient, req: Request) -> Result Result { if !self.registrations_open && !register.force_registration { - return Err(Error::msg("registrations closed") - .with_error_type(ErrorType::Forbidden(Some("registrations closed".into())))); + bail!(type = ErrorType::Forbidden(Some("registrations closed".into())), "registrations closed"); } register.validate(&RegisterContext { @@ -135,10 +134,7 @@ impl UserService { })?; if self.captcha_service.enabled() { - let invalid_captcha = || { - Error::msg("invalid captcha") - .with_error_type(ErrorType::BadRequest(Some("invalid captcha".into()))) - }; + let invalid_captcha = || kitsune_error!(type = ErrorType::BadRequest(Some("invalid captcha".into())), "invalid captcha"); let token = register.captcha_token.ok_or_else(invalid_captcha)?; let result = self.captcha_service.verify_token(&token).await?;