2 context::lemmy_context,
3 fetcher::object_id::ObjectId,
4 objects::{create_tombstone, FromApub, Source, ToApub},
8 object::{kind::NoteType, Tombstone},
13 use chrono::{DateTime, FixedOffset};
14 use lemmy_api_common::blocking;
16 values::{MediaTypeHtml, MediaTypeMarkdown},
17 verify::verify_domains_match,
19 use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud, DbPool};
20 use lemmy_db_schema::source::{
22 private_message::{PrivateMessage, PrivateMessageForm},
24 use lemmy_utils::{utils::convert_datetime, LemmyError};
25 use lemmy_websocket::LemmyContext;
26 use serde::{Deserialize, Serialize};
27 use serde_with::skip_serializing_none;
30 #[skip_serializing_none]
31 #[derive(Clone, Debug, Deserialize, Serialize)]
32 #[serde(rename_all = "camelCase")]
34 #[serde(rename = "@context")]
35 context: OneOrMany<AnyBase>,
38 pub(crate) attributed_to: ObjectId<Person>,
41 media_type: MediaTypeHtml,
43 published: DateTime<FixedOffset>,
44 updated: Option<DateTime<FixedOffset>>,
50 pub(crate) fn id_unchecked(&self) -> &Url {
53 pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
54 verify_domains_match(&self.id, expected_domain)?;
58 pub(crate) async fn verify(
60 context: &LemmyContext,
61 request_counter: &mut i32,
62 ) -> Result<(), LemmyError> {
63 verify_domains_match(self.attributed_to.inner(), &self.id)?;
66 .dereference(context, request_counter)
69 return Err(anyhow!("Person is banned from site").into());
75 #[async_trait::async_trait(?Send)]
76 impl ToApub for PrivateMessage {
79 async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
80 let creator_id = self.creator_id;
81 let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
83 let recipient_id = self.recipient_id;
84 let recipient = blocking(pool, move |conn| Person::read(conn, recipient_id)).await??;
87 context: lemmy_context(),
88 r#type: NoteType::Note,
89 id: self.ap_id.clone().into(),
90 attributed_to: ObjectId::new(creator.actor_id),
91 to: ObjectId::new(recipient.actor_id),
92 content: self.content.clone(),
93 media_type: MediaTypeHtml::Html,
95 content: self.content.clone(),
96 media_type: MediaTypeMarkdown::Markdown,
98 published: convert_datetime(self.published),
99 updated: self.updated.map(convert_datetime),
100 unparsed: Default::default(),
105 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
108 self.ap_id.to_owned().into(),
115 #[async_trait::async_trait(?Send)]
116 impl FromApub for PrivateMessage {
117 type ApubType = Note;
121 context: &LemmyContext,
122 expected_domain: &Url,
123 request_counter: &mut i32,
124 ) -> Result<PrivateMessage, LemmyError> {
125 let ap_id = Some(note.id(expected_domain)?.clone().into());
128 .dereference(context, request_counter)
130 let recipient = note.to.dereference(context, request_counter).await?;
132 let form = PrivateMessageForm {
133 creator_id: creator.id,
134 recipient_id: recipient.id,
135 content: note.source.content.clone(),
136 published: Some(note.published.naive_local()),
137 updated: note.updated.map(|u| u.to_owned().naive_local()),
144 blocking(context.pool(), move |conn| {
145 PrivateMessage::upsert(conn, &form)