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 mentions::MentionOrValue,
13 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
14 protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
17 use activitypub_federation::{
18 core::object_id::ObjectId,
20 traits::{ActivityHandler, Actor, ApubObject},
21 utils::verify_domains_match,
23 use activitystreams_kinds::public;
24 use lemmy_api_common::utils::check_post_deleted_or_removed;
25 use lemmy_db_schema::{
27 comment::{CommentLike, CommentLikeForm},
31 traits::{Crud, Likeable},
33 use lemmy_utils::error::LemmyError;
34 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
37 impl CreateOrUpdateComment {
38 #[tracing::instrument(skip(comment, actor, kind, context))]
42 kind: CreateOrUpdateType,
43 context: &LemmyContext,
44 request_counter: &mut i32,
45 ) -> Result<(), LemmyError> {
46 // TODO: might be helpful to add a comment method to retrieve community directly
47 let post_id = comment.post_id;
48 let post = Post::read(context.pool(), post_id).await?;
49 let community_id = post.community_id;
50 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
52 let id = generate_activity_id(
54 &context.settings().get_protocol_and_hostname(),
56 let note = comment.into_apub(context).await?;
58 let create_or_update = CreateOrUpdateComment {
59 actor: ObjectId::new(actor.actor_id()),
62 tag: note.tag.clone(),
68 let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
72 if let MentionOrValue::Mention(t) = t {
78 .map(|t| t.href.clone())
81 let mut inboxes = vec![];
82 for t in tagged_users {
84 .dereference(context, local_instance(context).await, request_counter)
86 inboxes.push(person.shared_inbox_or_inbox());
89 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
90 send_activity_in_community(activity, actor, &community, inboxes, context).await
94 #[async_trait::async_trait(?Send)]
95 impl ActivityHandler for CreateOrUpdateComment {
96 type DataType = LemmyContext;
97 type Error = LemmyError;
99 fn id(&self) -> &Url {
103 fn actor(&self) -> &Url {
107 #[tracing::instrument(skip_all)]
110 context: &Data<LemmyContext>,
111 request_counter: &mut i32,
112 ) -> Result<(), LemmyError> {
113 verify_is_public(&self.to, &self.cc)?;
114 let post = self.object.get_parents(context, request_counter).await?.0;
115 let community = self.get_community(context, request_counter).await?;
117 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
118 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
119 check_community_deleted_or_removed(&community)?;
120 check_post_deleted_or_removed(&post)?;
122 ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
126 #[tracing::instrument(skip_all)]
129 context: &Data<LemmyContext>,
130 request_counter: &mut i32,
131 ) -> Result<(), LemmyError> {
132 let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
134 // author likes their own comment by default
135 let like_form = CommentLikeForm {
136 comment_id: comment.id,
137 post_id: comment.post_id,
138 person_id: comment.creator_id,
141 CommentLike::like(context.pool(), &like_form).await?;
143 let do_send_email = self.kind == CreateOrUpdateType::Create;
144 let recipients = get_comment_notif_recipients(
152 let notif_type = match self.kind {
153 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
154 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
156 send_comment_ws_message(
157 comment.id, notif_type, None, None, None, recipients, context,
164 #[async_trait::async_trait(?Send)]
165 impl GetCommunity for CreateOrUpdateComment {
166 #[tracing::instrument(skip_all)]
167 async fn get_community(
169 context: &LemmyContext,
170 request_counter: &mut i32,
171 ) -> Result<ApubCommunity, LemmyError> {
172 let post = self.object.get_parents(context, request_counter).await?.0;
173 let community = Community::read(context.pool(), post.community_id).await?;