diff --git a/docs/src/contributing_apub_api_outline.md b/docs/src/contributing_apub_api_outline.md index 92cdf9c0e..74280bd5f 100644 --- a/docs/src/contributing_apub_api_outline.md +++ b/docs/src/contributing_apub_api_outline.md @@ -438,7 +438,7 @@ Sent to: Community "cc": [ "https://ds9.lemmy.ml/c/main/" ], - "object": ... + "object": "https://enterprise.lemmy.ml/p/123" } ``` @@ -465,7 +465,7 @@ Sent to: Community "cc": [ "https://ds9.lemmy.ml/c/main/" ], - "object": ... + "object": "https://enterprise.lemmy.ml/p/123" } ``` diff --git a/lemmy_apub/src/activities/receive/comment.rs b/lemmy_apub/src/activities/receive/comment.rs index 1d0260f77..ff0fb8199 100644 --- a/lemmy_apub/src/activities/receive/comment.rs +++ b/lemmy_apub/src/activities/receive/comment.rs @@ -1,20 +1,6 @@ -use crate::{ - activities::receive::{get_actor_as_user, get_like_object_id}, - fetcher::get_or_fetch_and_insert_comment, - objects::FromApub, - ActorType, - NoteExt, -}; +use crate::{activities::receive::get_actor_as_user, objects::FromApub, ActorType, NoteExt}; use activitystreams::{ - activity::{ - kind::{DislikeType, LikeType}, - ActorAndObjectRefExt, - Create, - Dislike, - Like, - Remove, - Update, - }, + activity::{ActorAndObjectRefExt, Create, Dislike, Like, Remove, Update}, base::ExtendsExt, }; use anyhow::Context; @@ -113,12 +99,11 @@ pub(crate) async fn receive_update_comment( pub(crate) async fn receive_like_comment( like: Like, + comment: Comment, context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { let user = get_actor_as_user(&like, context, request_counter).await?; - let comment_id = get_like_object_id::(&like)?; - let comment = get_or_fetch_and_insert_comment(&comment_id, context, request_counter).await?; let comment_id = comment.id; let like_form = CommentLikeForm { @@ -159,12 +144,11 @@ pub(crate) async fn receive_like_comment( pub(crate) async fn receive_dislike_comment( dislike: Dislike, + comment: Comment, context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { let user = get_actor_as_user(&dislike, context, request_counter).await?; - let comment_id = get_like_object_id::(&dislike)?; - let comment = get_or_fetch_and_insert_comment(&comment_id, context, request_counter).await?; let comment_id = comment.id; let like_form = CommentLikeForm { diff --git a/lemmy_apub/src/activities/receive/comment_undo.rs b/lemmy_apub/src/activities/receive/comment_undo.rs index da9246960..2ee8c6ea7 100644 --- a/lemmy_apub/src/activities/receive/comment_undo.rs +++ b/lemmy_apub/src/activities/receive/comment_undo.rs @@ -1,11 +1,5 @@ -use crate::{ - activities::receive::{get_actor_as_user, get_like_object_id}, - fetcher::get_or_fetch_and_insert_comment, -}; -use activitystreams::activity::{ - kind::{DislikeType, LikeType}, - *, -}; +use crate::activities::receive::get_actor_as_user; +use activitystreams::activity::{Dislike, Like}; use lemmy_db::{ comment::{Comment, CommentLike}, comment_view::CommentView, @@ -17,12 +11,11 @@ use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; pub(crate) async fn receive_undo_like_comment( like: &Like, + comment: Comment, context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { let user = get_actor_as_user(like, context, request_counter).await?; - let comment_id = get_like_object_id::(like)?; - let comment = get_or_fetch_and_insert_comment(&comment_id, context, request_counter).await?; let comment_id = comment.id; let user_id = user.id; @@ -56,12 +49,11 @@ pub(crate) async fn receive_undo_like_comment( pub(crate) async fn receive_undo_dislike_comment( dislike: &Dislike, + comment: Comment, context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { let user = get_actor_as_user(dislike, context, request_counter).await?; - let comment_id = get_like_object_id::(dislike)?; - let comment = get_or_fetch_and_insert_comment(&comment_id, context, request_counter).await?; let comment_id = comment.id; let user_id = user.id; diff --git a/lemmy_apub/src/activities/receive/mod.rs b/lemmy_apub/src/activities/receive/mod.rs index d67f12fac..1f17fe9f3 100644 --- a/lemmy_apub/src/activities/receive/mod.rs +++ b/lemmy_apub/src/activities/receive/mod.rs @@ -79,28 +79,3 @@ where Ok(()) } - -pub(in crate::activities::receive) fn get_like_object_id( - like_or_dislike: &Activity, -) -> Result -where - Activity: ActorAndObjectRefExt, -{ - // For backwards compatibility with older Lemmy versions where like.object contains a full - // post/comment. This can be removed after some time, using - // `activity.oject().as_single_xsd_any_uri()` instead. - let object = like_or_dislike.object(); - if let Some(xsd_uri) = object.as_single_xsd_any_uri() { - Ok(xsd_uri.to_owned()) - } else { - Ok( - object - .to_owned() - .one() - .context(location_info!())? - .id() - .context(location_info!())? - .to_owned(), - ) - } -} diff --git a/lemmy_apub/src/activities/receive/post.rs b/lemmy_apub/src/activities/receive/post.rs index 04d83d374..0bbf1eaf5 100644 --- a/lemmy_apub/src/activities/receive/post.rs +++ b/lemmy_apub/src/activities/receive/post.rs @@ -1,19 +1,6 @@ -use crate::{ - activities::receive::{get_actor_as_user, get_like_object_id}, - fetcher::get_or_fetch_and_insert_post, - objects::FromApub, - ActorType, - PageExt, -}; +use crate::{activities::receive::get_actor_as_user, objects::FromApub, ActorType, PageExt}; use activitystreams::{ - activity::{ - kind::{DislikeType, LikeType}, - Create, - Dislike, - Like, - Remove, - Update, - }, + activity::{Create, Dislike, Like, Remove, Update}, prelude::*, }; use anyhow::Context; @@ -86,12 +73,11 @@ pub(crate) async fn receive_update_post( pub(crate) async fn receive_like_post( like: Like, + post: Post, context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { let user = get_actor_as_user(&like, context, request_counter).await?; - let post_id = get_like_object_id::(&like)?; - let post = get_or_fetch_and_insert_post(&post_id, context, request_counter).await?; let post_id = post.id; let like_form = PostLikeForm { @@ -125,12 +111,11 @@ pub(crate) async fn receive_like_post( pub(crate) async fn receive_dislike_post( dislike: Dislike, + post: Post, context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { let user = get_actor_as_user(&dislike, context, request_counter).await?; - let post_id = get_like_object_id::(&dislike)?; - let post = get_or_fetch_and_insert_post(&post_id, context, request_counter).await?; let post_id = post.id; let like_form = PostLikeForm { diff --git a/lemmy_apub/src/activities/receive/post_undo.rs b/lemmy_apub/src/activities/receive/post_undo.rs index cf2f447b1..bcbb7fee9 100644 --- a/lemmy_apub/src/activities/receive/post_undo.rs +++ b/lemmy_apub/src/activities/receive/post_undo.rs @@ -1,11 +1,5 @@ -use crate::{ - activities::receive::{get_actor_as_user, get_like_object_id}, - fetcher::get_or_fetch_and_insert_post, -}; -use activitystreams::activity::{ - kind::{DislikeType, LikeType}, - *, -}; +use crate::activities::receive::get_actor_as_user; +use activitystreams::activity::{Dislike, Like}; use lemmy_db::{ post::{Post, PostLike}, post_view::PostView, @@ -17,12 +11,11 @@ use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; pub(crate) async fn receive_undo_like_post( like: &Like, + post: Post, context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { let user = get_actor_as_user(like, context, request_counter).await?; - let post_id = get_like_object_id::(&like)?; - let post = get_or_fetch_and_insert_post(&post_id, context, request_counter).await?; let post_id = post.id; let user_id = user.id; @@ -50,12 +43,11 @@ pub(crate) async fn receive_undo_like_post( pub(crate) async fn receive_undo_dislike_post( dislike: &Dislike, + post: Post, context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { let user = get_actor_as_user(dislike, context, request_counter).await?; - let post_id = get_like_object_id::(&dislike)?; - let post = get_or_fetch_and_insert_post(&post_id, context, request_counter).await?; let post_id = post.id; let user_id = user.id; diff --git a/lemmy_apub/src/activities/send/comment.rs b/lemmy_apub/src/activities/send/comment.rs index 4415211f3..744a1cddb 100644 --- a/lemmy_apub/src/activities/send/comment.rs +++ b/lemmy_apub/src/activities/send/comment.rs @@ -218,8 +218,6 @@ impl ApubObjectType for Comment { #[async_trait::async_trait(?Send)] impl ApubLikeableType for Comment { async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> { - let note = self.to_apub(context.pool()).await?; - let post_id = self.post_id; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; @@ -229,7 +227,7 @@ impl ApubLikeableType for Comment { }) .await??; - let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?); + let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?); like .set_many_contexts(lemmy_context()?) .set_id(generate_activity_id(LikeType::Like)?) @@ -241,8 +239,6 @@ impl ApubLikeableType for Comment { } async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> { - let note = self.to_apub(context.pool()).await?; - let post_id = self.post_id; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; @@ -252,7 +248,7 @@ impl ApubLikeableType for Comment { }) .await??; - let mut dislike = Dislike::new(creator.actor_id.to_owned(), note.into_any_base()?); + let mut dislike = Dislike::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?); dislike .set_many_contexts(lemmy_context()?) .set_id(generate_activity_id(DislikeType::Dislike)?) @@ -268,8 +264,6 @@ impl ApubLikeableType for Comment { creator: &User_, context: &LemmyContext, ) -> Result<(), LemmyError> { - let note = self.to_apub(context.pool()).await?; - let post_id = self.post_id; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; @@ -279,7 +273,7 @@ impl ApubLikeableType for Comment { }) .await??; - let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?); + let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?); like .set_many_contexts(lemmy_context()?) .set_id(generate_activity_id(DislikeType::Dislike)?) diff --git a/lemmy_apub/src/activities/send/post.rs b/lemmy_apub/src/activities/send/post.rs index d5ff56ace..f6eabb048 100644 --- a/lemmy_apub/src/activities/send/post.rs +++ b/lemmy_apub/src/activities/send/post.rs @@ -167,15 +167,13 @@ impl ApubObjectType for Post { #[async_trait::async_trait(?Send)] impl ApubLikeableType for Post { async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> { - let page = self.to_apub(context.pool()).await?; - let community_id = self.community_id; let community = blocking(context.pool(), move |conn| { Community::read(conn, community_id) }) .await??; - let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?); + let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?); like .set_many_contexts(lemmy_context()?) .set_id(generate_activity_id(LikeType::Like)?) @@ -187,15 +185,13 @@ impl ApubLikeableType for Post { } async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> { - let page = self.to_apub(context.pool()).await?; - let community_id = self.community_id; let community = blocking(context.pool(), move |conn| { Community::read(conn, community_id) }) .await??; - let mut dislike = Dislike::new(creator.actor_id.to_owned(), page.into_any_base()?); + let mut dislike = Dislike::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?); dislike .set_many_contexts(lemmy_context()?) .set_id(generate_activity_id(DislikeType::Dislike)?) @@ -211,15 +207,13 @@ impl ApubLikeableType for Post { creator: &User_, context: &LemmyContext, ) -> Result<(), LemmyError> { - let page = self.to_apub(context.pool()).await?; - let community_id = self.community_id; let community = blocking(context.pool(), move |conn| { Community::read(conn, community_id) }) .await??; - let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?); + let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?); like .set_many_contexts(lemmy_context()?) .set_id(generate_activity_id(LikeType::Like)?) diff --git a/lemmy_apub/src/inbox/receive_for_community.rs b/lemmy_apub/src/inbox/receive_for_community.rs index 710cf17bf..4eaf39793 100644 --- a/lemmy_apub/src/inbox/receive_for_community.rs +++ b/lemmy_apub/src/inbox/receive_for_community.rs @@ -31,6 +31,7 @@ use crate::{ receive_unhandled_activity, verify_activity_domains_valid, }, + fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, inbox::is_addressed_to_public, }; use activitystreams::{ @@ -96,10 +97,12 @@ pub(in crate::inbox) async fn receive_like_for_community( verify_activity_domains_valid(&like, &expected_domain, false)?; is_addressed_to_public(&like)?; - match like.object().as_single_kind_str() { - Some("Page") => receive_like_post(like, context, request_counter).await, - Some("Note") => receive_like_comment(like, context, request_counter).await, - _ => receive_unhandled_activity(like), + let object_id = get_like_object_id(&like)?; + match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? { + PostOrComment::Post(post) => receive_like_post(like, post, context, request_counter).await, + PostOrComment::Comment(comment) => { + receive_like_comment(like, comment, context, request_counter).await + } } } @@ -122,10 +125,14 @@ pub(in crate::inbox) async fn receive_dislike_for_community( verify_activity_domains_valid(&dislike, &expected_domain, false)?; is_addressed_to_public(&dislike)?; - match dislike.object().as_single_kind_str() { - Some("Page") => receive_dislike_post(dislike, context, request_counter).await, - Some("Note") => receive_dislike_comment(dislike, context, request_counter).await, - _ => receive_unhandled_activity(dislike), + let object_id = get_like_object_id(&dislike)?; + match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? { + PostOrComment::Post(post) => { + receive_dislike_post(dislike, post, context, request_counter).await + } + PostOrComment::Comment(comment) => { + receive_dislike_comment(dislike, comment, context, request_counter).await + } } } @@ -275,14 +282,14 @@ pub(in crate::inbox) async fn receive_undo_like_for_community( verify_activity_domains_valid(&like, &expected_domain, false)?; is_addressed_to_public(&like)?; - let type_ = like - .object() - .as_single_kind_str() - .context(location_info!())?; - match type_ { - "Note" => receive_undo_like_comment(&like, context, request_counter).await, - "Page" => receive_undo_like_post(&like, context, request_counter).await, - _ => receive_unhandled_activity(like), + let object_id = get_like_object_id(&like)?; + match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? { + PostOrComment::Post(post) => { + receive_undo_like_post(&like, post, context, request_counter).await + } + PostOrComment::Comment(comment) => { + receive_undo_like_comment(&like, comment, context, request_counter).await + } } } @@ -298,14 +305,14 @@ pub(in crate::inbox) async fn receive_undo_dislike_for_community( verify_activity_domains_valid(&dislike, &expected_domain, false)?; is_addressed_to_public(&dislike)?; - let type_ = dislike - .object() - .as_single_kind_str() - .context(location_info!())?; - match type_ { - "Note" => receive_undo_dislike_comment(&dislike, context, request_counter).await, - "Page" => receive_undo_dislike_post(&dislike, context, request_counter).await, - _ => receive_unhandled_activity(dislike), + let object_id = get_like_object_id(&dislike)?; + match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? { + PostOrComment::Post(post) => { + receive_undo_dislike_post(&dislike, post, context, request_counter).await + } + PostOrComment::Comment(comment) => { + receive_undo_dislike_comment(&dislike, comment, context, request_counter).await + } } } @@ -341,3 +348,42 @@ async fn find_post_or_comment_by_id( return Err(NotFound.into()); } + +async fn fetch_post_or_comment_by_id( + apub_id: &Url, + context: &LemmyContext, + request_counter: &mut i32, +) -> Result { + if let Ok(post) = get_or_fetch_and_insert_post(apub_id, context, request_counter).await { + return Ok(PostOrComment::Post(post)); + } + + if let Ok(comment) = get_or_fetch_and_insert_comment(apub_id, context, request_counter).await { + return Ok(PostOrComment::Comment(comment)); + } + + return Err(NotFound.into()); +} + +fn get_like_object_id(like_or_dislike: &Activity) -> Result +where + Activity: ActorAndObjectRefExt, +{ + // For backwards compatibility with older Lemmy versions where like.object contains a full + // post/comment. This can be removed after some time, using + // `activity.oject().as_single_xsd_any_uri()` instead. + let object = like_or_dislike.object(); + if let Some(xsd_uri) = object.as_single_xsd_any_uri() { + Ok(xsd_uri.to_owned()) + } else { + Ok( + object + .to_owned() + .one() + .context(location_info!())? + .id() + .context(location_info!())? + .to_owned(), + ) + } +}