2 activities::{generate_activity_id, send_lemmy_activity, verify_person},
3 objects::{person::ApubPerson, private_message::ApubPrivateMessage},
4 protocol::activities::{
5 create_or_update::private_message::CreateOrUpdatePrivateMessage,
10 use activitypub_federation::{
11 core::object_id::ObjectId,
13 traits::{ActivityHandler, Actor, ApubObject},
14 utils::verify_domains_match,
16 use lemmy_db_schema::{source::person::Person, traits::Crud};
17 use lemmy_utils::error::LemmyError;
18 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
21 impl CreateOrUpdatePrivateMessage {
22 #[tracing::instrument(skip_all)]
24 private_message: ApubPrivateMessage,
26 kind: CreateOrUpdateType,
27 context: &LemmyContext,
28 ) -> Result<(), LemmyError> {
29 let recipient_id = private_message.recipient_id;
30 let recipient: ApubPerson = Person::read(context.pool(), recipient_id).await?.into();
32 let id = generate_activity_id(
34 &context.settings().get_protocol_and_hostname(),
36 let create_or_update = CreateOrUpdatePrivateMessage {
38 actor: ObjectId::new(actor.actor_id()),
39 to: [ObjectId::new(recipient.actor_id())],
40 object: private_message.into_apub(context).await?,
43 let inbox = vec![recipient.shared_inbox_or_inbox()];
44 send_lemmy_activity(context, create_or_update, actor, inbox, true).await
48 #[async_trait::async_trait(?Send)]
49 impl ActivityHandler for CreateOrUpdatePrivateMessage {
50 type DataType = LemmyContext;
51 type Error = LemmyError;
53 fn id(&self) -> &Url {
57 fn actor(&self) -> &Url {
61 #[tracing::instrument(skip_all)]
64 context: &Data<LemmyContext>,
65 request_counter: &mut i32,
66 ) -> Result<(), LemmyError> {
67 verify_person(&self.actor, context, request_counter).await?;
68 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
69 verify_domains_match(self.to[0].inner(), self.object.to[0].inner())?;
70 ApubPrivateMessage::verify(&self.object, self.actor.inner(), context, request_counter).await?;
74 #[tracing::instrument(skip_all)]
77 context: &Data<LemmyContext>,
78 request_counter: &mut i32,
79 ) -> Result<(), LemmyError> {
81 ApubPrivateMessage::from_apub(self.object, context, request_counter).await?;
83 let notif_type = match self.kind {
84 CreateOrUpdateType::Create => UserOperationCrud::CreatePrivateMessage,
85 CreateOrUpdateType::Update => UserOperationCrud::EditPrivateMessage,
87 send_pm_ws_message(private_message.id, notif_type, None, context).await?;