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 default_theme: Option<String>,
pub default_post_listing_type: Option<ListingType>,
pub default_sort_type: Option<SortType>,
pub legal_information: Option<String>,
pub application_email_admins: Option<bool>,
pub hide_modlog_mod_names: Option<bool>,
@ -221,6 +222,8 @@ pub struct EditSite {
/// The default theme. Usually "browser"
pub default_theme: Option<String>,
pub default_post_listing_type: Option<ListingType>,
/// The default sort, usually "active"
pub default_sort_type: Option<SortType>,
/// An optional page of legal information
pub legal_information: Option<String>,
/// 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,
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,
@ -192,7 +193,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]
@ -214,6 +215,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -237,6 +239,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -260,6 +263,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
Some(String::from("(zeta|alpha)")),
None::<bool>,
None::<bool>,
@ -283,6 +287,7 @@ mod tests {
None::<String>,
None::<String>,
Some(ListingType::Subscribed),
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -306,6 +311,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
Some(true),
Some(true),
@ -329,6 +335,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
Some(true),
@ -352,6 +359,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -409,6 +417,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -431,6 +440,7 @@ mod tests {
Some(String::new()),
Some(String::new()),
Some(ListingType::All),
Some(SortType::Active),
Some(String::new()),
Some(false),
Some(true),
@ -453,6 +463,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
Some(String::new()),
None::<bool>,
None::<bool>,
@ -475,6 +486,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -524,6 +536,7 @@ mod tests {
site_description: Option<String>,
site_sidebar: Option<String>,
site_listing_type: Option<ListingType>,
site_sort_type: Option<SortType>,
site_slur_filter_regex: Option<String>,
site_is_private: Option<bool>,
site_is_federated: Option<bool>,
@ -544,6 +557,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,

View file

@ -92,6 +92,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,
@ -227,7 +228,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]
@ -248,6 +249,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -270,6 +272,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
Some(String::from("(zeta|alpha)")),
None::<bool>,
None::<bool>,
@ -292,6 +295,7 @@ mod tests {
None::<String>,
None::<String>,
Some(ListingType::Subscribed),
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -314,6 +318,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
Some(true),
Some(true),
@ -336,6 +341,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
Some(true),
@ -358,6 +364,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -411,6 +418,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -432,6 +440,7 @@ mod tests {
Some(String::new()),
Some(String::new()),
Some(ListingType::All),
Some(SortType::Active),
Some(String::new()),
Some(false),
Some(true),
@ -453,6 +462,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
Some(String::new()),
None::<bool>,
None::<bool>,
@ -474,6 +484,7 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -521,6 +532,7 @@ mod tests {
site_description: Option<String>,
site_sidebar: Option<String>,
site_listing_type: Option<ListingType>,
site_sort_type: Option<SortType>,
site_slur_filter_regex: Option<String>,
site_is_private: Option<bool>,
site_is_federated: Option<bool>,
@ -541,6 +553,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,

View file

@ -48,9 +48,10 @@ pub async fn list_comments(
let listing_type = Some(listing_type_with_default(
data.type_,
local_user_view.as_ref().map(|u| &u.local_user),
&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 {

View file

@ -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.local_site)?;
let sort = data.sort;
let page = data.page;
let limit = data.limit;
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));
}
let local_user_ref = local_user_view.as_ref().map(|u| &u.local_user);
let listing_type = Some(listing_type_with_default(
data.type_,
local_user_ref,
&local_site.local_site,
community_id,
)?);
));
let sort = Some(sort_type_with_default(
data.sort,
local_user_ref,
&local_site.local_site,
));
// parse pagination token
let page_after = if let Some(pa) = &data.page_cursor {
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_utils::error::LemmyError;
use lemmy_db_schema::{
newtypes::CommunityId,
source::{local_site::LocalSite, local_user::LocalUser},
ListingType,
SortType,
};
pub mod list_comments;
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.
fn listing_type_with_default(
type_: Option<ListingType>,
local_user: Option<&LocalUser>,
local_site: &LocalSite,
community_id: Option<CommunityId>,
) -> Result<ListingType, LemmyError> {
) -> ListingType {
// On frontpage use listing type from param or admin configured default
let listing_type = if community_id.is_none() {
type_.unwrap_or(local_site.default_post_listing_type)
if community_id.is_none() {
type_.unwrap_or(
local_user
.map(|u| u.default_listing_type)
.unwrap_or(local_site.default_post_listing_type),
)
} else {
// inside of community show everything
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::RegistrationModeEnum;
use super::sql_types::PostListingModeEnum;
use super::sql_types::SortTypeEnum;
local_site (id) {
id -> Int4,
@ -382,6 +383,7 @@ diesel::table! {
reports_email_admins -> Bool,
federation_signed_fetch -> Bool,
default_post_listing_mode -> PostListingModeEnum,
default_sort_type -> SortTypeEnum,
}
}

View file

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