]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/report.rs
Replace TypedBuilder with Default in update forms (#3814)
[lemmy.git] / crates / apub / src / activities / community / report.rs
1 use crate::{
2   activities::{generate_activity_id, send_lemmy_activity, verify_person_in_community},
3   insert_received_activity,
4   objects::{community::ApubCommunity, person::ApubPerson},
5   protocol::{activities::community::report::Report, InCommunity},
6   PostOrComment,
7 };
8 use activitypub_federation::{
9   config::Data,
10   fetch::object_id::ObjectId,
11   kinds::activity::FlagType,
12   traits::{ActivityHandler, Actor},
13 };
14 use lemmy_api_common::{context::LemmyContext, utils::sanitize_html};
15 use lemmy_db_schema::{
16   source::{
17     comment_report::{CommentReport, CommentReportForm},
18     community::Community,
19     person::Person,
20     post_report::{PostReport, PostReportForm},
21   },
22   traits::Reportable,
23 };
24 use lemmy_utils::error::LemmyError;
25 use url::Url;
26
27 impl Report {
28   #[tracing::instrument(skip_all)]
29   pub(crate) async fn send(
30     object_id: ObjectId<PostOrComment>,
31     actor: Person,
32     community: Community,
33     reason: String,
34     context: Data<LemmyContext>,
35   ) -> Result<(), LemmyError> {
36     let actor: ApubPerson = actor.into();
37     let community: ApubCommunity = community.into();
38     let kind = FlagType::Flag;
39     let id = generate_activity_id(
40       kind.clone(),
41       &context.settings().get_protocol_and_hostname(),
42     )?;
43     let report = Report {
44       actor: actor.id().into(),
45       to: [community.id().into()],
46       object: object_id,
47       summary: reason,
48       kind,
49       id: id.clone(),
50       audience: Some(community.id().into()),
51     };
52
53     let inbox = vec![community.shared_inbox_or_inbox()];
54     send_lemmy_activity(&context, report, &actor, inbox, false).await
55   }
56 }
57
58 #[async_trait::async_trait]
59 impl ActivityHandler for Report {
60   type DataType = LemmyContext;
61   type Error = LemmyError;
62
63   fn id(&self) -> &Url {
64     &self.id
65   }
66
67   fn actor(&self) -> &Url {
68     self.actor.inner()
69   }
70
71   #[tracing::instrument(skip_all)]
72   async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
73     insert_received_activity(&self.id, context).await?;
74     let community = self.community(context).await?;
75     verify_person_in_community(&self.actor, &community, context).await?;
76     Ok(())
77   }
78
79   #[tracing::instrument(skip_all)]
80   async fn receive(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
81     let actor = self.actor.dereference(context).await?;
82     match self.object.dereference(context).await? {
83       PostOrComment::Post(post) => {
84         let report_form = PostReportForm {
85           creator_id: actor.id,
86           post_id: post.id,
87           original_post_name: post.name.clone(),
88           original_post_url: post.url.clone(),
89           reason: sanitize_html(&self.summary),
90           original_post_body: post.body.clone(),
91         };
92         PostReport::report(&mut context.pool(), &report_form).await?;
93       }
94       PostOrComment::Comment(comment) => {
95         let report_form = CommentReportForm {
96           creator_id: actor.id,
97           comment_id: comment.id,
98           original_comment_text: comment.content.clone(),
99           reason: sanitize_html(&self.summary),
100         };
101         CommentReport::report(&mut context.pool(), &report_form).await?;
102       }
103     };
104     Ok(())
105   }
106 }