2 objects::chat_message::{ChatMessage, ChatMessageType},
5 use chrono::NaiveDateTime;
6 use html2md::parse_html;
7 use lemmy_api_common::blocking;
11 values::{MediaTypeHtml, MediaTypeMarkdown},
12 verify::verify_domains_match,
14 use lemmy_db_schema::{
17 private_message::{PrivateMessage, PrivateMessageForm},
22 utils::{convert_datetime, markdown_to_html},
25 use lemmy_websocket::LemmyContext;
29 #[derive(Clone, Debug)]
30 pub struct ApubPrivateMessage(PrivateMessage);
32 impl Deref for ApubPrivateMessage {
33 type Target = PrivateMessage;
34 fn deref(&self) -> &Self::Target {
39 impl From<PrivateMessage> for ApubPrivateMessage {
40 fn from(pm: PrivateMessage) -> Self {
41 ApubPrivateMessage { 0: pm }
45 #[async_trait::async_trait(?Send)]
46 impl ApubObject for ApubPrivateMessage {
47 type DataType = LemmyContext;
48 type ApubType = ChatMessage;
49 type TombstoneType = ();
51 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
55 #[tracing::instrument(skip_all)]
56 async fn read_from_apub_id(
58 context: &LemmyContext,
59 ) -> Result<Option<Self>, LemmyError> {
61 blocking(context.pool(), move |conn| {
62 PrivateMessage::read_from_apub_id(conn, object_id)
69 async fn delete(self, _context: &LemmyContext) -> Result<(), LemmyError> {
70 // do nothing, because pm can't be fetched over http
74 #[tracing::instrument(skip_all)]
75 async fn into_apub(self, context: &LemmyContext) -> Result<ChatMessage, LemmyError> {
76 let creator_id = self.creator_id;
77 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
79 let recipient_id = self.recipient_id;
81 blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??;
83 let note = ChatMessage {
84 r#type: ChatMessageType::ChatMessage,
85 id: ObjectId::new(self.ap_id.clone()),
86 attributed_to: ObjectId::new(creator.actor_id),
87 to: [ObjectId::new(recipient.actor_id)],
88 content: markdown_to_html(&self.content),
89 media_type: Some(MediaTypeHtml::Html),
91 content: self.content.clone(),
92 media_type: MediaTypeMarkdown::Markdown,
94 published: Some(convert_datetime(self.published)),
95 updated: self.updated.map(convert_datetime),
96 unparsed: Default::default(),
101 fn to_tombstone(&self) -> Result<(), LemmyError> {
105 #[tracing::instrument(skip_all)]
108 expected_domain: &Url,
109 context: &LemmyContext,
110 request_counter: &mut i32,
111 ) -> Result<(), LemmyError> {
112 verify_domains_match(note.id.inner(), expected_domain)?;
113 verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
116 .dereference(context, request_counter)
119 return Err(LemmyError::from_message("Person is banned from site"));
124 #[tracing::instrument(skip_all)]
127 context: &LemmyContext,
128 request_counter: &mut i32,
129 ) -> Result<ApubPrivateMessage, LemmyError> {
132 .dereference(context, request_counter)
134 let recipient = note.to[0].dereference(context, request_counter).await?;
135 let content = if let Some(source) = ¬e.source {
136 source.content.clone()
138 parse_html(¬e.content)
141 let form = PrivateMessageForm {
142 creator_id: creator.id,
143 recipient_id: recipient.id,
145 published: note.published.map(|u| u.naive_local()),
146 updated: note.updated.map(|u| u.naive_local()),
149 ap_id: Some(note.id.into()),
152 let pm = blocking(context.pool(), move |conn| {
153 PrivateMessage::upsert(conn, &form)
163 use crate::objects::{
165 tests::{file_to_json_object, init_context},
167 use assert_json_diff::assert_json_include;
168 use lemmy_apub_lib::activity_queue::create_activity_queue;
169 use serial_test::serial;
171 async fn prepare_comment_test(url: &Url, context: &LemmyContext) -> (ApubPerson, ApubPerson) {
172 let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json");
173 ApubPerson::verify(&lemmy_person, url, context, &mut 0)
176 let person1 = ApubPerson::from_apub(lemmy_person, context, &mut 0)
179 let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json");
180 let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
181 ApubPerson::verify(&pleroma_person, &pleroma_url, context, &mut 0)
184 let person2 = ApubPerson::from_apub(pleroma_person, context, &mut 0)
190 fn cleanup(data: (ApubPerson, ApubPerson), context: &LemmyContext) {
191 Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
192 Person::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
197 async fn test_parse_lemmy_pm() {
198 let manager = create_activity_queue();
199 let context = init_context(manager.queue_handle().clone());
200 let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
201 let data = prepare_comment_test(&url, &context).await;
202 let json: ChatMessage = file_to_json_object("assets/lemmy/objects/chat_message.json");
203 let mut request_counter = 0;
204 ApubPrivateMessage::verify(&json, &url, &context, &mut request_counter)
207 let pm = ApubPrivateMessage::from_apub(json.clone(), &context, &mut request_counter)
211 assert_eq!(pm.ap_id.clone(), url.into());
212 assert_eq!(pm.content.len(), 20);
213 assert_eq!(request_counter, 0);
216 let to_apub = pm.into_apub(&context).await.unwrap();
217 assert_json_include!(actual: json, expected: to_apub);
219 PrivateMessage::delete(&*context.pool().get().unwrap(), pm_id).unwrap();
220 cleanup(data, &context);
225 async fn test_parse_pleroma_pm() {
226 let manager = create_activity_queue();
227 let context = init_context(manager.queue_handle().clone());
228 let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
229 let data = prepare_comment_test(&url, &context).await;
230 let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap();
231 let json = file_to_json_object("assets/pleroma/objects/chat_message.json");
232 let mut request_counter = 0;
233 ApubPrivateMessage::verify(&json, &pleroma_url, &context, &mut request_counter)
236 let pm = ApubPrivateMessage::from_apub(json, &context, &mut request_counter)
240 assert_eq!(pm.ap_id, pleroma_url.into());
241 assert_eq!(pm.content.len(), 3);
242 assert_eq!(request_counter, 0);
244 PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap();
245 cleanup(data, &context);