3 deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
6 insert_received_activity,
7 objects::person::ApubPerson,
8 protocol::{activities::deletion::delete::Delete, IdOrNestedObject},
10 use activitypub_federation::{config::Data, kinds::activity::DeleteType, traits::ActivityHandler};
11 use lemmy_api_common::{context::LemmyContext, utils::sanitize_html_opt};
12 use lemmy_db_schema::{
14 comment::{Comment, CommentUpdateForm},
15 comment_report::CommentReport,
16 community::{Community, CommunityUpdateForm},
21 ModRemoveCommunityForm,
25 post::{Post, PostUpdateForm},
26 post_report::PostReport,
28 traits::{Crud, Reportable},
30 use lemmy_utils::error::{LemmyError, LemmyErrorType};
33 #[async_trait::async_trait]
34 impl ActivityHandler for Delete {
35 type DataType = LemmyContext;
36 type Error = LemmyError;
38 fn id(&self) -> &Url {
42 fn actor(&self) -> &Url {
46 #[tracing::instrument(skip_all)]
47 async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
48 insert_received_activity(&self.id, context).await?;
49 verify_delete_activity(self, self.summary.is_some(), context).await?;
53 #[tracing::instrument(skip_all)]
54 async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
55 if let Some(reason) = self.summary {
56 // We set reason to empty string if it doesn't exist, to distinguish between delete and
57 // remove. Here we change it back to option, so we don't write it to db.
58 let reason = if reason.is_empty() {
63 receive_remove_action(
64 &self.actor.dereference(context).await?,
71 receive_delete_action(self.object.id(), &self.actor, true, context).await
77 pub(in crate::activities::deletion) fn new(
79 object: DeletableObjects,
81 community: Option<&Community>,
82 summary: Option<String>,
83 context: &Data<LemmyContext>,
84 ) -> Result<Delete, LemmyError> {
85 let id = generate_activity_id(
87 &context.settings().get_protocol_and_hostname(),
89 let cc: Option<Url> = community.map(|c| c.actor_id.clone().into());
91 actor: actor.actor_id.clone().into(),
93 object: IdOrNestedObject::Id(object.id()),
94 cc: cc.into_iter().collect(),
95 kind: DeleteType::Delete,
98 audience: community.map(|c| c.actor_id.clone().into()),
103 #[tracing::instrument(skip_all)]
104 pub(in crate::activities) async fn receive_remove_action(
107 reason: Option<String>,
108 context: &Data<LemmyContext>,
109 ) -> Result<(), LemmyError> {
110 let reason = sanitize_html_opt(&reason);
112 match DeletableObjects::read_from_db(object, context).await? {
113 DeletableObjects::Community(community) => {
115 return Err(LemmyErrorType::OnlyLocalAdminCanRemoveCommunity)?;
117 let form = ModRemoveCommunityForm {
118 mod_person_id: actor.id,
119 community_id: community.id,
124 ModRemoveCommunity::create(&mut context.pool(), &form).await?;
128 &CommunityUpdateForm {
135 DeletableObjects::Post(post) => {
136 PostReport::resolve_all_for_object(&mut context.pool(), post.id, actor.id).await?;
137 let form = ModRemovePostForm {
138 mod_person_id: actor.id,
143 ModRemovePost::create(&mut context.pool(), &form).await?;
154 DeletableObjects::Comment(comment) => {
155 CommentReport::resolve_all_for_object(&mut context.pool(), comment.id, actor.id).await?;
156 let form = ModRemoveCommentForm {
157 mod_person_id: actor.id,
158 comment_id: comment.id,
162 ModRemoveComment::create(&mut context.pool(), &form).await?;
173 DeletableObjects::PrivateMessage(_) => unimplemented!(),