2024-04-30 16:51:30 +02:00
|
|
|
// TODO
|
|
|
|
// move this file somewhere else
|
|
|
|
// it's not a route
|
|
|
|
// maybe under src/server/jsonld.rs ??
|
|
|
|
|
2024-05-02 05:10:38 +02:00
|
|
|
use apb::Object;
|
2024-03-21 19:15:19 +01:00
|
|
|
use axum::response::{IntoResponse, Response};
|
2024-03-21 03:07:35 +01:00
|
|
|
|
2024-03-21 19:15:19 +01:00
|
|
|
pub trait LD {
|
|
|
|
fn ld_context(self) -> Self;
|
|
|
|
fn new_object() -> serde_json::Value {
|
|
|
|
serde_json::Value::Object(serde_json::Map::default())
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 03:07:35 +01:00
|
|
|
|
2024-03-21 19:15:19 +01:00
|
|
|
impl LD for serde_json::Value {
|
|
|
|
fn ld_context(mut self) -> Self {
|
2024-05-02 05:10:38 +02:00
|
|
|
let o_type = self.object_type();
|
2024-03-21 19:15:19 +01:00
|
|
|
if let Some(obj) = self.as_object_mut() {
|
2024-05-01 21:21:26 +02:00
|
|
|
let mut ctx = serde_json::Map::new();
|
|
|
|
ctx.insert("sensitive".to_string(), serde_json::Value::String("as:sensitive".into()));
|
2024-05-20 08:58:38 +02:00
|
|
|
ctx.insert("quoteUrl".to_string(), serde_json::Value::String("as:quoteUrl".into()));
|
2024-05-02 05:10:38 +02:00
|
|
|
match o_type {
|
|
|
|
Some(apb::ObjectType::Actor(_)) => {
|
|
|
|
ctx.insert("counters".to_string(), serde_json::Value::String("https://ns.alemi.dev/as/counters/#".into()));
|
|
|
|
ctx.insert("followingCount".to_string(), serde_json::Value::String("counters:followingCount".into()));
|
|
|
|
ctx.insert("followersCount".to_string(), serde_json::Value::String("counters:followersCount".into()));
|
|
|
|
ctx.insert("statusesCount".to_string(), serde_json::Value::String("counters:statusesCount".into()));
|
|
|
|
ctx.insert("fe".to_string(), serde_json::Value::String("https://ns.alemi.dev/as/fe/#".into()));
|
|
|
|
ctx.insert("followingMe".to_string(), serde_json::Value::String("fe:followingMe".into()));
|
|
|
|
ctx.insert("followedByMe".to_string(), serde_json::Value::String("fe:followedByMe".into()));
|
|
|
|
},
|
|
|
|
Some(_) => {
|
|
|
|
ctx.insert("fe".to_string(), serde_json::Value::String("https://ns.alemi.dev/as/fe/#".into()));
|
|
|
|
ctx.insert("likedByMe".to_string(), serde_json::Value::String("fe:likedByMe".into()));
|
|
|
|
},
|
|
|
|
None => {},
|
|
|
|
}
|
2024-03-21 19:15:19 +01:00
|
|
|
obj.insert(
|
|
|
|
"@context".to_string(),
|
|
|
|
serde_json::Value::Array(vec![
|
2024-05-01 21:21:26 +02:00
|
|
|
serde_json::Value::String("https://www.w3.org/ns/activitystreams".into()),
|
|
|
|
serde_json::Value::Object(ctx),
|
2024-03-21 19:15:19 +01:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
tracing::warn!("cannot add @context to json value different than object");
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 03:07:35 +01:00
|
|
|
|
2024-03-21 19:15:19 +01:00
|
|
|
// got this from https://github.com/kitsune-soc/kitsune/blob/b023a12b687dd9a274233a5a9950f2de5e192344/kitsune/src/http/responder.rs
|
|
|
|
// i was trying to do it with middlewares but this is way cleaner
|
|
|
|
pub struct JsonLD<T>(pub T);
|
|
|
|
impl<T: serde::Serialize> IntoResponse for JsonLD<T> {
|
2024-03-21 03:07:35 +01:00
|
|
|
fn into_response(self) -> Response {
|
2024-03-21 19:55:55 +01:00
|
|
|
(
|
|
|
|
[("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")],
|
|
|
|
axum::Json(self.0)
|
|
|
|
).into_response()
|
2024-03-21 03:07:35 +01:00
|
|
|
}
|
|
|
|
}
|