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 mentions::MentionOrValue,
13 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
14 protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
16 use activitystreams_kinds::public;
17 use lemmy_api_common::{blocking, check_post_deleted_or_removed};
21 traits::{ActivityHandler, ActorType, ApubObject},
22 verify::verify_domains_match,
24 use lemmy_db_schema::{
26 comment::{CommentLike, CommentLikeForm},
30 traits::{Crud, Likeable},
32 use lemmy_utils::LemmyError;
33 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
35 impl CreateOrUpdateComment {
36 #[tracing::instrument(skip(comment, actor, kind, context))]
40 kind: CreateOrUpdateType,
41 context: &LemmyContext,
42 request_counter: &mut i32,
43 ) -> Result<(), LemmyError> {
44 // TODO: might be helpful to add a comment method to retrieve community directly
45 let post_id = comment.post_id;
46 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
47 let community_id = post.community_id;
48 let community: ApubCommunity = blocking(context.pool(), move |conn| {
49 Community::read(conn, community_id)
54 let id = generate_activity_id(
56 &context.settings().get_protocol_and_hostname(),
58 let note = comment.into_apub(context).await?;
60 let create_or_update = CreateOrUpdateComment {
61 actor: ObjectId::new(actor.actor_id()),
64 tag: note.tag.clone(),
68 unparsed: Default::default(),
71 let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
75 if let MentionOrValue::Mention(t) = t {
81 .map(|t| t.href.clone())
84 let mut inboxes = vec![];
85 for t in tagged_users {
87 .dereference(context, context.client(), request_counter)
89 inboxes.push(person.shared_inbox_or_inbox_url());
92 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
93 send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
97 #[async_trait::async_trait(?Send)]
98 impl ActivityHandler for CreateOrUpdateComment {
99 type DataType = LemmyContext;
101 #[tracing::instrument(skip_all)]
104 context: &Data<LemmyContext>,
105 request_counter: &mut i32,
106 ) -> Result<(), LemmyError> {
107 verify_is_public(&self.to, &self.cc)?;
108 let post = self.object.get_parents(context, request_counter).await?.0;
109 let community = self.get_community(context, request_counter).await?;
111 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
112 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
113 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
114 check_community_deleted_or_removed(&community)?;
115 check_post_deleted_or_removed(&post)?;
117 ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
121 #[tracing::instrument(skip_all)]
124 context: &Data<LemmyContext>,
125 request_counter: &mut i32,
126 ) -> Result<(), LemmyError> {
127 let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
129 // author likes their own comment by default
130 let like_form = CommentLikeForm {
131 comment_id: comment.id,
132 post_id: comment.post_id,
133 person_id: comment.creator_id,
136 blocking(context.pool(), move |conn: &'_ _| {
137 CommentLike::like(conn, &like_form)
141 let do_send_email = self.kind == CreateOrUpdateType::Create;
142 let recipients = get_comment_notif_recipients(
150 let notif_type = match self.kind {
151 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
152 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
154 send_comment_ws_message(
155 comment.id, notif_type, None, None, None, recipients, context,
162 #[async_trait::async_trait(?Send)]
163 impl GetCommunity for CreateOrUpdateComment {
164 #[tracing::instrument(skip_all)]
165 async fn get_community(
167 context: &LemmyContext,
168 request_counter: &mut i32,
169 ) -> Result<ApubCommunity, LemmyError> {
170 let post = self.object.get_parents(context, request_counter).await?.0;
171 let community = blocking(context.pool(), move |conn| {
172 Community::read(conn, post.community_id)