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::{context::LemmyContext, utils::check_person_block};
16 use lemmy_db_schema::{
19 private_message::{PrivateMessage, PrivateMessageInsertForm},
24 error::{LemmyError, LemmyErrorType},
25 utils::{markdown::markdown_to_html, time::convert_datetime},
30 #[derive(Clone, Debug)]
31 pub struct ApubPrivateMessage(pub(crate) PrivateMessage);
33 impl Deref for ApubPrivateMessage {
34 type Target = PrivateMessage;
35 fn deref(&self) -> &Self::Target {
40 impl From<PrivateMessage> for ApubPrivateMessage {
41 fn from(pm: PrivateMessage) -> Self {
42 ApubPrivateMessage(pm)
46 #[async_trait::async_trait]
47 impl Object for ApubPrivateMessage {
48 type DataType = LemmyContext;
49 type Kind = ChatMessage;
50 type Error = LemmyError;
52 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
56 #[tracing::instrument(skip_all)]
57 async fn read_from_id(
59 context: &Data<Self::DataType>,
60 ) -> Result<Option<Self>, LemmyError> {
62 PrivateMessage::read_from_apub_id(&mut context.pool(), object_id)
68 async fn delete(self, _context: &Data<Self::DataType>) -> Result<(), LemmyError> {
69 // do nothing, because pm can't be fetched over http
73 #[tracing::instrument(skip_all)]
74 async fn into_json(self, context: &Data<Self::DataType>) -> Result<ChatMessage, LemmyError> {
75 let creator_id = self.creator_id;
76 let creator = Person::read(&mut context.pool(), creator_id).await?;
78 let recipient_id = self.recipient_id;
79 let recipient = Person::read(&mut context.pool(), recipient_id).await?;
81 let note = ChatMessage {
82 r#type: ChatMessageType::ChatMessage,
83 id: self.ap_id.clone().into(),
84 attributed_to: creator.actor_id.into(),
85 to: [recipient.actor_id.into()],
86 content: markdown_to_html(&self.content),
87 media_type: Some(MediaTypeHtml::Html),
88 source: Some(Source::new(self.content.clone())),
89 published: Some(convert_datetime(self.published)),
90 updated: self.updated.map(convert_datetime),
95 #[tracing::instrument(skip_all)]
98 expected_domain: &Url,
99 context: &Data<Self::DataType>,
100 ) -> Result<(), LemmyError> {
101 verify_domains_match(note.id.inner(), expected_domain)?;
102 verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
104 check_apub_id_valid_with_strictness(note.id.inner(), false, context).await?;
105 let person = note.attributed_to.dereference(context).await?;
107 return Err(LemmyErrorType::PersonIsBannedFromSite)?;
112 #[tracing::instrument(skip_all)]
115 context: &Data<Self::DataType>,
116 ) -> Result<ApubPrivateMessage, LemmyError> {
117 let creator = note.attributed_to.dereference(context).await?;
118 let recipient = note.to[0].dereference(context).await?;
119 check_person_block(creator.id, recipient.id, &mut context.pool()).await?;
121 let form = PrivateMessageInsertForm {
122 creator_id: creator.id,
123 recipient_id: recipient.id,
124 content: read_from_string_or_source(¬e.content, &None, ¬e.source),
125 published: note.published.map(|u| u.naive_local()),
126 updated: note.updated.map(|u| u.naive_local()),
127 deleted: Some(false),
129 ap_id: Some(note.id.into()),
132 let pm = PrivateMessage::create(&mut context.pool(), &form).await?;
139 #![allow(clippy::unwrap_used)]
140 #![allow(clippy::indexing_slicing)]
145 instance::{tests::parse_lemmy_instance, ApubSite},
149 protocol::tests::file_to_json_object,
151 use assert_json_diff::assert_json_include;
152 use lemmy_db_schema::source::site::Site;
153 use serial_test::serial;
155 async fn prepare_comment_test(
157 context: &Data<LemmyContext>,
158 ) -> (ApubPerson, ApubPerson, ApubSite) {
159 let context2 = context.reset_request_count();
160 let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json").unwrap();
161 let site = parse_lemmy_instance(&context2).await;
162 ApubPerson::verify(&lemmy_person, url, &context2)
165 let person1 = ApubPerson::from_json(lemmy_person, &context2)
168 let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
169 let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
170 ApubPerson::verify(&pleroma_person, &pleroma_url, &context2)
173 let person2 = ApubPerson::from_json(pleroma_person, &context2)
176 (person1, person2, site)
179 async fn cleanup(data: (ApubPerson, ApubPerson, ApubSite), context: &Data<LemmyContext>) {
180 Person::delete(&mut context.pool(), data.0.id)
183 Person::delete(&mut context.pool(), data.1.id)
186 Site::delete(&mut context.pool(), data.2.id).await.unwrap();
191 async fn test_parse_lemmy_pm() {
192 let context = init_context().await;
193 let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
194 let data = prepare_comment_test(&url, &context).await;
195 let json: ChatMessage = file_to_json_object("assets/lemmy/objects/chat_message.json").unwrap();
196 ApubPrivateMessage::verify(&json, &url, &context)
199 let pm = ApubPrivateMessage::from_json(json.clone(), &context)
203 assert_eq!(pm.ap_id.clone(), url.into());
204 assert_eq!(pm.content.len(), 20);
205 assert_eq!(context.request_count(), 0);
208 let to_apub = pm.into_json(&context).await.unwrap();
209 assert_json_include!(actual: json, expected: to_apub);
211 PrivateMessage::delete(&mut context.pool(), pm_id)
214 cleanup(data, &context).await;
219 async fn test_parse_pleroma_pm() {
220 let context = init_context().await;
221 let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
222 let data = prepare_comment_test(&url, &context).await;
223 let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap();
224 let json = file_to_json_object("assets/pleroma/objects/chat_message.json").unwrap();
225 ApubPrivateMessage::verify(&json, &pleroma_url, &context)
228 let pm = ApubPrivateMessage::from_json(json, &context).await.unwrap();
230 assert_eq!(pm.ap_id, pleroma_url.into());
231 assert_eq!(pm.content.len(), 3);
232 assert_eq!(context.request_count(), 0);
234 PrivateMessage::delete(&mut context.pool(), pm.id)
237 cleanup(data, &context).await;