From d8c416d1d9402f6f1bd7e20c0143bbcde14da889 Mon Sep 17 00:00:00 2001 From: alemi <me@alemi.dev> Date: Thu, 23 Jan 2025 01:09:51 +0100 Subject: [PATCH] feat(apb): introduce content-type constants --- apb/src/jsonld.rs | 16 ++++++++++++++++ apb/src/lib.rs | 2 +- core/src/traits/fetch.rs | 4 ++-- routes/src/activitypub/well_known.rs | 2 +- routes/src/builders.rs | 2 +- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/apb/src/jsonld.rs b/apb/src/jsonld.rs index f428359..3894c2d 100644 --- a/apb/src/jsonld.rs +++ b/apb/src/jsonld.rs @@ -1,5 +1,21 @@ use crate::Object; +/// recommended content-type header value for AP fetches and responses +pub const CONTENT_TYPE_LD_JSON_ACTIVITYPUB: &str = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""; +/// alternative content-type header value for AP fetches and responses +pub const CONTENT_TYPE_ACTIVITY_JSON: &str = "application/activity+json"; +/// uncommon and not officially supported content-type header value for AP fetches and responses +#[deprecated = "use CONTENT_TYPE_LD_JSON_ACTIVITYPUB: 'application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"'"] +pub const CONTENT_TYPE_LD_JSON: &str = "application/ld+json"; + +#[allow(deprecated)] +pub fn is_activity_pub_content_type<T: AsRef<str>>(txt: T) -> bool { + let r = txt.as_ref(); + r == CONTENT_TYPE_LD_JSON_ACTIVITYPUB + || r == CONTENT_TYPE_ACTIVITY_JSON + || r == CONTENT_TYPE_LD_JSON +} + pub trait LD { fn ld_context(self) -> Self; } diff --git a/apb/src/lib.rs b/apb/src/lib.rs index 03b239a..30736e8 100644 --- a/apb/src/lib.rs +++ b/apb/src/lib.rs @@ -110,7 +110,7 @@ pub mod shortcuts; pub use shortcuts::Shortcuts; #[cfg(feature = "jsonld")] -mod jsonld; +pub mod jsonld; #[cfg(feature = "jsonld")] pub use jsonld::LD; diff --git a/core/src/traits/fetch.rs b/core/src/traits/fetch.rs index f1a4440..5668d5a 100644 --- a/core/src/traits/fetch.rs +++ b/core/src/traits/fetch.rs @@ -146,8 +146,8 @@ pub trait Fetcher { let response = Self::client(domain) .request(method, url) - .header(ACCEPT, "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") - .header(CONTENT_TYPE, "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") + .header(ACCEPT, apb::jsonld::CONTENT_TYPE_LD_JSON_ACTIVITYPUB) + .header(CONTENT_TYPE, apb::jsonld::CONTENT_TYPE_LD_JSON_ACTIVITYPUB) .header("Host", host.clone()) .header("Date", date.clone()) .header("Digest", digest) diff --git a/routes/src/activitypub/well_known.rs b/routes/src/activitypub/well_known.rs index feef898..884a143 100644 --- a/routes/src/activitypub/well_known.rs +++ b/routes/src/activitypub/well_known.rs @@ -145,7 +145,7 @@ pub async fn webfinger( links: vec![ JsonResourceDescriptorLink { rel: "self".to_string(), - link_type: Some("application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"".to_string()), + link_type: Some(apb::jsonld::CONTENT_TYPE_LD_JSON_ACTIVITYPUB.to_string()), href: Some(user.id), properties: jrd::Map::default(), titles: jrd::Map::default(), diff --git a/routes/src/builders.rs b/routes/src/builders.rs index fd57efe..ee4f9da 100644 --- a/routes/src/builders.rs +++ b/routes/src/builders.rs @@ -39,7 +39,7 @@ pub struct JsonLD<T>(pub T); impl<T: serde::Serialize> IntoResponse for JsonLD<T> { fn into_response(self) -> Response { ( - [("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")], + [("Content-Type", apb::jsonld::CONTENT_TYPE_LD_JSON_ACTIVITYPUB)], axum::Json(self.0) ).into_response() }