X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fapub%2Fsrc%2Factivities%2Fcreate_or_update%2Fcomment.rs;h=51b87ed27fc2271230f434973f008c327937d721;hb=e9e76549a88cfbdab36f00d302cceabcaaa24f4c;hp=e367bec9a4720aeaea2c7c4817af113d9a3daba7;hpb=a0fed24ceeb7961e9e16045cdca55f3f756edaae;p=lemmy.git diff --git a/crates/apub/src/activities/create_or_update/comment.rs b/crates/apub/src/activities/create_or_update/comment.rs index e367bec9..51b87ed2 100644 --- a/crates/apub/src/activities/create_or_update/comment.rs +++ b/crates/apub/src/activities/create_or_update/comment.rs @@ -1,68 +1,118 @@ use crate::{ activities::{ check_community_deleted_or_removed, - community::{announce::GetCommunity, send_activity_in_community}, - create_or_update::get_comment_notif_recipients, + community::send_activity_in_community, generate_activity_id, verify_is_public, verify_person_in_community, }, activity_lists::AnnouncableActivities, - local_instance, + insert_received_activity, mentions::MentionOrValue, objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson}, - protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType}, - ActorType, + protocol::{ + activities::{create_or_update::note::CreateOrUpdateNote, CreateOrUpdateType}, + InCommunity, + }, + SendActivity, }; use activitypub_federation::{ - core::object_id::ObjectId, - data::Data, - traits::{ActivityHandler, Actor, ApubObject}, - utils::verify_domains_match, + config::Data, + fetch::object_id::ObjectId, + kinds::public, + protocol::verification::verify_domains_match, + traits::{ActivityHandler, Actor, Object}, +}; +use lemmy_api_common::{ + build_response::send_local_notifs, + comment::{CommentResponse, CreateComment, EditComment}, + context::LemmyContext, + utils::{check_post_deleted_or_removed, is_mod_or_admin}, }; -use activitystreams_kinds::public; -use lemmy_api_common::utils::check_post_deleted_or_removed; use lemmy_db_schema::{ + aggregates::structs::CommentAggregates, + newtypes::PersonId, source::{ - comment::{CommentLike, CommentLikeForm}, + comment::{Comment, CommentLike, CommentLikeForm}, community::Community, + person::Person, post::Post, }, traits::{Crud, Likeable}, }; -use lemmy_utils::error::LemmyError; -use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud}; +use lemmy_utils::{error::LemmyError, utils::mention::scrape_text_for_mentions}; use url::Url; -impl CreateOrUpdateComment { - #[tracing::instrument(skip(comment, actor, kind, context))] - pub async fn send( - comment: ApubComment, - actor: &ApubPerson, +#[async_trait::async_trait] +impl SendActivity for CreateComment { + type Response = CommentResponse; + + async fn send_activity( + _request: &Self, + response: &Self::Response, + context: &Data, + ) -> Result<(), LemmyError> { + CreateOrUpdateNote::send( + &response.comment_view.comment, + response.comment_view.creator.id, + CreateOrUpdateType::Create, + context, + ) + .await + } +} + +#[async_trait::async_trait] +impl SendActivity for EditComment { + type Response = CommentResponse; + + async fn send_activity( + _request: &Self, + response: &Self::Response, + context: &Data, + ) -> Result<(), LemmyError> { + CreateOrUpdateNote::send( + &response.comment_view.comment, + response.comment_view.creator.id, + CreateOrUpdateType::Update, + context, + ) + .await + } +} + +impl CreateOrUpdateNote { + #[tracing::instrument(skip(comment, person_id, kind, context))] + async fn send( + comment: &Comment, + person_id: PersonId, kind: CreateOrUpdateType, - context: &LemmyContext, - request_counter: &mut i32, + context: &Data, ) -> Result<(), LemmyError> { // TODO: might be helpful to add a comment method to retrieve community directly let post_id = comment.post_id; - let post = Post::read(context.pool(), post_id).await?; + let post = Post::read(&mut context.pool(), post_id).await?; let community_id = post.community_id; - let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into(); + let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into(); + let community: ApubCommunity = Community::read(&mut context.pool(), community_id) + .await? + .into(); let id = generate_activity_id( kind.clone(), &context.settings().get_protocol_and_hostname(), )?; - let note = comment.into_apub(context).await?; + let note = ApubComment(comment.clone()).into_json(context).await?; - let create_or_update = CreateOrUpdateComment { - actor: ObjectId::new(actor.actor_id()), + let create_or_update = CreateOrUpdateNote { + actor: person.id().into(), to: vec![public()], cc: note.cc.clone(), tag: note.tag.clone(), object: note, kind, id: id.clone(), + audience: Some(community.id().into()), }; let tagged_users: Vec> = create_or_update @@ -76,23 +126,21 @@ impl CreateOrUpdateComment { } }) .map(|t| t.href.clone()) - .map(ObjectId::new) + .map(ObjectId::from) .collect(); let mut inboxes = vec![]; for t in tagged_users { - let person = t - .dereference(context, local_instance(context).await, request_counter) - .await?; + let person = t.dereference(context).await?; inboxes.push(person.shared_inbox_or_inbox()); } let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update); - send_activity_in_community(activity, actor, &community, inboxes, context).await + send_activity_in_community(activity, &person, &community, inboxes, false, context).await } } -#[async_trait::async_trait(?Send)] -impl ActivityHandler for CreateOrUpdateComment { +#[async_trait::async_trait] +impl ActivityHandler for CreateOrUpdateNote { type DataType = LemmyContext; type Error = LemmyError; @@ -105,31 +153,37 @@ impl ActivityHandler for CreateOrUpdateComment { } #[tracing::instrument(skip_all)] - async fn verify( - &self, - context: &Data, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { + async fn verify(&self, context: &Data) -> Result<(), LemmyError> { + insert_received_activity(&self.id, context).await?; verify_is_public(&self.to, &self.cc)?; - let post = self.object.get_parents(context, request_counter).await?.0; - let community = self.get_community(context, request_counter).await?; + let post = self.object.get_parents(context).await?.0; + let community = self.community(context).await?; - verify_person_in_community(&self.actor, &community, context, request_counter).await?; + verify_person_in_community(&self.actor, &community, context).await?; verify_domains_match(self.actor.inner(), self.object.id.inner())?; check_community_deleted_or_removed(&community)?; check_post_deleted_or_removed(&post)?; - ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?; + ApubComment::verify(&self.object, self.actor.inner(), context).await?; Ok(()) } #[tracing::instrument(skip_all)] - async fn receive( - self, - context: &Data, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - let comment = ApubComment::from_apub(self.object, context, request_counter).await?; + async fn receive(self, context: &Data) -> Result<(), LemmyError> { + // Need to do this check here instead of Note::from_json because we need the person who + // send the activity, not the comment author. + let existing_comment = self.object.id.dereference_local(context).await.ok(); + if let (Some(distinguished), Some(existing_comment)) = + (self.object.distinguished, existing_comment) + { + if distinguished != existing_comment.distinguished { + let creator = self.actor.dereference(context).await?; + let (post, _) = self.object.get_parents(context).await?; + is_mod_or_admin(&mut context.pool(), creator.id, post.community_id).await?; + } + } + + let comment = ApubComment::from_json(self.object, context).await?; // author likes their own comment by default let like_form = CommentLikeForm { @@ -138,39 +192,23 @@ impl ActivityHandler for CreateOrUpdateComment { person_id: comment.creator_id, score: 1, }; - CommentLike::like(context.pool(), &like_form).await?; + CommentLike::like(&mut context.pool(), &like_form).await?; + + // Calculate initial hot_rank + CommentAggregates::update_hot_rank(&mut context.pool(), comment.id).await?; let do_send_email = self.kind == CreateOrUpdateType::Create; - let recipients = get_comment_notif_recipients( - &self.actor, - &comment, - do_send_email, - context, - request_counter, - ) - .await?; - let notif_type = match self.kind { - CreateOrUpdateType::Create => UserOperationCrud::CreateComment, - CreateOrUpdateType::Update => UserOperationCrud::EditComment, - }; - send_comment_ws_message( - comment.id, notif_type, None, None, None, recipients, context, - ) - .await?; - Ok(()) - } -} + let post_id = comment.post_id; + let post = Post::read(&mut context.pool(), post_id).await?; + let actor = self.actor.dereference(context).await?; -#[async_trait::async_trait(?Send)] -impl GetCommunity for CreateOrUpdateComment { - #[tracing::instrument(skip_all)] - async fn get_community( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result { - let post = self.object.get_parents(context, request_counter).await?.0; - let community = Community::read(context.pool(), post.community_id).await?; - Ok(community.into()) + // Note: + // Although mentions could be gotten from the post tags (they are included there), or the ccs, + // Its much easier to scrape them from the comment body, since the API has to do that + // anyway. + // TODO: for compatibility with other projects, it would be much better to read this from cc or tags + let mentions = scrape_text_for_mentions(&comment.content); + send_local_notifs(mentions, &comment.0, &actor, &post, do_send_email, context).await?; + Ok(()) } }