From 50176b00cc2621516c80efb72bf45c7533258e78 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 19 Feb 2023 22:14:10 +0000 Subject: [PATCH] Make actor objects pass JSON-LD validation --- CHANGELOG.md | 4 ++++ src/activitypub/actors/types.rs | 36 +++++++++++++++++++++++++-------- src/activitypub/constants.rs | 7 ++++++- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4e7a37..be84a61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Changed `/api/v1/statuses/{status_id}/context` response format to match Mastodon API. - Changed status code of `/api/v1/statuses` response to 200 to match Mastodon API. +### Fixed + +- Fixed actor object JSON-LD validation errors. + ## [1.13.1] - 2023-02-09 ### Fixed diff --git a/src/activitypub/actors/types.rs b/src/activitypub/actors/types.rs index cb5aed4..400a128 100644 --- a/src/activitypub/actors/types.rs +++ b/src/activitypub/actors/types.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use serde::{ Deserialize, Deserializer, @@ -15,6 +17,9 @@ use mitra_utils::{ use crate::activitypub::{ constants::{ AP_CONTEXT, + MASTODON_CONTEXT, + MITRA_CONTEXT, + SCHEMA_ORG_CONTEXT, W3ID_SECURITY_CONTEXT, }, identifiers::{ @@ -253,6 +258,27 @@ impl Actor { pub type ActorKeyError = rsa::pkcs8::Error; +fn build_actor_context() -> ( + &'static str, + &'static str, + HashMap<&'static str, &'static str>, +) { + ( + AP_CONTEXT, + W3ID_SECURITY_CONTEXT, + HashMap::from([ + ("manuallyApprovesFollowers", "as:manuallyApprovesFollowers"), + ("schema", SCHEMA_ORG_CONTEXT), + ("PropertyValue", "schema:PropertyValue"), + ("value", "schema:value"), + ("toot", MASTODON_CONTEXT), + ("IdentityProof", "toot:IdentityProof"), + ("mitra", MITRA_CONTEXT), + ("subscribers", "mitra:subscribers"), + ]), + ) +} + pub fn get_local_actor( user: &User, instance_url: &str, @@ -312,10 +338,7 @@ pub fn get_local_actor( attachments.push(attachment); }; let actor = Actor { - context: Some(json!([ - AP_CONTEXT.to_string(), - W3ID_SECURITY_CONTEXT.to_string(), - ])), + context: Some(json!(build_actor_context())), id: actor_id.clone(), object_type: PERSON.to_string(), name: user.profile.display_name.clone(), @@ -351,10 +374,7 @@ pub fn get_instance_actor( public_key_pem: public_key_pem, }; let actor = Actor { - context: Some(json!([ - AP_CONTEXT.to_string(), - W3ID_SECURITY_CONTEXT.to_string(), - ])), + context: Some(json!(build_actor_context())), id: actor_id, object_type: SERVICE.to_string(), name: Some(instance.hostname()), diff --git a/src/activitypub/constants.rs b/src/activitypub/constants.rs index 01163b4..e0c4270 100644 --- a/src/activitypub/constants.rs +++ b/src/activitypub/constants.rs @@ -4,6 +4,11 @@ pub const AS_MEDIA_TYPE: &str = "application/activity+json"; // Contexts pub const AP_CONTEXT: &str = "https://www.w3.org/ns/activitystreams"; -pub const AP_PUBLIC: &str = "https://www.w3.org/ns/activitystreams#Public"; pub const W3ID_SECURITY_CONTEXT: &str = "https://w3id.org/security/v1"; pub const W3ID_DATA_INTEGRITY_CONTEXT: &str = "https://w3id.org/security/data-integrity/v1"; +pub const SCHEMA_ORG_CONTEXT: &str = "http://schema.org/"; +pub const MASTODON_CONTEXT: &str = "http://joinmastodon.org/ns#"; +pub const MITRA_CONTEXT: &str = "http://jsonld.mitra.social#"; + +// Misc +pub const AP_PUBLIC: &str = "https://www.w3.org/ns/activitystreams#Public";