Adding an instance-level default_sort_type

- Fixes #3796
default_instance_sort_type
Dessalines 2024-02-15 13:15:24 -05:00
parent a3bf2f1cf1
commit 533a67dd3a
10 changed files with 64 additions and 13 deletions

View File

@ -161,6 +161,7 @@ pub struct CreateSite {
pub private_instance: Option<bool>, pub private_instance: Option<bool>,
pub default_theme: Option<String>, pub default_theme: Option<String>,
pub default_post_listing_type: Option<ListingType>, pub default_post_listing_type: Option<ListingType>,
pub default_sort_type: Option<SortType>,
pub legal_information: Option<String>, pub legal_information: Option<String>,
pub application_email_admins: Option<bool>, pub application_email_admins: Option<bool>,
pub hide_modlog_mod_names: Option<bool>, pub hide_modlog_mod_names: Option<bool>,
@ -218,6 +219,8 @@ pub struct EditSite {
/// The default theme. Usually "browser" /// The default theme. Usually "browser"
pub default_theme: Option<String>, pub default_theme: Option<String>,
pub default_post_listing_type: Option<ListingType>, pub default_post_listing_type: Option<ListingType>,
/// The default sort, usually "active"
pub default_sort_type: Option<SortType>,
/// An optional page of legal information /// An optional page of legal information
pub legal_information: Option<String>, pub legal_information: Option<String>,
/// Whether to email admins when receiving a new application. /// Whether to email admins when receiving a new application.

View File

@ -92,6 +92,7 @@ pub async fn create_site(
private_instance: data.private_instance, private_instance: data.private_instance,
default_theme: data.default_theme.clone(), default_theme: data.default_theme.clone(),
default_post_listing_type: data.default_post_listing_type, 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()), legal_information: diesel_option_overwrite(data.legal_information.clone()),
application_email_admins: data.application_email_admins, application_email_admins: data.application_email_admins,
hide_modlog_mod_names: data.hide_modlog_mod_names, hide_modlog_mod_names: data.hide_modlog_mod_names,
@ -190,7 +191,7 @@ mod tests {
use crate::site::create::validate_create_payload; use crate::site::create::validate_create_payload;
use lemmy_api_common::site::CreateSite; 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; use lemmy_utils::error::LemmyErrorType;
#[test] #[test]
@ -212,6 +213,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -235,6 +237,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -258,6 +261,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
Some(String::from("(zeta|alpha)")), Some(String::from("(zeta|alpha)")),
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -281,6 +285,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
Some(ListingType::Subscribed), Some(ListingType::Subscribed),
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -304,6 +309,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
Some(true), Some(true),
Some(true), Some(true),
@ -327,6 +333,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
Some(true), Some(true),
@ -350,6 +357,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -407,6 +415,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -429,6 +438,7 @@ mod tests {
Some(String::new()), Some(String::new()),
Some(String::new()), Some(String::new()),
Some(ListingType::All), Some(ListingType::All),
Some(SortType::Active),
Some(String::new()), Some(String::new()),
Some(false), Some(false),
Some(true), Some(true),
@ -451,6 +461,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
Some(String::new()), Some(String::new()),
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -473,6 +484,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -522,6 +534,7 @@ mod tests {
site_description: Option<String>, site_description: Option<String>,
site_sidebar: Option<String>, site_sidebar: Option<String>,
site_listing_type: Option<ListingType>, site_listing_type: Option<ListingType>,
site_sort_type: Option<SortType>,
site_slur_filter_regex: Option<String>, site_slur_filter_regex: Option<String>,
site_is_private: Option<bool>, site_is_private: Option<bool>,
site_is_federated: Option<bool>, site_is_federated: Option<bool>,
@ -542,6 +555,7 @@ mod tests {
private_instance: site_is_private, private_instance: site_is_private,
default_theme: None, default_theme: None,
default_post_listing_type: site_listing_type, default_post_listing_type: site_listing_type,
default_sort_type: site_sort_type,
legal_information: None, legal_information: None,
application_email_admins: None, application_email_admins: None,
hide_modlog_mod_names: None, hide_modlog_mod_names: None,

View File

@ -91,6 +91,7 @@ pub async fn update_site(
private_instance: data.private_instance, private_instance: data.private_instance,
default_theme: data.default_theme.clone(), default_theme: data.default_theme.clone(),
default_post_listing_type: data.default_post_listing_type, 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()), legal_information: diesel_option_overwrite(data.legal_information.clone()),
application_email_admins: data.application_email_admins, application_email_admins: data.application_email_admins,
hide_modlog_mod_names: data.hide_modlog_mod_names, hide_modlog_mod_names: data.hide_modlog_mod_names,
@ -225,7 +226,7 @@ mod tests {
use crate::site::update::validate_update_payload; use crate::site::update::validate_update_payload;
use lemmy_api_common::site::EditSite; 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; use lemmy_utils::error::LemmyErrorType;
#[test] #[test]
@ -246,6 +247,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -268,6 +270,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
Some(String::from("(zeta|alpha)")), Some(String::from("(zeta|alpha)")),
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -290,6 +293,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
Some(ListingType::Subscribed), Some(ListingType::Subscribed),
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -312,6 +316,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
Some(true), Some(true),
Some(true), Some(true),
@ -334,6 +339,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
Some(true), Some(true),
@ -356,6 +362,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -409,6 +416,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -430,6 +438,7 @@ mod tests {
Some(String::new()), Some(String::new()),
Some(String::new()), Some(String::new()),
Some(ListingType::All), Some(ListingType::All),
Some(SortType::Active),
Some(String::new()), Some(String::new()),
Some(false), Some(false),
Some(true), Some(true),
@ -451,6 +460,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
Some(String::new()), Some(String::new()),
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -472,6 +482,7 @@ mod tests {
None::<String>, None::<String>,
None::<String>, None::<String>,
None::<ListingType>, None::<ListingType>,
None::<SortType>,
None::<String>, None::<String>,
None::<bool>, None::<bool>,
None::<bool>, None::<bool>,
@ -519,6 +530,7 @@ mod tests {
site_description: Option<String>, site_description: Option<String>,
site_sidebar: Option<String>, site_sidebar: Option<String>,
site_listing_type: Option<ListingType>, site_listing_type: Option<ListingType>,
site_sort_type: Option<SortType>,
site_slur_filter_regex: Option<String>, site_slur_filter_regex: Option<String>,
site_is_private: Option<bool>, site_is_private: Option<bool>,
site_is_federated: Option<bool>, site_is_federated: Option<bool>,
@ -539,6 +551,7 @@ mod tests {
private_instance: site_is_private, private_instance: site_is_private,
default_theme: None, default_theme: None,
default_post_listing_type: site_listing_type, default_post_listing_type: site_listing_type,
default_sort_type: site_sort_type,
legal_information: None, legal_information: None,
application_email_admins: None, application_email_admins: None,
hide_modlog_mod_names: None, hide_modlog_mod_names: None,

View File

@ -50,7 +50,7 @@ pub async fn list_comments(
data.type_, data.type_,
&local_site, &local_site,
community_id, community_id,
)?); ));
// If a parent_id is given, fetch the comment to get the path // If a parent_id is given, fetch the comment to get the path
let parent_path = if let Some(parent_id) = parent_id { let parent_path = if let Some(parent_id) = parent_id {

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
api::listing_type_with_default, api::{listing_type_with_default, sort_type_with_default},
fetcher::resolve_actor_identifier, fetcher::resolve_actor_identifier,
objects::community::ApubCommunity, objects::community::ApubCommunity,
}; };
@ -27,8 +27,6 @@ pub async fn list_posts(
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;
let sort = data.sort;
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;
let community_id = if let Some(name) = &data.community_name { let community_id = if let Some(name) = &data.community_name {
@ -49,7 +47,10 @@ pub async fn list_posts(
data.type_, data.type_,
&local_site, &local_site,
community_id, community_id,
)?); ));
let sort = Some(sort_type_with_default(data.sort, &local_site));
// parse pagination token // parse pagination token
let page_after = if let Some(pa) = &data.page_cursor { let page_after = if let Some(pa) = &data.page_cursor {
Some(pa.read(&mut context.pool()).await?) Some(pa.read(&mut context.pool()).await?)

View File

@ -1,5 +1,9 @@
use lemmy_db_schema::{newtypes::CommunityId, source::local_site::LocalSite, ListingType}; use lemmy_db_schema::{
use lemmy_utils::error::LemmyError; newtypes::CommunityId,
source::local_site::LocalSite,
ListingType,
SortType,
};
pub mod list_comments; pub mod list_comments;
pub mod list_posts; pub mod list_posts;
@ -14,13 +18,17 @@ fn listing_type_with_default(
type_: Option<ListingType>, type_: Option<ListingType>,
local_site: &LocalSite, local_site: &LocalSite,
community_id: Option<CommunityId>, community_id: Option<CommunityId>,
) -> Result<ListingType, LemmyError> { ) -> ListingType {
// On frontpage use listing type from param or admin configured default // 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) type_.unwrap_or(local_site.default_post_listing_type)
} else { } else {
// inside of community show everything // inside of community show everything
ListingType::All 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<SortType>, local_site: &LocalSite) -> SortType {
type_.unwrap_or(local_site.default_sort_type)
} }

View File

@ -353,6 +353,7 @@ diesel::table! {
use diesel::sql_types::*; use diesel::sql_types::*;
use super::sql_types::ListingTypeEnum; use super::sql_types::ListingTypeEnum;
use super::sql_types::RegistrationModeEnum; use super::sql_types::RegistrationModeEnum;
use super::sql_types::SortTypeEnum;
local_site (id) { local_site (id) {
id -> Int4, id -> Int4,
@ -380,6 +381,7 @@ diesel::table! {
registration_mode -> RegistrationModeEnum, registration_mode -> RegistrationModeEnum,
reports_email_admins -> Bool, reports_email_admins -> Bool,
federation_signed_fetch -> Bool, federation_signed_fetch -> Bool,
default_sort_type -> SortTypeEnum,
} }
} }

View File

@ -4,6 +4,7 @@ use crate::{
newtypes::{LocalSiteId, SiteId}, newtypes::{LocalSiteId, SiteId},
ListingType, ListingType,
RegistrationMode, RegistrationMode,
SortType,
}; };
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -64,6 +65,7 @@ pub struct LocalSite {
/// Whether to sign outgoing Activitypub fetches with private key of local instance. Some /// Whether to sign outgoing Activitypub fetches with private key of local instance. Some
/// Fediverse instances and platforms require this. /// Fediverse instances and platforms require this.
pub federation_signed_fetch: bool, pub federation_signed_fetch: bool,
pub default_sort_type: SortType,
} }
#[derive(Clone, TypedBuilder)] #[derive(Clone, TypedBuilder)]
@ -93,6 +95,7 @@ pub struct LocalSiteInsertForm {
pub registration_mode: Option<RegistrationMode>, pub registration_mode: Option<RegistrationMode>,
pub reports_email_admins: Option<bool>, pub reports_email_admins: Option<bool>,
pub federation_signed_fetch: Option<bool>, pub federation_signed_fetch: Option<bool>,
pub default_sort_type: Option<SortType>,
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
@ -120,4 +123,5 @@ pub struct LocalSiteUpdateForm {
pub reports_email_admins: Option<bool>, pub reports_email_admins: Option<bool>,
pub updated: Option<Option<DateTime<Utc>>>, pub updated: Option<Option<DateTime<Utc>>>,
pub federation_signed_fetch: Option<bool>, pub federation_signed_fetch: Option<bool>,
pub default_sort_type: Option<SortType>,
} }

View File

@ -0,0 +1,3 @@
ALTER TABLE local_site
DROP COLUMN default_sort_type;

View File

@ -0,0 +1,3 @@
ALTER TABLE local_site
ADD COLUMN default_sort_type sort_type_enum DEFAULT 'Active' NOT NULL;