Send Create(Note) to author of parent post when creating reply

This commit is contained in:
silverpill 2021-11-01 00:23:02 +00:00
parent 5c10ffc590
commit ab9184f068
3 changed files with 35 additions and 9 deletions

View file

@ -121,13 +121,19 @@ pub fn create_note(
url, url,
} }
}).collect(); }).collect();
let mut recipients = vec![AP_PUBLIC.to_string()];
let in_reply_to_object_id = match post.in_reply_to_id { let in_reply_to_object_id = match post.in_reply_to_id {
Some(in_reply_to_id) => { Some(in_reply_to_id) => {
let post = in_reply_to.unwrap(); let post = in_reply_to.unwrap();
assert_eq!(post.id, in_reply_to_id); assert_eq!(post.id, in_reply_to_id);
match post.author.is_local() { match post.author.actor_json {
false => post.object_id.clone(), Some(ref actor_value) => {
true => Some(get_object_url(instance_url, &post.id)), // Replying to remote post
let remote_actor_id = actor_value["id"].as_str().unwrap();
recipients.push(remote_actor_id.to_string());
post.object_id.clone()
},
None => Some(get_object_url(instance_url, &post.id)),
} }
}, },
None => None, None => None,
@ -143,7 +149,7 @@ pub fn create_note(
attributed_to: Some(actor_id), attributed_to: Some(actor_id),
in_reply_to: in_reply_to_object_id, in_reply_to: in_reply_to_object_id,
content: Some(post.content.clone()), content: Some(post.content.clone()),
to: Some(json!(AP_PUBLIC)), to: Some(json!(recipients)),
} }
} }
@ -324,12 +330,16 @@ mod tests {
note.in_reply_to.unwrap(), note.in_reply_to.unwrap(),
format!("{}/objects/{}", INSTANCE_URL, parent.id), format!("{}/objects/{}", INSTANCE_URL, parent.id),
); );
assert_eq!(note.to.unwrap(), json!([AP_PUBLIC]));
} }
#[test] #[test]
fn test_create_note_with_remote_parent() { fn test_create_note_with_remote_parent() {
let parent_author_actor_id = "https://test.net/user/test";
let parent_author = DbActorProfile { let parent_author = DbActorProfile {
actor_json: Some(json!("test")), actor_json: Some(json!({
"id": parent_author_actor_id,
})),
..Default::default() ..Default::default()
}; };
let parent = Post { let parent = Post {
@ -347,6 +357,10 @@ mod tests {
note.in_reply_to.unwrap(), note.in_reply_to.unwrap(),
parent.object_id.unwrap(), parent.object_id.unwrap(),
); );
assert_eq!(
note.to.unwrap(),
json!([AP_PUBLIC, parent_author_actor_id]),
);
} }
#[test] #[test]

View file

@ -90,14 +90,19 @@ async fn deliver_activity_worker(
), ),
); );
let activity_json = serde_json::to_string(&activity)?; let activity_json = serde_json::to_string(&activity)?;
for recipient in recipients { let mut inboxes: Vec<String> = recipients.into_iter()
.map(|actor| actor.inbox)
.collect();
inboxes.sort();
inboxes.dedup();
for inbox_url in inboxes {
// TODO: retry on error // TODO: retry on error
if let Err(err) = send_activity( if let Err(err) = send_activity(
&config, &config,
&actor_key, &actor_key,
&actor_key_id, &actor_key_id,
&activity_json, &activity_json,
&recipient.inbox, &inbox_url,
).await { ).await {
log::error!("{}", err); log::error!("{}", err);
} }

View file

@ -49,7 +49,7 @@ async fn create_status(
post_data.validate()?; post_data.validate()?;
let post = create_post(db_client, &current_user.id, post_data).await?; let post = create_post(db_client, &current_user.id, post_data).await?;
// Federate // Federate
let in_reply_to = match post.in_reply_to_id { let maybe_in_reply_to = match post.in_reply_to_id {
Some(in_reply_to_id) => { Some(in_reply_to_id) => {
let in_reply_to = get_post_by_id(db_client, &in_reply_to_id).await?; let in_reply_to = get_post_by_id(db_client, &in_reply_to_id).await?;
Some(in_reply_to) Some(in_reply_to)
@ -59,7 +59,7 @@ async fn create_status(
let activity = create_activity_note( let activity = create_activity_note(
&config.instance_url(), &config.instance_url(),
&post, &post,
in_reply_to.as_ref(), maybe_in_reply_to.as_ref(),
); );
let followers = get_followers(db_client, &current_user.id).await?; let followers = get_followers(db_client, &current_user.id).await?;
let mut recipients: Vec<Actor> = Vec::new(); let mut recipients: Vec<Actor> = Vec::new();
@ -70,6 +70,13 @@ async fn create_status(
recipients.push(remote_actor); recipients.push(remote_actor);
}; };
}; };
if let Some(in_reply_to) = maybe_in_reply_to {
let maybe_remote_actor = in_reply_to.author.actor()
.map_err(|_| HttpError::InternalError)?;
if let Some(remote_actor) = maybe_remote_actor {
recipients.push(remote_actor);
}
}
deliver_activity(&config, &current_user, activity, recipients); deliver_activity(&config, &current_user, activity, recipients);
let status = Status::from_post(post, &config.instance_url()); let status = Status::from_post(post, &config.instance_url());
Ok(HttpResponse::Created().json(status)) Ok(HttpResponse::Created().json(status))