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,
}
}).collect();
let mut recipients = vec![AP_PUBLIC.to_string()];
let in_reply_to_object_id = match post.in_reply_to_id {
Some(in_reply_to_id) => {
let post = in_reply_to.unwrap();
assert_eq!(post.id, in_reply_to_id);
match post.author.is_local() {
false => post.object_id.clone(),
true => Some(get_object_url(instance_url, &post.id)),
match post.author.actor_json {
Some(ref actor_value) => {
// 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,
@ -143,7 +149,7 @@ pub fn create_note(
attributed_to: Some(actor_id),
in_reply_to: in_reply_to_object_id,
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(),
format!("{}/objects/{}", INSTANCE_URL, parent.id),
);
assert_eq!(note.to.unwrap(), json!([AP_PUBLIC]));
}
#[test]
fn test_create_note_with_remote_parent() {
let parent_author_actor_id = "https://test.net/user/test";
let parent_author = DbActorProfile {
actor_json: Some(json!("test")),
actor_json: Some(json!({
"id": parent_author_actor_id,
})),
..Default::default()
};
let parent = Post {
@ -347,6 +357,10 @@ mod tests {
note.in_reply_to.unwrap(),
parent.object_id.unwrap(),
);
assert_eq!(
note.to.unwrap(),
json!([AP_PUBLIC, parent_author_actor_id]),
);
}
#[test]

View file

@ -90,14 +90,19 @@ async fn deliver_activity_worker(
),
);
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
if let Err(err) = send_activity(
&config,
&actor_key,
&actor_key_id,
&activity_json,
&recipient.inbox,
&inbox_url,
).await {
log::error!("{}", err);
}

View file

@ -49,7 +49,7 @@ async fn create_status(
post_data.validate()?;
let post = create_post(db_client, &current_user.id, post_data).await?;
// 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) => {
let in_reply_to = get_post_by_id(db_client, &in_reply_to_id).await?;
Some(in_reply_to)
@ -59,7 +59,7 @@ async fn create_status(
let activity = create_activity_note(
&config.instance_url(),
&post,
in_reply_to.as_ref(),
maybe_in_reply_to.as_ref(),
);
let followers = get_followers(db_client, &current_user.id).await?;
let mut recipients: Vec<Actor> = Vec::new();
@ -70,6 +70,13 @@ async fn create_status(
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);
let status = Status::from_post(post, &config.instance_url());
Ok(HttpResponse::Created().json(status))