Skip to content

Commit

Permalink
Add default post listing type (fixes #2195)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Apr 12, 2022
1 parent 2180bd0 commit 61200e1
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions crates/api_common/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

Expand All @@ -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>,
}

Expand Down
7 changes: 5 additions & 2 deletions crates/api_crud/src/post/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use lemmy_api_common::{
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,
Expand All @@ -25,6 +25,7 @@ use lemmy_db_views_actor::{
};
use lemmy_utils::{ConnectionId, LemmyError};
use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
use std::str::FromStr;

#[async_trait::async_trait(?Send)]
impl PerformCrud for GetPost {
Expand Down Expand Up @@ -146,7 +147,9 @@ 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 site = blocking(context.pool(), Site::read_local_site).await??;
let listing_type: ListingType = from_opt_str_to_opt_enum(&data.type_)
.unwrap_or(ListingType::from_str(&site.default_post_listing_type)?);

let page = data.page;
let limit = data.limit;
Expand Down
2 changes: 2 additions & 0 deletions crates/api_crud/src/site/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use lemmy_db_schema::{
newtypes::DbUrl,
source::site::{Site, SiteForm},
traits::Crud,
ListingType,
};
use lemmy_db_views::site_view::SiteView;
use lemmy_utils::{
Expand Down Expand Up @@ -84,6 +85,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: Some(ListingType::All.to_string()),
..SiteForm::default()
};

Expand Down
1 change: 1 addition & 0 deletions crates/api_crud/src/site/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
12 changes: 11 additions & 1 deletion crates/api_crud/src/site/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -64,6 +65,14 @@ impl PerformCrud for EditSite {
return Err(LemmyError::from_message("application_question_required"));
}

if let Some(default_post_listing_type) = &data.default_post_listing_type {
if ListingType::from_str(default_post_listing_type).is_err() {
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,
Expand All @@ -80,6 +89,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()
};

Expand Down
1 change: 1 addition & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ table! {
private_key -> Nullable<Text>,
public_key -> Text,
default_theme -> Text,
default_post_listing_type -> Text,
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/db_schema/src/source/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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>,
}
2 changes: 2 additions & 0 deletions crates/utils/src/settings/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table site drop column default_post_listing_type;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table site add column default_post_listing_type varchar not null default 'All';

0 comments on commit 61200e1

Please sign in to comment.