From b0431847eccfb49b8bac68a2dd37c09315e53cb5 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 15 Nov 2024 09:37:58 +0100 Subject: [PATCH 1/8] Add note wrapper (fixes #2657) --- .../{create_note.json => create_comment.json} | 0 .../create_private_message.json | 2 +- .../lemmy/objects/{note.json => comment.json} | 0 ...chat_message.json => private_message.json} | 2 +- .../mastodon/activities/private_message.json | 51 ++++++++++++ .../assets/pleroma/objects/chat_message.json | 15 ---- .../src/activities/create_or_update/mod.rs | 1 + .../create_or_update/note_wrapper.rs | 50 +++++++++++ .../create_or_update/private_message.rs | 6 +- crates/apub/src/activities/deletion/mod.rs | 6 +- crates/apub/src/activity_lists.rs | 11 +-- crates/apub/src/objects/comment.rs | 2 +- crates/apub/src/objects/mod.rs | 1 + crates/apub/src/objects/note_wrapper.rs | 82 +++++++++++++++++++ crates/apub/src/objects/private_message.rs | 55 ++++--------- .../activities/create_or_update/mod.rs | 16 +++- .../create_or_update/note_wrapper.rs | 16 ++++ .../{chat_message.rs => private_message.rs} | 6 +- crates/apub/src/protocol/mod.rs | 1 + crates/apub/src/protocol/objects/mod.rs | 13 +-- .../apub/src/protocol/objects/note_wrapper.rs | 14 ++++ .../{chat_message.rs => private_message.rs} | 11 +-- 22 files changed, 273 insertions(+), 88 deletions(-) rename crates/apub/assets/lemmy/activities/create_or_update/{create_note.json => create_comment.json} (100%) rename crates/apub/assets/lemmy/objects/{note.json => comment.json} (100%) rename crates/apub/assets/lemmy/objects/{chat_message.json => private_message.json} (93%) create mode 100644 crates/apub/assets/mastodon/activities/private_message.json delete mode 100644 crates/apub/assets/pleroma/objects/chat_message.json create mode 100644 crates/apub/src/activities/create_or_update/note_wrapper.rs create mode 100644 crates/apub/src/objects/note_wrapper.rs create mode 100644 crates/apub/src/protocol/activities/create_or_update/note_wrapper.rs rename crates/apub/src/protocol/activities/create_or_update/{chat_message.rs => private_message.rs} (75%) create mode 100644 crates/apub/src/protocol/objects/note_wrapper.rs rename crates/apub/src/protocol/objects/{chat_message.rs => private_message.rs} (80%) diff --git a/crates/apub/assets/lemmy/activities/create_or_update/create_note.json b/crates/apub/assets/lemmy/activities/create_or_update/create_comment.json similarity index 100% rename from crates/apub/assets/lemmy/activities/create_or_update/create_note.json rename to crates/apub/assets/lemmy/activities/create_or_update/create_comment.json diff --git a/crates/apub/assets/lemmy/activities/create_or_update/create_private_message.json b/crates/apub/assets/lemmy/activities/create_or_update/create_private_message.json index 54ee393502..e7dbdd0f94 100644 --- a/crates/apub/assets/lemmy/activities/create_or_update/create_private_message.json +++ b/crates/apub/assets/lemmy/activities/create_or_update/create_private_message.json @@ -3,7 +3,7 @@ "actor": "http://enterprise.lemmy.ml/u/lemmy_beta", "to": ["http://ds9.lemmy.ml/u/lemmy_alpha"], "object": { - "type": "ChatMessage", + "type": "Note", "id": "http://enterprise.lemmy.ml/private_message/1", "attributedTo": "http://enterprise.lemmy.ml/u/lemmy_beta", "to": ["http://ds9.lemmy.ml/u/lemmy_alpha"], diff --git a/crates/apub/assets/lemmy/objects/note.json b/crates/apub/assets/lemmy/objects/comment.json similarity index 100% rename from crates/apub/assets/lemmy/objects/note.json rename to crates/apub/assets/lemmy/objects/comment.json diff --git a/crates/apub/assets/lemmy/objects/chat_message.json b/crates/apub/assets/lemmy/objects/private_message.json similarity index 93% rename from crates/apub/assets/lemmy/objects/chat_message.json rename to crates/apub/assets/lemmy/objects/private_message.json index 95b37322ec..a3579523e2 100644 --- a/crates/apub/assets/lemmy/objects/chat_message.json +++ b/crates/apub/assets/lemmy/objects/private_message.json @@ -1,6 +1,6 @@ { "id": "https://enterprise.lemmy.ml/private_message/1621", - "type": "ChatMessage", + "type": "Note", "attributedTo": "https://enterprise.lemmy.ml/u/picard", "to": ["https://queer.hacktivis.me/users/lanodan"], "content": "

Hello hello, testing

\n", diff --git a/crates/apub/assets/mastodon/activities/private_message.json b/crates/apub/assets/mastodon/activities/private_message.json new file mode 100644 index 0000000000..8d141dbeff --- /dev/null +++ b/crates/apub/assets/mastodon/activities/private_message.json @@ -0,0 +1,51 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + { + "ostatus": "http://ostatus.org#", + "atomUri": "ostatus:atomUri", + "inReplyToAtomUri": "ostatus:inReplyToAtomUri", + "conversation": "ostatus:conversation", + "sensitive": "as:sensitive", + "toot": "http://joinmastodon.org/ns#", + "votersCount": "toot:votersCount" + } + ], + "id": "https://mastodon.world/users/nutomic/statuses/110854468010322301", + "type": "Note", + "summary": null, + "inReplyTo": "https://mastodon.world/users/nutomic/statuses/110854464248188528", + "published": "2023-08-08T14:29:04Z", + "url": "https://mastodon.world/@nutomic/110854468010322301", + "attributedTo": "https://mastodon.world/users/nutomic", + "to": [ + "https://ds9.lemmy.ml/u/nutomic" + ], + "cc": [], + "sensitive": false, + "atomUri": "https://mastodon.world/users/nutomic/statuses/110854468010322301", + "inReplyToAtomUri": "https://mastodon.world/users/nutomic/statuses/110854464248188528", + "conversation": "tag:mastodon.world,2023-08-08:objectId=121377096:objectType=Conversation", + "content": "

@nutomic@ds9.lemmy.ml 444

", + "contentMap": { + "es": "

@nutomic@ds9.lemmy.ml 444

" + }, + "attachment": [], + "tag": [ + { + "type": "Mention", + "href": "https://ds9.lemmy.ml/u/nutomic", + "name": "@nutomic@ds9.lemmy.ml" + } + ], + "replies": { + "id": "https://mastodon.world/users/nutomic/statuses/110854468010322301/replies", + "type": "Collection", + "first": { + "type": "CollectionPage", + "next": "https://mastodon.world/users/nutomic/statuses/110854468010322301/replies?only_other_accounts=true&page=true", + "partOf": "https://mastodon.world/users/nutomic/statuses/110854468010322301/replies", + "items": [] + } + } + } \ No newline at end of file diff --git a/crates/apub/assets/pleroma/objects/chat_message.json b/crates/apub/assets/pleroma/objects/chat_message.json deleted file mode 100644 index 6a2afc82e2..0000000000 --- a/crates/apub/assets/pleroma/objects/chat_message.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "@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" -} diff --git a/crates/apub/src/activities/create_or_update/mod.rs b/crates/apub/src/activities/create_or_update/mod.rs index c69e00e91b..b442e5fa37 100644 --- a/crates/apub/src/activities/create_or_update/mod.rs +++ b/crates/apub/src/activities/create_or_update/mod.rs @@ -1,3 +1,4 @@ pub mod comment; +pub(crate) mod note_wrapper; pub mod post; pub mod private_message; diff --git a/crates/apub/src/activities/create_or_update/note_wrapper.rs b/crates/apub/src/activities/create_or_update/note_wrapper.rs new file mode 100644 index 0000000000..241d98b90f --- /dev/null +++ b/crates/apub/src/activities/create_or_update/note_wrapper.rs @@ -0,0 +1,50 @@ +use crate::{ + objects::note_wrapper::is_public, + protocol::activities::create_or_update::{ + note::CreateOrUpdateNote, + note_wrapper::CreateOrUpdateNoteWrapper, + private_message::CreateOrUpdatePrivateMessage, + }, +}; +use activitypub_federation::{config::Data, traits::ActivityHandler}; +use lemmy_api_common::context::LemmyContext; +use lemmy_utils::error::{LemmyError, LemmyResult}; +use serde_json::{from_value, to_value}; +use url::Url; + +#[async_trait::async_trait] +impl ActivityHandler for CreateOrUpdateNoteWrapper { + type DataType = LemmyContext; + type Error = LemmyError; + + fn id(&self) -> &Url { + &self.id + } + + fn actor(&self) -> &Url { + &self.actor + } + + #[tracing::instrument(skip_all)] + async fn verify(&self, context: &Data) -> LemmyResult<()> { + let val = to_value(self)?; + if is_public(&self.to, &self.cc) { + CreateOrUpdateNote::verify(&from_value(val)?, context).await?; + } else { + CreateOrUpdatePrivateMessage::verify(&from_value(val)?, context).await?; + } + Ok(()) + } + + #[tracing::instrument(skip_all)] + async fn receive(self, context: &Data) -> LemmyResult<()> { + let is_public = is_public(&self.to, &self.cc); + let val = to_value(self)?; + if is_public { + CreateOrUpdateNote::receive(from_value(val)?, context).await?; + } else { + CreateOrUpdatePrivateMessage::receive(from_value(val)?, context).await?; + } + Ok(()) + } +} diff --git a/crates/apub/src/activities/create_or_update/private_message.rs b/crates/apub/src/activities/create_or_update/private_message.rs index 6bba4e374e..b6e7478efa 100644 --- a/crates/apub/src/activities/create_or_update/private_message.rs +++ b/crates/apub/src/activities/create_or_update/private_message.rs @@ -3,7 +3,7 @@ use crate::{ insert_received_activity, objects::{person::ApubPerson, private_message::ApubPrivateMessage}, protocol::activities::{ - create_or_update::chat_message::CreateOrUpdateChatMessage, + create_or_update::private_message::CreateOrUpdatePrivateMessage, CreateOrUpdateType, }, }; @@ -30,7 +30,7 @@ pub(crate) async fn send_create_or_update_pm( kind.clone(), &context.settings().get_protocol_and_hostname(), )?; - let create_or_update = CreateOrUpdateChatMessage { + let create_or_update = CreateOrUpdatePrivateMessage { id: id.clone(), actor: actor.id().into(), to: [recipient.id().into()], @@ -44,7 +44,7 @@ pub(crate) async fn send_create_or_update_pm( } #[async_trait::async_trait] -impl ActivityHandler for CreateOrUpdateChatMessage { +impl ActivityHandler for CreateOrUpdatePrivateMessage { type DataType = LemmyContext; type Error = LemmyError; diff --git a/crates/apub/src/activities/deletion/mod.rs b/crates/apub/src/activities/deletion/mod.rs index 15118a4764..941ac4237d 100644 --- a/crates/apub/src/activities/deletion/mod.rs +++ b/crates/apub/src/activities/deletion/mod.rs @@ -36,7 +36,7 @@ use lemmy_db_schema::{ community::{Community, CommunityUpdateForm}, person::Person, post::{Post, PostUpdateForm}, - private_message::{PrivateMessage, PrivateMessageUpdateForm}, + private_message::{PrivateMessage as DbPrivateMessage, PrivateMessageUpdateForm}, }, traits::Crud, }; @@ -82,7 +82,7 @@ pub(crate) async fn send_apub_delete_in_community( #[tracing::instrument(skip_all)] pub(crate) async fn send_apub_delete_private_message( actor: &ApubPerson, - pm: PrivateMessage, + pm: DbPrivateMessage, deleted: bool, context: Data, ) -> LemmyResult<()> { @@ -298,7 +298,7 @@ async fn receive_delete_action( } } DeletableObjects::PrivateMessage(pm) => { - PrivateMessage::update( + DbPrivateMessage::update( &mut context.pool(), pm.id, &PrivateMessageUpdateForm { diff --git a/crates/apub/src/activity_lists.rs b/crates/apub/src/activity_lists.rs index 7ed1d8baf4..8aa18e9876 100644 --- a/crates/apub/src/activity_lists.rs +++ b/crates/apub/src/activity_lists.rs @@ -12,9 +12,10 @@ use crate::{ update::UpdateCommunity, }, create_or_update::{ - chat_message::CreateOrUpdateChatMessage, note::CreateOrUpdateNote, + note_wrapper::CreateOrUpdateNoteWrapper, page::CreateOrUpdatePage, + private_message::CreateOrUpdatePrivateMessage, }, deletion::{delete::Delete, undo_delete::UndoDelete}, following::{ @@ -48,7 +49,7 @@ pub enum SharedInboxActivities { AcceptFollow(AcceptFollow), RejectFollow(RejectFollow), UndoFollow(UndoFollow), - CreateOrUpdatePrivateMessage(CreateOrUpdateChatMessage), + CreateOrUpdatePrivateMessage(CreateOrUpdatePrivateMessage), Report(Report), AnnounceActivity(AnnounceActivity), /// This is a catch-all and needs to be last @@ -76,7 +77,7 @@ pub enum PersonInboxActivities { AcceptFollow(AcceptFollow), RejectFollow(RejectFollow), UndoFollow(UndoFollow), - CreateOrUpdatePrivateMessage(CreateOrUpdateChatMessage), + CreateOrUpdateNoteWrapper(CreateOrUpdateNoteWrapper), Delete(Delete), UndoDelete(UndoDelete), AnnounceActivity(AnnounceActivity), @@ -142,7 +143,7 @@ mod tests { fn test_group_inbox() -> LemmyResult<()> { test_parse_lemmy_item::("assets/lemmy/activities/following/follow.json")?; test_parse_lemmy_item::( - "assets/lemmy/activities/create_or_update/create_note.json", + "assets/lemmy/activities/create_or_update/create_comment.json", )?; Ok(()) } @@ -153,7 +154,7 @@ mod tests { "assets/lemmy/activities/following/accept.json", )?; test_parse_lemmy_item::( - "assets/lemmy/activities/create_or_update/create_note.json", + "assets/lemmy/activities/create_or_update/create_comment.json", )?; test_parse_lemmy_item::( "assets/lemmy/activities/create_or_update/create_private_message.json", diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index dc07214045..2c8ed9f9d3 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -266,7 +266,7 @@ pub(crate) mod tests { let url = Url::parse("https://enterprise.lemmy.ml/comment/38741")?; let data = prepare_comment_test(&url, &context).await?; - let json: Note = file_to_json_object("assets/lemmy/objects/note.json")?; + let json: Note = file_to_json_object("assets/lemmy/objects/comment.json")?; ApubComment::verify(&json, &url, &context).await?; let comment = ApubComment::from_json(json.clone(), &context).await?; diff --git a/crates/apub/src/objects/mod.rs b/crates/apub/src/objects/mod.rs index f837f7ad35..58841b29e6 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -15,6 +15,7 @@ use std::fmt::Debug; pub mod comment; pub mod community; pub mod instance; +pub mod note_wrapper; pub mod person; pub mod post; pub mod private_message; diff --git a/crates/apub/src/objects/note_wrapper.rs b/crates/apub/src/objects/note_wrapper.rs new file mode 100644 index 0000000000..9890ad87d2 --- /dev/null +++ b/crates/apub/src/objects/note_wrapper.rs @@ -0,0 +1,82 @@ +use super::comment::ApubComment; +use crate::{ + objects::private_message::ApubPrivateMessage, + protocol::objects::note_wrapper::NoteWrapper, +}; +use activitypub_federation::{config::Data, kinds::public, traits::Object}; +use chrono::{DateTime, Utc}; +use lemmy_api_common::context::LemmyContext; +use lemmy_utils::error::{LemmyError, LemmyResult}; +use serde_json::{from_value, to_value}; +use url::Url; + +#[derive(Debug)] +pub(crate) struct ApubNote {} +// TODO: change type of private message to `Note` + +#[async_trait::async_trait] +impl Object for ApubNote { + type DataType = LemmyContext; + type Kind = NoteWrapper; + type Error = LemmyError; + + fn last_refreshed_at(&self) -> Option> { + None + } + + #[tracing::instrument(skip_all)] + async fn read_from_id( + _object_id: Url, + _context: &Data, + ) -> LemmyResult> { + unimplemented!() + } + + #[tracing::instrument(skip_all)] + async fn delete(self, _context: &Data) -> LemmyResult<()> { + todo!() + } + + async fn verify( + note: &NoteWrapper, + expected_domain: &Url, + context: &Data, + ) -> LemmyResult<()> { + let val = to_value(note)?; + if is_public(¬e.to, ¬e.cc) { + ApubComment::verify(&from_value(val)?, expected_domain, context).await?; + } else { + ApubPrivateMessage::verify(&from_value(val)?, expected_domain, context).await?; + } + Ok(()) + } + + async fn from_json(note: NoteWrapper, context: &Data) -> LemmyResult { + let is_public = is_public(¬e.to, ¬e.cc); + let val = to_value(note)?; + if is_public { + ApubComment::from_json(from_value(val)?, context).await?; + } else { + ApubPrivateMessage::from_json(from_value(val)?, context).await?; + } + Ok(ApubNote {}) + } + + async fn into_json(self, _context: &Data) -> LemmyResult { + todo!() + } +} + +pub(crate) fn is_public(to: &Option>, cc: &Option>) -> bool { + if let Some(to) = to { + if to.contains(&public()) { + return true; + } + } + if let Some(cc) = cc { + if cc.contains(&public()) { + return true; + } + } + false +} diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index 9ada5f6571..1b8ec4ee59 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -3,10 +3,7 @@ use crate::{ check_apub_id_valid_with_strictness, fetcher::markdown_links::markdown_rewrite_remote_links, objects::read_from_string_or_source, - protocol::{ - objects::chat_message::{ChatMessage, ChatMessageType}, - Source, - }, + protocol::{objects::private_message::PrivateMessage, Source}, }; use activitypub_federation::{ config::Data, @@ -28,7 +25,7 @@ use lemmy_db_schema::{ local_site::LocalSite, person::Person, person_block::PersonBlock, - private_message::{PrivateMessage, PrivateMessageInsertForm}, + private_message::{PrivateMessage as DbPrivateMessage, PrivateMessageInsertForm}, }, traits::Crud, }; @@ -41,17 +38,17 @@ use std::ops::Deref; use url::Url; #[derive(Clone, Debug)] -pub struct ApubPrivateMessage(pub(crate) PrivateMessage); +pub struct ApubPrivateMessage(pub(crate) DbPrivateMessage); impl Deref for ApubPrivateMessage { - type Target = PrivateMessage; + type Target = DbPrivateMessage; fn deref(&self) -> &Self::Target { &self.0 } } -impl From for ApubPrivateMessage { - fn from(pm: PrivateMessage) -> Self { +impl From for ApubPrivateMessage { + fn from(pm: DbPrivateMessage) -> Self { ApubPrivateMessage(pm) } } @@ -59,7 +56,7 @@ impl From for ApubPrivateMessage { #[async_trait::async_trait] impl Object for ApubPrivateMessage { type DataType = LemmyContext; - type Kind = ChatMessage; + type Kind = PrivateMessage; type Error = LemmyError; fn last_refreshed_at(&self) -> Option> { @@ -72,7 +69,7 @@ impl Object for ApubPrivateMessage { context: &Data, ) -> LemmyResult> { Ok( - PrivateMessage::read_from_apub_id(&mut context.pool(), object_id) + DbPrivateMessage::read_from_apub_id(&mut context.pool(), object_id) .await? .map(Into::into), ) @@ -84,15 +81,15 @@ impl Object for ApubPrivateMessage { } #[tracing::instrument(skip_all)] - async fn into_json(self, context: &Data) -> LemmyResult { + async fn into_json(self, context: &Data) -> LemmyResult { let creator_id = self.creator_id; let creator = Person::read(&mut context.pool(), creator_id).await?; let recipient_id = self.recipient_id; let recipient = Person::read(&mut context.pool(), recipient_id).await?; - let note = ChatMessage { - r#type: ChatMessageType::ChatMessage, + let note = PrivateMessage { + r#type: Default::default(), id: self.ap_id.clone().into(), attributed_to: creator.actor_id.into(), to: [recipient.actor_id.into()], @@ -107,7 +104,7 @@ impl Object for ApubPrivateMessage { #[tracing::instrument(skip_all)] async fn verify( - note: &ChatMessage, + note: &PrivateMessage, expected_domain: &Url, context: &Data, ) -> LemmyResult<()> { @@ -128,7 +125,7 @@ impl Object for ApubPrivateMessage { #[tracing::instrument(skip_all)] async fn from_json( - note: ChatMessage, + note: PrivateMessage, context: &Data, ) -> LemmyResult { let creator = note.attributed_to.dereference(context).await?; @@ -161,7 +158,7 @@ impl Object for ApubPrivateMessage { local: Some(false), }; let timestamp = note.updated.or(note.published).unwrap_or_else(Utc::now); - let pm = PrivateMessage::insert_apub(&mut context.pool(), timestamp, &form).await?; + let pm = DbPrivateMessage::insert_apub(&mut context.pool(), timestamp, &form).await?; Ok(pm.into()) } } @@ -213,7 +210,7 @@ mod tests { let context = LemmyContext::init_test_context().await; let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621")?; let data = prepare_comment_test(&url, &context).await?; - let json: ChatMessage = file_to_json_object("assets/lemmy/objects/chat_message.json")?; + let json: PrivateMessage = file_to_json_object("assets/lemmy/objects/private_message.json")?; ApubPrivateMessage::verify(&json, &url, &context).await?; let pm = ApubPrivateMessage::from_json(json.clone(), &context).await?; @@ -225,27 +222,7 @@ mod tests { let to_apub = pm.into_json(&context).await?; assert_json_include!(actual: json, expected: to_apub); - PrivateMessage::delete(&mut context.pool(), pm_id).await?; - cleanup(data, &context).await?; - Ok(()) - } - - #[tokio::test] - #[serial] - async fn test_parse_pleroma_pm() -> LemmyResult<()> { - let context = LemmyContext::init_test_context().await; - let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621")?; - let data = prepare_comment_test(&url, &context).await?; - let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2")?; - let json = file_to_json_object("assets/pleroma/objects/chat_message.json")?; - ApubPrivateMessage::verify(&json, &pleroma_url, &context).await?; - let pm = ApubPrivateMessage::from_json(json, &context).await?; - - assert_eq!(pm.ap_id, pleroma_url.into()); - assert_eq!(pm.content.len(), 3); - assert_eq!(context.request_count(), 0); - - PrivateMessage::delete(&mut context.pool(), pm.id).await?; + DbPrivateMessage::delete(&mut context.pool(), pm_id).await?; cleanup(data, &context).await?; Ok(()) } diff --git a/crates/apub/src/protocol/activities/create_or_update/mod.rs b/crates/apub/src/protocol/activities/create_or_update/mod.rs index 3d9dbbb1d8..d34be93c8e 100644 --- a/crates/apub/src/protocol/activities/create_or_update/mod.rs +++ b/crates/apub/src/protocol/activities/create_or_update/mod.rs @@ -1,14 +1,16 @@ -pub mod chat_message; pub mod note; +pub(crate) mod note_wrapper; pub mod page; +pub mod private_message; #[cfg(test)] mod tests { + use super::note_wrapper::CreateOrUpdateNoteWrapper; use crate::protocol::{ activities::create_or_update::{ - chat_message::CreateOrUpdateChatMessage, note::CreateOrUpdateNote, page::CreateOrUpdatePage, + private_message::CreateOrUpdatePrivateMessage, }, tests::test_parse_lemmy_item, }; @@ -23,9 +25,15 @@ mod tests { "assets/lemmy/activities/create_or_update/update_page.json", )?; test_parse_lemmy_item::( - "assets/lemmy/activities/create_or_update/create_note.json", + "assets/lemmy/activities/create_or_update/create_comment.json", )?; - test_parse_lemmy_item::( + test_parse_lemmy_item::( + "assets/lemmy/activities/create_or_update/create_private_message.json", + )?; + test_parse_lemmy_item::( + "assets/lemmy/activities/create_or_update/create_comment.json", + )?; + test_parse_lemmy_item::( "assets/lemmy/activities/create_or_update/create_private_message.json", )?; Ok(()) diff --git a/crates/apub/src/protocol/activities/create_or_update/note_wrapper.rs b/crates/apub/src/protocol/activities/create_or_update/note_wrapper.rs new file mode 100644 index 0000000000..bc53c80fde --- /dev/null +++ b/crates/apub/src/protocol/activities/create_or_update/note_wrapper.rs @@ -0,0 +1,16 @@ +use crate::protocol::objects::note_wrapper::NoteWrapper; +use serde::{Deserialize, Serialize}; +use serde_json::{Map, Value}; +use url::Url; + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct CreateOrUpdateNoteWrapper { + object: NoteWrapper, + pub(crate) id: Url, + pub(crate) actor: Url, + pub(crate) to: Option>, + pub(crate) cc: Option>, + #[serde(flatten)] + other: Map, +} diff --git a/crates/apub/src/protocol/activities/create_or_update/chat_message.rs b/crates/apub/src/protocol/activities/create_or_update/private_message.rs similarity index 75% rename from crates/apub/src/protocol/activities/create_or_update/chat_message.rs rename to crates/apub/src/protocol/activities/create_or_update/private_message.rs index 30e94a28dd..0c08f3991e 100644 --- a/crates/apub/src/protocol/activities/create_or_update/chat_message.rs +++ b/crates/apub/src/protocol/activities/create_or_update/private_message.rs @@ -1,6 +1,6 @@ use crate::{ objects::person::ApubPerson, - protocol::{activities::CreateOrUpdateType, objects::chat_message::ChatMessage}, + protocol::{activities::CreateOrUpdateType, objects::private_message::PrivateMessage}, }; use activitypub_federation::{fetch::object_id::ObjectId, protocol::helpers::deserialize_one}; use serde::{Deserialize, Serialize}; @@ -8,12 +8,12 @@ use url::Url; #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -pub struct CreateOrUpdateChatMessage { +pub struct CreateOrUpdatePrivateMessage { pub(crate) id: Url, pub(crate) actor: ObjectId, #[serde(deserialize_with = "deserialize_one")] pub(crate) to: [ObjectId; 1], - pub(crate) object: ChatMessage, + pub(crate) object: PrivateMessage, #[serde(rename = "type")] pub(crate) kind: CreateOrUpdateType, } diff --git a/crates/apub/src/protocol/mod.rs b/crates/apub/src/protocol/mod.rs index a4774ac1d6..9f218e351e 100644 --- a/crates/apub/src/protocol/mod.rs +++ b/crates/apub/src/protocol/mod.rs @@ -116,6 +116,7 @@ pub(crate) mod tests { // parse file into hashmap, which ensures that every field is included let raw = file_to_json_object::>(path)?; // assert that all fields are identical, otherwise print diff + //dbg!(&parsed, &raw); assert_json_include!(actual: &parsed, expected: raw); Ok(parsed) } diff --git a/crates/apub/src/protocol/objects/mod.rs b/crates/apub/src/protocol/objects/mod.rs index 00fe26d2b7..757f49ae42 100644 --- a/crates/apub/src/protocol/objects/mod.rs +++ b/crates/apub/src/protocol/objects/mod.rs @@ -8,12 +8,13 @@ use lemmy_utils::error::LemmyResult; use serde::{Deserialize, Serialize}; use url::Url; -pub(crate) mod chat_message; pub(crate) mod group; pub(crate) mod instance; pub(crate) mod note; +pub(crate) mod note_wrapper; pub(crate) mod page; pub(crate) mod person; +pub(crate) mod private_message; pub(crate) mod tombstone; #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] @@ -101,14 +102,15 @@ impl LanguageTag { #[cfg(test)] mod tests { + use super::note_wrapper::NoteWrapper; use crate::protocol::{ objects::{ - chat_message::ChatMessage, group::Group, instance::Instance, note::Note, page::Page, person::Person, + private_message::PrivateMessage, tombstone::Tombstone, }, tests::{test_json, test_parse_lemmy_item}, @@ -121,8 +123,10 @@ mod tests { test_parse_lemmy_item::("assets/lemmy/objects/group.json")?; test_parse_lemmy_item::("assets/lemmy/objects/person.json")?; test_parse_lemmy_item::("assets/lemmy/objects/page.json")?; - test_parse_lemmy_item::("assets/lemmy/objects/note.json")?; - test_parse_lemmy_item::("assets/lemmy/objects/chat_message.json")?; + test_parse_lemmy_item::("assets/lemmy/objects/comment.json")?; + test_parse_lemmy_item::("assets/lemmy/objects/private_message.json")?; + test_parse_lemmy_item::("assets/lemmy/objects/comment.json")?; + test_parse_lemmy_item::("assets/lemmy/objects/private_message.json")?; test_parse_lemmy_item::("assets/lemmy/objects/tombstone.json")?; Ok(()) } @@ -131,7 +135,6 @@ mod tests { fn test_parse_objects_pleroma() -> LemmyResult<()> { test_json::("assets/pleroma/objects/person.json")?; test_json::("assets/pleroma/objects/note.json")?; - test_json::("assets/pleroma/objects/chat_message.json")?; Ok(()) } diff --git a/crates/apub/src/protocol/objects/note_wrapper.rs b/crates/apub/src/protocol/objects/note_wrapper.rs new file mode 100644 index 0000000000..f1bcf605bb --- /dev/null +++ b/crates/apub/src/protocol/objects/note_wrapper.rs @@ -0,0 +1,14 @@ +use activitypub_federation::kinds::object::NoteType; +use serde::{Deserialize, Serialize}; +use serde_json::{Map, Value}; +use url::Url; + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct NoteWrapper { + pub(crate) r#type: NoteType, + pub(crate) to: Option>, + pub(crate) cc: Option>, + #[serde(flatten)] + other: Map, +} diff --git a/crates/apub/src/protocol/objects/chat_message.rs b/crates/apub/src/protocol/objects/private_message.rs similarity index 80% rename from crates/apub/src/protocol/objects/chat_message.rs rename to crates/apub/src/protocol/objects/private_message.rs index 8cb83e6643..bb4357d9e2 100644 --- a/crates/apub/src/protocol/objects/chat_message.rs +++ b/crates/apub/src/protocol/objects/private_message.rs @@ -4,6 +4,7 @@ use crate::{ }; use activitypub_federation::{ fetch::object_id::ObjectId, + kinds::object::NoteType, protocol::{ helpers::{deserialize_one, deserialize_skip_error}, values::MediaTypeHtml, @@ -16,8 +17,8 @@ use serde_with::skip_serializing_none; #[skip_serializing_none] #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -pub struct ChatMessage { - pub(crate) r#type: ChatMessageType, +pub struct PrivateMessage { + pub(crate) r#type: NoteType, pub(crate) id: ObjectId, pub(crate) attributed_to: ObjectId, #[serde(deserialize_with = "deserialize_one")] @@ -30,9 +31,3 @@ pub struct ChatMessage { pub(crate) published: Option>, pub(crate) updated: Option>, } - -/// https://docs.pleroma.social/backend/development/ap_extensions/#chatmessages -#[derive(Clone, Debug, Deserialize, Serialize)] -pub enum ChatMessageType { - ChatMessage, -} From c95db2ae2b0bced25b36adfe5bd7ecd82eefd226 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 22 Nov 2024 11:20:09 +0100 Subject: [PATCH 2/8] fix activity list --- crates/apub/src/activity_lists.rs | 64 +++++++------------------------ 1 file changed, 13 insertions(+), 51 deletions(-) diff --git a/crates/apub/src/activity_lists.rs b/crates/apub/src/activity_lists.rs index 8aa18e9876..c9e407cd9c 100644 --- a/crates/apub/src/activity_lists.rs +++ b/crates/apub/src/activity_lists.rs @@ -15,7 +15,6 @@ use crate::{ note::CreateOrUpdateNote, note_wrapper::CreateOrUpdateNoteWrapper, page::CreateOrUpdatePage, - private_message::CreateOrUpdatePrivateMessage, }, deletion::{delete::Delete, undo_delete::UndoDelete}, following::{ @@ -49,42 +48,13 @@ pub enum SharedInboxActivities { AcceptFollow(AcceptFollow), RejectFollow(RejectFollow), UndoFollow(UndoFollow), - CreateOrUpdatePrivateMessage(CreateOrUpdatePrivateMessage), + CreateOrUpdateNoteWrapper(CreateOrUpdateNoteWrapper), Report(Report), AnnounceActivity(AnnounceActivity), /// This is a catch-all and needs to be last RawAnnouncableActivities(RawAnnouncableActivities), } -/// List of activities which the group inbox can handle. -#[derive(Debug, Deserialize, Serialize)] -#[serde(untagged)] -#[enum_delegate::implement(ActivityHandler)] -pub enum GroupInboxActivities { - Follow(Follow), - UndoFollow(UndoFollow), - Report(Report), - /// This is a catch-all and needs to be last - AnnouncableActivities(RawAnnouncableActivities), -} - -/// List of activities which the person inbox can handle. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[serde(untagged)] -#[enum_delegate::implement(ActivityHandler)] -pub enum PersonInboxActivities { - Follow(Follow), - AcceptFollow(AcceptFollow), - RejectFollow(RejectFollow), - UndoFollow(UndoFollow), - CreateOrUpdateNoteWrapper(CreateOrUpdateNoteWrapper), - Delete(Delete), - UndoDelete(UndoDelete), - AnnounceActivity(AnnounceActivity), - /// User can also receive some "announcable" activities, eg a comment mention. - AnnouncableActivities(AnnouncableActivities), -} - #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(untagged)] #[enum_delegate::implement(ActivityHandler)] @@ -134,40 +104,32 @@ impl InCommunity for AnnouncableActivities { mod tests { use crate::{ - activity_lists::{GroupInboxActivities, PersonInboxActivities, SharedInboxActivities}, + activity_lists::SharedInboxActivities, protocol::tests::{test_json, test_parse_lemmy_item}, }; use lemmy_utils::error::LemmyResult; #[test] - fn test_group_inbox() -> LemmyResult<()> { - test_parse_lemmy_item::("assets/lemmy/activities/following/follow.json")?; - test_parse_lemmy_item::( - "assets/lemmy/activities/create_or_update/create_comment.json", + fn test_shared_inbox() -> LemmyResult<()> { + test_parse_lemmy_item::( + "assets/lemmy/activities/deletion/delete_user.json", )?; - Ok(()) - } - - #[test] - fn test_person_inbox() -> LemmyResult<()> { - test_parse_lemmy_item::( + test_parse_lemmy_item::( "assets/lemmy/activities/following/accept.json", )?; - test_parse_lemmy_item::( + test_parse_lemmy_item::( "assets/lemmy/activities/create_or_update/create_comment.json", )?; - test_parse_lemmy_item::( + test_parse_lemmy_item::( "assets/lemmy/activities/create_or_update/create_private_message.json", )?; - test_json::("assets/mastodon/activities/follow.json")?; - Ok(()) - } - - #[test] - fn test_shared_inbox() -> LemmyResult<()> { test_parse_lemmy_item::( - "assets/lemmy/activities/deletion/delete_user.json", + "assets/lemmy/activities/following/follow.json", + )?; + test_parse_lemmy_item::( + "assets/lemmy/activities/create_or_update/create_comment.json", )?; + test_json::("assets/mastodon/activities/follow.json")?; Ok(()) } } From f570530a551aa59a0f6ef1c327fa3236a643ed03 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 22 Nov 2024 11:24:10 +0100 Subject: [PATCH 3/8] fmt --- .../mastodon/activities/private_message.json | 96 +++++++++---------- 1 file changed, 47 insertions(+), 49 deletions(-) diff --git a/crates/apub/assets/mastodon/activities/private_message.json b/crates/apub/assets/mastodon/activities/private_message.json index 8d141dbeff..b542859b52 100644 --- a/crates/apub/assets/mastodon/activities/private_message.json +++ b/crates/apub/assets/mastodon/activities/private_message.json @@ -1,51 +1,49 @@ { - "@context": [ - "https://www.w3.org/ns/activitystreams", - { - "ostatus": "http://ostatus.org#", - "atomUri": "ostatus:atomUri", - "inReplyToAtomUri": "ostatus:inReplyToAtomUri", - "conversation": "ostatus:conversation", - "sensitive": "as:sensitive", - "toot": "http://joinmastodon.org/ns#", - "votersCount": "toot:votersCount" - } - ], - "id": "https://mastodon.world/users/nutomic/statuses/110854468010322301", - "type": "Note", - "summary": null, - "inReplyTo": "https://mastodon.world/users/nutomic/statuses/110854464248188528", - "published": "2023-08-08T14:29:04Z", - "url": "https://mastodon.world/@nutomic/110854468010322301", - "attributedTo": "https://mastodon.world/users/nutomic", - "to": [ - "https://ds9.lemmy.ml/u/nutomic" - ], - "cc": [], - "sensitive": false, - "atomUri": "https://mastodon.world/users/nutomic/statuses/110854468010322301", - "inReplyToAtomUri": "https://mastodon.world/users/nutomic/statuses/110854464248188528", - "conversation": "tag:mastodon.world,2023-08-08:objectId=121377096:objectType=Conversation", - "content": "

@nutomic@ds9.lemmy.ml 444

", - "contentMap": { - "es": "

@nutomic@ds9.lemmy.ml 444

" - }, - "attachment": [], - "tag": [ - { - "type": "Mention", - "href": "https://ds9.lemmy.ml/u/nutomic", - "name": "@nutomic@ds9.lemmy.ml" - } - ], - "replies": { - "id": "https://mastodon.world/users/nutomic/statuses/110854468010322301/replies", - "type": "Collection", - "first": { - "type": "CollectionPage", - "next": "https://mastodon.world/users/nutomic/statuses/110854468010322301/replies?only_other_accounts=true&page=true", - "partOf": "https://mastodon.world/users/nutomic/statuses/110854468010322301/replies", - "items": [] - } + "@context": [ + "https://www.w3.org/ns/activitystreams", + { + "ostatus": "http://ostatus.org#", + "atomUri": "ostatus:atomUri", + "inReplyToAtomUri": "ostatus:inReplyToAtomUri", + "conversation": "ostatus:conversation", + "sensitive": "as:sensitive", + "toot": "http://joinmastodon.org/ns#", + "votersCount": "toot:votersCount" } - } \ No newline at end of file + ], + "id": "https://mastodon.world/users/nutomic/statuses/110854468010322301", + "type": "Note", + "summary": null, + "inReplyTo": "https://mastodon.world/users/nutomic/statuses/110854464248188528", + "published": "2023-08-08T14:29:04Z", + "url": "https://mastodon.world/@nutomic/110854468010322301", + "attributedTo": "https://mastodon.world/users/nutomic", + "to": ["https://ds9.lemmy.ml/u/nutomic"], + "cc": [], + "sensitive": false, + "atomUri": "https://mastodon.world/users/nutomic/statuses/110854468010322301", + "inReplyToAtomUri": "https://mastodon.world/users/nutomic/statuses/110854464248188528", + "conversation": "tag:mastodon.world,2023-08-08:objectId=121377096:objectType=Conversation", + "content": "

@nutomic@ds9.lemmy.ml 444

", + "contentMap": { + "es": "

@nutomic@ds9.lemmy.ml 444

" + }, + "attachment": [], + "tag": [ + { + "type": "Mention", + "href": "https://ds9.lemmy.ml/u/nutomic", + "name": "@nutomic@ds9.lemmy.ml" + } + ], + "replies": { + "id": "https://mastodon.world/users/nutomic/statuses/110854468010322301/replies", + "type": "Collection", + "first": { + "type": "CollectionPage", + "next": "https://mastodon.world/users/nutomic/statuses/110854468010322301/replies?only_other_accounts=true&page=true", + "partOf": "https://mastodon.world/users/nutomic/statuses/110854468010322301/replies", + "items": [] + } + } +} From da67719fe3197265d78650cde83c95bb114c8711 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 22 Nov 2024 11:32:02 +0100 Subject: [PATCH 4/8] add comment --- crates/apub/src/activity_lists.rs | 1 + crates/apub/src/objects/note_wrapper.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/apub/src/activity_lists.rs b/crates/apub/src/activity_lists.rs index c9e407cd9c..94130cdadc 100644 --- a/crates/apub/src/activity_lists.rs +++ b/crates/apub/src/activity_lists.rs @@ -48,6 +48,7 @@ pub enum SharedInboxActivities { AcceptFollow(AcceptFollow), RejectFollow(RejectFollow), UndoFollow(UndoFollow), + // Handle private messages, and comments sent directly to user (mentions) CreateOrUpdateNoteWrapper(CreateOrUpdateNoteWrapper), Report(Report), AnnounceActivity(AnnounceActivity), diff --git a/crates/apub/src/objects/note_wrapper.rs b/crates/apub/src/objects/note_wrapper.rs index 9890ad87d2..d05fd3833b 100644 --- a/crates/apub/src/objects/note_wrapper.rs +++ b/crates/apub/src/objects/note_wrapper.rs @@ -10,9 +10,12 @@ use lemmy_utils::error::{LemmyError, LemmyResult}; use serde_json::{from_value, to_value}; use url::Url; +/// Private messages and public comments are quite awkward in Activitypub, because the json +/// format looks identical. They only way to differentiate them is to check for the presence +/// or absence of `https://www.w3.org/ns/activitystreams#Public` in `to` or `cc` which this +/// wrapper does. #[derive(Debug)] pub(crate) struct ApubNote {} -// TODO: change type of private message to `Note` #[async_trait::async_trait] impl Object for ApubNote { From 94f0fd665581a61508fb490d3f3a2082c9c7c9e7 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 22 Nov 2024 12:03:34 +0100 Subject: [PATCH 5/8] fix api tests --- .../apub/src/activities/community/announce.rs | 5 ++-- .../activities/create_or_update/comment.rs | 4 ++- .../create_or_update/note_wrapper.rs | 28 +++++++++++++++---- crates/apub/src/activity_lists.rs | 12 +++----- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/crates/apub/src/activities/community/announce.rs b/crates/apub/src/activities/community/announce.rs index b31e4b74f7..81c4de0c21 100644 --- a/crates/apub/src/activities/community/announce.rs +++ b/crates/apub/src/activities/community/announce.rs @@ -64,16 +64,17 @@ impl ActivityHandler for RawAnnouncableActivities { // verify and receive activity activity.verify(context).await?; - activity.clone().receive(context).await?; + let actor_id = activity.actor().clone().into(); + activity.receive(context).await?; // if community is local, send activity to followers if let Some(community) = community { if community.local { - let actor_id = activity.actor().clone().into(); verify_person_in_community(&actor_id, &community, context).await?; AnnounceActivity::send(self, &community, context).await?; } } + Ok(()) } } diff --git a/crates/apub/src/activities/create_or_update/comment.rs b/crates/apub/src/activities/create_or_update/comment.rs index 90ab0153f8..9248f2aa83 100644 --- a/crates/apub/src/activities/create_or_update/comment.rs +++ b/crates/apub/src/activities/create_or_update/comment.rs @@ -43,6 +43,7 @@ use lemmy_utils::{ error::{LemmyError, LemmyResult}, utils::mention::scrape_text_for_mentions, }; +use serde_json::{from_value, to_value}; use url::Url; impl CreateOrUpdateNote { @@ -98,7 +99,8 @@ impl CreateOrUpdateNote { inboxes.add_inbox(person.shared_inbox_or_inbox()); } - let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update); + let activity = + AnnouncableActivities::CreateOrUpdateNoteWrapper(from_value(to_value(create_or_update)?)?); send_activity_in_community(activity, &person, &community, inboxes, false, &context).await } } diff --git a/crates/apub/src/activities/create_or_update/note_wrapper.rs b/crates/apub/src/activities/create_or_update/note_wrapper.rs index 241d98b90f..9206d0c052 100644 --- a/crates/apub/src/activities/create_or_update/note_wrapper.rs +++ b/crates/apub/src/activities/create_or_update/note_wrapper.rs @@ -1,14 +1,17 @@ use crate::{ - objects::note_wrapper::is_public, - protocol::activities::create_or_update::{ - note::CreateOrUpdateNote, - note_wrapper::CreateOrUpdateNoteWrapper, - private_message::CreateOrUpdatePrivateMessage, + objects::{community::ApubCommunity, note_wrapper::is_public}, + protocol::{ + activities::create_or_update::{ + note::CreateOrUpdateNote, + note_wrapper::CreateOrUpdateNoteWrapper, + private_message::CreateOrUpdatePrivateMessage, + }, + InCommunity, }, }; use activitypub_federation::{config::Data, traits::ActivityHandler}; use lemmy_api_common::context::LemmyContext; -use lemmy_utils::error::{LemmyError, LemmyResult}; +use lemmy_utils::error::{FederationError, LemmyError, LemmyResult}; use serde_json::{from_value, to_value}; use url::Url; @@ -48,3 +51,16 @@ impl ActivityHandler for CreateOrUpdateNoteWrapper { Ok(()) } } + +#[async_trait::async_trait] +impl InCommunity for CreateOrUpdateNoteWrapper { + #[tracing::instrument(skip(self, context))] + async fn community(&self, context: &Data) -> LemmyResult { + if is_public(&self.to, &self.cc) { + let comment: CreateOrUpdateNote = from_value(to_value(self)?)?; + comment.community(context).await + } else { + Err(FederationError::ObjectIsNotPublic.into()) + } + } +} diff --git a/crates/apub/src/activity_lists.rs b/crates/apub/src/activity_lists.rs index 94130cdadc..b536d0b959 100644 --- a/crates/apub/src/activity_lists.rs +++ b/crates/apub/src/activity_lists.rs @@ -11,11 +11,7 @@ use crate::{ report::Report, update::UpdateCommunity, }, - create_or_update::{ - note::CreateOrUpdateNote, - note_wrapper::CreateOrUpdateNoteWrapper, - page::CreateOrUpdatePage, - }, + create_or_update::{note_wrapper::CreateOrUpdateNoteWrapper, page::CreateOrUpdatePage}, deletion::{delete::Delete, undo_delete::UndoDelete}, following::{ accept::AcceptFollow, @@ -49,7 +45,7 @@ pub enum SharedInboxActivities { RejectFollow(RejectFollow), UndoFollow(UndoFollow), // Handle private messages, and comments sent directly to user (mentions) - CreateOrUpdateNoteWrapper(CreateOrUpdateNoteWrapper), + //CreateOrUpdateNoteWrapper(CreateOrUpdateNoteWrapper), Report(Report), AnnounceActivity(AnnounceActivity), /// This is a catch-all and needs to be last @@ -60,7 +56,7 @@ pub enum SharedInboxActivities { #[serde(untagged)] #[enum_delegate::implement(ActivityHandler)] pub enum AnnouncableActivities { - CreateOrUpdateComment(CreateOrUpdateNote), + CreateOrUpdateNoteWrapper(CreateOrUpdateNoteWrapper), CreateOrUpdatePost(CreateOrUpdatePage), Vote(Vote), UndoVote(UndoVote), @@ -83,7 +79,7 @@ impl InCommunity for AnnouncableActivities { async fn community(&self, context: &Data) -> LemmyResult { use AnnouncableActivities::*; match self { - CreateOrUpdateComment(a) => a.community(context).await, + CreateOrUpdateNoteWrapper(a) => a.community(context).await, CreateOrUpdatePost(a) => a.community(context).await, Vote(a) => a.community(context).await, UndoVote(a) => a.community(context).await, From b807f1fb24c3ede2292c3768752bd39b673fc2b6 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 22 Nov 2024 12:22:21 +0100 Subject: [PATCH 6/8] backwards compat --- Cargo.lock | 1 + crates/apub/Cargo.toml | 1 + crates/apub/src/objects/private_message.rs | 20 +++++++++++++++++-- .../src/protocol/objects/private_message.rs | 12 +++++++++-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b62cf624c2..da4e1d2138 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2584,6 +2584,7 @@ dependencies = [ "moka", "pretty_assertions", "reqwest 0.12.8", + "semver", "serde", "serde_json", "serde_with", diff --git a/crates/apub/Cargo.toml b/crates/apub/Cargo.toml index 057e9bd381..8124fda016 100644 --- a/crates/apub/Cargo.toml +++ b/crates/apub/Cargo.toml @@ -45,6 +45,7 @@ html2md = "0.2.14" html2text = "0.12.6" stringreader = "0.1.1" enum_delegate = "0.2.0" +semver = "1.0.23" [dev-dependencies] serial_test = { workspace = true } diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index 1b8ec4ee59..6b1696cf7b 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -3,7 +3,10 @@ use crate::{ check_apub_id_valid_with_strictness, fetcher::markdown_links::markdown_rewrite_remote_links, objects::read_from_string_or_source, - protocol::{objects::private_message::PrivateMessage, Source}, + protocol::{ + objects::private_message::{PrivateMessage, PrivateMessageType}, + Source, + }, }; use activitypub_federation::{ config::Data, @@ -22,6 +25,7 @@ use lemmy_api_common::{ }; use lemmy_db_schema::{ source::{ + instance::Instance, local_site::LocalSite, person::Person, person_block::PersonBlock, @@ -34,6 +38,7 @@ use lemmy_utils::{ error::{FederationError, LemmyError, LemmyErrorType, LemmyResult}, utils::markdown::markdown_to_html, }; +use semver::{Version, VersionReq}; use std::ops::Deref; use url::Url; @@ -88,8 +93,19 @@ impl Object for ApubPrivateMessage { let recipient_id = self.recipient_id; let recipient = Person::read(&mut context.pool(), recipient_id).await?; + let instance = Instance::read(&mut context.pool(), recipient.instance_id).await?; + let mut kind = PrivateMessageType::Note; + + // For Lemmy versions before 0.20, send private messages with old type + if let (Some(software), Some(version)) = (instance.software, &instance.version) { + let req = VersionReq::parse("<0.20")?; + if software == "lemmy" && req.matches(&Version::parse(version)?) { + kind = PrivateMessageType::ChatMessage + } + } + let note = PrivateMessage { - r#type: Default::default(), + kind, id: self.ap_id.clone().into(), attributed_to: creator.actor_id.into(), to: [recipient.actor_id.into()], diff --git a/crates/apub/src/protocol/objects/private_message.rs b/crates/apub/src/protocol/objects/private_message.rs index bb4357d9e2..bf7fe90cba 100644 --- a/crates/apub/src/protocol/objects/private_message.rs +++ b/crates/apub/src/protocol/objects/private_message.rs @@ -4,7 +4,6 @@ use crate::{ }; use activitypub_federation::{ fetch::object_id::ObjectId, - kinds::object::NoteType, protocol::{ helpers::{deserialize_one, deserialize_skip_error}, values::MediaTypeHtml, @@ -18,7 +17,8 @@ use serde_with::skip_serializing_none; #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct PrivateMessage { - pub(crate) r#type: NoteType, + #[serde(rename = "type")] + pub(crate) kind: PrivateMessageType, pub(crate) id: ObjectId, pub(crate) attributed_to: ObjectId, #[serde(deserialize_with = "deserialize_one")] @@ -31,3 +31,11 @@ pub struct PrivateMessage { pub(crate) published: Option>, pub(crate) updated: Option>, } + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub enum PrivateMessageType { + /// For compatibility with Lemmy 0.19 and earlier + /// https://docs.pleroma.social/backend/development/ap_extensions/#chatmessages + ChatMessage, + Note, +} From f622b776c2bf41e72bfb0584f4c3f9d7e711f3c7 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 22 Nov 2024 12:49:17 +0100 Subject: [PATCH 7/8] remove comment --- crates/apub/src/activity_lists.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/apub/src/activity_lists.rs b/crates/apub/src/activity_lists.rs index b536d0b959..0a11e30a0f 100644 --- a/crates/apub/src/activity_lists.rs +++ b/crates/apub/src/activity_lists.rs @@ -44,8 +44,6 @@ pub enum SharedInboxActivities { AcceptFollow(AcceptFollow), RejectFollow(RejectFollow), UndoFollow(UndoFollow), - // Handle private messages, and comments sent directly to user (mentions) - //CreateOrUpdateNoteWrapper(CreateOrUpdateNoteWrapper), Report(Report), AnnounceActivity(AnnounceActivity), /// This is a catch-all and needs to be last From f4c166e2b1ddf068e028467d35aec75aa406b046 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 22 Nov 2024 15:19:54 +0100 Subject: [PATCH 8/8] no todo, deprecated --- crates/apub/src/objects/note_wrapper.rs | 8 ++++---- crates/apub/src/objects/private_message.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/apub/src/objects/note_wrapper.rs b/crates/apub/src/objects/note_wrapper.rs index d05fd3833b..5d613c7ae0 100644 --- a/crates/apub/src/objects/note_wrapper.rs +++ b/crates/apub/src/objects/note_wrapper.rs @@ -5,7 +5,7 @@ use crate::{ }; use activitypub_federation::{config::Data, kinds::public, traits::Object}; use chrono::{DateTime, Utc}; -use lemmy_api_common::context::LemmyContext; +use lemmy_api_common::{context::LemmyContext, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyResult}; use serde_json::{from_value, to_value}; use url::Url; @@ -32,12 +32,12 @@ impl Object for ApubNote { _object_id: Url, _context: &Data, ) -> LemmyResult> { - unimplemented!() + Err(LemmyErrorType::Unknown("not implemented".to_string()).into()) } #[tracing::instrument(skip_all)] async fn delete(self, _context: &Data) -> LemmyResult<()> { - todo!() + Err(LemmyErrorType::Unknown("not implemented".to_string()).into()) } async fn verify( @@ -66,7 +66,7 @@ impl Object for ApubNote { } async fn into_json(self, _context: &Data) -> LemmyResult { - todo!() + Err(LemmyErrorType::Unknown("not implemented".to_string()).into()) } } diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index 6b1696cf7b..ba64bd5931 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -96,7 +96,7 @@ impl Object for ApubPrivateMessage { let instance = Instance::read(&mut context.pool(), recipient.instance_id).await?; let mut kind = PrivateMessageType::Note; - // For Lemmy versions before 0.20, send private messages with old type + // Deprecated: For Lemmy versions before 0.20, send private messages with old type if let (Some(software), Some(version)) = (instance.software, &instance.version) { let req = VersionReq::parse("<0.20")?; if software == "lemmy" && req.matches(&Version::parse(version)?) {