From: vpzomtrrfrt Date: Thu, 20 Jan 2022 14:12:45 +0000 (-0800) Subject: Accept single object as to for arrays too (#2048) X-Git-Url: http://these/git/%22https:/mastodon.uno/%40informapirata/%22?a=commitdiff_plain;h=272dc3e7a6a586050677ca2cbb35b6797c8b42ed;p=lemmy.git Accept single object as to for arrays too (#2048) --- diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 44c2d0d5..94752a39 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 bf1a84f1..18b74c23 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 a319daff..9558d074 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 1156b0e8..7d28e1c4 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 3ce26075..605938c4 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 64499ea3..608300ce 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,