From: Felix Ableitner Date: Fri, 30 Jul 2021 14:35:32 +0000 (+0200) Subject: Add docs for MediaType, PublicUrl values X-Git-Url: http://these/git/%22/display/44b525e5-4101b003e005e70a-d472d963/static/%7BelementUrl%7D?a=commitdiff_plain;h=433ab1e78b9da5c8f75cc9734dd5ce715cf0cc8b;p=lemmy.git Add docs for MediaType, PublicUrl values --- diff --git a/Cargo.lock b/Cargo.lock index 53e3e007..ffa6f79c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1760,6 +1760,7 @@ dependencies = [ "lemmy_utils", "lemmy_websocket", "serde", + "serde_json", "url", ] diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 89f71d93..b2034a36 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -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; diff --git a/crates/apub/src/activities/comment/create.rs b/crates/apub/src/activities/comment/create.rs index b3855335..0e947219 100644 --- a/crates/apub/src/activities/comment/create.rs +++ b/crates/apub/src/activities/comment/create.rs @@ -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}; diff --git a/crates/apub/src/activities/comment/remove.rs b/crates/apub/src/activities/comment/remove.rs index d60e3f80..5702f9fa 100644 --- a/crates/apub/src/activities/comment/remove.rs +++ b/crates/apub/src/activities/comment/remove.rs @@ -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; diff --git a/crates/apub/src/activities/comment/undo_remove.rs b/crates/apub/src/activities/comment/undo_remove.rs index f3ebdf38..07aa6711 100644 --- a/crates/apub/src/activities/comment/undo_remove.rs +++ b/crates/apub/src/activities/comment/undo_remove.rs @@ -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; diff --git a/crates/apub/src/activities/comment/update.rs b/crates/apub/src/activities/comment/update.rs index 5e785229..c0d01481 100644 --- a/crates/apub/src/activities/comment/update.rs +++ b/crates/apub/src/activities/comment/update.rs @@ -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}; diff --git a/crates/apub/src/activities/community/add_mod.rs b/crates/apub/src/activities/community/add_mod.rs index fd22b978..0dcd9818 100644 --- a/crates/apub/src/activities/community/add_mod.rs +++ b/crates/apub/src/activities/community/add_mod.rs @@ -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; diff --git a/crates/apub/src/activities/community/announce.rs b/crates/apub/src/activities/community/announce.rs index 04721eda..bc72d80f 100644 --- a/crates/apub/src/activities/community/announce.rs +++ b/crates/apub/src/activities/community/announce.rs @@ -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; diff --git a/crates/apub/src/activities/community/block_user.rs b/crates/apub/src/activities/community/block_user.rs index 1c5f0eaf..34909b2e 100644 --- a/crates/apub/src/activities/community/block_user.rs +++ b/crates/apub/src/activities/community/block_user.rs @@ -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, diff --git a/crates/apub/src/activities/community/undo_block_user.rs b/crates/apub/src/activities/community/undo_block_user.rs index d44fe266..53b665db 100644 --- a/crates/apub/src/activities/community/undo_block_user.rs +++ b/crates/apub/src/activities/community/undo_block_user.rs @@ -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; diff --git a/crates/apub/src/activities/community/update.rs b/crates/apub/src/activities/community/update.rs index ce185496..fd0bf2d4 100644 --- a/crates/apub/src/activities/community/update.rs +++ b/crates/apub/src/activities/community/update.rs @@ -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; diff --git a/crates/apub/src/activities/deletion/delete.rs b/crates/apub/src/activities/deletion/delete.rs index f7e7fe5c..cabfcce8 100644 --- a/crates/apub/src/activities/deletion/delete.rs +++ b/crates/apub/src/activities/deletion/delete.rs @@ -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, diff --git a/crates/apub/src/activities/deletion/undo_delete.rs b/crates/apub/src/activities/deletion/undo_delete.rs index 94e44d7a..ea70e5f5 100644 --- a/crates/apub/src/activities/deletion/undo_delete.rs +++ b/crates/apub/src/activities/deletion/undo_delete.rs @@ -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; diff --git a/crates/apub/src/activities/post/create.rs b/crates/apub/src/activities/post/create.rs index 270b712b..3dd8ce55 100644 --- a/crates/apub/src/activities/post/create.rs +++ b/crates/apub/src/activities/post/create.rs @@ -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}; diff --git a/crates/apub/src/activities/post/update.rs b/crates/apub/src/activities/post/update.rs index c9fbb548..f5cd07b9 100644 --- a/crates/apub/src/activities/post/update.rs +++ b/crates/apub/src/activities/post/update.rs @@ -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; diff --git a/crates/apub/src/activities/removal/remove.rs b/crates/apub/src/activities/removal/remove.rs index 7e62ab7b..053ddadf 100644 --- a/crates/apub/src/activities/removal/remove.rs +++ b/crates/apub/src/activities/removal/remove.rs @@ -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, diff --git a/crates/apub/src/activities/removal/undo_remove.rs b/crates/apub/src/activities/removal/undo_remove.rs index ca77a31d..db4518b2 100644 --- a/crates/apub/src/activities/removal/undo_remove.rs +++ b/crates/apub/src/activities/removal/undo_remove.rs @@ -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; diff --git a/crates/apub/src/activities/voting/dislike.rs b/crates/apub/src/activities/voting/dislike.rs index b34b2d10..54b67300 100644 --- a/crates/apub/src/activities/voting/dislike.rs +++ b/crates/apub/src/activities/voting/dislike.rs @@ -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; diff --git a/crates/apub/src/activities/voting/like.rs b/crates/apub/src/activities/voting/like.rs index 50a4d44d..90f29c42 100644 --- a/crates/apub/src/activities/voting/like.rs +++ b/crates/apub/src/activities/voting/like.rs @@ -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; diff --git a/crates/apub/src/activities/voting/undo_dislike.rs b/crates/apub/src/activities/voting/undo_dislike.rs index 5ba3b47f..13e3e1f7 100644 --- a/crates/apub/src/activities/voting/undo_dislike.rs +++ b/crates/apub/src/activities/voting/undo_dislike.rs @@ -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; diff --git a/crates/apub/src/activities/voting/undo_like.rs b/crates/apub/src/activities/voting/undo_like.rs index 2de03f4b..ab15da70 100644 --- a/crates/apub/src/activities/voting/undo_like.rs +++ b/crates/apub/src/activities/voting/undo_like.rs @@ -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; diff --git a/crates/apub/src/objects/mod.rs b/crates/apub/src/objects/mod.rs index 6a52673e..7191a4a1 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -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 { 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 { diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 7c79ff88..a7a6cfe9 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, 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, diff --git a/crates/apub_lib/Cargo.toml b/crates/apub_lib/Cargo.toml index 327670b5..6f8c8417 100644 --- a/crates/apub_lib/Cargo.toml +++ b/crates/apub_lib/Cargo.toml @@ -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"] } diff --git a/crates/apub_lib/src/lib.rs b/crates/apub_lib/src/lib.rs index 81564572..13fc4025 100644 --- a/crates/apub_lib/src/lib.rs +++ b/crates/apub_lib/src/lib.rs @@ -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 index 00000000..2c9f65ff --- /dev/null +++ b/crates/apub_lib/src/values/mod.rs @@ -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::(markdown_json); +//! assert!(from_markdown.is_ok()); +//! +//! let markdown_html = r#"{"content": "test", "media_type": "text/html"}"#; +//! let from_html = from_str::(markdown_html); +//! assert!(from_html.is_err()); +//! ``` + +use serde::{Deserialize, Serialize}; + +/// The identifier used to address activities to the public. +/// +/// +#[derive(Debug, Clone, Deserialize, Serialize)] +pub enum PublicUrl { + #[serde(rename = "https://www.w3.org/ns/activitystreams#Public")] + Public, +} + +/// Media type for markdown text. +/// +/// +#[derive(Clone, Debug, Deserialize, Serialize)] +pub enum MediaTypeMarkdown { + #[serde(rename = "text/markdown")] + Markdown, +} + +/// Media type for HTML text/ +/// +/// +#[derive(Clone, Debug, Deserialize, Serialize)] +pub enum MediaTypeHtml { + #[serde(rename = "text/html")] + Html, +}