From: Felix Ableitner Date: Sat, 6 Nov 2021 12:37:55 +0000 (+0100) Subject: Change to_apub and from_apub to take by value and avoid cloning X-Git-Url: http://these/git/%7B%60%24%7BwebArchiveUrl%7D/%22%7B%7D/%22https:/hacktivis.me/static/git-favicon.png?a=commitdiff_plain;h=c725514841cda13d8743c7e30e7527b848e04d67;p=lemmy.git Change to_apub and from_apub to take by value and avoid cloning --- diff --git a/crates/api/src/post.rs b/crates/api/src/post.rs index 3564f135..c7acb08f 100644 --- a/crates/api/src/post.rs +++ b/crates/api/src/post.rs @@ -169,7 +169,7 @@ impl Perform for LockPost { // apub updates CreateOrUpdatePost::send( - &updated_post, + updated_post, &local_user_view.person.clone().into(), CreateOrUpdateType::Update, context, @@ -242,7 +242,7 @@ impl Perform for StickyPost { // Apub updates // TODO stickied should pry work like locked for ease of use CreateOrUpdatePost::send( - &updated_post, + updated_post, &local_user_view.person.clone().into(), CreateOrUpdateType::Update, context, diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index ab093ea1..4c7bb34f 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -1,5 +1,5 @@ +use crate::PerformCrud; use actix_web::web::Data; - use lemmy_api_common::{ blocking, check_community_ban, @@ -13,6 +13,7 @@ use lemmy_api_common::{ use lemmy_apub::{ fetcher::post_or_comment::PostOrComment, generate_local_apub_endpoint, + objects::comment::ApubComment, protocol::activities::{ create_or_update::comment::CreateOrUpdateComment, voting::vote::{Vote, VoteType}, @@ -40,8 +41,6 @@ use lemmy_websocket::{ UserOperationCrud, }; -use crate::PerformCrud; - #[async_trait::async_trait(?Send)] impl PerformCrud for CreateComment { type Response = CommentResponse; @@ -121,14 +120,6 @@ impl PerformCrud for CreateComment { .await? .map_err(|e| ApiError::err("couldnt_create_comment", e))?; - CreateOrUpdateComment::send( - &updated_comment.clone().into(), - &local_user_view.person.clone().into(), - CreateOrUpdateType::Create, - context, - ) - .await?; - // Scan the comment for user mentions, add those rows let post_id = post.id; let mentions = scrape_text_for_mentions(&comment_form.content); @@ -155,7 +146,15 @@ impl PerformCrud for CreateComment { .await? .map_err(|e| ApiError::err("couldnt_like_comment", e))?; - let object = PostOrComment::Comment(updated_comment.into()); + let apub_comment: ApubComment = updated_comment.into(); + CreateOrUpdateComment::send( + apub_comment.clone(), + &local_user_view.person.clone().into(), + CreateOrUpdateType::Create, + context, + ) + .await?; + let object = PostOrComment::Comment(apub_comment); Vote::send( &object, &local_user_view.person.clone().into(), diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index 70f15dfb..4802b1d5 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -72,15 +72,6 @@ impl PerformCrud for EditComment { .await? .map_err(|e| ApiError::err("couldnt_update_comment", e))?; - // Send the apub update - CreateOrUpdateComment::send( - &updated_comment.clone().into(), - &local_user_view.person.clone().into(), - CreateOrUpdateType::Update, - context, - ) - .await?; - // Do the mentions / recipients let updated_comment_content = updated_comment.content.to_owned(); let mentions = scrape_text_for_mentions(&updated_comment_content); @@ -94,6 +85,15 @@ impl PerformCrud for EditComment { ) .await?; + // Send the apub update + CreateOrUpdateComment::send( + updated_comment.into(), + &local_user_view.person.into(), + CreateOrUpdateType::Update, + context, + ) + .await?; + send_comment_ws_message( data.comment_id, UserOperationCrud::EditComment, diff --git a/crates/api_crud/src/community/update.rs b/crates/api_crud/src/community/update.rs index 4764b025..d1fd8981 100644 --- a/crates/api_crud/src/community/update.rs +++ b/crates/api_crud/src/community/update.rs @@ -72,7 +72,7 @@ impl PerformCrud for EditCommunity { .map_err(|e| ApiError::err("couldnt_update_community", e))?; UpdateCommunity::send( - &updated_community.into(), + updated_community.into(), &local_user_view.person.into(), context, ) diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 4b5c9e73..248adee6 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -12,6 +12,7 @@ use lemmy_api_common::{ use lemmy_apub::{ fetcher::post_or_comment::PostOrComment, generate_local_apub_endpoint, + objects::post::ApubPost, protocol::activities::{ create_or_update::post::CreateOrUpdatePost, voting::vote::{Vote, VoteType}, @@ -109,14 +110,6 @@ impl PerformCrud for CreatePost { .await? .map_err(|e| ApiError::err("couldnt_create_post", e))?; - CreateOrUpdatePost::send( - &updated_post.clone().into(), - &local_user_view.person.clone().into(), - CreateOrUpdateType::Create, - context, - ) - .await?; - // They like their own post by default let person_id = local_user_view.person.id; let post_id = inserted_post.id; @@ -145,7 +138,15 @@ impl PerformCrud for CreatePost { } } - let object = PostOrComment::Post(Box::new(updated_post.into())); + let apub_post: ApubPost = updated_post.into(); + CreateOrUpdatePost::send( + apub_post.clone(), + &local_user_view.person.clone().into(), + CreateOrUpdateType::Create, + context, + ) + .await?; + let object = PostOrComment::Post(Box::new(apub_post)); Vote::send( &object, &local_user_view.person.clone().into(), diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index 0a982d68..2cfc67d4 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -109,7 +109,7 @@ impl PerformCrud for EditPost { // Send apub update CreateOrUpdatePost::send( - &updated_post.into(), + updated_post.into(), &local_user_view.person.clone().into(), CreateOrUpdateType::Update, context, diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index 705d781e..0c637826 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -83,7 +83,7 @@ impl PerformCrud for CreatePrivateMessage { .map_err(|e| ApiError::err("couldnt_create_private_message", e))?; CreateOrUpdatePrivateMessage::send( - &updated_private_message.into(), + updated_private_message.into(), &local_user_view.person.into(), CreateOrUpdateType::Create, context, diff --git a/crates/api_crud/src/private_message/update.rs b/crates/api_crud/src/private_message/update.rs index 8114556c..95cc5cb3 100644 --- a/crates/api_crud/src/private_message/update.rs +++ b/crates/api_crud/src/private_message/update.rs @@ -47,7 +47,7 @@ impl PerformCrud for EditPrivateMessage { // Send the apub update CreateOrUpdatePrivateMessage::send( - &updated_private_message.into(), + updated_private_message.into(), &local_user_view.person.into(), CreateOrUpdateType::Update, context, diff --git a/crates/apub/src/activities/comment/create_or_update.rs b/crates/apub/src/activities/comment/create_or_update.rs index 0d12c4b3..0095a511 100644 --- a/crates/apub/src/activities/comment/create_or_update.rs +++ b/crates/apub/src/activities/comment/create_or_update.rs @@ -29,7 +29,7 @@ use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperation impl CreateOrUpdateComment { pub async fn send( - comment: &ApubComment, + comment: ApubComment, actor: &ApubPerson, kind: CreateOrUpdateType, context: &LemmyContext, @@ -48,12 +48,12 @@ impl CreateOrUpdateComment { kind.clone(), &context.settings().get_protocol_and_hostname(), )?; - let maa = collect_non_local_mentions(comment, &community, context).await?; + let maa = collect_non_local_mentions(&comment, &community, context).await?; let create_or_update = CreateOrUpdateComment { actor: ObjectId::new(actor.actor_id()), to: vec![public()], - object: comment.to_apub(context).await?, + object: comment.into_apub(context).await?, cc: maa.ccs, tag: maa.tags, kind, @@ -97,7 +97,7 @@ impl ActivityHandler for CreateOrUpdateComment { request_counter: &mut i32, ) -> Result<(), LemmyError> { let comment = - ApubComment::from_apub(&self.object, context, self.actor.inner(), request_counter).await?; + ApubComment::from_apub(self.object, context, self.actor.inner(), request_counter).await?; let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?; let notif_type = match self.kind { CreateOrUpdateType::Create => UserOperationCrud::CreateComment, diff --git a/crates/apub/src/activities/community/update.rs b/crates/apub/src/activities/community/update.rs index 45b08e02..68872247 100644 --- a/crates/apub/src/activities/community/update.rs +++ b/crates/apub/src/activities/community/update.rs @@ -27,7 +27,7 @@ use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperati impl UpdateCommunity { pub async fn send( - community: &ApubCommunity, + community: ApubCommunity, actor: &ApubPerson, context: &LemmyContext, ) -> Result<(), LemmyError> { @@ -38,7 +38,7 @@ impl UpdateCommunity { let update = UpdateCommunity { actor: ObjectId::new(actor.actor_id()), to: vec![public()], - object: community.to_apub(context).await?, + object: community.clone().into_apub(context).await?, cc: vec![community.actor_id()], kind: UpdateType::Update, id: id.clone(), @@ -46,7 +46,7 @@ impl UpdateCommunity { }; let activity = AnnouncableActivities::UpdateCommunity(Box::new(update)); - send_to_community(activity, &id, actor, community, vec![], context).await + send_to_community(activity, &id, actor, &community, vec![], context).await } } @@ -73,8 +73,8 @@ impl ActivityHandler for UpdateCommunity { ) -> Result<(), LemmyError> { let community = self.get_community(context, request_counter).await?; - let updated_community = Group::from_apub_to_form( - &self.object, + let updated_community = Group::into_form( + self.object, &community.actor_id.clone().into(), &context.settings(), ) diff --git a/crates/apub/src/activities/post/create_or_update.rs b/crates/apub/src/activities/post/create_or_update.rs index 925a778f..982f90cc 100644 --- a/crates/apub/src/activities/post/create_or_update.rs +++ b/crates/apub/src/activities/post/create_or_update.rs @@ -27,7 +27,7 @@ use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCru impl CreateOrUpdatePost { pub(crate) async fn new( - post: &ApubPost, + post: ApubPost, actor: &ApubPerson, community: &ApubCommunity, kind: CreateOrUpdateType, @@ -40,7 +40,7 @@ impl CreateOrUpdatePost { Ok(CreateOrUpdatePost { actor: ObjectId::new(actor.actor_id()), to: vec![public()], - object: post.to_apub(context).await?, + object: post.into_apub(context).await?, cc: vec![community.actor_id()], kind, id: id.clone(), @@ -48,7 +48,7 @@ impl CreateOrUpdatePost { }) } pub async fn send( - post: &ApubPost, + post: ApubPost, actor: &ApubPerson, kind: CreateOrUpdateType, context: &LemmyContext, @@ -115,7 +115,7 @@ impl ActivityHandler for CreateOrUpdatePost { ) -> Result<(), LemmyError> { let actor = self.actor.dereference(context, request_counter).await?; let post = - ApubPost::from_apub(&self.object, context, &actor.actor_id(), request_counter).await?; + ApubPost::from_apub(self.object, context, &actor.actor_id(), request_counter).await?; let notif_type = match self.kind { CreateOrUpdateType::Create => UserOperationCrud::CreatePost, diff --git a/crates/apub/src/activities/private_message/create_or_update.rs b/crates/apub/src/activities/private_message/create_or_update.rs index 30599db8..7f40b5fb 100644 --- a/crates/apub/src/activities/private_message/create_or_update.rs +++ b/crates/apub/src/activities/private_message/create_or_update.rs @@ -19,7 +19,7 @@ use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud} impl CreateOrUpdatePrivateMessage { pub async fn send( - private_message: &ApubPrivateMessage, + private_message: ApubPrivateMessage, actor: &ApubPerson, kind: CreateOrUpdateType, context: &LemmyContext, @@ -38,7 +38,7 @@ impl CreateOrUpdatePrivateMessage { id: id.clone(), actor: ObjectId::new(actor.actor_id()), to: [ObjectId::new(recipient.actor_id())], - object: private_message.to_apub(context).await?, + object: private_message.into_apub(context).await?, kind, unparsed: Default::default(), }; @@ -67,7 +67,7 @@ impl ActivityHandler for CreateOrUpdatePrivateMessage { request_counter: &mut i32, ) -> Result<(), LemmyError> { let private_message = - ApubPrivateMessage::from_apub(&self.object, context, self.actor.inner(), request_counter) + ApubPrivateMessage::from_apub(self.object, context, self.actor.inner(), request_counter) .await?; let notif_type = match self.kind { diff --git a/crates/apub/src/collections/community_moderators.rs b/crates/apub/src/collections/community_moderators.rs index 3579ed38..cbbe6670 100644 --- a/crates/apub/src/collections/community_moderators.rs +++ b/crates/apub/src/collections/community_moderators.rs @@ -49,11 +49,11 @@ impl ApubObject for ApubCommunityModerators { unimplemented!() } - async fn to_apub(&self, data: &Self::DataType) -> Result { + async fn into_apub(self, data: &Self::DataType) -> Result { let ordered_items = self .0 - .iter() - .map(|m| ObjectId::::new(m.moderator.actor_id.clone())) + .into_iter() + .map(|m| ObjectId::::new(m.moderator.actor_id)) .collect(); Ok(GroupModerators { r#type: OrderedCollectionType::OrderedCollection, @@ -67,7 +67,7 @@ impl ApubObject for ApubCommunityModerators { } async fn from_apub( - apub: &Self::ApubType, + apub: Self::ApubType, data: &Self::DataType, expected_domain: &Url, request_counter: &mut i32, @@ -94,12 +94,11 @@ impl ApubObject for ApubCommunityModerators { } // Add new mods to database which have been added to moderators collection - for mod_id in &apub.ordered_items { - let mod_id = ObjectId::new(mod_id.clone()); + for mod_id in apub.ordered_items { + let mod_id = ObjectId::new(mod_id); let mod_user: ApubPerson = mod_id.dereference(&data.1, request_counter).await?; if !current_moderators - .clone() .iter() .map(|c| c.moderator.actor_id.clone()) .any(|x| x == mod_user.actor_id) @@ -166,7 +165,7 @@ mod tests { 0: community, 1: context, }; - ApubCommunityModerators::from_apub(&json, &community_context, &url, &mut request_counter) + ApubCommunityModerators::from_apub(json, &community_context, &url, &mut request_counter) .await .unwrap(); assert_eq!(request_counter, 0); diff --git a/crates/apub/src/collections/community_outbox.rs b/crates/apub/src/collections/community_outbox.rs index 96d0892c..cbad5c48 100644 --- a/crates/apub/src/collections/community_outbox.rs +++ b/crates/apub/src/collections/community_outbox.rs @@ -60,9 +60,9 @@ impl ApubObject for ApubCommunityOutbox { Ok(()) } - async fn to_apub(&self, data: &Self::DataType) -> Result { + async fn into_apub(self, data: &Self::DataType) -> Result { let mut ordered_items = vec![]; - for post in &self.0 { + for post in self.0 { let actor = post.creator_id; let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor)) .await?? @@ -86,13 +86,13 @@ impl ApubObject for ApubCommunityOutbox { } async fn from_apub( - apub: &Self::ApubType, + apub: Self::ApubType, data: &Self::DataType, expected_domain: &Url, request_counter: &mut i32, ) -> Result { verify_domains_match(expected_domain, &apub.id)?; - let mut outbox_activities = apub.ordered_items.clone(); + let mut outbox_activities = apub.ordered_items; if outbox_activities.len() > 20 { outbox_activities = outbox_activities[0..20].to_vec(); } diff --git a/crates/apub/src/fetcher/post_or_comment.rs b/crates/apub/src/fetcher/post_or_comment.rs index ef4e59e1..075b018e 100644 --- a/crates/apub/src/fetcher/post_or_comment.rs +++ b/crates/apub/src/fetcher/post_or_comment.rs @@ -53,7 +53,7 @@ impl ApubObject for PostOrComment { } } - async fn to_apub(&self, _data: &Self::DataType) -> Result { + async fn into_apub(self, _data: &Self::DataType) -> Result { unimplemented!() } @@ -62,17 +62,17 @@ impl ApubObject for PostOrComment { } async fn from_apub( - apub: &PageOrNote, + apub: PageOrNote, context: &LemmyContext, expected_domain: &Url, request_counter: &mut i32, ) -> Result { Ok(match apub { PageOrNote::Page(p) => PostOrComment::Post(Box::new( - ApubPost::from_apub(p, context, expected_domain, request_counter).await?, + ApubPost::from_apub(*p, context, expected_domain, request_counter).await?, )), PageOrNote::Note(n) => PostOrComment::Comment( - ApubComment::from_apub(n, context, expected_domain, request_counter).await?, + ApubComment::from_apub(*n, context, expected_domain, request_counter).await?, ), }) } diff --git a/crates/apub/src/fetcher/search.rs b/crates/apub/src/fetcher/search.rs index 88027c56..557fc1b5 100644 --- a/crates/apub/src/fetcher/search.rs +++ b/crates/apub/src/fetcher/search.rs @@ -155,7 +155,7 @@ impl ApubObject for SearchableObjects { } } - async fn to_apub(&self, _data: &Self::DataType) -> Result { + async fn into_apub(self, _data: &Self::DataType) -> Result { unimplemented!() } @@ -164,7 +164,7 @@ impl ApubObject for SearchableObjects { } async fn from_apub( - apub: &Self::ApubType, + apub: Self::ApubType, context: &LemmyContext, ed: &Url, rc: &mut i32, diff --git a/crates/apub/src/fetcher/user_or_community.rs b/crates/apub/src/fetcher/user_or_community.rs index 16a1d822..eccda7c6 100644 --- a/crates/apub/src/fetcher/user_or_community.rs +++ b/crates/apub/src/fetcher/user_or_community.rs @@ -54,7 +54,7 @@ impl ApubObject for UserOrCommunity { } } - async fn to_apub(&self, _data: &Self::DataType) -> Result { + async fn into_apub(self, _data: &Self::DataType) -> Result { unimplemented!() } @@ -63,7 +63,7 @@ impl ApubObject for UserOrCommunity { } async fn from_apub( - apub: &Self::ApubType, + apub: Self::ApubType, data: &Self::DataType, expected_domain: &Url, request_counter: &mut i32, diff --git a/crates/apub/src/http/comment.rs b/crates/apub/src/http/comment.rs index 3086f2fd..f62ff36e 100644 --- a/crates/apub/src/http/comment.rs +++ b/crates/apub/src/http/comment.rs @@ -30,7 +30,7 @@ pub(crate) async fn get_apub_comment( } if !comment.deleted { - Ok(create_apub_response(&comment.to_apub(&**context).await?)) + Ok(create_apub_response(&comment.into_apub(&**context).await?)) } else { Ok(create_apub_tombstone_response(&comment.to_tombstone()?)) } diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index d9228ca5..10794d99 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -47,7 +47,7 @@ pub(crate) async fn get_apub_community_http( .into(); if !community.deleted { - let apub = community.to_apub(&**context).await?; + let apub = community.into_apub(&**context).await?; Ok(create_apub_response(&apub)) } else { @@ -118,7 +118,7 @@ pub(crate) async fn get_apub_community_outbox( let id = ObjectId::new(generate_outbox_url(&community.actor_id)?); let outbox_data = CommunityContext(community.into(), context.get_ref().clone()); let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?; - Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?)) + Ok(create_apub_response(&outbox.into_apub(&outbox_data).await?)) } pub(crate) async fn get_apub_community_moderators( @@ -134,6 +134,6 @@ pub(crate) async fn get_apub_community_moderators( let outbox_data = CommunityContext(community, context.get_ref().clone()); let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?; Ok(create_apub_response( - &moderators.to_apub(&outbox_data).await?, + &moderators.into_apub(&outbox_data).await?, )) } diff --git a/crates/apub/src/http/person.rs b/crates/apub/src/http/person.rs index 193689c5..3a3a3403 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -39,7 +39,7 @@ pub(crate) async fn get_apub_person_http( .into(); if !person.deleted { - let apub = person.to_apub(&context).await?; + let apub = person.into_apub(&context).await?; Ok(create_apub_response(&apub)) } else { diff --git a/crates/apub/src/http/post.rs b/crates/apub/src/http/post.rs index 0459942a..52fe002a 100644 --- a/crates/apub/src/http/post.rs +++ b/crates/apub/src/http/post.rs @@ -30,7 +30,7 @@ pub(crate) async fn get_apub_post( } if !post.deleted { - Ok(create_apub_response(&post.to_apub(&context).await?)) + Ok(create_apub_response(&post.into_apub(&context).await?)) } else { Ok(create_apub_tombstone_response(&post.to_tombstone()?)) } diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index 09270fec..da96f8fa 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -87,7 +87,7 @@ impl ApubObject for ApubComment { Ok(()) } - async fn to_apub(&self, context: &LemmyContext) -> Result { + async fn into_apub(self, context: &LemmyContext) -> Result { let creator_id = self.creator_id; let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??; @@ -104,7 +104,7 @@ impl ApubObject for ApubComment { let note = Note { r#type: NoteType::Note, - id: ObjectId::new(self.ap_id.to_owned()), + id: ObjectId::new(self.ap_id.clone()), attributed_to: ObjectId::new(creator.actor_id), to: vec![public()], content: markdown_to_html(&self.content), @@ -133,13 +133,12 @@ impl ApubObject for ApubComment { /// /// If the parent community, post and comment(s) are not known locally, these are also fetched. async fn from_apub( - note: &Note, + note: Note, context: &LemmyContext, expected_domain: &Url, request_counter: &mut i32, ) -> Result { verify_domains_match(note.id.inner(), expected_domain)?; - let ap_id = Some(note.id.clone().into()); let creator = note .attributed_to .dereference(context, request_counter) @@ -176,10 +175,10 @@ impl ApubObject for ApubComment { content: content_slurs_removed, removed: None, read: None, - published: note.published.map(|u| u.to_owned().naive_local()), - updated: note.updated.map(|u| u.to_owned().naive_local()), + published: note.published.map(|u| u.naive_local()), + updated: note.updated.map(|u| u.naive_local()), deleted: None, - ap_id, + ap_id: Some(note.id.into()), local: Some(false), }; let comment = blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??; @@ -206,7 +205,7 @@ pub(crate) mod tests { let person = parse_lemmy_person(context).await; let community = parse_lemmy_community(context).await; let post_json = file_to_json_object("assets/lemmy/objects/page.json"); - let post = ApubPost::from_apub(&post_json, context, url, &mut 0) + let post = ApubPost::from_apub(post_json, context, url, &mut 0) .await .unwrap(); (person, community, post) @@ -225,9 +224,9 @@ pub(crate) mod tests { let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap(); let data = prepare_comment_test(&url, &context).await; - let json = file_to_json_object("assets/lemmy/objects/note.json"); + let json: Note = file_to_json_object("assets/lemmy/objects/note.json"); let mut request_counter = 0; - let comment = ApubComment::from_apub(&json, &context, &url, &mut request_counter) + let comment = ApubComment::from_apub(json.clone(), &context, &url, &mut request_counter) .await .unwrap(); @@ -236,10 +235,11 @@ pub(crate) mod tests { assert!(!comment.local); assert_eq!(request_counter, 0); - let to_apub = comment.to_apub(&context).await.unwrap(); + let comment_id = comment.id; + let to_apub = comment.into_apub(&context).await.unwrap(); assert_json_include!(actual: json, expected: to_apub); - Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap(); + Comment::delete(&*context.pool().get().unwrap(), comment_id).unwrap(); cleanup(data, &context); } @@ -254,12 +254,12 @@ pub(crate) mod tests { Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2") .unwrap(); let person_json = file_to_json_object("assets/pleroma/objects/person.json"); - ApubPerson::from_apub(&person_json, &context, &pleroma_url, &mut 0) + ApubPerson::from_apub(person_json, &context, &pleroma_url, &mut 0) .await .unwrap(); let json = file_to_json_object("assets/pleroma/objects/note.json"); let mut request_counter = 0; - let comment = ApubComment::from_apub(&json, &context, &pleroma_url, &mut request_counter) + let comment = ApubComment::from_apub(json, &context, &pleroma_url, &mut request_counter) .await .unwrap(); diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index 851563ab..1eb58995 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -79,7 +79,7 @@ impl ApubObject for ApubCommunity { Ok(()) } - async fn to_apub(&self, _context: &LemmyContext) -> Result { + async fn into_apub(self, _context: &LemmyContext) -> Result { let source = self.description.clone().map(|bio| Source { content: bio, media_type: MediaTypeMarkdown::Markdown, @@ -130,12 +130,12 @@ impl ApubObject for ApubCommunity { /// Converts a `Group` to `Community`, inserts it into the database and updates moderators. async fn from_apub( - group: &Group, + group: Group, context: &LemmyContext, expected_domain: &Url, request_counter: &mut i32, ) -> Result { - let form = Group::from_apub_to_form(group, expected_domain, &context.settings()).await?; + let form = Group::into_form(group.clone(), expected_domain, &context.settings()).await?; // Fetching mods and outbox is not necessary for Lemmy to work, so ignore errors. Besides, // we need to ignore these errors so that tests can work entirely offline. @@ -242,7 +242,7 @@ pub(crate) mod tests { let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap(); let mut request_counter = 0; - let community = ApubCommunity::from_apub(&json, context, &url, &mut request_counter) + let community = ApubCommunity::from_apub(json, context, &url, &mut request_counter) .await .unwrap(); // this makes two requests to the (intentionally) broken outbox/moderators collections diff --git a/crates/apub/src/objects/person.rs b/crates/apub/src/objects/person.rs index f1da6550..dcaa9cd6 100644 --- a/crates/apub/src/objects/person.rs +++ b/crates/apub/src/objects/person.rs @@ -76,7 +76,7 @@ impl ApubObject for ApubPerson { Ok(()) } - async fn to_apub(&self, _pool: &LemmyContext) -> Result { + async fn into_apub(self, _pool: &LemmyContext) -> Result { let kind = if self.bot_account { UserTypes::Service } else { @@ -124,17 +124,16 @@ impl ApubObject for ApubPerson { } async fn from_apub( - person: &Person, + person: Person, context: &LemmyContext, expected_domain: &Url, _request_counter: &mut i32, ) -> Result { verify_domains_match(person.id.inner(), expected_domain)?; - let actor_id = Some(person.id.clone().into()); - let name = person.preferred_username.clone(); - let display_name: Option = person.name.clone(); + let name = person.preferred_username; + let display_name: Option = person.name; let bio = get_summary_from_string_or_source(&person.summary, &person.source); - let shared_inbox = person.endpoints.shared_inbox.clone().map(|s| s.into()); + let shared_inbox = person.endpoints.shared_inbox.map(|s| s.into()); let bot_account = match person.kind { UserTypes::Person => false, UserTypes::Service => true, @@ -152,21 +151,21 @@ impl ApubObject for ApubPerson { display_name: Some(display_name), banned: None, deleted: None, - avatar: Some(person.icon.clone().map(|i| i.url.into())), - banner: Some(person.image.clone().map(|i| i.url.into())), - published: person.published.map(|u| u.clone().naive_local()), - updated: person.updated.map(|u| u.clone().naive_local()), - actor_id, + avatar: Some(person.icon.map(|i| i.url.into())), + banner: Some(person.image.map(|i| i.url.into())), + published: person.published.map(|u| u.naive_local()), + updated: person.updated.map(|u| u.naive_local()), + actor_id: Some(person.id.into()), bio: Some(bio), local: Some(false), admin: Some(false), bot_account: Some(bot_account), private_key: None, - public_key: Some(Some(person.public_key.public_key_pem.clone())), + public_key: Some(Some(person.public_key.public_key_pem)), last_refreshed_at: Some(naive_now()), - inbox_url: Some(person.inbox.to_owned().into()), + inbox_url: Some(person.inbox.into()), shared_inbox_url: Some(shared_inbox), - matrix_user_id: Some(person.matrix_user_id.clone()), + matrix_user_id: Some(person.matrix_user_id), }; let person = blocking(context.pool(), move |conn| { DbPerson::upsert(conn, &person_form) @@ -215,7 +214,7 @@ pub(crate) mod tests { let json = file_to_json_object("assets/lemmy/objects/person.json"); let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap(); let mut request_counter = 0; - let person = ApubPerson::from_apub(&json, context, &url, &mut request_counter) + let person = ApubPerson::from_apub(json, context, &url, &mut request_counter) .await .unwrap(); assert_eq!(request_counter, 0); @@ -243,7 +242,7 @@ pub(crate) mod tests { let json = file_to_json_object("assets/pleroma/objects/person.json"); let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap(); let mut request_counter = 0; - let person = ApubPerson::from_apub(&json, &context, &url, &mut request_counter) + let person = ApubPerson::from_apub(json, &context, &url, &mut request_counter) .await .unwrap(); diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 0ba80724..bbb5537e 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -87,7 +87,7 @@ impl ApubObject for ApubPost { } // Turn a Lemmy post into an ActivityPub page that can be sent out over the network. - async fn to_apub(&self, context: &LemmyContext) -> Result { + async fn into_apub(self, context: &LemmyContext) -> Result { let creator_id = self.creator_id; let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??; let community_id = self.community_id; @@ -134,7 +134,7 @@ impl ApubObject for ApubPost { } async fn from_apub( - page: &Page, + page: Page, context: &LemmyContext, expected_domain: &Url, request_counter: &mut i32, @@ -144,7 +144,6 @@ impl ApubObject for ApubPost { if !page.is_mod_action(context).await? { verify_domains_match(page.id.inner(), expected_domain)?; }; - let ap_id = Some(page.id.clone().into()); let creator = page .attributed_to .dereference(context, request_counter) @@ -153,7 +152,7 @@ impl ApubObject for ApubPost { check_is_apub_id_valid(page.id.inner(), community.local, &context.settings())?; verify_person_in_community(&page.attributed_to, &community, context, request_counter).await?; - let thumbnail_url: Option = page.image.clone().map(|i| i.url); + let thumbnail_url: Option = page.image.map(|i| i.url); let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url { fetch_site_data(context.client(), &context.settings(), Some(url)).await } else { @@ -168,8 +167,8 @@ impl ApubObject for ApubPost { .as_ref() .map(|s| remove_slurs(&s.content, &context.settings().slur_regex())); let form = PostForm { - name: page.name.clone(), - url: page.url.clone().map(|u| u.into()), + name: page.name, + url: page.url.map(|u| u.into()), body: body_slurs_removed, creator_id: creator.id, community_id: community.id, @@ -184,7 +183,7 @@ impl ApubObject for ApubPost { embed_description, embed_html, thumbnail_url: pictrs_thumbnail.map(|u| u.into()), - ap_id, + ap_id: Some(page.id.into()), local: Some(false), }; let post = blocking(context.pool(), move |conn| Post::upsert(conn, &form)).await??; @@ -213,7 +212,7 @@ mod tests { let json = file_to_json_object("assets/lemmy/objects/page.json"); let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap(); let mut request_counter = 0; - let post = ApubPost::from_apub(&json, &context, &url, &mut request_counter) + let post = ApubPost::from_apub(json, &context, &url, &mut request_counter) .await .unwrap(); diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index 00b3a26c..334f37c5 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -70,7 +70,7 @@ impl ApubObject for ApubPrivateMessage { unimplemented!() } - async fn to_apub(&self, context: &LemmyContext) -> Result { + async fn into_apub(self, context: &LemmyContext) -> Result { let creator_id = self.creator_id; let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??; @@ -101,13 +101,13 @@ impl ApubObject for ApubPrivateMessage { } async fn from_apub( - note: &ChatMessage, + note: ChatMessage, context: &LemmyContext, expected_domain: &Url, request_counter: &mut i32, ) -> Result { verify_domains_match(note.id.inner(), expected_domain)?; - let ap_id = Some(note.id.clone().into()); + let ap_id = Some(note.id.into()); let creator = note .attributed_to .dereference(context, request_counter) @@ -123,8 +123,8 @@ impl ApubObject for ApubPrivateMessage { creator_id: creator.id, recipient_id: recipient.id, content, - published: note.published.map(|u| u.to_owned().naive_local()), - updated: note.updated.map(|u| u.to_owned().naive_local()), + published: note.published.map(|u| u.naive_local()), + updated: note.updated.map(|u| u.naive_local()), deleted: None, read: None, ap_id, @@ -150,12 +150,12 @@ mod tests { async fn prepare_comment_test(url: &Url, context: &LemmyContext) -> (ApubPerson, ApubPerson) { let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json"); - let person1 = ApubPerson::from_apub(&lemmy_person, context, url, &mut 0) + let person1 = ApubPerson::from_apub(lemmy_person, context, url, &mut 0) .await .unwrap(); let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json"); let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap(); - let person2 = ApubPerson::from_apub(&pleroma_person, context, &pleroma_url, &mut 0) + let person2 = ApubPerson::from_apub(pleroma_person, context, &pleroma_url, &mut 0) .await .unwrap(); (person1, person2) @@ -172,9 +172,9 @@ mod tests { let context = init_context(); let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap(); let data = prepare_comment_test(&url, &context).await; - let json = file_to_json_object("assets/lemmy/objects/chat_message.json"); + let json: ChatMessage = file_to_json_object("assets/lemmy/objects/chat_message.json"); let mut request_counter = 0; - let pm = ApubPrivateMessage::from_apub(&json, &context, &url, &mut request_counter) + let pm = ApubPrivateMessage::from_apub(json.clone(), &context, &url, &mut request_counter) .await .unwrap(); @@ -182,10 +182,11 @@ mod tests { assert_eq!(pm.content.len(), 20); assert_eq!(request_counter, 0); - let to_apub = pm.to_apub(&context).await.unwrap(); + let pm_id = pm.id; + let to_apub = pm.into_apub(&context).await.unwrap(); assert_json_include!(actual: json, expected: to_apub); - PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap(); + PrivateMessage::delete(&*context.pool().get().unwrap(), pm_id).unwrap(); cleanup(data, &context); } @@ -198,7 +199,7 @@ mod tests { let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap(); let json = file_to_json_object("assets/pleroma/objects/chat_message.json"); let mut request_counter = 0; - let pm = ApubPrivateMessage::from_apub(&json, &context, &pleroma_url, &mut request_counter) + let pm = ApubPrivateMessage::from_apub(json, &context, &pleroma_url, &mut request_counter) .await .unwrap(); diff --git a/crates/apub/src/protocol/objects/group.rs b/crates/apub/src/protocol/objects/group.rs index dbc82374..acaf5909 100644 --- a/crates/apub/src/protocol/objects/group.rs +++ b/crates/apub/src/protocol/objects/group.rs @@ -55,17 +55,17 @@ pub struct Group { } impl Group { - pub(crate) async fn from_apub_to_form( - group: &Group, + pub(crate) async fn into_form( + self, expected_domain: &Url, settings: &Settings, ) -> Result { - check_is_apub_id_valid(group.id.inner(), true, settings)?; - verify_domains_match(expected_domain, group.id.inner())?; - let name = group.preferred_username.clone(); - let title = group.name.clone(); - let description = get_summary_from_string_or_source(&group.summary, &group.source); - let shared_inbox = group.endpoints.shared_inbox.clone().map(|s| s.into()); + check_is_apub_id_valid(self.id.inner(), true, settings)?; + verify_domains_match(expected_domain, self.id.inner())?; + let name = self.preferred_username; + let title = self.name; + let description = get_summary_from_string_or_source(&self.summary, &self.source); + let shared_inbox = self.endpoints.shared_inbox.map(|s| s.into()); let slur_regex = &settings.slur_regex(); check_slurs(&name, slur_regex)?; @@ -78,19 +78,19 @@ impl Group { title, description, removed: None, - published: group.published.map(|u| u.naive_local()), - updated: group.updated.map(|u| u.naive_local()), + published: self.published.map(|u| u.naive_local()), + updated: self.updated.map(|u| u.naive_local()), deleted: None, - nsfw: Some(group.sensitive.unwrap_or(false)), - actor_id: Some(group.id.clone().into()), + nsfw: Some(self.sensitive.unwrap_or(false)), + actor_id: Some(self.id.into()), local: Some(false), private_key: None, - public_key: Some(group.public_key.public_key_pem.clone()), + public_key: Some(self.public_key.public_key_pem), last_refreshed_at: Some(naive_now()), - icon: Some(group.icon.clone().map(|i| i.url.into())), - banner: Some(group.image.clone().map(|i| i.url.into())), - followers_url: Some(group.followers.clone().into()), - inbox_url: Some(group.inbox.clone().into()), + icon: Some(self.icon.map(|i| i.url.into())), + banner: Some(self.image.map(|i| i.url.into())), + followers_url: Some(self.followers.into()), + inbox_url: Some(self.inbox.into()), shared_inbox_url: Some(shared_inbox), }) } diff --git a/crates/apub_lib/src/object_id.rs b/crates/apub_lib/src/object_id.rs index fe55a44a..52fe5b47 100644 --- a/crates/apub_lib/src/object_id.rs +++ b/crates/apub_lib/src/object_id.rs @@ -139,7 +139,7 @@ where let res2: Kind::ApubType = res.json().await?; - Ok(Kind::from_apub(&res2, data, self.inner(), request_counter).await?) + Ok(Kind::from_apub(res2, data, self.inner(), request_counter).await?) } } diff --git a/crates/apub_lib/src/traits.rs b/crates/apub_lib/src/traits.rs index 2d542408..38b2d685 100644 --- a/crates/apub_lib/src/traits.rs +++ b/crates/apub_lib/src/traits.rs @@ -41,7 +41,7 @@ pub trait ApubObject { async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError>; /// Trait for converting an object or actor into the respective ActivityPub type. - async fn to_apub(&self, data: &Self::DataType) -> Result; + async fn into_apub(self, data: &Self::DataType) -> Result; fn to_tombstone(&self) -> Result; /// Converts an object from ActivityPub type to Lemmy internal type. @@ -51,7 +51,7 @@ pub trait ApubObject { /// * `expected_domain` Domain where the object was received from. None in case of mod action. /// * `mod_action_allowed` True if the object can be a mod activity, ignore `expected_domain` in this case async fn from_apub( - apub: &Self::ApubType, + apub: Self::ApubType, data: &Self::DataType, expected_domain: &Url, request_counter: &mut i32,