1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::activitypub;
use actix_web::guard::GuardContext;
use actix_web::http::header::{Accept, ContentType};
use actix_web::HttpResponse;
use mime::Mime;
use once_cell::sync::Lazy;
use std::str::FromStr;
pub const CT_ACTIVITY_JSON: &str = r#"application/ld+json; profile="https://www.w3.org/ns/activitystreams""#;
pub const CT_ACTIVITY_JSON_SHORT: &str = "application/activity+json";
static CONTENT_TYPE: Lazy<ContentType> = Lazy::new(|| ContentType(Mime::from_str(CT_ACTIVITY_JSON).unwrap()));
pub fn content_type_guard(ctx: &GuardContext) -> bool {
match ctx.header::<ContentType>() {
Some(ContentType(mime)) if mime == CT_ACTIVITY_JSON => true,
Some(ContentType(mime)) if mime == CT_ACTIVITY_JSON_SHORT => true,
_ => false,
}
}
pub fn accept_guard(ctx: &GuardContext) -> bool {
match ctx.header::<Accept>() {
Some(Accept(mimes)) => mimes
.iter()
.any(|mime| mime.item == CT_ACTIVITY_JSON || mime.item == CT_ACTIVITY_JSON_SHORT),
_ => false,
}
}
pub fn response(obj: activitypub::Object) -> HttpResponse {
match serde_json::to_string(&obj) {
Ok(obj) => HttpResponse::Ok().append_header(CONTENT_TYPE.clone()).body(obj),
Err(err) => {
tracing::warn!("Could not serialize response: {err}");
HttpResponse::InternalServerError().finish()
}
}
}