3 check_community_deleted_or_removed,
4 community::{announce::GetCommunity, send_activity_in_community},
5 create_or_update::get_comment_notif_recipients,
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
12 fetch_local_site_data,
14 mentions::MentionOrValue,
15 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
16 protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
19 use activitypub_federation::{
20 core::object_id::ObjectId,
22 traits::{ActivityHandler, Actor, ApubObject},
23 utils::verify_domains_match,
25 use activitystreams_kinds::public;
26 use lemmy_api_common::utils::{blocking, check_post_deleted_or_removed};
27 use lemmy_db_schema::{
29 comment::{CommentLike, CommentLikeForm},
33 traits::{Crud, Likeable},
35 use lemmy_utils::error::LemmyError;
36 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
39 impl CreateOrUpdateComment {
40 #[tracing::instrument(skip(comment, actor, kind, context))]
44 kind: CreateOrUpdateType,
45 context: &LemmyContext,
46 request_counter: &mut i32,
47 ) -> Result<(), LemmyError> {
48 // TODO: might be helpful to add a comment method to retrieve community directly
49 let post_id = comment.post_id;
50 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
51 let community_id = post.community_id;
52 let community: ApubCommunity = blocking(context.pool(), move |conn| {
53 Community::read(conn, community_id)
58 let id = generate_activity_id(
60 &context.settings().get_protocol_and_hostname(),
62 let note = comment.into_apub(context).await?;
64 let create_or_update = CreateOrUpdateComment {
65 actor: ObjectId::new(actor.actor_id()),
68 tag: note.tag.clone(),
72 unparsed: Default::default(),
75 let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
79 if let MentionOrValue::Mention(t) = t {
85 .map(|t| t.href.clone())
88 let mut inboxes = vec![];
89 for t in tagged_users {
91 .dereference(context, local_instance(context), request_counter)
93 inboxes.push(person.shared_inbox_or_inbox());
96 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
97 send_activity_in_community(activity, actor, &community, inboxes, context).await
101 #[async_trait::async_trait(?Send)]
102 impl ActivityHandler for CreateOrUpdateComment {
103 type DataType = LemmyContext;
104 type Error = LemmyError;
106 fn id(&self) -> &Url {
110 fn actor(&self) -> &Url {
114 #[tracing::instrument(skip_all)]
117 context: &Data<LemmyContext>,
118 request_counter: &mut i32,
119 ) -> Result<(), LemmyError> {
120 let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
121 check_apub_id_valid(self.id(), &local_site_data, context.settings())
122 .map_err(LemmyError::from_message)?;
124 verify_is_public(&self.to, &self.cc)?;
125 let post = self.object.get_parents(context, request_counter).await?.0;
126 let community = self.get_community(context, request_counter).await?;
128 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
129 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
130 check_community_deleted_or_removed(&community)?;
131 check_post_deleted_or_removed(&post)?;
133 ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
137 #[tracing::instrument(skip_all)]
140 context: &Data<LemmyContext>,
141 request_counter: &mut i32,
142 ) -> Result<(), LemmyError> {
143 let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
145 // author likes their own comment by default
146 let like_form = CommentLikeForm {
147 comment_id: comment.id,
148 post_id: comment.post_id,
149 person_id: comment.creator_id,
152 blocking(context.pool(), move |conn: &mut _| {
153 CommentLike::like(conn, &like_form)
157 let do_send_email = self.kind == CreateOrUpdateType::Create;
158 let recipients = get_comment_notif_recipients(
166 let notif_type = match self.kind {
167 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
168 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
170 send_comment_ws_message(
171 comment.id, notif_type, None, None, None, recipients, context,
178 #[async_trait::async_trait(?Send)]
179 impl GetCommunity for CreateOrUpdateComment {
180 #[tracing::instrument(skip_all)]
181 async fn get_community(
183 context: &LemmyContext,
184 request_counter: &mut i32,
185 ) -> Result<ApubCommunity, LemmyError> {
186 let post = self.object.get_parents(context, request_counter).await?.0;
187 let community = blocking(context.pool(), move |conn| {
188 Community::read(conn, post.community_id)