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 async fn read_from_apub_id(
57 context: &LemmyContext,
58 ) -> Result<Option<Self>, LemmyError> {
60 blocking(context.pool(), move |conn| {
61 PrivateMessage::read_from_apub_id(conn, object_id)
68 async fn delete(self, _context: &LemmyContext) -> Result<(), LemmyError> {
69 // do nothing, because pm can't be fetched over http
73 async fn to_apub(&self, context: &LemmyContext) -> Result<ChatMessage, LemmyError> {
74 let creator_id = self.creator_id;
75 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
77 let recipient_id = self.recipient_id;
79 blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??;
81 let note = ChatMessage {
82 r#type: ChatMessageType::ChatMessage,
83 id: ObjectId::new(self.ap_id.clone()),
84 attributed_to: ObjectId::new(creator.actor_id),
85 to: [ObjectId::new(recipient.actor_id)],
86 content: markdown_to_html(&self.content),
87 media_type: Some(MediaTypeHtml::Html),
89 content: self.content.clone(),
90 media_type: MediaTypeMarkdown::Markdown,
92 published: Some(convert_datetime(self.published)),
93 updated: self.updated.map(convert_datetime),
94 unparsed: Default::default(),
99 fn to_tombstone(&self) -> Result<(), LemmyError> {
105 context: &LemmyContext,
106 expected_domain: &Url,
107 request_counter: &mut i32,
108 ) -> Result<ApubPrivateMessage, LemmyError> {
109 verify_domains_match(note.id.inner(), expected_domain)?;
110 let ap_id = Some(note.id.clone().into());
113 .dereference(context, request_counter)
115 let recipient = note.to[0].dereference(context, request_counter).await?;
116 let content = if let Some(source) = ¬e.source {
117 source.content.clone()
119 parse_html(¬e.content)
122 let form = PrivateMessageForm {
123 creator_id: creator.id,
124 recipient_id: recipient.id,
126 published: note.published.map(|u| u.to_owned().naive_local()),
127 updated: note.updated.map(|u| u.to_owned().naive_local()),
133 let pm = blocking(context.pool(), move |conn| {
134 PrivateMessage::upsert(conn, &form)
144 use crate::objects::{
146 tests::{file_to_json_object, init_context},
148 use assert_json_diff::assert_json_include;
149 use serial_test::serial;
151 async fn prepare_comment_test(url: &Url, context: &LemmyContext) -> (ApubPerson, ApubPerson) {
152 let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json");
153 let person1 = ApubPerson::from_apub(&lemmy_person, context, url, &mut 0)
156 let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json");
157 let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
158 let person2 = ApubPerson::from_apub(&pleroma_person, context, &pleroma_url, &mut 0)
164 fn cleanup(data: (ApubPerson, ApubPerson), context: &LemmyContext) {
165 Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
166 Person::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
171 async fn test_parse_lemmy_pm() {
172 let context = init_context();
173 let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
174 let data = prepare_comment_test(&url, &context).await;
175 let json = file_to_json_object("assets/lemmy/objects/chat_message.json");
176 let mut request_counter = 0;
177 let pm = ApubPrivateMessage::from_apub(&json, &context, &url, &mut request_counter)
181 assert_eq!(pm.ap_id.clone(), url.into());
182 assert_eq!(pm.content.len(), 20);
183 assert_eq!(request_counter, 0);
185 let to_apub = pm.to_apub(&context).await.unwrap();
186 assert_json_include!(actual: json, expected: to_apub);
188 PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap();
189 cleanup(data, &context);
194 async fn test_parse_pleroma_pm() {
195 let context = init_context();
196 let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
197 let data = prepare_comment_test(&url, &context).await;
198 let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap();
199 let json = file_to_json_object("assets/pleroma/objects/chat_message.json");
200 let mut request_counter = 0;
201 let pm = ApubPrivateMessage::from_apub(&json, &context, &pleroma_url, &mut request_counter)
205 assert_eq!(pm.ap_id, pleroma_url.into());
206 assert_eq!(pm.content.len(), 3);
207 assert_eq!(request_counter, 0);
209 PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap();
210 cleanup(data, &context);