]> Untitled Git - lemmy.git/commitdiff
Accept single object as to for arrays too (#2048)
authorvpzomtrrfrt <vpzomtrrfrt@gmail.com>
Thu, 20 Jan 2022 14:12:45 +0000 (06:12 -0800)
committerGitHub <noreply@github.com>
Thu, 20 Jan 2022 14:12:45 +0000 (14:12 +0000)
crates/apub/src/lib.rs
crates/apub/src/protocol/activities/community/report.rs
crates/apub/src/protocol/activities/private_message/create_or_update.rs
crates/apub/src/protocol/activities/private_message/delete.rs
crates/apub/src/protocol/activities/private_message/undo_delete.rs
crates/apub/src/protocol/objects/chat_message.rs

index 44c2d0d578da0bbbdac52e37290b614756a33e90..94752a39787c0cde43bd11fadc94a46c44301446 100644 (file)
@@ -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<T> {
+    Simple(T),
+    Array([T; 1]),
+  }
+
+  let result: MaybeArray<T> = Deserialize::deserialize(deserializer)?;
+  Ok(match result {
+    MaybeArray::Simple(value) => [value],
+    MaybeArray::Array(value) => value,
+  })
+}
+
 pub enum EndpointType {
   Community,
   Person,
index bf1a84f17be26a6acc7a951b28de41f8ea99d090..18b74c23847246bfa1f666cbfc578b8f2e85f688 100644 (file)
@@ -12,6 +12,7 @@ use url::Url;
 #[serde(rename_all = "camelCase")]
 pub struct Report {
   pub(crate) actor: ObjectId<ApubPerson>,
+  #[serde(deserialize_with = "crate::deserialize_one")]
   pub(crate) to: [ObjectId<ApubCommunity>; 1],
   pub(crate) object: ObjectId<PostOrComment>,
   pub(crate) summary: String,
index a319daff5a545285753e2ed87797e508afef67b0..9558d074a6329b853db5a078539778a895a85c88 100644 (file)
@@ -11,6 +11,7 @@ use url::Url;
 pub struct CreateOrUpdatePrivateMessage {
   pub(crate) id: Url,
   pub(crate) actor: ObjectId<ApubPerson>,
+  #[serde(deserialize_with = "crate::deserialize_one")]
   pub(crate) to: [ObjectId<ApubPerson>; 1],
   pub(crate) object: ChatMessage,
   #[serde(rename = "type")]
index 1156b0e8e196fb2b8cb8e5ce347f76781766ae0e..7d28e1c469e8542cd75ae6fdbb8dd259ae410fbd 100644 (file)
@@ -11,6 +11,7 @@ use url::Url;
 #[serde(rename_all = "camelCase")]
 pub struct DeletePrivateMessage {
   pub(crate) actor: ObjectId<ApubPerson>,
+  #[serde(deserialize_with = "crate::deserialize_one")]
   pub(crate) to: [ObjectId<ApubPerson>; 1],
   pub(crate) object: ObjectId<ApubPrivateMessage>,
   #[serde(rename = "type")]
index 3ce2607582ffb3a35bcc162b3836fd7c760794e9..605938c4085301eeee154bb25137c8a3c3d61e64 100644 (file)
@@ -11,6 +11,7 @@ use url::Url;
 #[serde(rename_all = "camelCase")]
 pub struct UndoDeletePrivateMessage {
   pub(crate) actor: ObjectId<ApubPerson>,
+  #[serde(deserialize_with = "crate::deserialize_one")]
   pub(crate) to: [ObjectId<ApubPerson>; 1],
   pub(crate) object: DeletePrivateMessage,
   #[serde(rename = "type")]
index 64499ea33e9f05f729459949453ecf224f7fe590..608300ce10aa535910890f72fffd5f9e8a15aa98 100644 (file)
@@ -14,6 +14,7 @@ pub struct ChatMessage {
   pub(crate) r#type: ChatMessageType,
   pub(crate) id: ObjectId<ApubPrivateMessage>,
   pub(crate) attributed_to: ObjectId<ApubPerson>,
+  #[serde(deserialize_with = "crate::deserialize_one")]
   pub(crate) to: [ObjectId<ApubPerson>; 1],
   pub(crate) content: String,
   pub(crate) media_type: Option<MediaTypeHtml>,