2021-07-31 20:58:11 +00:00
|
|
|
use crate::{
|
2022-06-02 14:33:41 +00:00
|
|
|
activities::{generate_activity_id, send_lemmy_activity, verify_person},
|
2023-07-14 15:17:06 +00:00
|
|
|
insert_received_activity,
|
2021-10-28 21:17:59 +00:00
|
|
|
objects::{person::ApubPerson, private_message::ApubPrivateMessage},
|
2021-10-29 10:32:42 +00:00
|
|
|
protocol::activities::{
|
2022-12-01 20:52:49 +00:00
|
|
|
create_or_update::chat_message::CreateOrUpdateChatMessage,
|
2021-10-29 10:32:42 +00:00
|
|
|
CreateOrUpdateType,
|
|
|
|
},
|
2021-07-31 20:58:11 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
protocol::verification::verify_domains_match,
|
|
|
|
traits::{ActivityHandler, Actor, Object},
|
2021-10-06 20:20:05 +00:00
|
|
|
};
|
2023-08-02 16:52:41 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2023-09-09 16:25:03 +00:00
|
|
|
use lemmy_db_schema::source::activity::ActivitySendTargets;
|
2023-08-02 16:52:41 +00:00
|
|
|
use lemmy_db_views::structs::PrivateMessageView;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
|
|
|
use url::Url;
|
2021-07-31 20:58:11 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
pub(crate) async fn send_create_or_update_pm(
|
|
|
|
pm_view: PrivateMessageView,
|
|
|
|
kind: CreateOrUpdateType,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let actor: ApubPerson = pm_view.creator.into();
|
|
|
|
let recipient: ApubPerson = pm_view.recipient.into();
|
2022-11-28 14:29:33 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let id = generate_activity_id(
|
|
|
|
kind.clone(),
|
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?;
|
|
|
|
let create_or_update = CreateOrUpdateChatMessage {
|
|
|
|
id: id.clone(),
|
|
|
|
actor: actor.id().into(),
|
|
|
|
to: [recipient.id().into()],
|
|
|
|
object: ApubPrivateMessage(pm_view.private_message.clone())
|
|
|
|
.into_json(&context)
|
|
|
|
.await?,
|
|
|
|
kind,
|
|
|
|
};
|
2023-09-09 16:25:03 +00:00
|
|
|
let inbox = ActivitySendTargets::to_inbox(recipient.shared_inbox_or_inbox());
|
2023-08-02 16:52:41 +00:00
|
|
|
send_lemmy_activity(&context, create_or_update, &actor, inbox, true).await
|
2021-07-31 20:58:11 +00:00
|
|
|
}
|
2021-12-06 14:54:47 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-12-01 20:52:49 +00:00
|
|
|
impl ActivityHandler for CreateOrUpdateChatMessage {
|
2021-10-06 20:20:05 +00:00
|
|
|
type DataType = LemmyContext;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
|
|
|
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
self.actor.inner()
|
|
|
|
}
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
2023-07-14 15:17:06 +00:00
|
|
|
insert_received_activity(&self.id, context).await?;
|
2023-03-21 15:03:05 +00:00
|
|
|
verify_person(&self.actor, context).await?;
|
2021-11-03 16:26:09 +00:00
|
|
|
verify_domains_match(self.actor.inner(), self.object.id.inner())?;
|
2022-06-08 15:45:39 +00:00
|
|
|
verify_domains_match(self.to[0].inner(), self.object.to[0].inner())?;
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubPrivateMessage::verify(&self.object, self.actor.inner(), context).await?;
|
2021-07-31 20:58:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn receive(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
2023-06-06 16:27:22 +00:00
|
|
|
ApubPrivateMessage::from_json(self.object, context).await?;
|
2021-07-31 20:58:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|