Deserialize object tag to Vec<Value>

This commit is contained in:
silverpill 2023-02-28 00:20:59 +00:00
parent 849b201ab9
commit 721238d897
3 changed files with 239 additions and 221 deletions

View file

@ -254,7 +254,7 @@ pub async fn import_post(
// Fetch parent object on next iteration
queue.push(object_id.to_owned());
};
for object_id in get_object_links(&object)? {
for object_id in get_object_links(&object) {
// Fetch linked objects after fetching current thread
queue.insert(0, object_id);
};

View file

@ -199,12 +199,9 @@ pub async fn get_object_attachments(
pub fn get_object_links(
object: &Object,
) -> Result<Vec<String>, HandlerError> {
) -> Vec<String> {
let mut links = vec![];
if let Some(ref value) = object.tag {
let list: Vec<JsonValue> = parse_property_value(value)
.map_err(|_| ValidationError("invalid tag property"))?;
for tag_value in list {
for tag_value in object.tag.clone() {
let tag_type = tag_value["type"].as_str().unwrap_or(HASHTAG);
if tag_type == LINK {
let tag: LinkTag = match serde_json::from_value(tag_value) {
@ -225,13 +222,12 @@ pub fn get_object_links(
};
};
};
};
if let Some(ref object_id) = object.quote_url {
if !links.contains(object_id) {
links.push(object_id.to_owned());
};
};
Ok(links)
links
}
pub async fn get_object_tags(
@ -246,10 +242,7 @@ pub async fn get_object_tags(
let mut hashtags = vec![];
let mut links = vec![];
let mut emojis = vec![];
if let Some(ref value) = object.tag {
let list: Vec<JsonValue> = parse_property_value(value)
.map_err(|_| ValidationError("invalid tag property"))?;
for tag_value in list {
for tag_value in object.tag.clone() {
let tag_type = tag_value["type"].as_str().unwrap_or(HASHTAG);
if tag_type == HASHTAG {
let tag: Tag = match serde_json::from_value(tag_value) {
@ -461,7 +454,6 @@ pub async fn get_object_tags(
);
};
};
};
if let Some(ref object_id) = object.quote_url {
let object_id = redirects.get(object_id).unwrap_or(object_id);
let linked = get_post_by_object_id(

View file

@ -1,7 +1,12 @@
use std::collections::HashMap;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde::{
Deserialize,
Deserializer,
Serialize,
de::{Error as DeserializerError},
};
use serde_json::Value;
use super::constants::{
@ -10,6 +15,7 @@ use super::constants::{
W3ID_DATA_INTEGRITY_CONTEXT,
W3ID_SECURITY_CONTEXT,
};
use super::receiver::parse_property_value;
use super::vocabulary::HASHTAG;
#[derive(Deserialize, Serialize)]
@ -82,6 +88,20 @@ pub struct EmojiTag {
pub updated: DateTime<Utc>,
}
fn deserialize_value_array<'de, D>(
deserializer: D,
) -> Result<Vec<Value>, D::Error>
where D: Deserializer<'de>
{
let maybe_value: Option<Value> = Option::deserialize(deserializer)?;
let values = if let Some(value) = maybe_value {
parse_property_value(&value).map_err(DeserializerError::custom)?
} else {
vec![]
};
Ok(values)
}
#[derive(Deserialize)]
#[cfg_attr(test, derive(Default))]
#[serde(rename_all = "camelCase")]
@ -102,7 +122,13 @@ pub struct Object {
pub in_reply_to: Option<String>,
pub content: Option<String>,
pub quote_url: Option<String>,
pub tag: Option<Value>,
#[serde(
default,
deserialize_with = "deserialize_value_array",
)]
pub tag: Vec<Value>,
pub to: Option<Value>,
pub updated: Option<DateTime<Utc>>,
pub url: Option<Value>,