Skip to content

Commit

Permalink
Improved error message when attempting to fetch non-local object (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Feb 8, 2023
1 parent ab9b60e commit 3bfa8ab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
5 changes: 2 additions & 3 deletions crates/apub/src/http/comment.rs
@@ -1,10 +1,9 @@
use crate::{
http::{create_apub_response, create_apub_tombstone_response},
http::{create_apub_response, create_apub_tombstone_response, err_object_not_local},
objects::comment::ApubComment,
};
use activitypub_federation::traits::ApubObject;
use actix_web::{web, web::Path, HttpResponse};
use diesel::result::Error::NotFound;
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{newtypes::CommentId, source::comment::Comment, traits::Crud};
use lemmy_utils::error::LemmyError;
Expand All @@ -24,7 +23,7 @@ pub(crate) async fn get_apub_comment(
let id = CommentId(info.comment_id.parse::<i32>()?);
let comment: ApubComment = Comment::read(context.pool(), id).await?.into();
if !comment.local {
return Err(NotFound.into());
return Err(err_object_not_local());
}

if !comment.deleted && !comment.removed {
Expand Down
10 changes: 8 additions & 2 deletions crates/apub/src/http/mod.rs
Expand Up @@ -100,6 +100,10 @@ fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> HttpResponse {
.json(WithContext::new(tombstone, CONTEXT.deref().clone()))
}

fn err_object_not_local() -> LemmyError {
LemmyError::from_message("Object not local, fetch it from original instance")
}

#[derive(Deserialize)]
pub struct ActivityQuery {
type_: String,
Expand All @@ -123,8 +127,10 @@ pub(crate) async fn get_activity(
let activity = Activity::read_from_apub_id(context.pool(), &activity_id).await?;

let sensitive = activity.sensitive.unwrap_or(true);
if !activity.local || sensitive {
Ok(HttpResponse::NotFound().finish())
if !activity.local {
return Err(err_object_not_local());
} else if sensitive {
Ok(HttpResponse::Forbidden().finish())
} else {
Ok(create_json_apub_response(activity.data))
}
Expand Down
5 changes: 2 additions & 3 deletions crates/apub/src/http/post.rs
@@ -1,10 +1,9 @@
use crate::{
http::{create_apub_response, create_apub_tombstone_response},
http::{create_apub_response, create_apub_tombstone_response, err_object_not_local},
objects::post::ApubPost,
};
use activitypub_federation::traits::ApubObject;
use actix_web::{web, HttpResponse};
use diesel::result::Error::NotFound;
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{newtypes::PostId, source::post::Post, traits::Crud};
use lemmy_utils::error::LemmyError;
Expand All @@ -24,7 +23,7 @@ pub(crate) async fn get_apub_post(
let id = PostId(info.post_id.parse::<i32>()?);
let post: ApubPost = Post::read(context.pool(), id).await?.into();
if !post.local {
return Err(NotFound.into());
return Err(err_object_not_local());
}

if !post.deleted && !post.removed {
Expand Down

0 comments on commit 3bfa8ab

Please sign in to comment.