Deserialize actor attachments into Vec instead of Option<Vec>

This commit is contained in:
silverpill 2023-02-27 23:47:44 +00:00
parent 8d479ac15d
commit 849b201ab9

View file

@ -115,20 +115,18 @@ fn deserialize_image_opt<'de, D>(
} }
// Some implementations use single object instead of array // Some implementations use single object instead of array
fn deserialize_attachments_opt<'de, D>( fn deserialize_attachments<'de, D>(
deserializer: D, deserializer: D,
) -> Result<Option<Vec<ActorAttachment>>, D::Error> ) -> Result<Vec<ActorAttachment>, D::Error>
where D: Deserializer<'de> where D: Deserializer<'de>
{ {
let maybe_value: Option<Value> = Option::deserialize(deserializer)?; let maybe_value: Option<Value> = Option::deserialize(deserializer)?;
let maybe_attachments = if let Some(value) = maybe_value { let attachments = if let Some(value) = maybe_value {
let attachments: Vec<ActorAttachment> = parse_property_value(&value) parse_property_value(&value).map_err(DeserializerError::custom)?
.map_err(DeserializerError::custom)?;
Some(attachments)
} else { } else {
None vec![]
}; };
Ok(maybe_attachments) Ok(attachments)
} }
// Clone and Debug traits are required by FromSql // Clone and Debug traits are required by FromSql
@ -179,10 +177,10 @@ pub struct Actor {
#[serde( #[serde(
default, default,
deserialize_with = "deserialize_attachments_opt", deserialize_with = "deserialize_attachments",
skip_serializing_if = "Option::is_none", skip_serializing_if = "Vec::is_empty",
)] )]
pub attachment: Option<Vec<ActorAttachment>>, pub attachment: Vec<ActorAttachment>,
#[serde(default)] #[serde(default)]
pub manually_approves_followers: bool, pub manually_approves_followers: bool,
@ -222,34 +220,32 @@ impl Actor {
error, error,
); );
}; };
if let Some(attachments) = &self.attachment { for attachment in self.attachment.iter() {
for attachment in attachments { match attachment.object_type.as_str() {
match attachment.object_type.as_str() { IDENTITY_PROOF => {
IDENTITY_PROOF => { match parse_identity_proof(&self.id, attachment) {
match parse_identity_proof(&self.id, attachment) { Ok(proof) => identity_proofs.push(proof),
Ok(proof) => identity_proofs.push(proof), Err(error) => log_error(attachment, error),
Err(error) => log_error(attachment, error), };
}; },
}, LINK => {
LINK => { match parse_payment_option(attachment) {
match parse_payment_option(attachment) { Ok(option) => payment_options.push(option),
Ok(option) => payment_options.push(option), Err(error) => log_error(attachment, error),
Err(error) => log_error(attachment, error), };
}; },
}, PROPERTY_VALUE => {
PROPERTY_VALUE => { match parse_extra_field(attachment) {
match parse_extra_field(attachment) { Ok(field) => extra_fields.push(field),
Ok(field) => extra_fields.push(field), Err(error) => log_error(attachment, error),
Err(error) => log_error(attachment, error), };
}; },
}, _ => {
_ => { log_error(
log_error( attachment,
attachment, ValidationError("unsupported attachment type"),
ValidationError("unsupported attachment type"), );
); },
},
};
}; };
}; };
(identity_proofs, payment_options, extra_fields) (identity_proofs, payment_options, extra_fields)
@ -353,7 +349,7 @@ pub fn get_local_actor(
image: banner, image: banner,
summary: user.profile.bio.clone(), summary: user.profile.bio.clone(),
also_known_as: None, also_known_as: None,
attachment: Some(attachments), attachment: attachments,
manually_approves_followers: false, manually_approves_followers: false,
tag: None, tag: None,
url: Some(actor_id), url: Some(actor_id),
@ -389,7 +385,7 @@ pub fn get_instance_actor(
image: None, image: None,
summary: None, summary: None,
also_known_as: None, also_known_as: None,
attachment: None, attachment: vec![],
manually_approves_followers: false, manually_approves_followers: false,
tag: None, tag: None,
url: None, url: None,
@ -455,7 +451,7 @@ mod tests {
actor.public_key.id, actor.public_key.id,
"https://example.com/users/testuser#main-key", "https://example.com/users/testuser#main-key",
); );
assert_eq!(actor.attachment.unwrap().len(), 0); assert_eq!(actor.attachment.len(), 0);
assert_eq!(actor.summary, user.profile.bio); assert_eq!(actor.summary, user.profile.bio);
} }