]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/create_or_update/comment.rs
Add logging to debug federation issues (ref #2096) (#2099)
[lemmy.git] / crates / apub / src / activities / create_or_update / comment.rs
1 use crate::{
2   activities::{
3     check_community_deleted_or_removed,
4     community::{announce::GetCommunity, send_activity_in_community},
5     create_or_update::get_comment_notif_recipients,
6     generate_activity_id,
7     verify_activity,
8     verify_is_public,
9     verify_person_in_community,
10   },
11   activity_lists::AnnouncableActivities,
12   objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
13   protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
14 };
15 use activitystreams_kinds::public;
16 use lemmy_api_common::{blocking, check_post_deleted_or_removed};
17 use lemmy_apub_lib::{
18   data::Data,
19   object_id::ObjectId,
20   traits::{ActivityHandler, ActorType, ApubObject},
21   verify::verify_domains_match,
22 };
23 use lemmy_db_schema::{
24   source::{community::Community, post::Post},
25   traits::Crud,
26 };
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
29 use tracing::info;
30
31 impl CreateOrUpdateComment {
32   #[tracing::instrument(skip(comment, actor, kind, context))]
33   pub async fn send(
34     comment: ApubComment,
35     actor: &ApubPerson,
36     kind: CreateOrUpdateType,
37     context: &LemmyContext,
38     request_counter: &mut i32,
39   ) -> Result<(), LemmyError> {
40     // TODO: might be helpful to add a comment method to retrieve community directly
41     let post_id = comment.post_id;
42     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
43     let community_id = post.community_id;
44     let community: ApubCommunity = blocking(context.pool(), move |conn| {
45       Community::read(conn, community_id)
46     })
47     .await??
48     .into();
49
50     let id = generate_activity_id(
51       kind.clone(),
52       &context.settings().get_protocol_and_hostname(),
53     )?;
54     info!("Sending Create/Comment for {} as {}", comment.ap_id, id);
55     let note = comment.into_apub(context).await?;
56
57     let create_or_update = CreateOrUpdateComment {
58       actor: ObjectId::new(actor.actor_id()),
59       to: vec![public()],
60       cc: note.cc.clone(),
61       tag: note.tag.clone(),
62       object: note,
63       kind,
64       id: id.clone(),
65       unparsed: Default::default(),
66     };
67
68     let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
69       .tag
70       .iter()
71       .map(|t| t.href.clone())
72       .map(ObjectId::new)
73       .collect();
74     let mut inboxes = vec![];
75     for t in tagged_users {
76       let person = t
77         .dereference(context, context.client(), request_counter)
78         .await?;
79       inboxes.push(person.shared_inbox_or_inbox_url());
80     }
81
82     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
83     send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
84   }
85 }
86
87 #[async_trait::async_trait(?Send)]
88 impl ActivityHandler for CreateOrUpdateComment {
89   type DataType = LemmyContext;
90
91   #[tracing::instrument(skip_all)]
92   async fn verify(
93     &self,
94     context: &Data<LemmyContext>,
95     request_counter: &mut i32,
96   ) -> Result<(), LemmyError> {
97     verify_is_public(&self.to, &self.cc)?;
98     let post = self.object.get_parents(context, request_counter).await?.0;
99     let community = self.get_community(context, request_counter).await?;
100
101     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
102     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
103     verify_domains_match(self.actor.inner(), self.object.id.inner())?;
104     check_community_deleted_or_removed(&community)?;
105     check_post_deleted_or_removed(&post)?;
106
107     ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
108     Ok(())
109   }
110
111   #[tracing::instrument(skip_all)]
112   async fn receive(
113     self,
114     context: &Data<LemmyContext>,
115     request_counter: &mut i32,
116   ) -> Result<(), LemmyError> {
117     let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
118     let do_send_email = self.kind == CreateOrUpdateType::Create;
119     let recipients = get_comment_notif_recipients(
120       &self.actor,
121       &comment,
122       do_send_email,
123       context,
124       request_counter,
125     )
126     .await?;
127     let notif_type = match self.kind {
128       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
129       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
130     };
131     send_comment_ws_message(
132       comment.id, notif_type, None, None, None, recipients, context,
133     )
134     .await?;
135     Ok(())
136   }
137 }
138
139 #[async_trait::async_trait(?Send)]
140 impl GetCommunity for CreateOrUpdateComment {
141   #[tracing::instrument(skip_all)]
142   async fn get_community(
143     &self,
144     context: &LemmyContext,
145     request_counter: &mut i32,
146   ) -> Result<ApubCommunity, LemmyError> {
147     let post = self.object.get_parents(context, request_counter).await?.0;
148     let community = blocking(context.pool(), move |conn| {
149       Community::read(conn, post.community_id)
150     })
151     .await??;
152     Ok(community.into())
153   }
154 }