2 check_apub_id_valid_with_strictness,
3 objects::read_from_string_or_source,
5 objects::chat_message::{ChatMessage, ChatMessageType},
9 use activitypub_federation::{
11 protocol::{values::MediaTypeHtml, verification::verify_domains_match},
14 use chrono::NaiveDateTime;
15 use lemmy_api_common::{
16 context::LemmyContext,
17 utils::{check_person_block, sanitize_html},
19 use lemmy_db_schema::{
22 private_message::{PrivateMessage, PrivateMessageInsertForm},
27 error::{LemmyError, LemmyErrorType},
28 utils::{markdown::markdown_to_html, time::convert_datetime},
33 #[derive(Clone, Debug)]
34 pub struct ApubPrivateMessage(pub(crate) PrivateMessage);
36 impl Deref for ApubPrivateMessage {
37 type Target = PrivateMessage;
38 fn deref(&self) -> &Self::Target {
43 impl From<PrivateMessage> for ApubPrivateMessage {
44 fn from(pm: PrivateMessage) -> Self {
45 ApubPrivateMessage(pm)
49 #[async_trait::async_trait]
50 impl Object for ApubPrivateMessage {
51 type DataType = LemmyContext;
52 type Kind = ChatMessage;
53 type Error = LemmyError;
55 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
59 #[tracing::instrument(skip_all)]
60 async fn read_from_id(
62 context: &Data<Self::DataType>,
63 ) -> Result<Option<Self>, LemmyError> {
65 PrivateMessage::read_from_apub_id(&mut context.pool(), object_id)
71 async fn delete(self, _context: &Data<Self::DataType>) -> Result<(), LemmyError> {
72 // do nothing, because pm can't be fetched over http
76 #[tracing::instrument(skip_all)]
77 async fn into_json(self, context: &Data<Self::DataType>) -> Result<ChatMessage, LemmyError> {
78 let creator_id = self.creator_id;
79 let creator = Person::read(&mut context.pool(), creator_id).await?;
81 let recipient_id = self.recipient_id;
82 let recipient = Person::read(&mut context.pool(), recipient_id).await?;
84 let note = ChatMessage {
85 r#type: ChatMessageType::ChatMessage,
86 id: self.ap_id.clone().into(),
87 attributed_to: creator.actor_id.into(),
88 to: [recipient.actor_id.into()],
89 content: markdown_to_html(&self.content),
90 media_type: Some(MediaTypeHtml::Html),
91 source: Some(Source::new(self.content.clone())),
92 published: Some(convert_datetime(self.published)),
93 updated: self.updated.map(convert_datetime),
98 #[tracing::instrument(skip_all)]
101 expected_domain: &Url,
102 context: &Data<Self::DataType>,
103 ) -> Result<(), LemmyError> {
104 verify_domains_match(note.id.inner(), expected_domain)?;
105 verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
107 check_apub_id_valid_with_strictness(note.id.inner(), false, context).await?;
108 let person = note.attributed_to.dereference(context).await?;
110 return Err(LemmyErrorType::PersonIsBannedFromSite)?;
115 #[tracing::instrument(skip_all)]
118 context: &Data<Self::DataType>,
119 ) -> Result<ApubPrivateMessage, LemmyError> {
120 let creator = note.attributed_to.dereference(context).await?;
121 let recipient = note.to[0].dereference(context).await?;
122 check_person_block(creator.id, recipient.id, &mut context.pool()).await?;
124 let content = read_from_string_or_source(¬e.content, &None, ¬e.source);
125 let content = sanitize_html(&content);
127 let form = PrivateMessageInsertForm {
128 creator_id: creator.id,
129 recipient_id: recipient.id,
131 published: note.published.map(|u| u.naive_local()),
132 updated: note.updated.map(|u| u.naive_local()),
133 deleted: Some(false),
135 ap_id: Some(note.id.into()),
138 let pm = PrivateMessage::create(&mut context.pool(), &form).await?;
145 #![allow(clippy::unwrap_used)]
146 #![allow(clippy::indexing_slicing)]
151 instance::{tests::parse_lemmy_instance, ApubSite},
155 protocol::tests::file_to_json_object,
157 use assert_json_diff::assert_json_include;
158 use lemmy_db_schema::source::site::Site;
159 use serial_test::serial;
161 async fn prepare_comment_test(
163 context: &Data<LemmyContext>,
164 ) -> (ApubPerson, ApubPerson, ApubSite) {
165 let context2 = context.reset_request_count();
166 let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json").unwrap();
167 let site = parse_lemmy_instance(&context2).await;
168 ApubPerson::verify(&lemmy_person, url, &context2)
171 let person1 = ApubPerson::from_json(lemmy_person, &context2)
174 let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
175 let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
176 ApubPerson::verify(&pleroma_person, &pleroma_url, &context2)
179 let person2 = ApubPerson::from_json(pleroma_person, &context2)
182 (person1, person2, site)
185 async fn cleanup(data: (ApubPerson, ApubPerson, ApubSite), context: &Data<LemmyContext>) {
186 Person::delete(&mut context.pool(), data.0.id)
189 Person::delete(&mut context.pool(), data.1.id)
192 Site::delete(&mut context.pool(), data.2.id).await.unwrap();
197 async fn test_parse_lemmy_pm() {
198 let context = init_context().await;
199 let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
200 let data = prepare_comment_test(&url, &context).await;
201 let json: ChatMessage = file_to_json_object("assets/lemmy/objects/chat_message.json").unwrap();
202 ApubPrivateMessage::verify(&json, &url, &context)
205 let pm = ApubPrivateMessage::from_json(json.clone(), &context)
209 assert_eq!(pm.ap_id.clone(), url.into());
210 assert_eq!(pm.content.len(), 20);
211 assert_eq!(context.request_count(), 0);
214 let to_apub = pm.into_json(&context).await.unwrap();
215 assert_json_include!(actual: json, expected: to_apub);
217 PrivateMessage::delete(&mut context.pool(), pm_id)
220 cleanup(data, &context).await;
225 async fn test_parse_pleroma_pm() {
226 let context = init_context().await;
227 let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
228 let data = prepare_comment_test(&url, &context).await;
229 let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap();
230 let json = file_to_json_object("assets/pleroma/objects/chat_message.json").unwrap();
231 ApubPrivateMessage::verify(&json, &pleroma_url, &context)
234 let pm = ApubPrivateMessage::from_json(json, &context).await.unwrap();
236 assert_eq!(pm.ap_id, pleroma_url.into());
237 assert_eq!(pm.content.len(), 3);
238 assert_eq!(context.request_count(), 0);
240 PrivateMessage::delete(&mut context.pool(), pm.id)
243 cleanup(data, &context).await;