Rewrite private message activity sending

This commit is contained in:
Felix Ableitner 2021-08-07 19:08:21 +02:00
parent 83e1bf311e
commit 99ffa1f22c
7 changed files with 107 additions and 143 deletions

View file

@ -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?;
}

View file

@ -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)]

View file

@ -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(

View file

@ -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(

View file

@ -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;

View file

@ -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!()
}
}

View file

@ -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<T, Kind>(
activity: T,
creator: &dyn ActorType,
inbox: Url,
context: &LemmyContext,
) -> Result<(), LemmyError>
where
T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
Kind: Serialize,
<T as Extends<Kind>>::Error: From<serde_json::Error> + 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