2 activities::{generate_activity_id, verify_activity, verify_person, CreateOrUpdateType},
3 activity_queue::send_activity_new,
4 extensions::context::lemmy_context,
5 objects::{private_message::Note, FromApub, ToApub},
8 use lemmy_api_common::blocking;
9 use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandler};
10 use lemmy_db_queries::Crud;
11 use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage};
12 use lemmy_utils::LemmyError;
13 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
16 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
17 #[serde(rename_all = "camelCase")]
18 pub struct CreateOrUpdatePrivateMessage {
21 #[serde(rename = "type")]
22 kind: CreateOrUpdateType,
24 common: ActivityCommonFields,
27 impl CreateOrUpdatePrivateMessage {
29 private_message: &PrivateMessage,
31 kind: CreateOrUpdateType,
32 context: &LemmyContext,
33 ) -> Result<(), LemmyError> {
34 let recipient_id = private_message.recipient_id;
36 blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??;
38 let id = generate_activity_id(kind.clone())?;
39 let create_or_update = CreateOrUpdatePrivateMessage {
40 to: recipient.actor_id(),
41 object: private_message.to_apub(context.pool()).await?,
43 common: ActivityCommonFields {
44 context: lemmy_context(),
46 actor: actor.actor_id(),
47 unparsed: Default::default(),
50 let inbox = vec![recipient.get_shared_inbox_or_inbox_url()];
51 send_activity_new(context, &create_or_update, &id, actor, inbox, true).await
54 #[async_trait::async_trait(?Send)]
55 impl ActivityHandler for CreateOrUpdatePrivateMessage {
58 context: &LemmyContext,
59 request_counter: &mut i32,
60 ) -> Result<(), LemmyError> {
61 verify_activity(self.common())?;
62 verify_person(&self.common.actor, context, request_counter).await?;
63 verify_domains_match(&self.common.actor, self.object.id_unchecked())?;
64 self.object.verify(context, request_counter).await?;
70 context: &LemmyContext,
71 request_counter: &mut i32,
72 ) -> Result<(), LemmyError> {
74 PrivateMessage::from_apub(&self.object, context, &self.common.actor, request_counter).await?;
76 let notif_type = match self.kind {
77 CreateOrUpdateType::Create => UserOperationCrud::CreatePrivateMessage,
78 CreateOrUpdateType::Update => UserOperationCrud::EditPrivateMessage,
80 send_pm_ws_message(private_message.id, notif_type, None, context).await?;
85 fn common(&self) -> &ActivityCommonFields {