X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fapub%2Fsrc%2Factivities%2Fdeletion%2Fdelete.rs;h=fa0a44aa38bcf1b7c2099fef7cc9a96435bf867a;hb=f7f6766650b9b573a72075e564aed353c0131cd7;hp=0124f23313dec0a755e58933a7306695aeeaebdc;hpb=5d837780f5d149cb8d3b861c63a7dc4466a7cbf1;p=lemmy.git diff --git a/crates/apub/src/activities/deletion/delete.rs b/crates/apub/src/activities/deletion/delete.rs index 0124f233..fa0a44aa 100644 --- a/crates/apub/src/activities/deletion/delete.rs +++ b/crates/apub/src/activities/deletion/delete.rs @@ -1,19 +1,18 @@ use crate::{ activities::{ - community::announce::GetCommunity, deletion::{receive_delete_action, verify_delete_activity, DeletableObjects}, generate_activity_id, }, - local_instance, - objects::{community::ApubCommunity, person::ApubPerson}, + insert_received_activity, + objects::person::ApubPerson, protocol::{activities::deletion::delete::Delete, IdOrNestedObject}, }; -use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler}; -use activitystreams_kinds::activity::DeleteType; -use anyhow::anyhow; +use activitypub_federation::{config::Data, kinds::activity::DeleteType, traits::ActivityHandler}; +use lemmy_api_common::{context::LemmyContext, utils::sanitize_html_opt}; use lemmy_db_schema::{ source::{ comment::{Comment, CommentUpdateForm}, + comment_report::CommentReport, community::{Community, CommunityUpdateForm}, moderator::{ ModRemoveComment, @@ -24,18 +23,14 @@ use lemmy_db_schema::{ ModRemovePostForm, }, post::{Post, PostUpdateForm}, + post_report::PostReport, }, - traits::Crud, -}; -use lemmy_utils::error::LemmyError; -use lemmy_websocket::{ - send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message}, - LemmyContext, - UserOperationCrud, + traits::{Crud, Reportable}, }; +use lemmy_utils::error::{LemmyError, LemmyErrorType}; use url::Url; -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] impl ActivityHandler for Delete { type DataType = LemmyContext; type Error = LemmyError; @@ -49,21 +44,14 @@ impl ActivityHandler for Delete { } #[tracing::instrument(skip_all)] - async fn verify( - &self, - context: &Data, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - verify_delete_activity(self, self.summary.is_some(), context, request_counter).await?; + async fn verify(&self, context: &Data) -> Result<(), LemmyError> { + insert_received_activity(&self.id, context).await?; + verify_delete_activity(self, self.summary.is_some(), context).await?; Ok(()) } #[tracing::instrument(skip_all)] - async fn receive( - self, - context: &Data, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { + async fn receive(self, context: &Data) -> Result<(), LemmyError> { if let Some(reason) = self.summary { // We set reason to empty string if it doesn't exist, to distinguish between delete and // remove. Here we change it back to option, so we don't write it to db. @@ -73,24 +61,14 @@ impl ActivityHandler for Delete { Some(reason) }; receive_remove_action( - &self - .actor - .dereference(context, local_instance(context).await, request_counter) - .await?, + &self.actor.dereference(context).await?, self.object.id(), reason, context, ) .await } else { - receive_delete_action( - self.object.id(), - &self.actor, - true, - context, - request_counter, - ) - .await + receive_delete_action(self.object.id(), &self.actor, true, context).await } } } @@ -102,7 +80,7 @@ impl Delete { to: Url, community: Option<&Community>, summary: Option, - context: &LemmyContext, + context: &Data, ) -> Result { let id = generate_activity_id( DeleteType::Delete, @@ -110,14 +88,14 @@ impl Delete { )?; let cc: Option = community.map(|c| c.actor_id.clone().into()); Ok(Delete { - actor: ObjectId::new(actor.actor_id.clone()), + actor: actor.actor_id.clone().into(), to: vec![to], object: IdOrNestedObject::Id(object.id()), cc: cc.into_iter().collect(), kind: DeleteType::Delete, summary, id, - unparsed: Default::default(), + audience: community.map(|c| c.actor_id.clone().into()), }) } } @@ -127,15 +105,14 @@ pub(in crate::activities) async fn receive_remove_action( actor: &ApubPerson, object: &Url, reason: Option, - context: &LemmyContext, + context: &Data, ) -> Result<(), LemmyError> { - use UserOperationCrud::*; + let reason = sanitize_html_opt(&reason); + match DeletableObjects::read_from_db(object, context).await? { DeletableObjects::Community(community) => { if community.local { - return Err(LemmyError::from_message( - "Only local admin can remove community", - )); + return Err(LemmyErrorType::OnlyLocalAdminCanRemoveCommunity)?; } let form = ModRemoveCommunityForm { mod_person_id: actor.id, @@ -144,75 +121,56 @@ pub(in crate::activities) async fn receive_remove_action( reason, expires: None, }; - ModRemoveCommunity::create(context.pool(), &form).await?; - let deleted_community = Community::update( - context.pool(), + ModRemoveCommunity::create(&mut context.pool(), &form).await?; + Community::update( + &mut context.pool(), community.id, - &CommunityUpdateForm::builder().removed(Some(true)).build(), + &CommunityUpdateForm { + removed: Some(true), + ..Default::default() + }, ) .await?; - - send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?; } DeletableObjects::Post(post) => { + PostReport::resolve_all_for_object(&mut context.pool(), post.id, actor.id).await?; let form = ModRemovePostForm { mod_person_id: actor.id, post_id: post.id, removed: Some(true), reason, }; - ModRemovePost::create(context.pool(), &form).await?; - let removed_post = Post::update( - context.pool(), + ModRemovePost::create(&mut context.pool(), &form).await?; + Post::update( + &mut context.pool(), post.id, - &PostUpdateForm::builder().removed(Some(true)).build(), + &PostUpdateForm { + removed: Some(true), + ..Default::default() + }, ) .await?; - - send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?; } DeletableObjects::Comment(comment) => { + CommentReport::resolve_all_for_object(&mut context.pool(), comment.id, actor.id).await?; let form = ModRemoveCommentForm { mod_person_id: actor.id, comment_id: comment.id, removed: Some(true), reason, }; - ModRemoveComment::create(context.pool(), &form).await?; - let removed_comment = Comment::update( - context.pool(), + ModRemoveComment::create(&mut context.pool(), &form).await?; + Comment::update( + &mut context.pool(), comment.id, - &CommentUpdateForm::builder().removed(Some(true)).build(), + &CommentUpdateForm { + removed: Some(true), + ..Default::default() + }, ) .await?; - - send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?; } DeletableObjects::PrivateMessage(_) => unimplemented!(), } Ok(()) } - -#[async_trait::async_trait(?Send)] -impl GetCommunity for Delete { - #[tracing::instrument(skip_all)] - async fn get_community( - &self, - context: &LemmyContext, - _request_counter: &mut i32, - ) -> Result { - let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? { - DeletableObjects::Community(c) => c.id, - DeletableObjects::Comment(c) => { - let post = Post::read(context.pool(), c.post_id).await?; - post.community_id - } - DeletableObjects::Post(p) => p.community_id, - DeletableObjects::PrivateMessage(_) => { - return Err(anyhow!("Private message is not part of community").into()) - } - }; - let community = Community::read(context.pool(), community_id).await?; - Ok(community.into()) - } -}