From 8577e55e1b4e93a1d28c67629cd74d88661fb0c4 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 12 Apr 2022 13:56:00 +0200 Subject: [PATCH] Add default post listing type (fixes #2195) --- 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 | 17 ++++++++++++++++- 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, 40 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 f14bacb51c..681e60966f 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 df752c00c1..f7329822ba 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 8a714e9928..154faaaafe 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 0d4f94ff2a..03507ba4db 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 3ee2874b4f..7ec772b91d 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 9131c3ecfa..17c838964d 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 1fa33b2b08..b0980127d2 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,19 @@ 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 ok = match ListingType::from_str(default_post_listing_type) { + Ok(l) => l == ListingType::All || l == ListingType::Local, + Err(_) => false, + }; + if !ok { + 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 +94,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 724fffcd4d..bd6e0d1906 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 390a8e7230..3ff563175d 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 6a05584205..8e73cca41b 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 9a7a953a43..561d5cec94 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 0000000000..b70a7dd0cc --- /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 0000000000..a5f8c8ff5d --- /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 varchar not null default 'All';