2 activities::{generate_activity_id, send_lemmy_activity, verify_activity, verify_person},
3 objects::{person::ApubPerson, private_message::ApubPrivateMessage},
4 protocol::activities::{
5 private_message::create_or_update::CreateOrUpdatePrivateMessage,
9 use lemmy_api_common::blocking;
13 traits::{ActivityHandler, ActorType, ApubObject},
14 verify::verify_domains_match,
16 use lemmy_db_schema::{source::person::Person, traits::Crud};
17 use lemmy_utils::LemmyError;
18 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
20 impl CreateOrUpdatePrivateMessage {
21 #[tracing::instrument(skip_all)]
23 private_message: ApubPrivateMessage,
25 kind: CreateOrUpdateType,
26 context: &LemmyContext,
27 ) -> Result<(), LemmyError> {
28 let recipient_id = private_message.recipient_id;
29 let recipient: ApubPerson =
30 blocking(context.pool(), move |conn| Person::read(conn, recipient_id))
34 let id = generate_activity_id(
36 &context.settings().get_protocol_and_hostname(),
38 let create_or_update = CreateOrUpdatePrivateMessage {
40 actor: ObjectId::new(actor.actor_id()),
41 to: [ObjectId::new(recipient.actor_id())],
42 object: private_message.into_apub(context).await?,
44 unparsed: Default::default(),
46 let inbox = vec![recipient.shared_inbox_or_inbox_url()];
47 send_lemmy_activity(context, &create_or_update, &id, actor, inbox, true).await
51 #[async_trait::async_trait(?Send)]
52 impl ActivityHandler for CreateOrUpdatePrivateMessage {
53 type DataType = LemmyContext;
55 #[tracing::instrument(skip_all)]
58 context: &Data<LemmyContext>,
59 request_counter: &mut i32,
60 ) -> Result<(), LemmyError> {
61 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
62 verify_person(&self.actor, context, request_counter).await?;
63 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
64 ApubPrivateMessage::verify(&self.object, self.actor.inner(), context, request_counter).await?;
68 #[tracing::instrument(skip_all)]
71 context: &Data<LemmyContext>,
72 request_counter: &mut i32,
73 ) -> Result<(), LemmyError> {
75 ApubPrivateMessage::from_apub(self.object, context, request_counter).await?;
77 let notif_type = match self.kind {
78 CreateOrUpdateType::Create => UserOperationCrud::CreatePrivateMessage,
79 CreateOrUpdateType::Update => UserOperationCrud::EditPrivateMessage,
81 send_pm_ws_message(private_message.id, notif_type, None, context).await?;