]> Untitled Git - lemmy.git/commitdiff
Add docs for MediaType, PublicUrl values
authorFelix Ableitner <me@nutomic.com>
Fri, 30 Jul 2021 14:35:32 +0000 (16:35 +0200)
committerFelix Ableitner <me@nutomic.com>
Fri, 30 Jul 2021 21:16:33 +0000 (23:16 +0200)
26 files changed:
Cargo.lock
crates/api_crud/src/post/create.rs
crates/apub/src/activities/comment/create.rs
crates/apub/src/activities/comment/remove.rs
crates/apub/src/activities/comment/undo_remove.rs
crates/apub/src/activities/comment/update.rs
crates/apub/src/activities/community/add_mod.rs
crates/apub/src/activities/community/announce.rs
crates/apub/src/activities/community/block_user.rs
crates/apub/src/activities/community/undo_block_user.rs
crates/apub/src/activities/community/update.rs
crates/apub/src/activities/deletion/delete.rs
crates/apub/src/activities/deletion/undo_delete.rs
crates/apub/src/activities/post/create.rs
crates/apub/src/activities/post/update.rs
crates/apub/src/activities/removal/remove.rs
crates/apub/src/activities/removal/undo_remove.rs
crates/apub/src/activities/voting/dislike.rs
crates/apub/src/activities/voting/like.rs
crates/apub/src/activities/voting/undo_dislike.rs
crates/apub/src/activities/voting/undo_like.rs
crates/apub/src/objects/mod.rs
crates/apub/src/objects/post.rs
crates/apub_lib/Cargo.toml
crates/apub_lib/src/lib.rs
crates/apub_lib/src/values/mod.rs [new file with mode: 0644]

index 53e3e0071e56f6a122714b536f2f970c86c2b7ba..ffa6f79c9e21954f44749f690f5831561fa131a3 100644 (file)
@@ -1760,6 +1760,7 @@ dependencies = [
  "lemmy_utils",
  "lemmy_websocket",
  "serde",
+ "serde_json",
  "url",
 ]
 
index 89f71d939d7d5b5082d6936247593420cb2a8a2e..b2034a36741aa3c40fe3ffad0192567316e62b16 100644 (file)
@@ -7,7 +7,12 @@ use lemmy_api_common::{
   mark_post_as_read,
   post::*,
 };
-use lemmy_apub::{generate_apub_endpoint, ApubLikeableType, EndpointType};
+use lemmy_apub::{
+  activities::post::create::CreatePost as CreateApubPost,
+  generate_apub_endpoint,
+  ApubLikeableType,
+  EndpointType,
+};
 use lemmy_db_queries::{source::post::Post_, Crud, Likeable};
 use lemmy_db_schema::source::post::*;
 use lemmy_db_views::post_view::PostView;
