From 00b0b26dfa0f79fdb4867fd4545b119ca81a521b Mon Sep 17 00:00:00 2001 From: Nutomic Date: Tue, 19 Apr 2022 19:05:08 +0000 Subject: [PATCH] Add default post listing type (fixes #2195) (#2209) * Add default post listing type (fixes #2195) * review fixes * change column type --- config/defaults.hjson | 1 + crates/api_common/src/site.rs | 2 ++ crates/api_crud/src/post/list.rs | 13 ++++++++++--- crates/api_crud/src/post/read.rs | 2 +- crates/api_crud/src/site/create.rs | 1 + crates/api_crud/src/site/read.rs | 1 + crates/api_crud/src/site/update.rs | 14 +++++++++++++- crates/db_schema/src/lib.rs | 2 +- crates/db_schema/src/schema.rs | 1 + crates/db_schema/src/source/site.rs | 2 ++ crates/utils/src/settings/structs.rs | 2 ++ .../down.sql | 1 + .../up.sql | 1 + 13 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 migrations/2022-04-12-114352_default_post_listing_type/down.sql create mode 100644 migrations/2022-04-12-114352_default_post_listing_type/up.sql diff --git a/config/defaults.hjson b/config/defaults.hjson index f14bacb5..681e6096 100644 --- a/config/defaults.hjson +++ b/config/defaults.hjson @@ -109,6 +109,7 @@ application_question: "string" private_instance: true default_theme: "string" + default_post_listing_type: "string" } # the domain name of your instance (mandatory) hostname: "unset" diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index df752c00..f7329822 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -107,6 +107,7 @@ pub struct CreateSite { pub application_question: Option, pub private_instance: Option, pub default_theme: Option, + pub default_post_listing_type: Option, pub auth: Sensitive, } @@ -126,6 +127,7 @@ pub struct EditSite { pub application_question: Option, pub private_instance: Option, pub default_theme: Option, + pub default_post_listing_type: Option, pub auth: Sensitive, } diff --git a/crates/api_crud/src/post/list.rs b/crates/api_crud/src/post/list.rs index 8a714e99..154faaaa 100644 --- a/crates/api_crud/src/post/list.rs +++ b/crates/api_crud/src/post/list.rs @@ -4,12 +4,12 @@ use lemmy_api_common::{ blocking, check_private_instance, get_local_user_view_from_jwt_opt, - post::*, + post::{GetPosts, GetPostsResponse}, }; use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity}; use lemmy_db_schema::{ from_opt_str_to_opt_enum, - source::community::Community, + source::{community::Community, site::Site}, traits::DeleteableOrRemoveable, ListingType, SortType, @@ -17,6 +17,7 @@ use lemmy_db_schema::{ use lemmy_db_views::post_view::PostQueryBuilder; use lemmy_utils::{ConnectionId, LemmyError}; use lemmy_websocket::LemmyContext; +use std::str::FromStr; #[async_trait::async_trait(?Send)] impl PerformCrud for GetPosts { @@ -46,7 +47,13 @@ impl PerformCrud for GetPosts { .map(|t| t.local_user.show_read_posts); let sort: Option = from_opt_str_to_opt_enum(&data.sort); - let listing_type: Option = from_opt_str_to_opt_enum(&data.type_); + let listing_type: ListingType = match from_opt_str_to_opt_enum(&data.type_) { + Some(l) => l, + None => { + let site = blocking(context.pool(), Site::read_local_site).await??; + ListingType::from_str(&site.default_post_listing_type)? + } + }; let page = data.page; let limit = data.limit; diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 0d4f94ff..03507ba4 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -5,7 +5,7 @@ use lemmy_api_common::{ check_private_instance, get_local_user_view_from_jwt_opt, mark_post_as_read, - post::*, + post::{GetPost, GetPostResponse}, }; use lemmy_db_schema::traits::DeleteableOrRemoveable; use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostView}; diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 3ee2874b..7ec772b9 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -84,6 +84,7 @@ impl PerformCrud for CreateSite { private_key: Some(Some(keypair.private_key)), public_key: Some(keypair.public_key), default_theme: data.default_theme.clone(), + default_post_listing_type: data.default_post_listing_type.clone(), ..SiteForm::default() }; diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 9131c3ec..17c83896 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -69,6 +69,7 @@ impl PerformCrud for GetSite { application_question: setup.application_question.to_owned(), private_instance: setup.private_instance, default_theme: setup.default_theme.to_owned(), + default_post_listing_type: setup.default_post_listing_type.to_owned(), auth: admin_jwt, }; create_site.perform(context, websocket_id).await?; diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 1fa33b2b..a6890d26 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -17,11 +17,12 @@ use lemmy_db_schema::{ site::{Site, SiteForm}, }, traits::Crud, + ListingType, }; use lemmy_db_views::site_view::SiteView; use lemmy_utils::{utils::check_slurs_opt, ConnectionId, LemmyError}; use lemmy_websocket::{messages::SendAllMessage, LemmyContext, UserOperationCrud}; -use std::default::Default; +use std::{default::Default, str::FromStr}; #[async_trait::async_trait(?Send)] impl PerformCrud for EditSite { @@ -64,6 +65,16 @@ impl PerformCrud for EditSite { return Err(LemmyError::from_message("application_question_required")); } + if let Some(default_post_listing_type) = &data.default_post_listing_type { + // only allow all or local as default listing types + let val = ListingType::from_str(default_post_listing_type); + if val != Ok(ListingType::All) && val != Ok(ListingType::Local) { + return Err(LemmyError::from_message( + "invalid_default_post_listing_type", + )); + } + } + let site_form = SiteForm { name: data.name.to_owned().unwrap_or(local_site.name), sidebar, @@ -80,6 +91,7 @@ impl PerformCrud for EditSite { application_question, private_instance: data.private_instance, default_theme: data.default_theme.clone(), + default_post_listing_type: data.default_post_listing_type.clone(), ..SiteForm::default() }; diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index 724fffcd..bd6e0d19 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -46,7 +46,7 @@ pub enum SortType { NewComments, } -#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)] +#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] pub enum ListingType { All, Local, diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 390a8e72..3ff56317 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -461,6 +461,7 @@ table! { private_key -> Nullable, public_key -> Text, default_theme -> Text, + default_post_listing_type -> Text, } } diff --git a/crates/db_schema/src/source/site.rs b/crates/db_schema/src/source/site.rs index 6a055842..8e73cca4 100644 --- a/crates/db_schema/src/source/site.rs +++ b/crates/db_schema/src/source/site.rs @@ -26,6 +26,7 @@ pub struct Site { pub private_key: Option, pub public_key: String, pub default_theme: String, + pub default_post_listing_type: String, } #[derive(Insertable, AsChangeset, Default)] @@ -52,4 +53,5 @@ pub struct SiteForm { pub private_key: Option>, pub public_key: Option, pub default_theme: Option, + pub default_post_listing_type: Option, } diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index 9a7a953a..561d5cec 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -217,4 +217,6 @@ pub struct SetupConfig { pub private_instance: Option, #[default(None)] pub default_theme: Option, + #[default(None)] + pub default_post_listing_type: Option, } diff --git a/migrations/2022-04-12-114352_default_post_listing_type/down.sql b/migrations/2022-04-12-114352_default_post_listing_type/down.sql new file mode 100644 index 00000000..b70a7dd0 --- /dev/null +++ b/migrations/2022-04-12-114352_default_post_listing_type/down.sql @@ -0,0 +1 @@ +alter table site drop column default_post_listing_type; diff --git a/migrations/2022-04-12-114352_default_post_listing_type/up.sql b/migrations/2022-04-12-114352_default_post_listing_type/up.sql new file mode 100644 index 00000000..f68e198f --- /dev/null +++ b/migrations/2022-04-12-114352_default_post_listing_type/up.sql @@ -0,0 +1 @@ +alter table site add column default_post_listing_type text not null default 'Local'; -- 2.44.1