diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 44c2d0d57..94752a397 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -112,6 +112,25 @@ where }) } +pub(crate) fn deserialize_one<'de, T, D>(deserializer: D) -> Result<[T; 1], D::Error> +where + T: Deserialize<'de>, + D: Deserializer<'de>, +{ + #[derive(Deserialize)] + #[serde(untagged)] + enum MaybeArray { + Simple(T), + Array([T; 1]), + } + + let result: MaybeArray = Deserialize::deserialize(deserializer)?; + Ok(match result { + MaybeArray::Simple(value) => [value], + MaybeArray::Array(value) => value, + }) +} + pub enum EndpointType { Community, Person, diff --git a/crates/apub/src/protocol/activities/community/report.rs b/crates/apub/src/protocol/activities/community/report.rs index bf1a84f17..18b74c238 100644 --- a/crates/apub/src/protocol/activities/community/report.rs +++ b/crates/apub/src/protocol/activities/community/report.rs @@ -12,6 +12,7 @@ use url::Url; #[serde(rename_all = "camelCase")] pub struct Report { pub(crate) actor: ObjectId, + #[serde(deserialize_with = "crate::deserialize_one")] pub(crate) to: [ObjectId; 1], pub(crate) object: ObjectId, pub(crate) summary: String, diff --git a/crates/apub/src/protocol/activities/private_message/create_or_update.rs b/crates/apub/src/protocol/activities/private_message/create_or_update.rs index a319daff5..9558d074a 100644 --- a/crates/apub/src/protocol/activities/private_message/create_or_update.rs +++ b/crates/apub/src/protocol/activities/private_message/create_or_update.rs @@ -11,6 +11,7 @@ use url::Url; pub struct CreateOrUpdatePrivateMessage { pub(crate) id: Url, pub(crate) actor: ObjectId, + #[serde(deserialize_with = "crate::deserialize_one")] pub(crate) to: [ObjectId; 1], pub(crate) object: ChatMessage, #[serde(rename = "type")] diff --git a/crates/apub/src/protocol/activities/private_message/delete.rs b/crates/apub/src/protocol/activities/private_message/delete.rs index 1156b0e8e..7d28e1c46 100644 --- a/crates/apub/src/protocol/activities/private_message/delete.rs +++ b/crates/apub/src/protocol/activities/private_message/delete.rs @@ -11,6 +11,7 @@ use url::Url; #[serde(rename_all = "camelCase")] pub struct DeletePrivateMessage { pub(crate) actor: ObjectId, + #[serde(deserialize_with = "crate::deserialize_one")] pub(crate) to: [ObjectId; 1], pub(crate) object: ObjectId, #[serde(rename = "type")] diff --git a/crates/apub/src/protocol/activities/private_message/undo_delete.rs b/crates/apub/src/protocol/activities/private_message/undo_delete.rs index 3ce260758..605938c40 100644 --- a/crates/apub/src/protocol/activities/private_message/undo_delete.rs +++ b/crates/apub/src/protocol/activities/private_message/undo_delete.rs @@ -11,6 +11,7 @@ use url::Url; #[serde(rename_all = "camelCase")] pub struct UndoDeletePrivateMessage { pub(crate) actor: ObjectId, + #[serde(deserialize_with = "crate::deserialize_one")] pub(crate) to: [ObjectId; 1], pub(crate) object: DeletePrivateMessage, #[serde(rename = "type")] diff --git a/crates/apub/src/protocol/objects/chat_message.rs b/crates/apub/src/protocol/objects/chat_message.rs index 64499ea33..608300ce1 100644 --- a/crates/apub/src/protocol/objects/chat_message.rs +++ b/crates/apub/src/protocol/objects/chat_message.rs @@ -14,6 +14,7 @@ pub struct ChatMessage { pub(crate) r#type: ChatMessageType, pub(crate) id: ObjectId, pub(crate) attributed_to: ObjectId, + #[serde(deserialize_with = "crate::deserialize_one")] pub(crate) to: [ObjectId; 1], pub(crate) content: String, pub(crate) media_type: Option,