feat(apb): introduce content-type constants

This commit is contained in:
əlemi 2025-01-23 01:09:51 +01:00
parent 908662628b
commit d8c416d1d9
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 21 additions and 5 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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)

View file

@ -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(),

View file

@ -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()
}