Refactor Create(Note) activity processor

This commit is contained in:
silverpill 2021-10-09 16:49:20 +00:00
parent 9f114f4255
commit 8d66bcb034

View file

@ -95,40 +95,44 @@ pub async fn receive_activity(
(CREATE, NOTE) => { (CREATE, NOTE) => {
let object: Object = serde_json::from_value(activity.object) let object: Object = serde_json::from_value(activity.object)
.map_err(|_| ValidationError("invalid object"))?; .map_err(|_| ValidationError("invalid object"))?;
let attributed_to = object.attributed_to // TOOD: fetch the whole thread
.ok_or(ValidationError("unattributed note"))?; let objects = vec![object];
let author = get_profile_by_actor_id(db_client, &attributed_to).await?; for object in objects {
let content = object.content let attributed_to = object.attributed_to
.ok_or(ValidationError("no content"))?; .ok_or(ValidationError("unattributed note"))?;
let mut attachments: Vec<Uuid> = Vec::new(); let author = get_profile_by_actor_id(db_client, &attributed_to).await?;
if let Some(list) = object.attachment { let content = object.content
let mut downloaded: Vec<(String, String)> = Vec::new(); .ok_or(ValidationError("no content"))?;
let output_dir = config.media_dir(); let mut attachments: Vec<Uuid> = Vec::new();
for attachment in list { if let Some(list) = object.attachment {
let file_name = fetch_attachment(&attachment.url, &output_dir).await let mut downloaded: Vec<(String, String)> = Vec::new();
.map_err(|_| ValidationError("failed to fetch attachment"))?; let output_dir = config.media_dir();
log::info!("downloaded attachment {}", attachment.url); for attachment in list {
downloaded.push((file_name, attachment.media_type)); let file_name = fetch_attachment(&attachment.url, &output_dir).await
} .map_err(|_| ValidationError("failed to fetch attachment"))?;
for (file_name, media_type) in downloaded { log::info!("downloaded attachment {}", attachment.url);
let db_attachment = create_attachment( downloaded.push((file_name, attachment.media_type));
db_client, }
&author.id, for (file_name, media_type) in downloaded {
Some(media_type), let db_attachment = create_attachment(
file_name, db_client,
).await?; &author.id,
attachments.push(db_attachment.id); Some(media_type),
file_name,
).await?;
attachments.push(db_attachment.id);
}
} }
let post_data = PostCreateData {
content,
// TODO: parse inReplyTo field
in_reply_to_id: None,
attachments: attachments,
object_id: Some(object.id),
created_at: object.published,
};
create_post(db_client, &author.id, post_data).await?;
} }
let post_data = PostCreateData {
content,
// TODO: parse inReplyTo field
in_reply_to_id: None,
attachments: attachments,
object_id: Some(object.id),
created_at: object.published,
};
create_post(db_client, &author.id, post_data).await?;
}, },
(FOLLOW, _) => { (FOLLOW, _) => {
let source_profile = get_profile_by_actor_id(db_client, &activity.actor).await?; let source_profile = get_profile_by_actor_id(db_client, &activity.actor).await?;