From 99ffa1f22c76d1da94e7e58405d1ea04b14073bb Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 7 Aug 2021 19:08:21 +0200 Subject: [PATCH] Rewrite private message activity sending --- crates/api_crud/src/private_message/delete.rs | 18 ++-- .../private_message/create_or_update.rs | 11 +-- .../src/activities/private_message/delete.rs | 47 ++++++++-- .../activities/private_message/undo_delete.rs | 59 +++++++++++-- crates/apub/src/activities/send/mod.rs | 1 - .../src/activities/send/private_message.rs | 86 ------------------- crates/apub/src/activity_queue.rs | 28 ------ 7 files changed, 107 insertions(+), 143 deletions(-) delete mode 100644 crates/apub/src/activities/send/private_message.rs diff --git a/crates/api_crud/src/private_message/delete.rs b/crates/api_crud/src/private_message/delete.rs index 757596cd1..78dcd9311 100644 --- a/crates/api_crud/src/private_message/delete.rs +++ b/crates/api_crud/src/private_message/delete.rs @@ -5,7 +5,10 @@ use lemmy_api_common::{ get_local_user_view_from_jwt, person::{DeletePrivateMessage, PrivateMessageResponse}, }; -use lemmy_apub::ApubObjectType; +use lemmy_apub::activities::private_message::{ + delete::DeletePrivateMessage as DeletePrivateMessageApub, + undo_delete::UndoDeletePrivateMessage, +}; use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud, DeleteableOrRemoveable}; use lemmy_db_schema::source::private_message::PrivateMessage; use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView}; @@ -45,13 +48,14 @@ impl PerformCrud for DeletePrivateMessage { // Send the apub update if data.deleted { - updated_private_message - .blank_out_deleted_or_removed_info() - .send_delete(&local_user_view.person, context) - .await?; + DeletePrivateMessageApub::send( + &local_user_view.person, + &updated_private_message.blank_out_deleted_or_removed_info(), + context, + ) + .await?; } else { - updated_private_message - .send_undo_delete(&local_user_view.person, context) + UndoDeletePrivateMessage::send(&local_user_view.person, &updated_private_message, context) .await?; } diff --git a/crates/apub/src/activities/private_message/create_or_update.rs b/crates/apub/src/activities/private_message/create_or_update.rs index d51e2d033..09582689b 100644 --- a/crates/apub/src/activities/private_message/create_or_update.rs +++ b/crates/apub/src/activities/private_message/create_or_update.rs @@ -53,15 +53,8 @@ impl CreateOrUpdatePrivateMessage { unparsed: Default::default(), }, }; - send_activity_new( - context, - &create_or_update, - &id, - actor, - vec![recipient.get_shared_inbox_or_inbox_url()], - true, - ) - .await + let inbox = vec![recipient.get_shared_inbox_or_inbox_url()]; + send_activity_new(context, &create_or_update, &id, actor, inbox, true).await } } #[async_trait::async_trait(?Send)] diff --git a/crates/apub/src/activities/private_message/delete.rs b/crates/apub/src/activities/private_message/delete.rs index a57e35eea..4c990a2d1 100644 --- a/crates/apub/src/activities/private_message/delete.rs +++ b/crates/apub/src/activities/private_message/delete.rs @@ -1,9 +1,19 @@ -use crate::activities::{private_message::send_websocket_message, verify_activity, verify_person}; +use crate::{ + activities::{ + generate_activity_id, + private_message::send_websocket_message, + verify_activity, + verify_person, + }, + activity_queue::send_activity_new, + extensions::context::lemmy_context, + ActorType, +}; use activitystreams::activity::kind::DeleteType; use lemmy_api_common::blocking; use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandler}; -use lemmy_db_queries::{source::private_message::PrivateMessage_, ApubObject}; -use lemmy_db_schema::source::private_message::PrivateMessage; +use lemmy_db_queries::{source::private_message::PrivateMessage_, ApubObject, Crud}; +use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage}; use lemmy_utils::LemmyError; use lemmy_websocket::{LemmyContext, UserOperationCrud}; use url::Url; @@ -11,14 +21,41 @@ use url::Url; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct DeletePrivateMessage { - to: Url, + pub(in crate::activities::private_message) to: Url, pub(in crate::activities::private_message) object: Url, #[serde(rename = "type")] - kind: DeleteType, + pub(in crate::activities::private_message) kind: DeleteType, #[serde(flatten)] pub(in crate::activities::private_message) common: ActivityCommonFields, } +impl DeletePrivateMessage { + pub async fn send( + actor: &Person, + pm: &PrivateMessage, + context: &LemmyContext, + ) -> Result<(), LemmyError> { + let recipient_id = pm.recipient_id; + let recipient = + blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??; + + let id = generate_activity_id(DeleteType::Delete)?; + let delete = DeletePrivateMessage { + to: actor.actor_id(), + object: pm.ap_id.clone().into(), + kind: DeleteType::Delete, + common: ActivityCommonFields { + context: lemmy_context(), + id: id.clone(), + actor: actor.actor_id(), + unparsed: Default::default(), + }, + }; + let inbox = vec![recipient.get_shared_inbox_or_inbox_url()]; + send_activity_new(context, &delete, &id, actor, inbox, true).await + } +} + #[async_trait::async_trait(?Send)] impl ActivityHandler for DeletePrivateMessage { async fn verify( diff --git a/crates/apub/src/activities/private_message/undo_delete.rs b/crates/apub/src/activities/private_message/undo_delete.rs index a04197fd6..5bbca90e6 100644 --- a/crates/apub/src/activities/private_message/undo_delete.rs +++ b/crates/apub/src/activities/private_message/undo_delete.rs @@ -1,9 +1,15 @@ -use crate::activities::{ - private_message::{delete::DeletePrivateMessage, send_websocket_message}, - verify_activity, - verify_person, +use crate::{ + activities::{ + generate_activity_id, + private_message::{delete::DeletePrivateMessage, send_websocket_message}, + verify_activity, + verify_person, + }, + activity_queue::send_activity_new, + extensions::context::lemmy_context, + ActorType, }; -use activitystreams::activity::kind::UndoType; +use activitystreams::activity::kind::{DeleteType, UndoType}; use lemmy_api_common::blocking; use lemmy_apub_lib::{ verify_domains_match, @@ -11,8 +17,8 @@ use lemmy_apub_lib::{ ActivityCommonFields, ActivityHandler, }; -use lemmy_db_queries::{source::private_message::PrivateMessage_, ApubObject}; -use lemmy_db_schema::source::private_message::PrivateMessage; +use lemmy_db_queries::{source::private_message::PrivateMessage_, ApubObject, Crud}; +use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage}; use lemmy_utils::LemmyError; use lemmy_websocket::{LemmyContext, UserOperationCrud}; use url::Url; @@ -28,6 +34,45 @@ pub struct UndoDeletePrivateMessage { common: ActivityCommonFields, } +impl UndoDeletePrivateMessage { + pub async fn send( + actor: &Person, + pm: &PrivateMessage, + context: &LemmyContext, + ) -> Result<(), LemmyError> { + let recipient_id = pm.recipient_id; + let recipient = + blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??; + + let object = DeletePrivateMessage { + to: recipient.actor_id(), + object: pm.ap_id.clone().into(), + kind: DeleteType::Delete, + common: ActivityCommonFields { + context: lemmy_context(), + id: generate_activity_id(DeleteType::Delete)?, + actor: actor.actor_id(), + unparsed: Default::default(), + }, + }; + + let id = generate_activity_id(UndoType::Undo)?; + let undo = UndoDeletePrivateMessage { + to: recipient.actor_id(), + object, + kind: UndoType::Undo, + common: ActivityCommonFields { + context: lemmy_context(), + id: id.clone(), + actor: actor.actor_id(), + unparsed: Default::default(), + }, + }; + let inbox = vec![recipient.get_shared_inbox_or_inbox_url()]; + send_activity_new(context, &undo, &id, actor, inbox, true).await + } +} + #[async_trait::async_trait(?Send)] impl ActivityHandler for UndoDeletePrivateMessage { async fn verify( diff --git a/crates/apub/src/activities/send/mod.rs b/crates/apub/src/activities/send/mod.rs index 65135bdda..c00ecb1a7 100644 --- a/crates/apub/src/activities/send/mod.rs +++ b/crates/apub/src/activities/send/mod.rs @@ -2,4 +2,3 @@ pub(crate) mod comment; pub(crate) mod community; pub(crate) mod person; pub(crate) mod post; -pub(crate) mod private_message; diff --git a/crates/apub/src/activities/send/private_message.rs b/crates/apub/src/activities/send/private_message.rs deleted file mode 100644 index edcff3771..000000000 --- a/crates/apub/src/activities/send/private_message.rs +++ /dev/null @@ -1,86 +0,0 @@ -use crate::{ - activities::generate_activity_id, - activity_queue::send_activity_single_dest, - extensions::context::lemmy_context, - ActorType, - ApubObjectType, -}; -use activitystreams::{ - activity::{ - kind::{DeleteType, UndoType}, - Delete, - Undo, - }, - base::{BaseExt, ExtendsExt}, - object::ObjectExt, -}; -use lemmy_api_common::blocking; -use lemmy_db_queries::Crud; -use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage}; -use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; - -#[async_trait::async_trait(?Send)] -impl ApubObjectType for PrivateMessage { - async fn send_delete(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> { - let recipient_id = self.recipient_id; - let recipient = - blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??; - - let mut delete = Delete::new( - creator.actor_id.to_owned().into_inner(), - self.ap_id.to_owned().into_inner(), - ); - delete - .set_many_contexts(lemmy_context()) - .set_id(generate_activity_id(DeleteType::Delete)?) - .set_to(recipient.actor_id()); - - send_activity_single_dest(delete, creator, recipient.inbox_url.into(), context).await?; - Ok(()) - } - - async fn send_undo_delete( - &self, - creator: &Person, - context: &LemmyContext, - ) -> Result<(), LemmyError> { - let recipient_id = self.recipient_id; - let recipient = - blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??; - - let mut delete = Delete::new( - creator.actor_id.to_owned().into_inner(), - self.ap_id.to_owned().into_inner(), - ); - delete - .set_many_contexts(lemmy_context()) - .set_id(generate_activity_id(DeleteType::Delete)?) - .set_to(recipient.actor_id()); - - // Undo that fake activity - let mut undo = Undo::new( - creator.actor_id.to_owned().into_inner(), - delete.into_any_base()?, - ); - undo - .set_many_contexts(lemmy_context()) - .set_id(generate_activity_id(UndoType::Undo)?) - .set_to(recipient.actor_id()); - - send_activity_single_dest(undo, creator, recipient.inbox_url.into(), context).await?; - Ok(()) - } - - async fn send_remove(&self, _mod_: &Person, _context: &LemmyContext) -> Result<(), LemmyError> { - unimplemented!() - } - - async fn send_undo_remove( - &self, - _mod_: &Person, - _context: &LemmyContext, - ) -> Result<(), LemmyError> { - unimplemented!() - } -} diff --git a/crates/apub/src/activity_queue.rs b/crates/apub/src/activity_queue.rs index c7ff0a81d..48cccc612 100644 --- a/crates/apub/src/activity_queue.rs +++ b/crates/apub/src/activity_queue.rs @@ -31,34 +31,6 @@ use serde::{Deserialize, Serialize}; use std::{collections::BTreeMap, env, fmt::Debug, future::Future, pin::Pin}; use url::Url; -/// Sends a local activity to a single, remote actor. -/// -/// * `activity` the apub activity to be sent -/// * `creator` the local actor which created the activity -/// * `inbox` the inbox url where the activity should be delivered to -pub(crate) async fn send_activity_single_dest( - activity: T, - creator: &dyn ActorType, - inbox: Url, - context: &LemmyContext, -) -> Result<(), LemmyError> -where - T: AsObject + Extends + Debug + BaseExt, - Kind: Serialize, - >::Error: From + Send + Sync + 'static, -{ - if check_is_apub_id_valid(&inbox, false).is_ok() { - debug!( - "Sending activity {:?} to {}", - &activity.id_unchecked().map(ToString::to_string), - &inbox - ); - send_activity_internal(context, activity, creator, vec![inbox], true, true).await?; - } - - Ok(()) -} - /// From a local community, send activity to all remote followers. /// /// * `activity` the apub activity to send