Also add test case for parsing pleroma private message

This commit is contained in:
Felix Ableitner 2021-10-22 14:44:21 +02:00
parent 88fa0dbada
commit b462fa9aac
7 changed files with 75 additions and 22 deletions

View file

@ -0,0 +1,17 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://queer.hacktivis.me/schemas/litepub-0.1.jsonld",
{
"@language": "und"
}
],
"attributedTo": "https://queer.hacktivis.me/users/lanodan",
"content": "Hi!",
"id": "https://queer.hacktivis.me/objects/2",
"published": "2020-02-12T14:08:20Z",
"to": [
"https://enterprise.lemmy.ml/u/picard"
],
"type": "ChatMessage"
}

View file

@ -323,7 +323,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
#[serial] #[serial]
async fn test_fetch_lemmy_comment() { async fn test_parse_lemmy_comment() {
let context = init_context(); let context = init_context();
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap(); let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
let data = prepare_comment_test(&url, &context).await; let data = prepare_comment_test(&url, &context).await;
@ -348,7 +348,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
#[serial] #[serial]
async fn test_fetch_pleroma_comment() { async fn test_parse_pleroma_comment() {
let context = init_context(); let context = init_context();
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap(); let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
let data = prepare_comment_test(&url, &context).await; let data = prepare_comment_test(&url, &context).await;

View file

@ -322,7 +322,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
#[serial] #[serial]
async fn test_fetch_lemmy_community() { async fn test_parse_lemmy_community() {
let context = init_context(); let context = init_context();
let mut json: Group = file_to_json_object("assets/lemmy-community.json"); let mut json: Group = file_to_json_object("assets/lemmy-community.json");
let json_orig = json.clone(); let json_orig = json.clone();

View file

@ -275,7 +275,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
#[serial] #[serial]
async fn test_fetch_lemmy_person() { async fn test_parse_lemmy_person() {
let context = init_context(); let context = init_context();
let json = file_to_json_object("assets/lemmy-person.json"); let json = file_to_json_object("assets/lemmy-person.json");
let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap(); let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap();
@ -299,7 +299,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
#[serial] #[serial]
async fn test_fetch_pleroma_person() { async fn test_parse_pleroma_person() {
let context = init_context(); let context = init_context();
let json = file_to_json_object("assets/pleroma-person.json"); let json = file_to_json_object("assets/pleroma-person.json");
let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap(); let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();

View file

@ -290,7 +290,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
#[serial] #[serial]
async fn test_fetch_lemmy_post() { async fn test_parse_lemmy_post() {
let context = init_context(); let context = init_context();
let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap(); let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
let community_json = file_to_json_object("assets/lemmy-community.json"); let community_json = file_to_json_object("assets/lemmy-community.json");

View file

@ -12,6 +12,7 @@ use activitystreams::{
}; };
use anyhow::anyhow; use anyhow::anyhow;
use chrono::{DateTime, FixedOffset}; use chrono::{DateTime, FixedOffset};
use html2md::parse_html;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub_lib::{ use lemmy_apub_lib::{
traits::{ApubObject, FromApub, ToApub}, traits::{ApubObject, FromApub, ToApub},
@ -44,8 +45,8 @@ pub struct Note {
pub(crate) attributed_to: ObjectId<ApubPerson>, pub(crate) attributed_to: ObjectId<ApubPerson>,
to: [ObjectId<ApubPerson>; 1], to: [ObjectId<ApubPerson>; 1],
content: String, content: String,
media_type: MediaTypeHtml, media_type: Option<MediaTypeHtml>,
source: Source, source: Option<Source>,
published: Option<DateTime<FixedOffset>>, published: Option<DateTime<FixedOffset>>,
updated: Option<DateTime<FixedOffset>>, updated: Option<DateTime<FixedOffset>>,
#[serde(flatten)] #[serde(flatten)]
@ -147,11 +148,11 @@ impl ToApub for ApubPrivateMessage {
attributed_to: ObjectId::new(creator.actor_id), attributed_to: ObjectId::new(creator.actor_id),
to: [ObjectId::new(recipient.actor_id)], to: [ObjectId::new(recipient.actor_id)],
content: self.content.clone(), content: self.content.clone(),
media_type: MediaTypeHtml::Html, media_type: Some(MediaTypeHtml::Html),
source: Source { source: Some(Source {
content: self.content.clone(), content: self.content.clone(),
media_type: MediaTypeMarkdown::Markdown, media_type: MediaTypeMarkdown::Markdown,
}, }),
published: Some(convert_datetime(self.published)), published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime), updated: self.updated.map(convert_datetime),
unparsed: Default::default(), unparsed: Default::default(),
@ -186,11 +187,16 @@ impl FromApub for ApubPrivateMessage {
.dereference(context, request_counter) .dereference(context, request_counter)
.await?; .await?;
let recipient = note.to[0].dereference(context, request_counter).await?; let recipient = note.to[0].dereference(context, request_counter).await?;
let content = if let Some(source) = &note.source {
source.content.clone()
} else {
parse_html(&note.content)
};
let form = PrivateMessageForm { let form = PrivateMessageForm {
creator_id: creator.id, creator_id: creator.id,
recipient_id: recipient.id, recipient_id: recipient.id,
content: note.source.content.clone(), content,
published: note.published.map(|u| u.to_owned().naive_local()), published: note.published.map(|u| u.to_owned().naive_local()),
updated: note.updated.map(|u| u.to_owned().naive_local()), updated: note.updated.map(|u| u.to_owned().naive_local()),
deleted: None, deleted: None,
@ -213,20 +219,30 @@ mod tests {
use assert_json_diff::assert_json_include; use assert_json_diff::assert_json_include;
use serial_test::serial; use serial_test::serial;
#[actix_rt::test] async fn prepare_comment_test(url: &Url, context: &LemmyContext) -> (ApubPerson, ApubPerson) {
#[serial]
async fn test_fetch_lemmy_pm() {
let context = init_context();
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
let lemmy_person = file_to_json_object("assets/lemmy-person.json"); let lemmy_person = file_to_json_object("assets/lemmy-person.json");
let person1 = ApubPerson::from_apub(&lemmy_person, &context, &url, &mut 0) let person1 = ApubPerson::from_apub(&lemmy_person, context, url, &mut 0)
.await .await
.unwrap(); .unwrap();
let pleroma_person = file_to_json_object("assets/pleroma-person.json"); let pleroma_person = file_to_json_object("assets/pleroma-person.json");
let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap(); let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
let person2 = ApubPerson::from_apub(&pleroma_person, &context, &pleroma_url, &mut 0) let person2 = ApubPerson::from_apub(&pleroma_person, context, &pleroma_url, &mut 0)
.await .await
.unwrap(); .unwrap();
(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;
let json = file_to_json_object("assets/lemmy-private-message.json"); let json = file_to_json_object("assets/lemmy-private-message.json");
let mut request_counter = 0; let mut request_counter = 0;
let pm = ApubPrivateMessage::from_apub(&json, &context, &url, &mut request_counter) let pm = ApubPrivateMessage::from_apub(&json, &context, &url, &mut request_counter)
@ -241,7 +257,27 @@ mod tests {
assert_json_include!(actual: json, expected: to_apub); assert_json_include!(actual: json, expected: to_apub);
PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap(); PrivateMessage::delete(&*context.pool().get().unwrap(), pm.id).unwrap();
Person::delete(&*context.pool().get().unwrap(), person1.id).unwrap(); cleanup(data, &context);
Person::delete(&*context.pool().get().unwrap(), person2.id).unwrap(); }
#[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);
} }
} }