Make Note.context optional for pleroma compat (hack)

This commit is contained in:
Felix Ableitner 2021-10-26 12:47:26 +02:00
parent 03b8ae7215
commit 74523fb534
4 changed files with 75 additions and 8 deletions

View file

@ -0,0 +1,52 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://greenish.red/schemas/litepub-0.1.jsonld",
{
"@language": "und"
}
],
"actor": "https://greenish.red/users/nutomic",
"cc": [
"https://greenish.red/users/nutomic/followers"
],
"context": "https://greenish.red/contexts/f6244742-0526-4b84-ac4f-ceadf1fb4e56",
"context_id": 6336544,
"directMessage": false,
"id": "https://greenish.red/activities/db61d52b-9c35-486a-bf27-bbd4edc6c6a1",
"object": {
"actor": "https://greenish.red/users/nutomic",
"attachment": [],
"attributedTo": "https://greenish.red/users/nutomic",
"cc": [
"https://greenish.red/users/nutomic/followers"
],
"content": "<span class=\"h-card\"><a class=\"u-url mention\" data-user=\"ACimPLEXPDd7enu3cm\" href=\"https://enterprise.lemmy.ml/u/picard\" rel=\"ugc\">@<span>lanodan</span></a></span> test",
"context": "https://greenish.red/contexts/f6244742-0526-4b84-ac4f-ceadf1fb4e56",
"conversation": "https://greenish.red/contexts/f6244742-0526-4b84-ac4f-ceadf1fb4e56",
"id": "https://greenish.red/objects/1a522f2e-d5ab-454b-93d7-e58bc0650c2a",
"inReplyTo": "https://enterprise.lemmy.ml/post/55143",
"published": "2021-10-26T10:28:35.602455Z",
"sensitive": false,
"source": "@lanodan@ds9.lemmy.ml test",
"summary": "",
"tag": [
{
"href": "https://enterprise.lemmy.ml/u/picard",
"name": "@lanodan@ds9.lemmy.ml",
"type": "Mention"
}
],
"to": [
"https://enterprise.lemmy.ml/u/picard",
"https://www.w3.org/ns/activitystreams#Public"
],
"type": "Note"
},
"published": "2021-10-26T10:28:35.595650Z",
"to": [
"https://enterprise.lemmy.ml/u/picard",
"https://www.w3.org/ns/activitystreams#Public"
],
"type": "Create"
}

View file

@ -47,6 +47,7 @@ pub struct CreateOrUpdateComment {
to: Vec<Url>, to: Vec<Url>,
object: Note, object: Note,
cc: Vec<Url>, cc: Vec<Url>,
#[serde(default)]
tag: Vec<Mention>, tag: Vec<Mention>,
#[serde(rename = "type")] #[serde(rename = "type")]
kind: CreateOrUpdateType, kind: CreateOrUpdateType,
@ -142,3 +143,16 @@ impl ActivityHandler for CreateOrUpdateComment {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use crate::objects::tests::file_to_json_object;
use serial_test::serial;
#[actix_rt::test]
#[serial]
async fn test_parse_pleroma_create_comment() {
let _: CreateOrUpdateComment = file_to_json_object("assets/pleroma-create-comment.json");
}
}

View file

@ -46,8 +46,11 @@ use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Note { pub struct Note {
/// Necessary to make this optional to make Pleroma Create/Note work.
/// TODO: change this so that context is not defined in the struct itself, but added in activity
/// queue and http handlers
#[serde(rename = "@context")] #[serde(rename = "@context")]
context: OneOrMany<AnyBase>, context: Option<OneOrMany<AnyBase>>,
r#type: NoteType, r#type: NoteType,
id: Url, id: Url,
pub(crate) attributed_to: ObjectId<ApubPerson>, pub(crate) attributed_to: ObjectId<ApubPerson>,
@ -202,7 +205,7 @@ impl ApubObject for ApubComment {
}; };
let note = Note { let note = Note {
context: lemmy_context(), context: Some(lemmy_context()),
r#type: NoteType::Note, r#type: NoteType::Note,
id: self.ap_id.to_owned().into_inner(), id: self.ap_id.to_owned().into_inner(),
attributed_to: ObjectId::new(creator.actor_id), attributed_to: ObjectId::new(creator.actor_id),
@ -274,7 +277,7 @@ impl ApubObject for ApubComment {
} }
#[cfg(test)] #[cfg(test)]
mod tests { pub(crate) mod tests {
use super::*; use super::*;
use crate::objects::{ use crate::objects::{
community::ApubCommunity, community::ApubCommunity,
@ -283,7 +286,7 @@ mod tests {
use assert_json_diff::assert_json_include; use assert_json_diff::assert_json_include;
use serial_test::serial; use serial_test::serial;
async fn prepare_comment_test( pub(crate) async fn prepare_comment_test(
url: &Url, url: &Url,
context: &LemmyContext, context: &LemmyContext,
) -> (ApubPerson, ApubCommunity, ApubPost) { ) -> (ApubPerson, ApubCommunity, ApubPost) {
@ -310,9 +313,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
#[serial] #[serial]
async fn test_parse_lemmy_comment() { pub(crate) async fn test_parse_lemmy_comment() {
// TODO: changed ObjectId::dereference() so that it always fetches if
// last_refreshed_at() == None. But post doesnt store that and expects to never be refetched
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

@ -38,7 +38,7 @@ fn get_summary_from_string_or_source(
} }
#[cfg(test)] #[cfg(test)]
mod tests { pub(crate) mod tests {
use actix::Actor; use actix::Actor;
use diesel::{ use diesel::{
r2d2::{ConnectionManager, Pool}, r2d2::{ConnectionManager, Pool},