Fix federation tests

This commit is contained in:
Felix Ableitner 2021-03-23 17:03:14 +01:00
parent d6bd072ea1
commit c7524d924b
8 changed files with 128 additions and 57 deletions

View file

@ -134,46 +134,95 @@ pub(crate) async fn community_receive_message(
let activity_kind = activity.kind().context(location_info!())?; let activity_kind = activity.kind().context(location_info!())?;
let do_announce = match activity_kind { let do_announce = match activity_kind {
CommunityValidTypes::Follow => { CommunityValidTypes::Follow => {
handle_follow(any_base.clone(), person, &to_community, &context).await?; Box::pin(handle_follow(
any_base.clone(),
person,
&to_community,
&context,
))
.await?;
false false
} }
CommunityValidTypes::Undo => { CommunityValidTypes::Undo => {
handle_undo( Box::pin(handle_undo(
context, context,
activity.clone(), activity.clone(),
actor_url, actor_url,
&to_community, &to_community,
request_counter, request_counter,
) ))
.await? .await?
} }
CommunityValidTypes::Create => { CommunityValidTypes::Create => {
receive_create_for_community(context, any_base.clone(), &actor_url, request_counter).await?; Box::pin(receive_create_for_community(
context,
any_base.clone(),
&actor_url,
request_counter,
))
.await?;
true true
} }
CommunityValidTypes::Update => { CommunityValidTypes::Update => {
receive_update_for_community(context, any_base.clone(), None, &actor_url, request_counter) Box::pin(receive_update_for_community(
context,
any_base.clone(),
None,
&actor_url,
request_counter,
))
.await?; .await?;
true true
} }
CommunityValidTypes::Like => { CommunityValidTypes::Like => {
receive_like_for_community(context, any_base.clone(), &actor_url, request_counter).await?; Box::pin(receive_like_for_community(
context,
any_base.clone(),
&actor_url,
request_counter,
))
.await?;
true true
} }
CommunityValidTypes::Dislike => { CommunityValidTypes::Dislike => {
receive_dislike_for_community(context, any_base.clone(), &actor_url, request_counter).await?; Box::pin(receive_dislike_for_community(
context,
any_base.clone(),
&actor_url,
request_counter,
))
.await?;
true true
} }
CommunityValidTypes::Delete => { CommunityValidTypes::Delete => {
receive_delete_for_community(context, any_base.clone(), None, &actor_url).await?; Box::pin(receive_delete_for_community(
context,
any_base.clone(),
None,
&actor_url,
request_counter,
))
.await?;
true true
} }
CommunityValidTypes::Add => { CommunityValidTypes::Add => {
receive_add_for_community(context, any_base.clone(), None, request_counter).await?; Box::pin(receive_add_for_community(
context,
any_base.clone(),
None,
request_counter,
))
.await?;
true true
} }
CommunityValidTypes::Remove => { CommunityValidTypes::Remove => {
receive_remove_for_community(context, any_base.clone(), None, request_counter).await?; Box::pin(receive_remove_for_community(
context,
any_base.clone(),
None,
request_counter,
))
.await?;
true true
} }
}; };

View file

@ -58,7 +58,7 @@ pub(crate) async fn is_activity_already_known(
pub(crate) fn get_activity_to_and_cc<T, Kind>(activity: &T) -> Vec<Url> pub(crate) fn get_activity_to_and_cc<T, Kind>(activity: &T) -> Vec<Url>
where where
T: AsBase<Kind> + AsObject<Kind> + ActorAndObjectRefExt, T: AsObject<Kind>,
{ {
let mut to_and_cc = vec![]; let mut to_and_cc = vec![];
if let Some(to) = activity.to() { if let Some(to) = activity.to() {

View file

@ -154,19 +154,39 @@ pub(crate) async fn person_receive_message(
.await?; .await?;
} }
PersonValidTypes::Announce => { PersonValidTypes::Announce => {
receive_announce(&context, any_base, actor, request_counter).await? Box::pin(receive_announce(&context, any_base, actor, request_counter)).await?
} }
PersonValidTypes::Create => { PersonValidTypes::Create => {
receive_create(&context, any_base, actor_url, request_counter).await? Box::pin(receive_create(
&context,
any_base,
actor_url,
request_counter,
))
.await?
} }
PersonValidTypes::Update => { PersonValidTypes::Update => {
receive_update(&context, any_base, actor_url, request_counter).await? Box::pin(receive_update(
&context,
any_base,
actor_url,
request_counter,
))
.await?
} }
PersonValidTypes::Delete => { PersonValidTypes::Delete => {
receive_delete(context, any_base, &actor_url, request_counter).await? Box::pin(receive_delete(
context,
any_base,
&actor_url,
request_counter,
))
.await?
} }
PersonValidTypes::Undo => receive_undo(context, any_base, &actor_url, request_counter).await?, PersonValidTypes::Undo => {
PersonValidTypes::Remove => receive_remove(context, any_base, &actor_url).await?, Box::pin(receive_undo(context, any_base, &actor_url, request_counter)).await?
}
PersonValidTypes::Remove => Box::pin(receive_remove(context, any_base, &actor_url)).await?,
}; };
// TODO: would be logical to move websocket notification code here // TODO: would be logical to move websocket notification code here
@ -305,7 +325,14 @@ pub async fn receive_announce(
receive_dislike_for_community(context, inner_activity, &inner_id, request_counter).await receive_dislike_for_community(context, inner_activity, &inner_id, request_counter).await
} }
Some(Delete) => { Some(Delete) => {
receive_delete_for_community(context, inner_activity, Some(announce), &inner_id).await receive_delete_for_community(
context,
inner_activity,
Some(announce),
&inner_id,
request_counter,
)
.await
} }
Some(Remove) => { Some(Remove) => {
receive_remove_for_community(context, inner_activity, Some(announce), request_counter).await receive_remove_for_community(context, inner_activity, Some(announce), request_counter).await

View file

@ -35,13 +35,10 @@ use crate::{
objects::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, objects::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
person::get_or_fetch_and_upsert_person, person::get_or_fetch_and_upsert_person,
}, },
find_object_by_id,
find_post_or_comment_by_id, find_post_or_comment_by_id,
generate_moderators_url, generate_moderators_url,
inbox::verify_is_addressed_to_public, inbox::verify_is_addressed_to_public,
ActorType,
CommunityType, CommunityType,
Object,
PostOrComment, PostOrComment,
}; };
use activitystreams::{ use activitystreams::{
@ -122,7 +119,7 @@ pub(in crate::inbox) async fn receive_update_for_community(
let update = Update::from_any_base(activity)?.context(location_info!())?; let update = Update::from_any_base(activity)?.context(location_info!())?;
verify_activity_domains_valid(&update, &expected_domain, false)?; verify_activity_domains_valid(&update, &expected_domain, false)?;
verify_is_addressed_to_public(&update)?; verify_is_addressed_to_public(&update)?;
verify_modification_actor_instance(&update, &announce, context).await?; verify_modification_actor_instance(&update, &announce, context, request_counter).await?;
let kind = update let kind = update
.object() .object()
@ -197,11 +194,12 @@ pub(in crate::inbox) async fn receive_delete_for_community(
activity: AnyBase, activity: AnyBase,
announce: Option<Announce>, announce: Option<Announce>,
expected_domain: &Url, expected_domain: &Url,
request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let delete = Delete::from_any_base(activity)?.context(location_info!())?; let delete = Delete::from_any_base(activity)?.context(location_info!())?;
verify_activity_domains_valid(&delete, &expected_domain, true)?; verify_activity_domains_valid(&delete, &expected_domain, true)?;
verify_is_addressed_to_public(&delete)?; verify_is_addressed_to_public(&delete)?;
verify_modification_actor_instance(&delete, &announce, context).await?; verify_modification_actor_instance(&delete, &announce, context, request_counter).await?;
let object = delete let object = delete
.object() .object()
@ -588,6 +586,7 @@ async fn verify_modification_actor_instance<T, Kind>(
activity: &T, activity: &T,
announce: &Option<Announce>, announce: &Option<Announce>,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> ) -> Result<(), LemmyError>
where where
T: ActorAndObjectRef + BaseExt<Kind> + AsObject<Kind>, T: ActorAndObjectRef + BaseExt<Kind> + AsObject<Kind>,
@ -603,12 +602,9 @@ where
.map(|o| o.id()) .map(|o| o.id())
.flatten() .flatten()
.context(location_info!())?; .context(location_info!())?;
let original_id = match find_object_by_id(context, object_id.to_owned()).await? { let original_id = match fetch_post_or_comment_by_id(object_id, context, request_counter).await? {
Object::Post(p) => p.ap_id.into_inner(), PostOrComment::Post(p) => p.ap_id.into_inner(),
Object::Comment(c) => c.ap_id.into_inner(), PostOrComment::Comment(c) => c.ap_id.into_inner(),
Object::Community(c) => c.actor_id(),
Object::Person(p) => p.actor_id(),
Object::PrivateMessage(p) => p.ap_id.into_inner(),
}; };
if actor_id.domain() != original_id.domain() { if actor_id.domain() != original_id.domain() {
let community = extract_community_from_cc(activity, context).await?; let community = extract_community_from_cc(activity, context).await?;

View file

@ -80,13 +80,13 @@ pub async fn shared_inbox(
let community_activity = CommunityAcceptedActivities::from_any_base(activity_any_base.clone())? let community_activity = CommunityAcceptedActivities::from_any_base(activity_any_base.clone())?
.context(location_info!())?; .context(location_info!())?;
res = Some( res = Some(
community_receive_message( Box::pin(community_receive_message(
community_activity, community_activity,
community, community,
actor.as_ref(), actor.as_ref(),
&context, &context,
request_counter, request_counter,
) ))
.await?, .await?,
); );
} else if is_addressed_to_local_person(&to_and_cc, context.pool()).await? { } else if is_addressed_to_local_person(&to_and_cc, context.pool()).await? {
@ -94,13 +94,13 @@ pub async fn shared_inbox(
.context(location_info!())?; .context(location_info!())?;
// `to_person` is only used for follow activities (which we dont receive here), so no need to pass // `to_person` is only used for follow activities (which we dont receive here), so no need to pass
// it in // it in
person_receive_message( Box::pin(person_receive_message(
person_activity, person_activity,
None, None,
actor.as_ref(), actor.as_ref(),
&context, &context,
request_counter, request_counter,
) ))
.await?; .await?;
} else if is_addressed_to_community_followers(&to_and_cc, context.pool()) } else if is_addressed_to_community_followers(&to_and_cc, context.pool())
.await? .await?
@ -109,13 +109,13 @@ pub async fn shared_inbox(
let person_activity = PersonAcceptedActivities::from_any_base(activity_any_base.clone())? let person_activity = PersonAcceptedActivities::from_any_base(activity_any_base.clone())?
.context(location_info!())?; .context(location_info!())?;
res = Some( res = Some(
person_receive_message( Box::pin(person_receive_message(
person_activity, person_activity,
None, None,
actor.as_ref(), actor.as_ref(),
&context, &context,
request_counter, request_counter,
) ))
.await?, .await?,
); );
} }

View file

@ -165,15 +165,24 @@ impl FromApubToForm<NoteExt> for CommentForm {
let post_ap_id = in_reply_tos.next().context(location_info!())??; let post_ap_id = in_reply_tos.next().context(location_info!())??;
// This post, or the parent comment might not yet exist on this server yet, fetch them. // This post, or the parent comment might not yet exist on this server yet, fetch them.
let post = get_or_fetch_and_insert_post(&post_ap_id, context, request_counter).await?; let post = Box::pin(get_or_fetch_and_insert_post(
&post_ap_id,
context,
request_counter,
))
.await?;
// The 2nd item, if it exists, is the parent comment apub_id // The 2nd item, if it exists, is the parent comment apub_id
// For deeply nested comments, FromApub automatically gets called recursively // For deeply nested comments, FromApub automatically gets called recursively
let parent_id: Option<CommentId> = match in_reply_tos.next() { let parent_id: Option<CommentId> = match in_reply_tos.next() {
Some(parent_comment_uri) => { Some(parent_comment_uri) => {
let parent_comment_ap_id = &parent_comment_uri?; let parent_comment_ap_id = &parent_comment_uri?;
let parent_comment = let parent_comment = Box::pin(get_or_fetch_and_insert_comment(
get_or_fetch_and_insert_comment(&parent_comment_ap_id, context, request_counter).await?; &parent_comment_ap_id,
context,
request_counter,
))
.await?;
Some(parent_comment.id) Some(parent_comment.id)
} }

View file

@ -1,7 +1,8 @@
use crate::{ use crate::{
check_is_apub_id_valid, check_is_apub_id_valid,
fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person}, fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
inbox::community_inbox::check_community_or_site_ban, inbox::{community_inbox::check_community_or_site_ban, get_activity_to_and_cc},
PageExt,
}; };
use activitystreams::{ use activitystreams::{
base::{AsBase, BaseExt, ExtendsExt}, base::{AsBase, BaseExt, ExtendsExt},
@ -230,23 +231,12 @@ where
check_community_or_site_ban(&person, community_id, context.pool()).await check_community_or_site_ban(&person, community_id, context.pool()).await
} }
pub(in crate::objects) async fn get_to_community<T, Kind>( pub(in crate::objects) async fn get_community_from_to_or_cc(
object: &T, page: &PageExt,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<Community, LemmyError> ) -> Result<Community, LemmyError> {
where for cid in get_activity_to_and_cc(page) {
T: ObjectExt<Kind>,
{
let community_ids = object
.to()
.context(location_info!())?
.as_many()
.context(location_info!())?
.iter()
.map(|a| a.as_xsd_any_uri().context(location_info!()))
.collect::<Result<Vec<&Url>, anyhow::Error>>()?;
for cid in community_ids {
let community = get_or_fetch_and_upsert_community(&cid, context, request_counter).await; let community = get_or_fetch_and_upsert_community(&cid, context, request_counter).await;
if community.is_ok() { if community.is_ok() {
return community; return community;

View file

@ -6,9 +6,9 @@ use crate::{
check_object_domain, check_object_domain,
check_object_for_community_or_site_ban, check_object_for_community_or_site_ban,
create_tombstone, create_tombstone,
get_community_from_to_or_cc,
get_object_from_apub, get_object_from_apub,
get_source_markdown_value, get_source_markdown_value,
get_to_community,
set_content_and_source, set_content_and_source,
FromApub, FromApub,
FromApubToForm, FromApubToForm,
@ -162,7 +162,7 @@ impl FromApubToForm<PageExt> for PostForm {
let creator = let creator =
get_or_fetch_and_upsert_person(creator_actor_id, context, request_counter).await?; get_or_fetch_and_upsert_person(creator_actor_id, context, request_counter).await?;
let community = get_to_community(page, context, request_counter).await?; let community = get_community_from_to_or_cc(page, context, request_counter).await?;
let thumbnail_url: Option<Url> = match &page.inner.image() { let thumbnail_url: Option<Url> = match &page.inner.image() {
Some(any_image) => Image::from_any_base( Some(any_image) => Image::from_any_base(