2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
2021-09-25 15:44:52 +00:00
|
|
|
fetcher::object_id::ObjectId,
|
2021-10-28 21:17:59 +00:00
|
|
|
protocol::{
|
|
|
|
objects::chat_message::{ChatMessage, ChatMessageType},
|
|
|
|
Source,
|
|
|
|
},
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2021-10-28 21:17:59 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2021-10-22 16:21:26 +00:00
|
|
|
use html2md::parse_html;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-07-31 20:58:11 +00:00
|
|
|
use lemmy_apub_lib::{
|
2021-10-27 16:03:07 +00:00
|
|
|
traits::ApubObject,
|
2021-07-31 20:58:11 +00:00
|
|
|
values::{MediaTypeHtml, MediaTypeMarkdown},
|
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
person::Person,
|
|
|
|
private_message::{PrivateMessage, PrivateMessageForm},
|
|
|
|
},
|
|
|
|
traits::Crud,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2021-07-31 20:58:11 +00:00
|
|
|
use lemmy_utils::{utils::convert_datetime, LemmyError};
|
2020-10-12 14:10:09 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-10-18 21:36:44 +00:00
|
|
|
use std::ops::Deref;
|
2020-10-12 14:10:09 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ApubPrivateMessage(PrivateMessage);
|
|
|
|
|
|
|
|
impl Deref for ApubPrivateMessage {
|
|
|
|
type Target = PrivateMessage;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PrivateMessage> for ApubPrivateMessage {
|
|
|
|
fn from(pm: PrivateMessage) -> Self {
|
|
|
|
ApubPrivateMessage { 0: pm }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2021-10-18 21:36:44 +00:00
|
|
|
impl ApubObject for ApubPrivateMessage {
|
|
|
|
type DataType = LemmyContext;
|
2021-10-28 11:46:48 +00:00
|
|
|
type ApubType = ChatMessage;
|
2021-10-28 15:25:26 +00:00
|
|
|
type TombstoneType = ();
|
2021-10-18 21:36:44 +00:00
|
|
|
|
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn read_from_apub_id(
|
|
|
|
object_id: Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
PrivateMessage::read_from_apub_id(conn, object_id)
|
|
|
|
})
|
|
|
|
.await??
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete(self, _context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
// do nothing, because pm can't be fetched over http
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-10-28 11:46:48 +00:00
|
|
|
async fn to_apub(&self, context: &LemmyContext) -> Result<ChatMessage, LemmyError> {
|
2020-10-12 14:10:09 +00:00
|
|
|
let creator_id = self.creator_id;
|
2021-10-27 16:03:07 +00:00
|
|
|
let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
let recipient_id = self.recipient_id;
|
2021-10-27 16:03:07 +00:00
|
|
|
let recipient =
|
|
|
|
blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-10-28 11:46:48 +00:00
|
|
|
let note = ChatMessage {
|
2021-10-22 16:21:26 +00:00
|
|
|
r#type: ChatMessageType::ChatMessage,
|
2021-07-31 20:58:11 +00:00
|
|
|
id: self.ap_id.clone().into(),
|
2021-09-25 15:44:52 +00:00
|
|
|
attributed_to: ObjectId::new(creator.actor_id),
|
2021-10-22 16:21:26 +00:00
|
|
|
to: [ObjectId::new(recipient.actor_id)],
|
2021-07-31 20:58:11 +00:00
|
|
|
content: self.content.clone(),
|
2021-10-22 16:21:26 +00:00
|
|
|
media_type: Some(MediaTypeHtml::Html),
|
|
|
|
source: Some(Source {
|
2021-07-31 20:58:11 +00:00
|
|
|
content: self.content.clone(),
|
|
|
|
media_type: MediaTypeMarkdown::Markdown,
|
2021-10-22 16:21:26 +00:00
|
|
|
}),
|
2021-10-21 17:25:35 +00:00
|
|
|
published: Some(convert_datetime(self.published)),
|
2021-07-31 20:58:11 +00:00
|
|
|
updated: self.updated.map(convert_datetime),
|
|
|
|
unparsed: Default::default(),
|
|
|
|
};
|
|
|
|
Ok(note)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:25:26 +00:00
|
|
|
fn to_tombstone(&self) -> Result<(), LemmyError> {
|
2021-10-27 16:03:07 +00:00
|
|
|
unimplemented!()
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn from_apub(
|
2021-10-28 11:46:48 +00:00
|
|
|
note: &ChatMessage,
|
2020-10-12 14:10:09 +00:00
|
|
|
context: &LemmyContext,
|
2021-08-12 12:48:09 +00:00
|
|
|
expected_domain: &Url,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<ApubPrivateMessage, LemmyError> {
|
2021-08-12 12:48:09 +00:00
|
|
|
let ap_id = Some(note.id(expected_domain)?.clone().into());
|
2021-09-25 15:44:52 +00:00
|
|
|
let creator = note
|
|
|
|
.attributed_to
|
|
|
|
.dereference(context, request_counter)
|
|
|
|
.await?;
|
2021-10-22 16:21:26 +00:00
|
|
|
let recipient = note.to[0].dereference(context, request_counter).await?;
|
|
|
|
let content = if let Some(source) = ¬e.source {
|
|
|
|
source.content.clone()
|
|
|
|
} else {
|
|
|
|
parse_html(¬e.content)
|
|
|
|
};
|
2020-11-24 17:53:43 +00:00
|
|
|
|
2021-07-31 20:58:11 +00:00
|
|
|
let form = PrivateMessageForm {
|
2020-10-12 14:10:09 +00:00
|
|
|
creator_id: creator.id,
|
|
|
|
recipient_id: recipient.id,
|
2021-10-22 16:21:26 +00:00
|
|
|
content,
|
2021-10-21 17:25:35 +00:00
|
|
|
published: note.published.map(|u| u.to_owned().naive_local()),
|
2021-07-31 20:58:11 +00:00
|
|
|
updated: note.updated.map(|u| u.to_owned().naive_local()),
|
2020-10-12 14:10:09 +00:00
|
|
|
deleted: None,
|
|
|
|
read: None,
|
2021-08-12 12:48:09 +00:00
|
|
|
ap_id,
|
2021-03-20 20:59:07 +00:00
|
|
|
local: Some(false),
|
2021-07-31 20:58:11 +00:00
|
|
|
};
|
2021-10-18 21:36:44 +00:00
|
|
|
let pm = blocking(context.pool(), move |conn| {
|
|
|
|
PrivateMessage::upsert(conn, &form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
Ok(pm.into())
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-21 17:25:35 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-10-28 21:17:59 +00:00
|
|
|
use crate::objects::{
|
|
|
|
person::ApubPerson,
|
|
|
|
tests::{file_to_json_object, init_context},
|
|
|
|
};
|
2021-10-21 17:25:35 +00:00
|
|
|
use assert_json_diff::assert_json_include;
|
|
|
|
use serial_test::serial;
|
|
|
|
|
2021-10-22 16:21:26 +00:00
|
|
|
async fn prepare_comment_test(url: &Url, context: &LemmyContext) -> (ApubPerson, ApubPerson) {
|
2021-10-21 17:25:35 +00:00
|
|
|
let lemmy_person = file_to_json_object("assets/lemmy-person.json");
|
2021-10-22 16:21:26 +00:00
|
|
|
let person1 = ApubPerson::from_apub(&lemmy_person, context, url, &mut 0)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let pleroma_person = file_to_json_object("assets/pleroma-person.json");
|
|
|
|
let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
|
2021-10-22 16:21:26 +00:00
|
|
|
let person2 = ApubPerson::from_apub(&pleroma_person, context, &pleroma_url, &mut 0)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-10-22 16:21:26 +00:00
|
|
|
(person1, person2)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cleanup(data: (ApubPerson, ApubPerson), context: &LemmyContext) {
|
|
|
|
Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
|
|
|
|
Person::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_lemmy_pm() {
|
|
|
|
let context = init_context();
|
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
|
|
|
|
let data = prepare_comment_test(&url, &context).await;
|
2021-10-21 17:25:35 +00:00
|
|
|
let json = file_to_json_object("assets/lemmy-private-message.json");
|
|
|
|
let mut request_counter = 0;
|
|
|
|
let pm = ApubPrivateMessage::from_apub(&json, &context, &url, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(pm.ap_id.clone().into_inner(), url);
|
2021-10-22 16:21:26 +00:00
|
|
|
assert_eq!(pm.content.len(), 20);
|
2021-10-21 17:25:35 +00:00
|
|
|
assert_eq!(request_counter, 0);
|
|
|
|
|
2021-10-27 16:03:07 +00:00
|
|
|
let to_apub = pm.to_apub(&context).await.unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
assert_json_include!(actual: json, expected: to_apub);
|
|
|
|
|
|
|
|
PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap();
|
2021-10-22 16:21:26 +00:00
|
|
|
cleanup(data, &context);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_pleroma_pm() {
|
|
|
|
let context = init_context();
|
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
|
|
|
|
let data = prepare_comment_test(&url, &context).await;
|
|
|
|
let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap();
|
|
|
|
let json = file_to_json_object("assets/pleroma-private-message.json");
|
|
|
|
let mut request_counter = 0;
|
|
|
|
let pm = ApubPrivateMessage::from_apub(&json, &context, &pleroma_url, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(pm.ap_id.clone().into_inner(), pleroma_url);
|
|
|
|
assert_eq!(pm.content.len(), 3);
|
|
|
|
assert_eq!(request_counter, 0);
|
|
|
|
|
|
|
|
PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap();
|
|
|
|
cleanup(data, &context);
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
}
|