@@ -82,12 +87,7 @@ impl PerformCrud for CreatePost {
     .await?
     .map_err(|_| ApiError::err("couldnt_create_post"))?;
 
-    lemmy_apub::activities::post::create::CreatePost::send(
-      &updated_post,
-      &local_user_view.person,
-      context,
-    )
-    .await?;
+    CreateApubPost::send(&updated_post, &local_user_view.person, context).await?;
 
     // They like their own post by default
     let person_id = local_user_view.person.id;
index b385533597a0d5cf0f32bf2c4077b386f4ee4f00..0e9472194246a6b64b80c3f66072a15e950271ba 100644 (file)
@@ -10,7 +10,12 @@ use crate::{
   NoteExt,
 };
 use activitystreams::{activity::kind::CreateType, base::BaseExt};
-use lemmy_apub_lib::{verify_domains_match_opt, ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{
+  values::PublicUrl,
+  verify_domains_match_opt,
+  ActivityCommonFields,
+  ActivityHandler,
+};
 use lemmy_db_schema::source::comment::Comment;
 use lemmy_utils::LemmyError;
 use lemmy_websocket::{LemmyContext, UserOperationCrud};
index d60e3f80444d347a98a890e34330f075ca7fb35b..5702f9fa76facfedf955a5343d3d0b169e481881 100644 (file)
@@ -1,8 +1,16 @@
-use crate::activities::{comment::send_websocket_message, verify_mod_action};
+use crate::{
+  activities::{comment::send_websocket_message, verify_mod_action},
+  check_is_apub_id_valid,
+  fetcher::objects::get_or_fetch_and_insert_comment,
+};
 use activitystreams::activity::kind::RemoveType;
 use lemmy_api_common::blocking;
-use crate::{check_is_apub_id_valid, fetcher::objects::get_or_fetch_and_insert_comment};
-use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew, PublicUrl};
+use lemmy_apub_lib::{
+  values::PublicUrl,
+  verify_domains_match,
+  ActivityCommonFields,
+  ActivityHandlerNew,
+};
 use lemmy_db_queries::source::comment::Comment_;
 use lemmy_db_schema::source::comment::Comment;
 use lemmy_utils::LemmyError;
index f3ebdf3851ab2d4ef5b203c7634d22146a26ce6b..07aa67119b0c31d425271b5f29491e75de44e8bb 100644 (file)
@@ -1,11 +1,19 @@
-use crate::activities::{
-  comment::{remove::RemoveComment, send_websocket_message},
-  verify_mod_action,
+use crate::{
+  activities::{
+    comment::{remove::RemoveComment, send_websocket_message},
+    verify_mod_action,
+  },
+  check_is_apub_id_valid,
+  fetcher::objects::get_or_fetch_and_insert_comment,
 };
 use activitystreams::activity::kind::UndoType;
 use lemmy_api_common::blocking;
-use crate::{check_is_apub_id_valid, fetcher::objects::get_or_fetch_and_insert_comment};
-use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew, PublicUrl};
+use lemmy_apub_lib::{
+  values::PublicUrl,
+  verify_domains_match,
+  ActivityCommonFields,
+  ActivityHandlerNew,
+};
 use lemmy_db_queries::source::comment::Comment_;
 use lemmy_db_schema::source::comment::Comment;
 use lemmy_utils::LemmyError;
index 5e785229f4739b78b530aa4a7a9ebce33cf1e052..c0d0148138084edbf19c2aba715ead8dc0faad97 100644 (file)
@@ -10,7 +10,12 @@ use crate::{
   NoteExt,
 };
 use activitystreams::{activity::kind::UpdateType, base::BaseExt};
-use lemmy_apub_lib::{verify_domains_match_opt, ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{
+  values::PublicUrl,
+  verify_domains_match_opt,
+  ActivityCommonFields,
+  ActivityHandler,
+};
 use lemmy_db_schema::source::comment::Comment;
 use lemmy_utils::LemmyError;
 use lemmy_websocket::{LemmyContext, UserOperationCrud};
index fd22b978a6496614446533b4ff103575d617e39e..0dcd9818b5a9966e8c94823c4aa2e65e386a28bf 100644 (file)
@@ -10,7 +10,7 @@ use crate::{
 };
 use activitystreams::{activity::kind::AddType, base::AnyBase};
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
 use lemmy_db_queries::{source::community::CommunityModerator_, Joinable};
 use lemmy_db_schema::source::community::{CommunityModerator, CommunityModeratorForm};
 use lemmy_utils::LemmyError;
index 04721edac3df7ed015472d548dbc7833f9bfd0b5..bc72d80fe91ae15b6287f256700007078b3c69e5 100644 (file)
@@ -34,7 +34,7 @@ use crate::{
   CommunityType,
 };
 use activitystreams::activity::kind::AnnounceType;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
 use lemmy_db_schema::source::community::Community;
 use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
index 1c5f0eaf58a081c8fb4b51a9e91ef49ad1485f49..34909b2e6086904264c906d152fa3f3acebe6d8c 100644 (file)
@@ -4,7 +4,7 @@ use crate::{
 };
 use activitystreams::activity::kind::BlockType;
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
 use lemmy_db_queries::{Bannable, Followable};
 use lemmy_db_schema::source::community::{
   CommunityFollower,
index d44fe266905c5d6ef5b938add6b521bf5fbbafd8..53b665db15fd210e918580a97d3f1cdf1381081f 100644 (file)
@@ -9,7 +9,7 @@ use crate::{
 };
 use activitystreams::activity::kind::UndoType;
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
 use lemmy_db_queries::Bannable;
 use lemmy_db_schema::source::community::{CommunityPersonBan, CommunityPersonBanForm};
 use lemmy_utils::LemmyError;
index ce1854969bd4d8220cbcb6111b046e76453c7d3d..fd0bf2d456a76c3fe70dcaaa5dcc3b0390456ce3 100644 (file)
@@ -10,7 +10,7 @@ use crate::{
 };
 use activitystreams::activity::kind::UpdateType;
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
 use lemmy_db_queries::{ApubObject, Crud};
 use lemmy_db_schema::source::community::{Community, CommunityForm};
 use lemmy_utils::LemmyError;
index f7e7fe5c2b6765b3e19bdbf9a9c676f40e9c7a0b..cabfcce8659fb0f847cf04480a22d219273bdc1d 100644 (file)
@@ -18,7 +18,7 @@ use crate::{
 };
 use activitystreams::activity::kind::DeleteType;
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
 use lemmy_db_queries::{
   source::{comment::Comment_, community::Community_, post::Post_},
   Crud,
index 94e44d7acd969862298b649d754d8492719b75a5..ea70e5f5e76486a744dd959416ce0281a457d1da 100644 (file)
@@ -18,7 +18,7 @@ use crate::{
 };
 use activitystreams::activity::kind::UndoType;
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
 use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_};
 use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
 use lemmy_utils::LemmyError;
index 270b712b3e9600a72c1fcf2579ebbf5e643432ae..3dd8ce5540134cd8305dfed05f55fd5a8a02c813 100644 (file)
@@ -17,11 +17,11 @@ use activitystreams::activity::kind::CreateType;
 use anyhow::anyhow;
 use lemmy_api_common::blocking;
 use lemmy_apub_lib::{
+  values::PublicUrl,
   verify_domains_match,
   verify_urls_match,
   ActivityCommonFields,
   ActivityHandler,
-  PublicUrl,
 };
 use lemmy_db_queries::Crud;
 use lemmy_db_schema::source::{community::Community, person::Person, post::Post};
index c9fbb548f6a8ff081f6453b759cac9f597741b67..f5cd07b9f69e6576b905be8c5856b039af103b0a 100644 (file)
@@ -15,7 +15,7 @@ use crate::{
 };
 use activitystreams::activity::kind::UpdateType;
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
 use lemmy_db_queries::Crud;
 use lemmy_db_schema::source::{community::Community, person::Person, post::Post};
 use lemmy_utils::LemmyError;
index 7e62ab7bdb258ebea7db6d861c193adc5edae88f..053ddadf8a2b55a272887e979613aa99b9c7c2c3 100644 (file)
@@ -19,7 +19,7 @@ use crate::{
 use activitystreams::{activity::kind::RemoveType, base::AnyBase};
 use anyhow::anyhow;
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
 use lemmy_db_queries::{
   source::{comment::Comment_, community::Community_, post::Post_},
   Joinable,
index ca77a31da0e76968b00ac242fe3f8b8297f8f6b6..db4518b27de52a3ebb623093e807efda69457db3 100644 (file)
@@ -17,7 +17,7 @@ use crate::{
 use activitystreams::activity::kind::UndoType;
 use anyhow::anyhow;
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
 use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_};
 use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
 use lemmy_utils::LemmyError;
index b34b2d10b403625419e78d5e5c2b4a3f54327b7d..54b673008aa65037dad43a5609365bc8b57d0254 100644 (file)
@@ -4,7 +4,7 @@ use crate::activities::{
   voting::receive_like_or_dislike,
 };
 use activitystreams::activity::kind::DislikeType;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
 use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use url::Url;
index 50a4d44d48a0b54f7b619893fcf2593c731834e6..90f29c427a63a194147c30f563f960dfe90c69a0 100644 (file)
@@ -4,7 +4,7 @@ use crate::activities::{
   voting::receive_like_or_dislike,
 };
 use activitystreams::activity::kind::LikeType;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
 use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use url::Url;
index 5ba3b47f93ddc8792cc706bf131d2abb23537bc2..13e3e1f7685ab4df46b72681ef876d8cfcbe7de0 100644 (file)
@@ -4,7 +4,7 @@ use crate::activities::{
   voting::{dislike::DislikePostOrComment, receive_undo_like_or_dislike},
 };
 use activitystreams::activity::kind::UndoType;
-use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
 use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use url::Url;
index 2de03f4ba19a091403f3173ed5a8c819893b13da..ab15da70b31694ceb3e224f4a94d2f3fcc94cd68 100644 (file)
@@ -4,7 +4,7 @@ use crate::activities::{
   voting::{like::LikePostOrComment, receive_undo_like_or_dislike},
 };
 use activitystreams::activity::kind::UndoType;
-use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandler, PublicUrl};
+use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
 use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use url::Url;
index 6a52673e6c0a883c2f591e19b2771f07c747a894..7191a4a1b0e9fbab401b1f3dbe4a84a04f333775 100644 (file)
@@ -12,6 +12,7 @@ use activitystreams::{
 use anyhow::{anyhow, Context};
 use chrono::NaiveDateTime;
 use lemmy_api_common::blocking;
+use lemmy_apub_lib::values::MediaTypeMarkdown;
 use lemmy_db_queries::{ApubObject, Crud, DbPool};
 use lemmy_db_schema::{CommunityId, DbUrl};
 use lemmy_utils::{
@@ -70,18 +71,6 @@ pub trait FromApubToForm<ApubType> {
     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 {
index 7c79ff88b8e77f5229db26151d954c375c8c7266..a7a6cfe93e542a39e2c5a59be929f7a66d1ed001 100644 (file)
@@ -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, MediaTypeHtml, MediaTypeMarkdown, Source, ToApub},
+  objects::{create_tombstone, FromApub, Source, ToApub},
 };
 use activitystreams::{
   base::AnyBase,
@@ -16,7 +16,10 @@ use activitystreams::{
 };
 use chrono::{DateTime, FixedOffset};
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::verify_domains_match;
+use lemmy_apub_lib::{
+  values::{MediaTypeHtml, MediaTypeMarkdown},
+  verify_domains_match,
+};
 use lemmy_db_queries::{ApubObject, Crud, DbPool};
 use lemmy_db_schema::{
   self,
@@ -126,7 +129,7 @@ impl ToApub for Post {
       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,
+      media_type: MediaTypeHtml::Html,
       source,
       url: self.url.clone().map(|u| u.into()),
       image,
index 327670b504a1c5fe67a89df9a60758c7376768f8..6f8c841701ef4068085cceabb9feb89d1a69fa60 100644 (file)
@@ -12,3 +12,4 @@ activitystreams-ext = "0.1.0-alpha.2"
 serde = { version = "1.0.123", features = ["derive"] }
 async-trait = "0.1.42"
 url = { version = "2.2.1", features = ["serde"] }
+serde_json = { version = "1.0.64", features = ["preserve_order"] }
index 8156457211d136b20ff26c721f2eb715791897a9..13fc4025d8047cd96a42fe052705a1c428de929b 100644 (file)
@@ -1,3 +1,5 @@
+pub mod values;
+
 use activitystreams::{
   base::AnyBase,
   error::DomainError,
@@ -9,12 +11,6 @@ use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use url::Url;
 
-#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
-pub enum PublicUrl {
-  #[serde(rename = "https://www.w3.org/ns/activitystreams#Public")]
-  Public,
-}
-
 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
 #[serde(rename_all = "camelCase")]
 pub struct ActivityCommonFields {
diff --git a/crates/apub_lib/src/values/mod.rs b/crates/apub_lib/src/values/mod.rs
new file mode 100644 (file)
index 0000000..2c9f65f
--- /dev/null
@@ -0,0 +1,61 @@
+//! The enums here serve to limit a json string value to a single, hardcoded value which can be
+//! verified at compilation time. When using it as the type of a struct field, the struct can only
+//! be constructed or deserialized if the field has the exact same value.
+//!
+//! If we used String as the field type, any value would be accepted, and we would have to check
+//! manually at runtime that it contains the expected value.
+//!
+//! The enums in [`activitystreams::activity::kind`] work in the same way, and can be used to
+//! distinguish different activity types.
+//!
+//! In the example below, `MyObject` can only be constructed or
+//! deserialized if `media_type` is `text/markdown`, but not if it is `text/html`.
+//!
+//! ```
+//! use lemmy_apub_lib::values::MediaTypeMarkdown;
+//! use serde_json::from_str;
+//! use serde::{Deserialize, Serialize};
+//!
+//! #[derive(Deserialize, Serialize)]
+//! struct MyObject {
+//!   content: String,
+//!   media_type: MediaTypeMarkdown,
+//! }
+//!
+//! let markdown_json = r#"{"content": "**test**", "media_type": "text/markdown"}"#;
+//! let from_markdown = from_str::<MyObject>(markdown_json);
+//! assert!(from_markdown.is_ok());
+//!
+//! let markdown_html = r#"{"content": "<b>test</b>", "media_type": "text/html"}"#;
+//! let from_html = from_str::<MyObject>(markdown_html);
+//! assert!(from_html.is_err());
+//! ```
+
+use serde::{Deserialize, Serialize};
+
+/// The identifier used to address activities to the public.
+///
+/// <https://www.w3.org/TR/activitypub/#public-addressing>
+#[derive(Debug, Clone, Deserialize, Serialize)]
+pub enum PublicUrl {
+  #[serde(rename = "https://www.w3.org/ns/activitystreams#Public")]
+  Public,
+}
+
+/// Media type for markdown text.
+///
+/// <https://www.iana.org/assignments/media-types/media-types.xhtml>
+#[derive(Clone, Debug, Deserialize, Serialize)]
+pub enum MediaTypeMarkdown {
+  #[serde(rename = "text/markdown")]
+  Markdown,
+}
+
+/// Media type for HTML text/
+///
+/// <https://www.iana.org/assignments/media-types/media-types.xhtml>
+#[derive(Clone, Debug, Deserialize, Serialize)]
+pub enum MediaTypeHtml {
+  #[serde(rename = "text/html")]
+  Html,
+}