fix: redirect after fetching if id is different

for example, pleroma servers objects under /notice/abcd... but the
object id itself is different, under /objects/<uuid>. when fetching
pleroma redirects, but we get unreliable behavior. redirect so that we
can force clients to use the proper id
This commit is contained in:
əlemi 2024-04-29 20:11:19 +02:00
parent a3cf7a17e8
commit 95f9d05875
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 12 additions and 2 deletions

View file

@ -1,4 +1,4 @@
use axum::http::StatusCode; use axum::{http::StatusCode, response::Redirect};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum UpubError { pub enum UpubError {
@ -22,6 +22,11 @@ pub enum UpubError {
#[error("invalid base64 string: {0}")] #[error("invalid base64 string: {0}")]
Base64(#[from] base64::DecodeError), Base64(#[from] base64::DecodeError),
// TODO this isn't really an error but i need to redirect from some routes so this allows me to
// keep the type hints on the return type, still what the hell!!!!
#[error("redirecting to {0}")]
Redirect(String),
} }
impl UpubError { impl UpubError {
@ -66,6 +71,7 @@ impl axum::response::IntoResponse for UpubError {
fn into_response(self) -> axum::response::Response { fn into_response(self) -> axum::response::Response {
let descr = self.to_string(); let descr = self.to_string();
match self { match self {
UpubError::Redirect(to) => Redirect::to(&to).into_response(),
UpubError::Status(status) => (status, descr).into_response(), UpubError::Status(status) => (status, descr).into_response(),
UpubError::Database(_) => (StatusCode::SERVICE_UNAVAILABLE, descr).into_response(), UpubError::Database(_) => (StatusCode::SERVICE_UNAVAILABLE, descr).into_response(),
UpubError::Reqwest(x) => UpubError::Reqwest(x) =>

View file

@ -20,7 +20,11 @@ pub async fn view(
ctx.oid(id.clone()) ctx.oid(id.clone())
}; };
if auth.is_local() && query.fetch && !ctx.is_local(&oid) { if auth.is_local() && query.fetch && !ctx.is_local(&oid) {
ctx.fetch_object(&oid).await?; let obj = ctx.fetch_object(&oid).await?;
// some implementations serve statuses on different urls than their AP id
if obj.id != oid {
return Err(UpubError::Redirect(crate::url!(ctx, "/objects/{}", ctx.id(&obj.id))));
}
} }
let item = model::addressing::Entity::find_addressed() let item = model::addressing::Entity::find_addressed()