diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 3f02663c5..7d1dddf89 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -161,6 +161,7 @@ pub struct CreateSite { pub private_instance: Option, pub default_theme: Option, pub default_post_listing_type: Option, + pub default_sort_type: Option, pub legal_information: Option, pub application_email_admins: Option, pub hide_modlog_mod_names: Option, @@ -218,6 +219,8 @@ pub struct EditSite { /// The default theme. Usually "browser" pub default_theme: Option, pub default_post_listing_type: Option, + /// The default sort, usually "active" + pub default_sort_type: Option, /// An optional page of legal information pub legal_information: Option, /// Whether to email admins when receiving a new application. diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 994375027..0f836012d 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -92,6 +92,7 @@ pub async fn create_site( private_instance: data.private_instance, default_theme: data.default_theme.clone(), default_post_listing_type: data.default_post_listing_type, + default_sort_type: data.default_sort_type, legal_information: diesel_option_overwrite(data.legal_information.clone()), application_email_admins: data.application_email_admins, hide_modlog_mod_names: data.hide_modlog_mod_names, @@ -190,7 +191,7 @@ mod tests { use crate::site::create::validate_create_payload; use lemmy_api_common::site::CreateSite; - use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode}; + use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode, SortType}; use lemmy_utils::error::LemmyErrorType; #[test] @@ -212,6 +213,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, None::, @@ -235,6 +237,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, None::, @@ -258,6 +261,7 @@ mod tests { None::, None::, None::, + None::, Some(String::from("(zeta|alpha)")), None::, None::, @@ -281,6 +285,7 @@ mod tests { None::, None::, Some(ListingType::Subscribed), + None::, None::, None::, None::, @@ -304,6 +309,7 @@ mod tests { None::, None::, None::, + None::, None::, Some(true), Some(true), @@ -327,6 +333,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, Some(true), @@ -350,6 +357,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, None::, @@ -407,6 +415,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, None::, @@ -429,6 +438,7 @@ mod tests { Some(String::new()), Some(String::new()), Some(ListingType::All), + Some(SortType::Active), Some(String::new()), Some(false), Some(true), @@ -451,6 +461,7 @@ mod tests { None::, None::, None::, + None::, Some(String::new()), None::, None::, @@ -473,6 +484,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, None::, @@ -522,6 +534,7 @@ mod tests { site_description: Option, site_sidebar: Option, site_listing_type: Option, + site_sort_type: Option, site_slur_filter_regex: Option, site_is_private: Option, site_is_federated: Option, @@ -542,6 +555,7 @@ mod tests { private_instance: site_is_private, default_theme: None, default_post_listing_type: site_listing_type, + default_sort_type: site_sort_type, legal_information: None, application_email_admins: None, hide_modlog_mod_names: None, diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index ba716c9a8..5a0e424e9 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -91,6 +91,7 @@ pub async fn update_site( private_instance: data.private_instance, default_theme: data.default_theme.clone(), default_post_listing_type: data.default_post_listing_type, + default_sort_type: data.default_sort_type, legal_information: diesel_option_overwrite(data.legal_information.clone()), application_email_admins: data.application_email_admins, hide_modlog_mod_names: data.hide_modlog_mod_names, @@ -225,7 +226,7 @@ mod tests { use crate::site::update::validate_update_payload; use lemmy_api_common::site::EditSite; - use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode}; + use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode, SortType}; use lemmy_utils::error::LemmyErrorType; #[test] @@ -246,6 +247,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, None::, @@ -268,6 +270,7 @@ mod tests { None::, None::, None::, + None::, Some(String::from("(zeta|alpha)")), None::, None::, @@ -290,6 +293,7 @@ mod tests { None::, None::, Some(ListingType::Subscribed), + None::, None::, None::, None::, @@ -312,6 +316,7 @@ mod tests { None::, None::, None::, + None::, None::, Some(true), Some(true), @@ -334,6 +339,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, Some(true), @@ -356,6 +362,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, None::, @@ -409,6 +416,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, None::, @@ -430,6 +438,7 @@ mod tests { Some(String::new()), Some(String::new()), Some(ListingType::All), + Some(SortType::Active), Some(String::new()), Some(false), Some(true), @@ -451,6 +460,7 @@ mod tests { None::, None::, None::, + None::, Some(String::new()), None::, None::, @@ -472,6 +482,7 @@ mod tests { None::, None::, None::, + None::, None::, None::, None::, @@ -519,6 +530,7 @@ mod tests { site_description: Option, site_sidebar: Option, site_listing_type: Option, + site_sort_type: Option, site_slur_filter_regex: Option, site_is_private: Option, site_is_federated: Option, @@ -539,6 +551,7 @@ mod tests { private_instance: site_is_private, default_theme: None, default_post_listing_type: site_listing_type, + default_sort_type: site_sort_type, legal_information: None, application_email_admins: None, hide_modlog_mod_names: None, diff --git a/crates/apub/src/api/list_comments.rs b/crates/apub/src/api/list_comments.rs index 7d1de019e..7d0327465 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -50,7 +50,7 @@ pub async fn list_comments( data.type_, &local_site, community_id, - )?); + )); // If a parent_id is given, fetch the comment to get the path let parent_path = if let Some(parent_id) = parent_id { diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index dc3618c50..633f5b48c 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -1,5 +1,5 @@ use crate::{ - api::listing_type_with_default, + api::{listing_type_with_default, sort_type_with_default}, fetcher::resolve_actor_identifier, objects::community::ApubCommunity, }; @@ -27,8 +27,6 @@ pub async fn list_posts( check_private_instance(&local_user_view, &local_site)?; - let sort = data.sort; - let page = data.page; let limit = data.limit; let community_id = if let Some(name) = &data.community_name { @@ -49,7 +47,10 @@ pub async fn list_posts( data.type_, &local_site, community_id, - )?); + )); + + let sort = Some(sort_type_with_default(data.sort, &local_site)); + // parse pagination token let page_after = if let Some(pa) = &data.page_cursor { Some(pa.read(&mut context.pool()).await?) diff --git a/crates/apub/src/api/mod.rs b/crates/apub/src/api/mod.rs index 59586e477..c5bc3ebaf 100644 --- a/crates/apub/src/api/mod.rs +++ b/crates/apub/src/api/mod.rs @@ -1,5 +1,9 @@ -use lemmy_db_schema::{newtypes::CommunityId, source::local_site::LocalSite, ListingType}; -use lemmy_utils::error::LemmyError; +use lemmy_db_schema::{ + newtypes::CommunityId, + source::local_site::LocalSite, + ListingType, + SortType, +}; pub mod list_comments; pub mod list_posts; @@ -14,13 +18,17 @@ fn listing_type_with_default( type_: Option, local_site: &LocalSite, community_id: Option, -) -> Result { +) -> ListingType { // On frontpage use listing type from param or admin configured default - let listing_type = if community_id.is_none() { + if community_id.is_none() { type_.unwrap_or(local_site.default_post_listing_type) } else { // inside of community show everything ListingType::All - }; - Ok(listing_type) + } +} + +/// Returns default listing type, depending if the query is for frontpage or community. +fn sort_type_with_default(type_: Option, local_site: &LocalSite) -> SortType { + type_.unwrap_or(local_site.default_sort_type) } diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 195e99660..be0a688ac 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -353,6 +353,7 @@ diesel::table! { use diesel::sql_types::*; use super::sql_types::ListingTypeEnum; use super::sql_types::RegistrationModeEnum; + use super::sql_types::SortTypeEnum; local_site (id) { id -> Int4, @@ -380,6 +381,7 @@ diesel::table! { registration_mode -> RegistrationModeEnum, reports_email_admins -> Bool, federation_signed_fetch -> Bool, + default_sort_type -> SortTypeEnum, } } diff --git a/crates/db_schema/src/source/local_site.rs b/crates/db_schema/src/source/local_site.rs index ac545d574..8937de850 100644 --- a/crates/db_schema/src/source/local_site.rs +++ b/crates/db_schema/src/source/local_site.rs @@ -4,6 +4,7 @@ use crate::{ newtypes::{LocalSiteId, SiteId}, ListingType, RegistrationMode, + SortType, }; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; @@ -64,6 +65,7 @@ pub struct LocalSite { /// Whether to sign outgoing Activitypub fetches with private key of local instance. Some /// Fediverse instances and platforms require this. pub federation_signed_fetch: bool, + pub default_sort_type: SortType, } #[derive(Clone, TypedBuilder)] @@ -93,6 +95,7 @@ pub struct LocalSiteInsertForm { pub registration_mode: Option, pub reports_email_admins: Option, pub federation_signed_fetch: Option, + pub default_sort_type: Option, } #[derive(Clone, Default)] @@ -120,4 +123,5 @@ pub struct LocalSiteUpdateForm { pub reports_email_admins: Option, pub updated: Option>>, pub federation_signed_fetch: Option, + pub default_sort_type: Option, } diff --git a/migrations/2024-02-15-171358_default_instance_sort_type/down.sql b/migrations/2024-02-15-171358_default_instance_sort_type/down.sql new file mode 100644 index 000000000..8ba51592b --- /dev/null +++ b/migrations/2024-02-15-171358_default_instance_sort_type/down.sql @@ -0,0 +1,3 @@ +ALTER TABLE local_site + DROP COLUMN default_sort_type; + diff --git a/migrations/2024-02-15-171358_default_instance_sort_type/up.sql b/migrations/2024-02-15-171358_default_instance_sort_type/up.sql new file mode 100644 index 000000000..f2bcddcff --- /dev/null +++ b/migrations/2024-02-15-171358_default_instance_sort_type/up.sql @@ -0,0 +1,3 @@ +ALTER TABLE local_site + ADD COLUMN default_sort_type sort_type_enum DEFAULT 'Active' NOT NULL; +