From 26f4694b98ea17ba3eca96067bd702cd2617626e Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 24 Jun 2021 05:42:19 +0200 Subject: [PATCH] convert all comment receivers (except undo comment) --- .../src/activities/receive/comment.rs | 211 ------------------ .../src/activities/receive/mod.rs | 1 - .../src/activities_new/comment/create.rs | 68 +++--- .../src/activities_new/comment/delete.rs | 47 ++++ .../src/activities_new/comment/dislike.rs | 61 +++++ .../src/activities_new/comment/like.rs | 61 +++++ .../src/activities_new/comment/mod.rs | 63 ++++++ .../src/activities_new/comment/remove.rs | 47 ++++ .../src/activities_new/comment/update.rs | 56 +++++ .../activities_new/private_message/create.rs | 9 +- .../activities_new/private_message/delete.rs | 9 +- .../src/activities_new/private_message/mod.rs | 3 +- .../private_message/undo_delete.rs | 9 +- .../activities_new/private_message/update.rs | 9 +- .../src/inbox/new_inbox_routing.rs | 18 +- crates/apub_receive/src/inbox/person_inbox.rs | 30 +-- .../src/inbox/receive_for_community.rs | 23 +- 17 files changed, 417 insertions(+), 308 deletions(-) delete mode 100644 crates/apub_receive/src/activities/receive/comment.rs create mode 100644 crates/apub_receive/src/activities_new/comment/delete.rs create mode 100644 crates/apub_receive/src/activities_new/comment/dislike.rs create mode 100644 crates/apub_receive/src/activities_new/comment/like.rs create mode 100644 crates/apub_receive/src/activities_new/comment/remove.rs create mode 100644 crates/apub_receive/src/activities_new/comment/update.rs diff --git a/crates/apub_receive/src/activities/receive/comment.rs b/crates/apub_receive/src/activities/receive/comment.rs deleted file mode 100644 index 419fb70c3..000000000 --- a/crates/apub_receive/src/activities/receive/comment.rs +++ /dev/null @@ -1,211 +0,0 @@ -use crate::activities::receive::get_actor_as_person; -use activitystreams::{ - activity::{ActorAndObjectRefExt, Dislike, Like, Update}, - base::ExtendsExt, -}; -use anyhow::Context; -use lemmy_api_common::{blocking, comment::CommentResponse, send_local_notifs}; -use lemmy_apub::{objects::FromApub, ActorType, NoteExt}; -use lemmy_db_queries::{source::comment::Comment_, Crud, Likeable}; -use lemmy_db_schema::source::{ - comment::{Comment, CommentLike, CommentLikeForm}, - post::Post, -}; -use lemmy_db_views::comment_view::CommentView; -use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError}; -use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation, UserOperationCrud}; - -pub(crate) async fn receive_update_comment( - update: Update, - context: &LemmyContext, - request_counter: &mut i32, -) -> Result<(), LemmyError> { - let note = NoteExt::from_any_base(update.object().to_owned().one().context(location_info!())?)? - .context(location_info!())?; - let person = get_actor_as_person(&update, context, request_counter).await?; - - let comment = - Comment::from_apub(¬e, context, person.actor_id(), request_counter, false).await?; - - let comment_id = comment.id; - let post_id = comment.post_id; - let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; - - let mentions = scrape_text_for_mentions(&comment.content); - let recipient_ids = - send_local_notifs(mentions, comment, person, post, context.pool(), false).await?; - - // Refetch the view - let comment_view = blocking(context.pool(), move |conn| { - CommentView::read(conn, comment_id, None) - }) - .await??; - - let res = CommentResponse { - comment_view, - recipient_ids, - form_id: None, - }; - - context.chat_server().do_send(SendComment { - op: UserOperationCrud::EditComment, - comment: res, - websocket_id: None, - }); - - Ok(()) -} - -pub(crate) async fn receive_like_comment( - like: Like, - comment: Comment, - context: &LemmyContext, - request_counter: &mut i32, -) -> Result<(), LemmyError> { - let person = get_actor_as_person(&like, context, request_counter).await?; - - let comment_id = comment.id; - let like_form = CommentLikeForm { - comment_id, - post_id: comment.post_id, - person_id: person.id, - score: 1, - }; - let person_id = person.id; - blocking(context.pool(), move |conn| { - CommentLike::remove(conn, person_id, comment_id)?; - CommentLike::like(conn, &like_form) - }) - .await??; - - // Refetch the view - let comment_view = blocking(context.pool(), move |conn| { - CommentView::read(conn, comment_id, None) - }) - .await??; - - // TODO get those recipient actor ids from somewhere - let recipient_ids = vec![]; - let res = CommentResponse { - comment_view, - recipient_ids, - form_id: None, - }; - - context.chat_server().do_send(SendComment { - op: UserOperation::CreateCommentLike, - comment: res, - websocket_id: None, - }); - - Ok(()) -} - -pub(crate) async fn receive_dislike_comment( - dislike: Dislike, - comment: Comment, - context: &LemmyContext, - request_counter: &mut i32, -) -> Result<(), LemmyError> { - let person = get_actor_as_person(&dislike, context, request_counter).await?; - - let comment_id = comment.id; - let like_form = CommentLikeForm { - comment_id, - post_id: comment.post_id, - person_id: person.id, - score: -1, - }; - let person_id = person.id; - blocking(context.pool(), move |conn| { - CommentLike::remove(conn, person_id, comment_id)?; - CommentLike::like(conn, &like_form) - }) - .await??; - - // Refetch the view - let comment_view = blocking(context.pool(), move |conn| { - CommentView::read(conn, comment_id, None) - }) - .await??; - - // TODO get those recipient actor ids from somewhere - let recipient_ids = vec![]; - let res = CommentResponse { - comment_view, - recipient_ids, - form_id: None, - }; - - context.chat_server().do_send(SendComment { - op: UserOperation::CreateCommentLike, - comment: res, - websocket_id: None, - }); - - Ok(()) -} - -pub(crate) async fn receive_delete_comment( - context: &LemmyContext, - comment: Comment, -) -> Result<(), LemmyError> { - let deleted_comment = blocking(context.pool(), move |conn| { - Comment::update_deleted(conn, comment.id, true) - }) - .await??; - - // Refetch the view - let comment_id = deleted_comment.id; - let comment_view = blocking(context.pool(), move |conn| { - CommentView::read(conn, comment_id, None) - }) - .await??; - - // TODO get those recipient actor ids from somewhere - let recipient_ids = vec![]; - let res = CommentResponse { - comment_view, - recipient_ids, - form_id: None, - }; - context.chat_server().do_send(SendComment { - op: UserOperationCrud::EditComment, - comment: res, - websocket_id: None, - }); - - Ok(()) -} - -pub(crate) async fn receive_remove_comment( - context: &LemmyContext, - comment: Comment, -) -> Result<(), LemmyError> { - let removed_comment = blocking(context.pool(), move |conn| { - Comment::update_removed(conn, comment.id, true) - }) - .await??; - - // Refetch the view - let comment_id = removed_comment.id; - let comment_view = blocking(context.pool(), move |conn| { - CommentView::read(conn, comment_id, None) - }) - .await??; - - // TODO get those recipient actor ids from somewhere - let recipient_ids = vec![]; - let res = CommentResponse { - comment_view, - recipient_ids, - form_id: None, - }; - context.chat_server().do_send(SendComment { - op: UserOperationCrud::EditComment, - comment: res, - websocket_id: None, - }); - - Ok(()) -} diff --git a/crates/apub_receive/src/activities/receive/mod.rs b/crates/apub_receive/src/activities/receive/mod.rs index afad2dfc9..088369baf 100644 --- a/crates/apub_receive/src/activities/receive/mod.rs +++ b/crates/apub_receive/src/activities/receive/mod.rs @@ -12,7 +12,6 @@ use log::debug; use std::fmt::Debug; use url::Url; -pub(crate) mod comment; pub(crate) mod comment_undo; pub(crate) mod community; pub(crate) mod post; diff --git a/crates/apub_receive/src/activities_new/comment/create.rs b/crates/apub_receive/src/activities_new/comment/create.rs index 88bbe6acc..8c764af08 100644 --- a/crates/apub_receive/src/activities_new/comment/create.rs +++ b/crates/apub_receive/src/activities_new/comment/create.rs @@ -1,13 +1,14 @@ -use crate::inbox::new_inbox_routing::Activity; -use activitystreams::activity::kind::CreateType; -use lemmy_api_common::{blocking, comment::CommentResponse, send_local_notifs}; -use lemmy_apub::{fetcher::person::get_or_fetch_and_upsert_person, objects::FromApub, NoteExt}; -use lemmy_apub_lib::{PublicUrl, ReceiveActivity}; -use lemmy_db_queries::Crud; -use lemmy_db_schema::source::{comment::Comment, post::Post}; -use lemmy_db_views::comment_view::CommentView; -use lemmy_utils::{utils::scrape_text_for_mentions, LemmyError}; -use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperationCrud}; +use crate::{ + activities_new::comment::{get_notif_recipients, send_websocket_message}, + inbox::new_inbox_routing::Activity, +}; +use activitystreams::{activity::kind::CreateType, base::BaseExt}; +use anyhow::Context; +use lemmy_apub::{objects::FromApub, NoteExt}; +use lemmy_apub_lib::{verify_domains_match, PublicUrl, ReceiveActivity}; +use lemmy_db_schema::source::comment::Comment; +use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::{LemmyContext, UserOperationCrud}; use url::Url; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] @@ -27,6 +28,12 @@ impl ReceiveActivity for Activity { context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { + verify_domains_match(&self.inner.actor, self.id_unchecked())?; + verify_domains_match( + &self.inner.actor, + self.inner.object.id_unchecked().context(location_info!())?, + )?; + let comment = Comment::from_apub( &self.inner.object, context, @@ -35,37 +42,14 @@ impl ReceiveActivity for Activity { false, ) .await?; - - let post_id = comment.post_id; - let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; - let actor = get_or_fetch_and_upsert_person(&self.inner.actor, context, request_counter).await?; - - // 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. - let mentions = scrape_text_for_mentions(&comment.content); - let recipient_ids = - send_local_notifs(mentions, comment.clone(), actor, post, context.pool(), true).await?; - - // Refetch the view - let comment_view = blocking(context.pool(), move |conn| { - CommentView::read(conn, comment.id, None) - }) - .await??; - - let res = CommentResponse { - comment_view, - recipient_ids, - form_id: None, - }; - - context.chat_server().do_send(SendComment { - op: UserOperationCrud::CreateComment, - comment: res, - websocket_id: None, - }); - - Ok(()) + let recipients = + get_notif_recipients(&self.inner.actor, &comment, context, request_counter).await?; + send_websocket_message( + comment.id, + recipients, + UserOperationCrud::CreateComment, + context, + ) + .await } } diff --git a/crates/apub_receive/src/activities_new/comment/delete.rs b/crates/apub_receive/src/activities_new/comment/delete.rs new file mode 100644 index 000000000..b9ba675bc --- /dev/null +++ b/crates/apub_receive/src/activities_new/comment/delete.rs @@ -0,0 +1,47 @@ +use crate::{activities_new::comment::send_websocket_message, inbox::new_inbox_routing::Activity}; +use activitystreams::activity::kind::DeleteType; +use lemmy_api_common::blocking; +use lemmy_apub::fetcher::objects::get_or_fetch_and_insert_comment; +use lemmy_apub_lib::{PublicUrl, ReceiveActivity}; +use lemmy_db_queries::source::comment::Comment_; +use lemmy_db_schema::source::comment::Comment; +use lemmy_utils::LemmyError; +use lemmy_websocket::{LemmyContext, UserOperationCrud}; +use url::Url; + +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DeleteComment { + actor: Url, + to: PublicUrl, + object: Url, + cc: Vec, + #[serde(rename = "type")] + kind: DeleteType, +} + +#[async_trait::async_trait(?Send)] +impl ReceiveActivity for Activity { + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { + let comment = + get_or_fetch_and_insert_comment(&self.inner.object, context, request_counter).await?; + + let deleted_comment = blocking(context.pool(), move |conn| { + Comment::update_deleted(conn, comment.id, true) + }) + .await??; + + // TODO get those recipient actor ids from somewhere + send_websocket_message( + deleted_comment.id, + vec![], + UserOperationCrud::EditComment, + context, + ) + .await + } +} diff --git a/crates/apub_receive/src/activities_new/comment/dislike.rs b/crates/apub_receive/src/activities_new/comment/dislike.rs new file mode 100644 index 000000000..0b3192544 --- /dev/null +++ b/crates/apub_receive/src/activities_new/comment/dislike.rs @@ -0,0 +1,61 @@ +use crate::{activities_new::comment::send_websocket_message, inbox::new_inbox_routing::Activity}; +use activitystreams::activity::kind::DislikeType; +use lemmy_api_common::blocking; +use lemmy_apub::fetcher::{ + objects::get_or_fetch_and_insert_comment, + person::get_or_fetch_and_upsert_person, +}; +use lemmy_apub_lib::{PublicUrl, ReceiveActivity}; +use lemmy_db_queries::Likeable; +use lemmy_db_schema::source::comment::{CommentLike, CommentLikeForm}; +use lemmy_utils::LemmyError; +use lemmy_websocket::{LemmyContext, UserOperation}; +use url::Url; + +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DislikeComment { + actor: Url, + to: PublicUrl, + object: Url, + cc: Vec, + #[serde(rename = "type")] + kind: DislikeType, +} + +#[async_trait::async_trait(?Send)] +impl ReceiveActivity for Activity { + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { + let person = + get_or_fetch_and_upsert_person(&self.inner.actor, context, request_counter).await?; + let comment = + get_or_fetch_and_insert_comment(&self.inner.object, context, request_counter).await?; + + let comment_id = comment.id; + let like_form = CommentLikeForm { + comment_id, + post_id: comment.post_id, + person_id: person.id, + score: -1, + }; + let person_id = person.id; + blocking(context.pool(), move |conn| { + CommentLike::remove(conn, person_id, comment_id)?; + CommentLike::like(conn, &like_form) + }) + .await??; + + // TODO get those recipient actor ids from somewhere + send_websocket_message( + comment_id, + vec![], + UserOperation::CreateCommentLike, + context, + ) + .await + } +} diff --git a/crates/apub_receive/src/activities_new/comment/like.rs b/crates/apub_receive/src/activities_new/comment/like.rs new file mode 100644 index 000000000..25aff45fc --- /dev/null +++ b/crates/apub_receive/src/activities_new/comment/like.rs @@ -0,0 +1,61 @@ +use crate::{activities_new::comment::send_websocket_message, inbox::new_inbox_routing::Activity}; +use activitystreams::activity::kind::LikeType; +use lemmy_api_common::blocking; +use lemmy_apub::fetcher::{ + objects::get_or_fetch_and_insert_comment, + person::get_or_fetch_and_upsert_person, +}; +use lemmy_apub_lib::{PublicUrl, ReceiveActivity}; +use lemmy_db_queries::Likeable; +use lemmy_db_schema::source::comment::{CommentLike, CommentLikeForm}; +use lemmy_utils::LemmyError; +use lemmy_websocket::{LemmyContext, UserOperation}; +use url::Url; + +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct LikeComment { + actor: Url, + to: PublicUrl, + object: Url, + cc: Vec, + #[serde(rename = "type")] + kind: LikeType, +} + +#[async_trait::async_trait(?Send)] +impl ReceiveActivity for Activity { + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { + let person = + get_or_fetch_and_upsert_person(&self.inner.actor, context, request_counter).await?; + let comment = + get_or_fetch_and_insert_comment(&self.inner.object, context, request_counter).await?; + + let comment_id = comment.id; + let like_form = CommentLikeForm { + comment_id, + post_id: comment.post_id, + person_id: person.id, + score: 1, + }; + let person_id = person.id; + blocking(context.pool(), move |conn| { + CommentLike::remove(conn, person_id, comment_id)?; + CommentLike::like(conn, &like_form) + }) + .await??; + + // TODO get those recipient actor ids from somewhere + send_websocket_message( + comment_id, + vec![], + UserOperation::CreateCommentLike, + context, + ) + .await + } +} diff --git a/crates/apub_receive/src/activities_new/comment/mod.rs b/crates/apub_receive/src/activities_new/comment/mod.rs index c5fb369c1..cc435f4aa 100644 --- a/crates/apub_receive/src/activities_new/comment/mod.rs +++ b/crates/apub_receive/src/activities_new/comment/mod.rs @@ -1 +1,64 @@ +use lemmy_api_common::{blocking, comment::CommentResponse, send_local_notifs}; +use lemmy_apub::fetcher::person::get_or_fetch_and_upsert_person; +use lemmy_db_queries::Crud; +use lemmy_db_schema::{ + source::{comment::Comment, post::Post}, + CommentId, + LocalUserId, +}; +use lemmy_db_views::comment_view::CommentView; +use lemmy_utils::{utils::scrape_text_for_mentions, LemmyError}; +use lemmy_websocket::{messages::SendComment, LemmyContext}; +use url::Url; + pub mod create; +pub mod delete; +pub mod dislike; +pub mod like; +pub mod remove; +pub mod update; + +async fn get_notif_recipients( + actor: &Url, + comment: &Comment, + context: &LemmyContext, + request_counter: &mut i32, +) -> Result, LemmyError> { + let post_id = comment.post_id; + let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; + let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?; + + // 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. + let mentions = scrape_text_for_mentions(&comment.content); + send_local_notifs(mentions, comment.clone(), actor, post, context.pool(), true).await +} + +async fn send_websocket_message( + comment_id: CommentId, + recipient_ids: Vec, + op: OP, + context: &LemmyContext, +) -> Result<(), LemmyError> { + // Refetch the view + let comment_view = blocking(context.pool(), move |conn| { + CommentView::read(conn, comment_id, None) + }) + .await??; + + let res = CommentResponse { + comment_view, + recipient_ids, + form_id: None, + }; + + context.chat_server().do_send(SendComment { + op, + comment: res, + websocket_id: None, + }); + + Ok(()) +} diff --git a/crates/apub_receive/src/activities_new/comment/remove.rs b/crates/apub_receive/src/activities_new/comment/remove.rs new file mode 100644 index 000000000..5027a8717 --- /dev/null +++ b/crates/apub_receive/src/activities_new/comment/remove.rs @@ -0,0 +1,47 @@ +use crate::{activities_new::comment::send_websocket_message, inbox::new_inbox_routing::Activity}; +use activitystreams::activity::kind::RemoveType; +use lemmy_api_common::blocking; +use lemmy_apub::fetcher::objects::get_or_fetch_and_insert_comment; +use lemmy_apub_lib::{PublicUrl, ReceiveActivity}; +use lemmy_db_queries::source::comment::Comment_; +use lemmy_db_schema::source::comment::Comment; +use lemmy_utils::LemmyError; +use lemmy_websocket::{LemmyContext, UserOperationCrud}; +use url::Url; + +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct RemoveComment { + actor: Url, + to: PublicUrl, + object: Url, + cc: Vec, + #[serde(rename = "type")] + kind: RemoveType, +} + +#[async_trait::async_trait(?Send)] +impl ReceiveActivity for Activity { + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { + let comment = + get_or_fetch_and_insert_comment(&self.inner.object, context, request_counter).await?; + + let removed_comment = blocking(context.pool(), move |conn| { + Comment::update_removed(conn, comment.id, true) + }) + .await??; + + // TODO get those recipient actor ids from somewhere + send_websocket_message( + removed_comment.id, + vec![], + UserOperationCrud::EditComment, + context, + ) + .await + } +} diff --git a/crates/apub_receive/src/activities_new/comment/update.rs b/crates/apub_receive/src/activities_new/comment/update.rs new file mode 100644 index 000000000..f4e50c947 --- /dev/null +++ b/crates/apub_receive/src/activities_new/comment/update.rs @@ -0,0 +1,56 @@ +use crate::{ + activities_new::comment::{get_notif_recipients, send_websocket_message}, + inbox::new_inbox_routing::Activity, +}; +use activitystreams::{activity::kind::UpdateType, base::BaseExt}; +use anyhow::Context; +use lemmy_apub::{objects::FromApub, NoteExt}; +use lemmy_apub_lib::{verify_domains_match, PublicUrl, ReceiveActivity}; +use lemmy_db_schema::source::comment::Comment; +use lemmy_utils::{location_info, LemmyError}; +use lemmy_websocket::{LemmyContext, UserOperationCrud}; +use url::Url; + +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct UpdateComment { + actor: Url, + to: PublicUrl, + object: NoteExt, + #[serde(rename = "type")] + kind: UpdateType, +} + +#[async_trait::async_trait(?Send)] +impl ReceiveActivity for Activity { + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { + verify_domains_match(&self.inner.actor, self.id_unchecked())?; + verify_domains_match( + &self.inner.actor, + self.inner.object.id_unchecked().context(location_info!())?, + )?; + + let comment = Comment::from_apub( + &self.inner.object, + context, + self.inner.actor.clone(), + request_counter, + false, + ) + .await?; + + let recipients = + get_notif_recipients(&self.inner.actor, &comment, context, request_counter).await?; + send_websocket_message( + comment.id, + recipients, + UserOperationCrud::EditComment, + context, + ) + .await + } +} diff --git a/crates/apub_receive/src/activities_new/private_message/create.rs b/crates/apub_receive/src/activities_new/private_message/create.rs index e611b97f3..66d64c437 100644 --- a/crates/apub_receive/src/activities_new/private_message/create.rs +++ b/crates/apub_receive/src/activities_new/private_message/create.rs @@ -7,7 +7,7 @@ use lemmy_apub::{objects::FromApub, NoteExt}; use lemmy_apub_lib::{verify_domains_match, ReceiveActivity}; use lemmy_db_schema::source::private_message::PrivateMessage; use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; +use lemmy_websocket::{LemmyContext, UserOperationCrud}; use url::Url; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] @@ -38,7 +38,12 @@ impl ReceiveActivity for Activity { ) .await?; - send_websocket_message(private_message.id, context).await?; + send_websocket_message( + private_message.id, + UserOperationCrud::CreatePrivateMessage, + context, + ) + .await?; Ok(()) } diff --git a/crates/apub_receive/src/activities_new/private_message/delete.rs b/crates/apub_receive/src/activities_new/private_message/delete.rs index d21357be8..21eaff0a8 100644 --- a/crates/apub_receive/src/activities_new/private_message/delete.rs +++ b/crates/apub_receive/src/activities_new/private_message/delete.rs @@ -8,7 +8,7 @@ use lemmy_apub_lib::{verify_domains_match, ReceiveActivity}; use lemmy_db_queries::{source::private_message::PrivateMessage_, ApubObject}; use lemmy_db_schema::source::private_message::PrivateMessage; use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; +use lemmy_websocket::{LemmyContext, UserOperationCrud}; use url::Url; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] @@ -41,7 +41,12 @@ impl ReceiveActivity for Activity { }) .await??; - send_websocket_message(deleted_private_message.id, context).await?; + send_websocket_message( + deleted_private_message.id, + UserOperationCrud::DeletePrivateMessage, + context, + ) + .await?; Ok(()) } diff --git a/crates/apub_receive/src/activities_new/private_message/mod.rs b/crates/apub_receive/src/activities_new/private_message/mod.rs index 47fd292ec..ac5094ba1 100644 --- a/crates/apub_receive/src/activities_new/private_message/mod.rs +++ b/crates/apub_receive/src/activities_new/private_message/mod.rs @@ -11,6 +11,7 @@ pub mod update; async fn send_websocket_message( private_message_id: PrivateMessageId, + op: UserOperationCrud, context: &LemmyContext, ) -> Result<(), LemmyError> { let message = blocking(&context.pool(), move |conn| { @@ -31,7 +32,7 @@ async fn send_websocket_message( .id; context.chat_server().do_send(SendUserRoomMessage { - op: UserOperationCrud::CreatePrivateMessage, + op, response: res, local_recipient_id, websocket_id: None, diff --git a/crates/apub_receive/src/activities_new/private_message/undo_delete.rs b/crates/apub_receive/src/activities_new/private_message/undo_delete.rs index 6be096062..1c1fdb8c2 100644 --- a/crates/apub_receive/src/activities_new/private_message/undo_delete.rs +++ b/crates/apub_receive/src/activities_new/private_message/undo_delete.rs @@ -8,7 +8,7 @@ use lemmy_apub_lib::{verify_domains_match, ReceiveActivity}; use lemmy_db_queries::{source::private_message::PrivateMessage_, ApubObject}; use lemmy_db_schema::source::private_message::PrivateMessage; use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; +use lemmy_websocket::{LemmyContext, UserOperationCrud}; use url::Url; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] @@ -43,7 +43,12 @@ impl ReceiveActivity for Activity { }) .await??; - send_websocket_message(deleted_private_message.id, context).await?; + send_websocket_message( + deleted_private_message.id, + UserOperationCrud::EditPrivateMessage, + context, + ) + .await?; Ok(()) } diff --git a/crates/apub_receive/src/activities_new/private_message/update.rs b/crates/apub_receive/src/activities_new/private_message/update.rs index e125e883a..2ec1853d6 100644 --- a/crates/apub_receive/src/activities_new/private_message/update.rs +++ b/crates/apub_receive/src/activities_new/private_message/update.rs @@ -7,7 +7,7 @@ use lemmy_apub::{objects::FromApub, NoteExt}; use lemmy_apub_lib::{verify_domains_match, ReceiveActivity}; use lemmy_db_schema::source::private_message::PrivateMessage; use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; +use lemmy_websocket::{LemmyContext, UserOperationCrud}; use url::Url; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] @@ -38,7 +38,12 @@ impl ReceiveActivity for Activity { ) .await?; - send_websocket_message(private_message.id, context).await?; + send_websocket_message( + private_message.id, + UserOperationCrud::EditPrivateMessage, + context, + ) + .await?; Ok(()) } diff --git a/crates/apub_receive/src/inbox/new_inbox_routing.rs b/crates/apub_receive/src/inbox/new_inbox_routing.rs index 1d62de392..24a69405f 100644 --- a/crates/apub_receive/src/inbox/new_inbox_routing.rs +++ b/crates/apub_receive/src/inbox/new_inbox_routing.rs @@ -1,5 +1,12 @@ use crate::activities_new::{ - comment::create::CreateComment, + comment::{ + create::CreateComment, + delete::DeleteComment, + dislike::DislikeComment, + like::LikeComment, + remove::RemoveComment, + update::UpdateComment, + }, follow::AcceptFollowCommunity, private_message::{ create::CreatePrivateMessage, @@ -14,6 +21,9 @@ use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; use url::Url; +// TODO: add security checks for received activities back in +// mainly check that domain of actor and id are identical (and object/object.id where applicable) + // TODO: would be nice if we could move this to lemmy_apub_lib crate. doing that gives error: // "only traits defined in the current crate can be implemented for arbitrary types" #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)] @@ -28,7 +38,6 @@ pub struct Activity { pub inner: Kind, // unparsed fields - // todo: can probably remove this field #[serde(flatten)] unparsed: Unparsed, } @@ -47,6 +56,11 @@ pub enum PersonAcceptedActivitiesNew { DeletePrivateMessage(DeletePrivateMessage), UndoDeletePrivateMessage(UndoDeletePrivateMessage), CreateComment(CreateComment), + UpdateComment(UpdateComment), + LikeComment(LikeComment), + DislikeComment(DislikeComment), + DeleteComment(DeleteComment), + RemoveComment(RemoveComment), } // todo: can probably get rid of this? diff --git a/crates/apub_receive/src/inbox/person_inbox.rs b/crates/apub_receive/src/inbox/person_inbox.rs index 5e6bb0a91..31668775f 100644 --- a/crates/apub_receive/src/inbox/person_inbox.rs +++ b/crates/apub_receive/src/inbox/person_inbox.rs @@ -1,6 +1,5 @@ use crate::{ activities::receive::{ - comment::receive_update_comment, community::{ receive_delete_community, receive_remove_community, @@ -30,7 +29,7 @@ use crate::{ }, }; use activitystreams::{ - activity::{ActorAndObject, Announce, Delete, Remove, Undo, Update}, + activity::{ActorAndObject, Announce, Delete, Remove, Undo}, base::AnyBase, prelude::*, }; @@ -145,15 +144,7 @@ pub(crate) async fn person_receive_message( Box::pin(receive_announce(&context, any_base, actor, request_counter)).await? } PersonValidTypes::Create => {} - PersonValidTypes::Update => { - Box::pin(receive_update( - &context, - any_base, - actor_url, - request_counter, - )) - .await? - } + PersonValidTypes::Update => {} PersonValidTypes::Delete => { Box::pin(receive_delete( context, @@ -304,23 +295,6 @@ pub async fn receive_announce( } } -/// Receive either an updated private message, or an updated comment mention. We distinguish -/// them by checking whether the activity is public. -async fn receive_update( - context: &LemmyContext, - activity: AnyBase, - expected_domain: Url, - request_counter: &mut i32, -) -> Result<(), LemmyError> { - let update = Update::from_any_base(activity)?.context(location_info!())?; - verify_activity_domains_valid(&update, &expected_domain, true)?; - if verify_is_addressed_to_public(&update).is_ok() { - receive_update_comment(update, context, request_counter).await - } else { - todo!() - } -} - async fn receive_delete( context: &LemmyContext, any_base: AnyBase, diff --git a/crates/apub_receive/src/inbox/receive_for_community.rs b/crates/apub_receive/src/inbox/receive_for_community.rs index 776f0b54d..5ac123736 100644 --- a/crates/apub_receive/src/inbox/receive_for_community.rs +++ b/crates/apub_receive/src/inbox/receive_for_community.rs @@ -1,12 +1,5 @@ use crate::{ activities::receive::{ - comment::{ - receive_delete_comment, - receive_dislike_comment, - receive_like_comment, - receive_remove_comment, - receive_update_comment, - }, comment_undo::{ receive_undo_delete_comment, receive_undo_dislike_comment, @@ -162,7 +155,7 @@ pub(in crate::inbox) async fn receive_update_for_community( Some(ObjectTypes::Page) => { receive_update_post(update, announce, context, request_counter).await } - Some(ObjectTypes::Note) => receive_update_comment(update, context, request_counter).await, + Some(ObjectTypes::Note) => todo!(), Some(ObjectTypes::Group) => { receive_remote_mod_update_community(update, context, request_counter).await } @@ -187,8 +180,8 @@ pub(in crate::inbox) async fn receive_like_for_community( .context(location_info!())?; match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? { PostOrComment::Post(post) => receive_like_post(like, *post, context, request_counter).await, - PostOrComment::Comment(comment) => { - receive_like_comment(like, *comment, context, request_counter).await + PostOrComment::Comment(_) => { + todo!() } } } @@ -220,8 +213,8 @@ pub(in crate::inbox) async fn receive_dislike_for_community( PostOrComment::Post(post) => { receive_dislike_post(dislike, *post, context, request_counter).await } - PostOrComment::Comment(comment) => { - receive_dislike_comment(dislike, *comment, context, request_counter).await + PostOrComment::Comment(_) => { + todo!() } } } @@ -250,9 +243,9 @@ pub(in crate::inbox) async fn receive_delete_for_community( verify_activity_domains_valid(&delete, &expected_domain, true)?; receive_delete_post(context, *p).await } - Ok(Object::Comment(c)) => { + Ok(Object::Comment(_)) => { verify_activity_domains_valid(&delete, &expected_domain, true)?; - receive_delete_comment(context, *c).await + todo!() } Ok(Object::Community(c)) => { receive_remote_mod_delete_community(delete, *c, context, request_counter).await @@ -309,7 +302,7 @@ pub(in crate::inbox) async fn receive_remove_for_community( match find_post_or_comment_by_id(context, object).await { Ok(PostOrComment::Post(p)) => receive_remove_post(context, *p).await, - Ok(PostOrComment::Comment(c)) => receive_remove_comment(context, *c).await, + Ok(PostOrComment::Comment(_)) => todo!(), // if we dont have the object, no need to do anything Err(_) => Ok(()), }