]> Untitled Git - lemmy.git/commitdiff
Add default post listing type (fixes #2195) (#2209)
authorNutomic <me@nutomic.com>
Tue, 19 Apr 2022 19:05:08 +0000 (19:05 +0000)
committerGitHub <noreply@github.com>
Tue, 19 Apr 2022 19:05:08 +0000 (19:05 +0000)
* Add default post listing type (fixes #2195)

* review fixes

* change column type

13 files changed:
config/defaults.hjson
crates/api_common/src/site.rs
crates/api_crud/src/post/list.rs
crates/api_crud/src/post/read.rs
crates/api_crud/src/site/create.rs
crates/api_crud/src/site/read.rs
crates/api_crud/src/site/update.rs
crates/db_schema/src/lib.rs
crates/db_schema/src/schema.rs
crates/db_schema/src/source/site.rs
crates/utils/src/settings/structs.rs
migrations/2022-04-12-114352_default_post_listing_type/down.sql [new file with mode: 0644]
migrations/2022-04-12-114352_default_post_listing_type/up.sql [new file with mode: 0644]

index f14bacb51c86444afdf2dd0843e0096d83a134ec..681e60966fafc6722b12ffa4c24150626dcea061 100644 (file)
     application_question: "string"
     private_instance: true
     default_theme: "string"
+    default_post_listing_type: "string"
   }
   # the domain name of your instance (mandatory)
   hostname: "unset"
index df752c00c13a9f8a2fff4843839fc099ff2263a3..f7329822ba25e74c191b9f08a6f5fceb6a5dfdcb 100644 (file)
@@ -107,6 +107,7 @@ pub struct CreateSite {
   pub application_question: Option<String>,
   pub private_instance: Option<bool>,
   pub default_theme: Option<String>,
+  pub default_post_listing_type: Option<String>,
   pub auth: Sensitive<String>,
 }
 
@@ -126,6 +127,7 @@ pub struct EditSite {
   pub application_question: Option<String>,
   pub private_instance: Option<bool>,
   pub default_theme: Option<String>,
+  pub default_post_listing_type: Option<String>,
   pub auth: Sensitive<String>,
 }
 
index 8a714e992883c35a106438e0261cdc894494b30e..154faaaafedf9dcc6ea0c8b5f709655516ccf5ed 100644 (file)
@@ -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<SortType> = from_opt_str_to_opt_enum(&data.sort);
-    let listing_type: Option<ListingType> = 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;
index 0d4f94ff2a0a4b00f485441d19e92e77459a5a49..03507ba4db8cb4677ef06a48de6c1368743c7e88 100644 (file)
@@ -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};
index 3ee2874b4f1d8c70e68e96702c6495b1746e041e..7ec772b91d61874fe09fe7f2dc008cce087b5cc8 100644 (file)
@@ -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()
     };
 
index 9131c3ecfa9bb99fda3a7b290978aef65db48c12..17c838964d3d6d14178875dc20a891f30ffef50b 100644 (file)
@@ -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?;
index 1fa33b2b08014e9a5a505b915090434c9a66764e..a6890d266a78da67797060268c18e6c0246ab816 100644 (file)
@@ -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()
     };
 
index 724fffcd4d756d9de52d98562f2f28ba486d1418..bd6e0d1906cf07551de105cdad5c6eb48d311925 100644 (file)
@@ -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,
index 390a8e723035ca625cf643f175400a7143444b46..3ff563175d046e872d74d6bf3f4a6fd278e26649 100644 (file)
@@ -461,6 +461,7 @@ table! {
         private_key -> Nullable<Text>,
         public_key -> Text,
         default_theme -> Text,
+        default_post_listing_type -> Text,
     }
 }
 
index 6a055842054d72854880ef239e70f24f050316d3..8e73cca41b411fb78e1a9a60dd2e8f78d4e69a1d 100644 (file)
@@ -26,6 +26,7 @@ pub struct Site {
   pub private_key: Option<String>,
   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<Option<String>>,
   pub public_key: Option<String>,
   pub default_theme: Option<String>,
+  pub default_post_listing_type: Option<String>,
 }
index 9a7a953a43ac5de2531fac3f3c9e5413b87b3a2e..561d5cec94f57262a2a8f46bef3ee15059a9ee93 100644 (file)
@@ -217,4 +217,6 @@ pub struct SetupConfig {
   pub private_instance: Option<bool>,
   #[default(None)]
   pub default_theme: Option<String>,
+  #[default(None)]
+  pub default_post_listing_type: Option<String>,
 }
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 (file)
index 0000000..b70a7dd
--- /dev/null
@@ -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 (file)
index 0000000..f68e198
--- /dev/null
@@ -0,0 +1 @@
+alter table site add column default_post_listing_type text not null default 'Local';