From: Felix Ableitner Date: Thu, 29 Jul 2021 08:58:29 +0000 (+0200) Subject: Simplify lemmy_context() function (dont return errors) X-Git-Url: http://these/git/%7B%60/feeds/%7B%60%24%7BarchiveUrl%7D/?a=commitdiff_plain;h=57b6ecaf40b72ce07c9851a22399bfe606fd9bd8;p=lemmy.git Simplify lemmy_context() function (dont return errors) --- diff --git a/crates/apub/src/activities/community/announce.rs b/crates/apub/src/activities/community/announce.rs index 0bdb8bc0..04721eda 100644 --- a/crates/apub/src/activities/community/announce.rs +++ b/crates/apub/src/activities/community/announce.rs @@ -86,7 +86,7 @@ impl AnnounceActivity { cc: vec![community.followers_url()], kind: AnnounceType::Announce, common: ActivityCommonFields { - context: lemmy_context()?.into(), + context: lemmy_context(), id: generate_activity_id(AnnounceType::Announce)?, actor: community.actor_id(), unparsed: Default::default(), diff --git a/crates/apub/src/activities/post/create.rs b/crates/apub/src/activities/post/create.rs index 909a6148..270b712b 100644 --- a/crates/apub/src/activities/post/create.rs +++ b/crates/apub/src/activities/post/create.rs @@ -55,7 +55,7 @@ impl CreatePost { cc: [community.actor_id()], r#type: Default::default(), common: ActivityCommonFields { - context: lemmy_context()?.into(), + context: lemmy_context(), id: id.clone(), actor: actor.actor_id(), unparsed: Default::default(), diff --git a/crates/apub/src/activities/post/update.rs b/crates/apub/src/activities/post/update.rs index a8cec9e7..c9fbb548 100644 --- a/crates/apub/src/activities/post/update.rs +++ b/crates/apub/src/activities/post/update.rs @@ -48,7 +48,7 @@ impl UpdatePost { cc: [community.actor_id()], r#type: Default::default(), common: ActivityCommonFields { - context: lemmy_context()?.into(), + context: lemmy_context(), id: id.clone(), actor: actor.actor_id(), unparsed: Default::default(), diff --git a/crates/apub/src/activities/send/comment.rs b/crates/apub/src/activities/send/comment.rs index be24dd07..47fc2a3e 100644 --- a/crates/apub/src/activities/send/comment.rs +++ b/crates/apub/src/activities/send/comment.rs @@ -64,7 +64,7 @@ impl ApubObjectType for Comment { note.into_any_base()?, ); create - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(CreateType::Create)?) .set_to(public()) .set_many_ccs(maa.ccs.to_owned()) @@ -97,7 +97,7 @@ impl ApubObjectType for Comment { note.into_any_base()?, ); update - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UpdateType::Update)?) .set_to(public()) .set_many_ccs(maa.ccs.to_owned()) @@ -124,7 +124,7 @@ impl ApubObjectType for Comment { self.ap_id.to_owned().into_inner(), ); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -153,7 +153,7 @@ impl ApubObjectType for Comment { self.ap_id.to_owned().into_inner(), ); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -164,7 +164,7 @@ impl ApubObjectType for Comment { delete.into_any_base()?, ); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -188,7 +188,7 @@ impl ApubObjectType for Comment { self.ap_id.to_owned().into_inner(), ); remove - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -217,7 +217,7 @@ impl ApubObjectType for Comment { self.ap_id.to_owned().into_inner(), ); remove - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -228,7 +228,7 @@ impl ApubObjectType for Comment { remove.into_any_base()?, ); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -255,7 +255,7 @@ impl ApubLikeableType for Comment { self.ap_id.to_owned().into_inner(), ); like - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -279,7 +279,7 @@ impl ApubLikeableType for Comment { self.ap_id.to_owned().into_inner(), ); dislike - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DislikeType::Dislike)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -307,7 +307,7 @@ impl ApubLikeableType for Comment { self.ap_id.to_owned().into_inner(), ); like - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DislikeType::Dislike)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -318,7 +318,7 @@ impl ApubLikeableType for Comment { like.into_any_base()?, ); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); diff --git a/crates/apub/src/activities/send/community.rs b/crates/apub/src/activities/send/community.rs index 96cb058a..fbc61fc0 100644 --- a/crates/apub/src/activities/send/community.rs +++ b/crates/apub/src/activities/send/community.rs @@ -98,7 +98,7 @@ impl CommunityType for Community { follow.into_any_base()?, ); accept - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(AcceptType::Accept)?) .set_to(person.actor_id()); @@ -117,7 +117,7 @@ impl CommunityType for Community { self.to_apub(context.pool()).await?.into_any_base()?, ); update - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UpdateType::Update)?) .set_to(public()) .set_many_ccs(vec![self.actor_id()]); @@ -134,7 +134,7 @@ impl CommunityType for Community { if self.local { let mut delete = Delete::new(self.actor_id(), self.actor_id()); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![self.followers_url()]); @@ -145,7 +145,7 @@ impl CommunityType for Community { else { let mut delete = Delete::new(mod_.actor_id(), self.actor_id()); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![self.actor_id()]); @@ -163,14 +163,14 @@ impl CommunityType for Community { if self.local { let mut delete = Delete::new(self.actor_id(), self.actor_id()); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![self.followers_url()]); let mut undo = Undo::new(self.actor_id(), delete.into_any_base()?); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![self.followers_url()]); @@ -181,14 +181,14 @@ impl CommunityType for Community { else { let mut delete = Delete::new(mod_.actor_id(), self.actor_id()); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![self.actor_id()]); let mut undo = Undo::new(mod_.actor_id(), delete.into_any_base()?); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![self.actor_id()]); @@ -202,7 +202,7 @@ impl CommunityType for Community { async fn send_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> { let mut remove = Remove::new(self.actor_id(), self.actor_id()); remove - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![self.followers_url()]); @@ -215,7 +215,7 @@ impl CommunityType for Community { async fn send_undo_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> { let mut remove = Remove::new(self.actor_id(), self.actor_id()); remove - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![self.followers_url()]); @@ -223,7 +223,7 @@ impl CommunityType for Community { // Undo that fake activity let mut undo = Undo::new(self.actor_id(), remove.into_any_base()?); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) .set_many_ccs(vec![self.followers_url()]); @@ -267,7 +267,7 @@ impl CommunityType for Community { } let mut announce = Announce::new(self.actor_id(), activity); announce - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(AnnounceType::Announce)?) .set_to(public()) .set_many_ccs(ccs); @@ -306,7 +306,7 @@ impl CommunityType for Community { ) -> Result<(), LemmyError> { let mut add = Add::new(actor.actor_id(), added_mod.actor_id()); add - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(AddType::Add)?) .set_to(public()) .set_many_ccs(vec![self.actor_id()]) @@ -324,7 +324,7 @@ impl CommunityType for Community { ) -> Result<(), LemmyError> { let mut remove = Remove::new(actor.actor_id(), removed_mod.actor_id()); remove - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![self.actor_id()]) @@ -342,7 +342,7 @@ impl CommunityType for Community { ) -> Result<(), LemmyError> { let mut block = Block::new(actor.actor_id(), blocked_user.actor_id()); block - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(BlockType::Block)?) .set_to(public()) .set_many_ccs(vec![self.actor_id()]); @@ -359,7 +359,7 @@ impl CommunityType for Community { ) -> Result<(), LemmyError> { let mut block = Block::new(actor.actor_id(), unblocked_user.actor_id()); block - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(BlockType::Block)?) .set_to(public()) .set_many_ccs(vec![self.actor_id()]); @@ -367,7 +367,7 @@ impl CommunityType for Community { // Undo that fake activity let mut undo = Undo::new(actor.actor_id(), block.into_any_base()?); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![self.actor_id()]); diff --git a/crates/apub/src/activities/send/person.rs b/crates/apub/src/activities/send/person.rs index a5e79251..b7b43c43 100644 --- a/crates/apub/src/activities/send/person.rs +++ b/crates/apub/src/activities/send/person.rs @@ -78,7 +78,7 @@ impl UserType for Person { let mut follow = Follow::new(self.actor_id(), community.actor_id()); follow - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(FollowType::Follow)?) .set_to(community.actor_id()); @@ -99,7 +99,7 @@ impl UserType for Person { let mut follow = Follow::new(self.actor_id(), community.actor_id()); follow - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(FollowType::Follow)?) .set_to(community.actor_id()); @@ -109,7 +109,7 @@ impl UserType for Person { follow.into_any_base()?, ); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(community.actor_id()); diff --git a/crates/apub/src/activities/send/post.rs b/crates/apub/src/activities/send/post.rs index db96621b..677e7845 100644 --- a/crates/apub/src/activities/send/post.rs +++ b/crates/apub/src/activities/send/post.rs @@ -54,7 +54,7 @@ impl ApubObjectType for Post { self.ap_id.to_owned().into_inner(), ); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -79,7 +79,7 @@ impl ApubObjectType for Post { self.ap_id.to_owned().into_inner(), ); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -90,7 +90,7 @@ impl ApubObjectType for Post { delete.into_any_base()?, ); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -111,7 +111,7 @@ impl ApubObjectType for Post { self.ap_id.to_owned().into_inner(), ); remove - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -136,7 +136,7 @@ impl ApubObjectType for Post { self.ap_id.to_owned().into_inner(), ); remove - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -147,7 +147,7 @@ impl ApubObjectType for Post { remove.into_any_base()?, ); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -171,7 +171,7 @@ impl ApubLikeableType for Post { self.ap_id.to_owned().into_inner(), ); like - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -192,7 +192,7 @@ impl ApubLikeableType for Post { self.ap_id.to_owned().into_inner(), ); dislike - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DislikeType::Dislike)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -217,7 +217,7 @@ impl ApubLikeableType for Post { self.ap_id.to_owned().into_inner(), ); like - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); @@ -228,7 +228,7 @@ impl ApubLikeableType for Post { like.into_any_base()?, ); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.actor_id()]); diff --git a/crates/apub/src/activities/send/private_message.rs b/crates/apub/src/activities/send/private_message.rs index d208b25d..67d0afcd 100644 --- a/crates/apub/src/activities/send/private_message.rs +++ b/crates/apub/src/activities/send/private_message.rs @@ -38,7 +38,7 @@ impl ApubObjectType for PrivateMessage { ); create - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(CreateType::Create)?) .set_to(recipient.actor_id()); @@ -59,7 +59,7 @@ impl ApubObjectType for PrivateMessage { note.into_any_base()?, ); update - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UpdateType::Update)?) .set_to(recipient.actor_id()); @@ -77,7 +77,7 @@ impl ApubObjectType for PrivateMessage { self.ap_id.to_owned().into_inner(), ); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(recipient.actor_id()); @@ -99,7 +99,7 @@ impl ApubObjectType for PrivateMessage { self.ap_id.to_owned().into_inner(), ); delete - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(recipient.actor_id()); @@ -109,7 +109,7 @@ impl ApubObjectType for PrivateMessage { delete.into_any_base()?, ); undo - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(recipient.actor_id()); diff --git a/crates/apub/src/extensions/context.rs b/crates/apub/src/extensions/context.rs index 08491dcb..1d6f83bd 100644 --- a/crates/apub/src/extensions/context.rs +++ b/crates/apub/src/extensions/context.rs @@ -1,9 +1,8 @@ -use activitystreams::{base::AnyBase, context}; -use lemmy_utils::LemmyError; +use activitystreams::{base::AnyBase, context, primitives::OneOrMany}; use serde_json::json; use url::Url; -pub fn lemmy_context() -> Result, LemmyError> { +pub fn lemmy_context() -> OneOrMany { let context_ext = AnyBase::from_arbitrary_json(json!( { "sc": "http://schema.org#", @@ -19,10 +18,11 @@ pub fn lemmy_context() -> Result, LemmyError> { "type": "sc:Text", "id": "as:alsoKnownAs" }, - }))?; - Ok(vec![ + })) + .expect("parse context"); + OneOrMany::from(vec![ AnyBase::from(context()), context_ext, - AnyBase::from(Url::parse("https://w3id.org/security/v1")?), + AnyBase::from(Url::parse("https://w3id.org/security/v1").expect("parse context")), ]) } diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index 3173bca1..6b264fad 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -81,7 +81,7 @@ pub(crate) async fn get_apub_community_followers( let mut collection = UnorderedCollection::new(); collection - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(community.followers_url.into()) .set_total_items(community_followers.len() as u64); Ok(create_apub_response(&collection)) @@ -112,7 +112,7 @@ pub(crate) async fn get_apub_community_outbox( let mut collection = OrderedCollection::new(); collection .set_many_items(activities) - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(community.get_outbox_url()?) .set_total_items(len as u64); Ok(create_apub_response(&collection)) @@ -130,7 +130,7 @@ pub(crate) async fn get_apub_community_inbox( let mut collection = OrderedCollection::new(); collection .set_id(community.inbox_url.into()) - .set_many_contexts(lemmy_context()?); + .set_many_contexts(lemmy_context()); Ok(create_apub_response(&collection)) } @@ -162,6 +162,6 @@ pub(crate) async fn get_apub_community_moderators( .set_id(generate_moderators_url(&community.actor_id)?.into()) .set_total_items(moderators.len() as u64) .set_many_items(moderators) - .set_many_contexts(lemmy_context()?); + .set_many_contexts(lemmy_context()); Ok(create_apub_response(&collection)) } diff --git a/crates/apub/src/http/person.rs b/crates/apub/src/http/person.rs index 2c96aafd..42f25bb6 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -71,7 +71,7 @@ pub(crate) async fn get_apub_person_outbox( let mut collection = OrderedCollection::new(); collection .set_many_items(Vec::::new()) - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(person.get_outbox_url()?) .set_total_items(0_u64); Ok(create_apub_response(&collection)) @@ -89,6 +89,6 @@ pub(crate) async fn get_apub_person_inbox( let mut collection = OrderedCollection::new(); collection .set_id(person.inbox_url.into()) - .set_many_contexts(lemmy_context()?); + .set_many_contexts(lemmy_context()); Ok(create_apub_response(&collection)) } diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index 7b181eff..8501ea2a 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -69,7 +69,7 @@ impl ToApub for Comment { comment // Not needed when the Post is embedded in a collection (like for community outbox) - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(self.ap_id.to_owned().into_inner()) .set_published(convert_datetime(self.published)) // NOTE: included community id for compatibility with lemmy v0.9.9 diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index 34a15793..f9948588 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -55,7 +55,7 @@ impl ToApub for Community { let mut group = ApObject::new(Group::new()); group - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(self.actor_id.to_owned().into()) .set_name(self.title.to_owned()) .set_published(convert_datetime(self.published)) diff --git a/crates/apub/src/objects/mod.rs b/crates/apub/src/objects/mod.rs index c87fc043..6a52673e 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -70,6 +70,25 @@ pub trait FromApubToForm { Self: Sized; } +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +pub enum MediaTypeMarkdown { + #[serde(rename = "text/markdown")] + Markdown, +} + +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +pub enum MediaTypeHtml { + #[serde(rename = "text/html")] + Markdown, +} + +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Source { + content: String, + media_type: MediaTypeMarkdown, +} + /// Updated is actually the deletion time fn create_tombstone( deleted: bool, diff --git a/crates/apub/src/objects/person.rs b/crates/apub/src/objects/person.rs index a508f4e0..29935ec0 100644 --- a/crates/apub/src/objects/person.rs +++ b/crates/apub/src/objects/person.rs @@ -50,7 +50,7 @@ impl ToApub for DbPerson { let mut person = ApObject::new(actor); person - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(self.actor_id.to_owned().into_inner()) .set_published(convert_datetime(self.published)); diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index ace6b913..7c79ff88 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -2,7 +2,7 @@ use crate::{ activities::extract_community, extensions::context::lemmy_context, fetcher::person::get_or_fetch_and_upsert_person, - objects::{create_tombstone, FromApub, ToApub}, + objects::{create_tombstone, FromApub, MediaTypeHtml, MediaTypeMarkdown, Source, ToApub}, }; use activitystreams::{ base::AnyBase, @@ -34,84 +34,6 @@ use lemmy_utils::{ use lemmy_websocket::LemmyContext; use url::Url; -#[async_trait::async_trait(?Send)] -impl ToApub for Post { - type ApubType = Page; - - // Turn a Lemmy post into an ActivityPub page that can be sent out over the network. - async fn to_apub(&self, pool: &DbPool) -> Result { - let creator_id = self.creator_id; - let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??; - let community_id = self.community_id; - let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - - let source = self.body.clone().map(|body| Source { - content: body, - media_type: MediaTypeMarkdown::Markdown, - }); - let image = self.thumbnail_url.clone().map(|thumb| ImageObject { - content: ImageType::Image, - url: thumb.into(), - }); - - let page = Page { - context: lemmy_context()?.into(), - r#type: PageType::Page, - id: self.ap_id.clone().into(), - attributed_to: creator.actor_id.into(), - to: [community.actor_id.into(), public()], - name: self.name.clone(), - content: self.body.as_ref().map(|b| markdown_to_html(b)), - media_type: MediaTypeHtml::Markdown, - source, - url: self.url.clone().map(|u| u.into()), - image, - comments_enabled: Some(!self.locked), - sensitive: Some(self.nsfw), - stickied: Some(self.stickied), - published: convert_datetime(self.published), - updated: self.updated.map(convert_datetime), - unparsed: Default::default(), - }; - Ok(page) - } - - fn to_tombstone(&self) -> Result { - create_tombstone( - self.deleted, - self.ap_id.to_owned().into(), - self.updated, - PageType::Page, - ) - } -} - -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -pub enum MediaTypeMarkdown { - #[serde(rename = "text/markdown")] - Markdown, -} - -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -pub enum MediaTypeHtml { - #[serde(rename = "text/html")] - Markdown, -} - -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Source { - content: String, - media_type: MediaTypeMarkdown, -} - -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -#[serde(rename_all = "camelCase")] -pub struct ImageObject { - content: ImageType, - url: Url, -} - #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct Page { @@ -138,6 +60,13 @@ pub struct Page { unparsed: Unparsed, } +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ImageObject { + content: ImageType, + url: Url, +} + impl Page { /// Only mods can change the post's stickied/locked status. So if either of these is changed from /// the current value, it is a mod action and needs to be verified as such. @@ -169,6 +98,58 @@ impl Page { } } +#[async_trait::async_trait(?Send)] +impl ToApub for Post { + type ApubType = Page; + + // Turn a Lemmy post into an ActivityPub page that can be sent out over the network. + async fn to_apub(&self, pool: &DbPool) -> Result { + let creator_id = self.creator_id; + let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??; + let community_id = self.community_id; + let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; + + let source = self.body.clone().map(|body| Source { + content: body, + media_type: MediaTypeMarkdown::Markdown, + }); + let image = self.thumbnail_url.clone().map(|thumb| ImageObject { + content: ImageType::Image, + url: thumb.into(), + }); + + let page = Page { + context: lemmy_context(), + r#type: PageType::Page, + id: self.ap_id.clone().into(), + attributed_to: creator.actor_id.into(), + to: [community.actor_id.into(), public()], + name: self.name.clone(), + content: self.body.as_ref().map(|b| markdown_to_html(b)), + media_type: MediaTypeHtml::Markdown, + source, + url: self.url.clone().map(|u| u.into()), + image, + comments_enabled: Some(!self.locked), + sensitive: Some(self.nsfw), + stickied: Some(self.stickied), + published: convert_datetime(self.published), + updated: self.updated.map(convert_datetime), + unparsed: Default::default(), + }; + Ok(page) + } + + fn to_tombstone(&self) -> Result { + create_tombstone( + self.deleted, + self.ap_id.to_owned().into(), + self.updated, + PageType::Page, + ) + } +} + #[async_trait::async_trait(?Send)] impl FromApub for Post { type ApubType = Page; diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index 6bb4820f..624123ec 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -42,7 +42,7 @@ impl ToApub for PrivateMessage { let recipient = blocking(pool, move |conn| Person::read(conn, recipient_id)).await??; private_message - .set_many_contexts(lemmy_context()?) + .set_many_contexts(lemmy_context()) .set_id(self.ap_id.to_owned().into_inner()) .set_published(convert_datetime(self.published)) .set_to(recipient.actor_id.into_inner())