X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fapub%2Fsrc%2Factivities%2Fdeletion%2Fdelete.rs;h=fa0a44aa38bcf1b7c2099fef7cc9a96435bf867a;hb=f7f6766650b9b573a72075e564aed353c0131cd7;hp=093919b88e5ce68692afa554396e87dbd4756ea2;hpb=28d6370c359d368882d9337d1b32ec2c14987a08;p=lemmy.git diff --git a/crates/apub/src/activities/deletion/delete.rs b/crates/apub/src/activities/deletion/delete.rs index 093919b8..fa0a44aa 100644 --- a/crates/apub/src/activities/deletion/delete.rs +++ b/crates/apub/src/activities/deletion/delete.rs @@ -1,98 +1,57 @@ use crate::{ activities::{ - community::announce::AnnouncableActivities, - deletion::{ - receive_delete_action, - verify_delete_activity, - DeletableObjects, - WebsocketMessages, - }, + deletion::{receive_delete_action, verify_delete_activity, DeletableObjects}, generate_activity_id, - verify_activity, }, - activity_queue::send_to_community_new, - extensions::context::lemmy_context, - fetcher::person::get_or_fetch_and_upsert_person, - ActorType, -}; -use activitystreams::activity::kind::DeleteType; -use anyhow::anyhow; -use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler}; -use lemmy_db_queries::{ - source::{comment::Comment_, community::Community_, post::Post_}, - Crud, + insert_received_activity, + objects::person::ApubPerson, + protocol::{activities::deletion::delete::Delete, IdOrNestedObject}, }; -use lemmy_db_schema::source::{ - comment::Comment, - community::Community, - moderator::{ - ModRemoveComment, - ModRemoveCommentForm, - ModRemoveCommunity, - ModRemoveCommunityForm, - ModRemovePost, - ModRemovePostForm, +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, + ModRemoveCommentForm, + ModRemoveCommunity, + ModRemoveCommunityForm, + ModRemovePost, + ModRemovePostForm, + }, + post::{Post, PostUpdateForm}, + post_report::PostReport, }, - person::Person, - post::Post, -}; -use lemmy_utils::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; -/// This is very confusing, because there are four distinct cases to handle: -/// - user deletes their post -/// - user deletes their comment -/// - remote community mod deletes local community -/// - remote community deletes itself (triggered by a mod) -/// -/// TODO: we should probably change how community deletions work to simplify this. Probably by -/// wrapping it in an announce just like other activities, instead of having the community send it. -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Delete { - pub(in crate::activities::deletion) to: PublicUrl, - pub(in crate::activities::deletion) object: Url, - pub(in crate::activities::deletion) cc: [Url; 1], - #[serde(rename = "type")] - pub(in crate::activities::deletion) kind: DeleteType, - /// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user - /// deleting their own content. - pub(in crate::activities::deletion) summary: Option, - #[serde(flatten)] - pub(in crate::activities::deletion) common: ActivityCommonFields, -} - -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] impl ActivityHandler for Delete { - async fn verify( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - verify_activity(self.common())?; - verify_delete_activity( - &self.object, - &self.cc[0], - &self.common, - self.summary.is_some(), - context, - request_counter, - ) - .await?; + type DataType = LemmyContext; + type Error = LemmyError; + + fn id(&self) -> &Url { + &self.id + } + + fn actor(&self) -> &Url { + self.actor.inner() + } + + #[tracing::instrument(skip_all)] + 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(()) } - async fn receive( - self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { + #[tracing::instrument(skip_all)] + 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. @@ -102,76 +61,58 @@ impl ActivityHandler for Delete { Some(reason) }; receive_remove_action( - &self.common.actor, - &self.object, + &self.actor.dereference(context).await?, + self.object.id(), reason, context, - request_counter, ) .await } else { - receive_delete_action( - &self.object, - &self.common.actor, - WebsocketMessages { - community: UserOperationCrud::DeleteCommunity, - post: UserOperationCrud::DeletePost, - comment: UserOperationCrud::DeleteComment, - }, - true, - context, - request_counter, - ) - .await + receive_delete_action(self.object.id(), &self.actor, true, context).await } } - - fn common(&self) -> &ActivityCommonFields { - &self.common - } } impl Delete { - pub(in crate::activities::deletion) async fn send( - actor: &Person, - community: &Community, - object_id: Url, + pub(in crate::activities::deletion) fn new( + actor: &ApubPerson, + object: DeletableObjects, + to: Url, + community: Option<&Community>, summary: Option, - context: &LemmyContext, - ) -> Result<(), LemmyError> { - let id = generate_activity_id(DeleteType::Delete)?; - let delete = Delete { - to: PublicUrl::Public, - object: object_id, - cc: [community.actor_id()], + context: &Data, + ) -> Result { + let id = generate_activity_id( + DeleteType::Delete, + &context.settings().get_protocol_and_hostname(), + )?; + let cc: Option = community.map(|c| c.actor_id.clone().into()); + Ok(Delete { + actor: actor.actor_id.clone().into(), + to: vec![to], + object: IdOrNestedObject::Id(object.id()), + cc: cc.into_iter().collect(), kind: DeleteType::Delete, summary, - common: ActivityCommonFields { - context: lemmy_context(), - id: id.clone(), - actor: actor.actor_id(), - unparsed: Default::default(), - }, - }; - - let activity = AnnouncableActivities::Delete(delete); - send_to_community_new(activity, &id, actor, community, vec![], context).await + id, + audience: community.map(|c| c.actor_id.clone().into()), + }) } } +#[tracing::instrument(skip_all)] pub(in crate::activities) async fn receive_remove_action( - actor: &Url, + actor: &ApubPerson, object: &Url, reason: Option, - context: &LemmyContext, - request_counter: &mut i32, + context: &Data, ) -> Result<(), LemmyError> { - let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?; - use UserOperationCrud::*; + let reason = sanitize_html_opt(&reason); + match DeletableObjects::read_from_db(object, context).await? { DeletableObjects::Community(community) => { if community.local { - return Err(anyhow!("Only local admin can remove community").into()); + return Err(LemmyErrorType::OnlyLocalAdminCanRemoveCommunity)?; } let form = ModRemoveCommunityForm { mod_person_id: actor.id, @@ -180,53 +121,56 @@ pub(in crate::activities) async fn receive_remove_action( reason, expires: None, }; - blocking(context.pool(), move |conn| { - ModRemoveCommunity::create(conn, &form) - }) - .await??; - let deleted_community = blocking(context.pool(), move |conn| { - Community::update_removed(conn, community.id, true) - }) - .await??; - - send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?; + ModRemoveCommunity::create(&mut context.pool(), &form).await?; + Community::update( + &mut context.pool(), + community.id, + &CommunityUpdateForm { + removed: Some(true), + ..Default::default() + }, + ) + .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, }; - blocking(context.pool(), move |conn| { - ModRemovePost::create(conn, &form) - }) - .await??; - let removed_post = blocking(context.pool(), move |conn| { - Post::update_removed(conn, post.id, true) - }) - .await??; - - send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?; + ModRemovePost::create(&mut context.pool(), &form).await?; + Post::update( + &mut context.pool(), + post.id, + &PostUpdateForm { + removed: Some(true), + ..Default::default() + }, + ) + .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, }; - blocking(context.pool(), move |conn| { - ModRemoveComment::create(conn, &form) - }) - .await??; - let removed_comment = blocking(context.pool(), move |conn| { - Comment::update_removed(conn, comment.id, true) - }) - .await??; - - send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?; + ModRemoveComment::create(&mut context.pool(), &form).await?; + Comment::update( + &mut context.pool(), + comment.id, + &CommentUpdateForm { + removed: Some(true), + ..Default::default() + }, + ) + .await?; } + DeletableObjects::PrivateMessage(_) => unimplemented!(), } Ok(()) }