3 check_community_deleted_or_removed,
4 community::{announce::GetCommunity, send_activity_in_community},
5 create_or_update::get_comment_notif_recipients,
9 verify_person_in_community,
11 activity_lists::AnnouncableActivities,
12 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
13 protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
15 use activitystreams_kinds::public;
16 use lemmy_api_common::{blocking, check_post_deleted_or_removed};
20 traits::{ActivityHandler, ActorType, ApubObject},
21 verify::verify_domains_match,
23 use lemmy_db_schema::{
25 comment::{CommentLike, CommentLikeForm},
29 traits::{Crud, Likeable},
31 use lemmy_utils::LemmyError;
32 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
34 impl CreateOrUpdateComment {
35 #[tracing::instrument(skip(comment, actor, kind, context))]
39 kind: CreateOrUpdateType,
40 context: &LemmyContext,
41 request_counter: &mut i32,
42 ) -> Result<(), LemmyError> {
43 // TODO: might be helpful to add a comment method to retrieve community directly
44 let post_id = comment.post_id;
45 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
46 let community_id = post.community_id;
47 let community: ApubCommunity = blocking(context.pool(), move |conn| {
48 Community::read(conn, community_id)
53 let id = generate_activity_id(
55 &context.settings().get_protocol_and_hostname(),
57 let note = comment.into_apub(context).await?;
59 let create_or_update = CreateOrUpdateComment {
60 actor: ObjectId::new(actor.actor_id()),
63 tag: note.tag.clone(),
67 unparsed: Default::default(),
70 let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
73 .map(|t| t.href.clone())
76 let mut inboxes = vec![];
77 for t in tagged_users {
79 .dereference(context, context.client(), request_counter)
81 inboxes.push(person.shared_inbox_or_inbox_url());
84 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
85 send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
89 #[async_trait::async_trait(?Send)]
90 impl ActivityHandler for CreateOrUpdateComment {
91 type DataType = LemmyContext;
93 #[tracing::instrument(skip_all)]
96 context: &Data<LemmyContext>,
97 request_counter: &mut i32,
98 ) -> Result<(), LemmyError> {
99 verify_is_public(&self.to, &self.cc)?;
100 let post = self.object.get_parents(context, request_counter).await?.0;
101 let community = self.get_community(context, request_counter).await?;
103 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
104 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
105 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
106 check_community_deleted_or_removed(&community)?;
107 check_post_deleted_or_removed(&post)?;
109 ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
113 #[tracing::instrument(skip_all)]
116 context: &Data<LemmyContext>,
117 request_counter: &mut i32,
118 ) -> Result<(), LemmyError> {
119 let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
121 // author likes their own comment by default
122 let like_form = CommentLikeForm {
123 comment_id: comment.id,
124 post_id: comment.post_id,
125 person_id: comment.creator_id,
128 blocking(context.pool(), move |conn: &'_ _| {
129 CommentLike::like(conn, &like_form)
133 let do_send_email = self.kind == CreateOrUpdateType::Create;
134 let recipients = get_comment_notif_recipients(
142 let notif_type = match self.kind {
143 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
144 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
146 send_comment_ws_message(
147 comment.id, notif_type, None, None, None, recipients, context,
154 #[async_trait::async_trait(?Send)]
155 impl GetCommunity for CreateOrUpdateComment {
156 #[tracing::instrument(skip_all)]
157 async fn get_community(
159 context: &LemmyContext,
160 request_counter: &mut i32,
161 ) -> Result<ApubCommunity, LemmyError> {
162 let post = self.object.get_parents(context, request_counter).await?.0;
163 let community = blocking(context.pool(), move |conn| {
164 Community::read(conn, post.community_id)