Adding an instance-level default_sort_type (#4454)

* Adding an instance-level default_sort_type

- Fixes #3796

* Fixing comment.

* Put user sort before site sort.
This commit is contained in:
Dessalines 2024-02-16 09:36:46 -05:00 committed by GitHub
parent ffcf415cac
commit 5d551e6da5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 88 additions and 15 deletions

View file

@ -162,6 +162,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>,
@ -221,6 +222,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

@ -93,6 +93,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,
@ -192,7 +193,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]
@ -214,6 +215,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>,
@ -237,6 +239,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>,
@ -260,6 +263,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>,
@ -283,6 +287,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>,
@ -306,6 +311,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),
@ -329,6 +335,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),
@ -352,6 +359,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 +417,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>,
@ -431,6 +440,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),
@ -453,6 +463,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>,
@ -475,6 +486,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>,
@ -524,6 +536,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>,
@ -544,6 +557,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

@ -92,6 +92,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,
@ -227,7 +228,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]
@ -248,6 +249,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>,
@ -270,6 +272,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>,
@ -292,6 +295,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>,
@ -314,6 +318,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),
@ -336,6 +341,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),
@ -358,6 +364,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>,
@ -411,6 +418,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>,
@ -432,6 +440,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),
@ -453,6 +462,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>,
@ -474,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>,
@ -521,6 +532,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>,
@ -541,6 +553,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

@ -48,9 +48,10 @@ pub async fn list_comments(
let listing_type = Some(listing_type_with_default( let listing_type = Some(listing_type_with_default(
data.type_, data.type_,
local_user_view.as_ref().map(|u| &u.local_user),
&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.local_site)?; check_private_instance(&local_user_view, &local_site.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 {
@ -45,11 +43,20 @@ pub async fn list_posts(
return Err(LemmyError::from(LemmyErrorType::ContradictingFilters)); return Err(LemmyError::from(LemmyErrorType::ContradictingFilters));
} }
let local_user_ref = local_user_view.as_ref().map(|u| &u.local_user);
let listing_type = Some(listing_type_with_default( let listing_type = Some(listing_type_with_default(
data.type_, data.type_,
local_user_ref,
&local_site.local_site, &local_site.local_site,
community_id, community_id,
)?); ));
let sort = Some(sort_type_with_default(
data.sort,
local_user_ref,
&local_site.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, local_user::LocalUser},
ListingType,
SortType,
};
pub mod list_comments; pub mod list_comments;
pub mod list_posts; pub mod list_posts;
@ -12,15 +16,33 @@ pub mod user_settings_backup;
/// Returns default listing type, depending if the query is for frontpage or community. /// Returns default listing type, depending if the query is for frontpage or community.
fn listing_type_with_default( fn listing_type_with_default(
type_: Option<ListingType>, type_: Option<ListingType>,
local_user: Option<&LocalUser>,
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_user
.map(|u| u.default_listing_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 a default instance-level sort type, if none is given by the user.
/// Order is type, local user default, then site default.
fn sort_type_with_default(
type_: Option<SortType>,
local_user: Option<&LocalUser>,
local_site: &LocalSite,
) -> SortType {
type_.unwrap_or(
local_user
.map(|u| u.default_sort_type)
.unwrap_or(local_site.default_sort_type),
)
} }

View file

@ -354,6 +354,7 @@ diesel::table! {
use super::sql_types::ListingTypeEnum; use super::sql_types::ListingTypeEnum;
use super::sql_types::RegistrationModeEnum; use super::sql_types::RegistrationModeEnum;
use super::sql_types::PostListingModeEnum; use super::sql_types::PostListingModeEnum;
use super::sql_types::SortTypeEnum;
local_site (id) { local_site (id) {
id -> Int4, id -> Int4,
@ -382,6 +383,7 @@ diesel::table! {
reports_email_admins -> Bool, reports_email_admins -> Bool,
federation_signed_fetch -> Bool, federation_signed_fetch -> Bool,
default_post_listing_mode -> PostListingModeEnum, default_post_listing_mode -> PostListingModeEnum,
default_sort_type -> SortTypeEnum,
} }
} }

View file

@ -5,6 +5,7 @@ use crate::{
ListingType, ListingType,
PostListingMode, PostListingMode,
RegistrationMode, RegistrationMode,
SortType,
}; };
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -65,8 +66,10 @@ 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,
/// Default value for [LocalUser.post_listing_mode] /// Default value for [LocalSite.post_listing_mode]
pub default_post_listing_mode: PostListingMode, pub default_post_listing_mode: PostListingMode,
/// Default value for [LocalUser.post_listing_mode]
pub default_sort_type: SortType,
} }
#[derive(Clone, TypedBuilder)] #[derive(Clone, TypedBuilder)]
@ -97,6 +100,7 @@ pub struct LocalSiteInsertForm {
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_post_listing_mode: Option<PostListingMode>, pub default_post_listing_mode: Option<PostListingMode>,
pub default_sort_type: Option<SortType>,
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
@ -125,4 +129,5 @@ pub struct LocalSiteUpdateForm {
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_post_listing_mode: Option<PostListingMode>, pub default_post_listing_mode: Option<PostListingMode>,
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;