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 {
22 private_message: ApubPrivateMessage,
24 kind: CreateOrUpdateType,
25 context: &LemmyContext,
26 ) -> Result<(), LemmyError> {
27 let recipient_id = private_message.recipient_id;
28 let recipient: ApubPerson =
29 blocking(context.pool(), move |conn| Person::read(conn, recipient_id))
33 let id = generate_activity_id(
35 &context.settings().get_protocol_and_hostname(),
37 let create_or_update = CreateOrUpdatePrivateMessage {
39 actor: ObjectId::new(actor.actor_id()),
40 to: [ObjectId::new(recipient.actor_id())],
41 object: private_message.into_apub(context).await?,
43 unparsed: Default::default(),
45 let inbox = vec![recipient.shared_inbox_or_inbox_url()];
46 send_lemmy_activity(context, &create_or_update, &id, actor, inbox, true).await
49 #[async_trait::async_trait(?Send)]
50 impl ActivityHandler for CreateOrUpdatePrivateMessage {
51 type DataType = LemmyContext;
54 context: &Data<LemmyContext>,
55 request_counter: &mut i32,
56 ) -> Result<(), LemmyError> {
57 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
58 verify_person(&self.actor, context, request_counter).await?;
59 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
60 ApubPrivateMessage::verify(&self.object, self.actor.inner(), context, request_counter).await?;
66 context: &Data<LemmyContext>,
67 request_counter: &mut i32,
68 ) -> Result<(), LemmyError> {
70 ApubPrivateMessage::from_apub(self.object, context, request_counter).await?;
72 let notif_type = match self.kind {
73 CreateOrUpdateType::Create => UserOperationCrud::CreatePrivateMessage,
74 CreateOrUpdateType::Update => UserOperationCrud::EditPrivateMessage,
76 send_pm_ws_message(private_message.id, notif_type, None, context).await?;