Adding a default_comment_sort_type column for local_site and local_user.

- Renamed SortType to PostSortType in the DB and code.
- Renamed references to default_sort_type to default_post_sort_type.
- Fixes #4128
default_comment_sort
Dessalines 3 months ago
parent f56b84615c
commit be89c12f40

@ -85,7 +85,8 @@ pub async fn save_user_settings(
let local_user_id = local_user_view.local_user.id;
let person_id = local_user_view.person.id;
let default_listing_type = data.default_listing_type;
let default_sort_type = data.default_sort_type;
let default_post_sort_type = data.default_post_sort_type;
let default_comment_sort_type = data.default_comment_sort_type;
let person_form = PersonUpdateForm {
display_name,
@ -117,7 +118,8 @@ pub async fn save_user_settings(
auto_expand: data.auto_expand,
show_bot_accounts: data.show_bot_accounts,
show_scores: data.show_scores,
default_sort_type,
default_post_sort_type,
default_comment_sort_type,
default_listing_type,
theme: data.theme.clone(),
interface_language: data.interface_language.clone(),

@ -3,7 +3,7 @@ use lemmy_db_schema::{
source::site::Site,
CommunityVisibility,
ListingType,
SortType,
PostSortType,
};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonView};
use serde::{Deserialize, Serialize};
@ -74,7 +74,7 @@ pub struct CommunityResponse {
/// Fetches a list of communities.
pub struct ListCommunities {
pub type_: Option<ListingType>,
pub sort: Option<SortType>,
pub sort: Option<PostSortType>,
pub show_nsfw: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,

@ -5,7 +5,7 @@ use lemmy_db_schema::{
CommentSortType,
ListingType,
PostListingMode,
SortType,
PostSortType,
};
use lemmy_db_views::structs::{CommentView, PostView};
use lemmy_db_views_actor::structs::{
@ -90,8 +90,14 @@ pub struct SaveUserSettings {
pub show_scores: Option<bool>,
/// Your user's theme.
pub theme: Option<String>,
pub default_sort_type: Option<SortType>,
/// The default post listing type, usually "local"
pub default_listing_type: Option<ListingType>,
/// Default value for listing mode, usually "list"
pub post_listing_mode: Option<PostListingMode>,
/// The default post sort, usually "active"
pub default_post_sort_type: Option<PostSortType>,
/// The default comment sort, usually "hot"
pub default_comment_sort_type: Option<CommentSortType>,
/// The language of the lemmy interface
pub interface_language: Option<String>,
/// A URL for your avatar.
@ -122,7 +128,6 @@ pub struct SaveUserSettings {
pub open_links_in_new_tab: Option<bool>,
/// Enable infinite scroll
pub infinite_scroll_enabled: Option<bool>,
pub post_listing_mode: Option<PostListingMode>,
/// Whether to allow keyboard navigation (for browsing and interacting with posts and comments).
pub enable_keyboard_navigation: Option<bool>,
/// Whether user avatars or inline images in the UI that are gifs should be allowed to play or should be paused
@ -166,7 +171,7 @@ pub struct GetPersonDetails {
pub person_id: Option<PersonId>,
/// Example: dessalines , or dessalines@xyz.tld
pub username: Option<String>,
pub sort: Option<SortType>,
pub sort: Option<PostSortType>,
pub page: Option<i64>,
pub limit: Option<i64>,
pub community_id: Option<CommunityId>,

@ -2,7 +2,7 @@ use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, DbUrl, LanguageId, PostId, PostReportId},
ListingType,
PostFeatureType,
SortType,
PostSortType,
};
use lemmy_db_views::structs::{PaginationCursor, PostReportView, PostView, VoteView};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
@ -70,7 +70,7 @@ pub struct GetPostResponse {
/// Get a list of posts.
pub struct GetPosts {
pub type_: Option<ListingType>,
pub sort: Option<SortType>,
pub sort: Option<PostSortType>,
/// DEPRECATED, use page_cursor
pub page: Option<i64>,
pub limit: Option<i64>,

@ -8,12 +8,13 @@ use lemmy_db_schema::{
language::Language,
tagline::Tagline,
},
CommentSortType,
ListingType,
ModlogActionType,
PostListingMode,
PostSortType,
RegistrationMode,
SearchType,
SortType,
};
use lemmy_db_views::structs::{
CommentView,
@ -65,7 +66,7 @@ pub struct Search {
pub community_name: Option<String>,
pub creator_id: Option<PersonId>,
pub type_: Option<SearchType>,
pub sort: Option<SortType>,
pub sort: Option<PostSortType>,
pub listing_type: Option<ListingType>,
pub page: Option<i64>,
pub limit: Option<i64>,
@ -162,7 +163,9 @@ 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 default_post_listing_mode: Option<PostListingMode>,
pub default_post_sort_type: Option<PostSortType>,
pub default_comment_sort_type: Option<CommentSortType>,
pub legal_information: Option<String>,
pub application_email_admins: Option<bool>,
pub hide_modlog_mod_names: Option<bool>,
@ -190,7 +193,6 @@ pub struct CreateSite {
pub taglines: Option<Vec<String>>,
pub registration_mode: Option<RegistrationMode>,
pub content_warning: Option<String>,
pub default_post_listing_mode: Option<PostListingMode>,
}
#[skip_serializing_none]
@ -221,9 +223,14 @@ pub struct EditSite {
pub private_instance: Option<bool>,
/// The default theme. Usually "browser"
pub default_theme: Option<String>,
/// The default post listing type, usually "local"
pub default_post_listing_type: Option<ListingType>,
/// The default sort, usually "active"
pub default_sort_type: Option<SortType>,
/// Default value for listing mode, usually "list"
pub default_post_listing_mode: Option<PostListingMode>,
/// The default post sort, usually "active"
pub default_post_sort_type: Option<PostSortType>,
/// The default comment sort, usually "hot"
pub default_comment_sort_type: Option<CommentSortType>,
/// An optional page of legal information
pub legal_information: Option<String>,
/// Whether to email admins when receiving a new application.
@ -274,8 +281,6 @@ pub struct EditSite {
/// If present, nsfw content is visible by default. Should be displayed by frontends/clients
/// when the site is first opened by a user.
pub content_warning: Option<String>,
/// Default value for [LocalUser.post_listing_mode]
pub default_post_listing_mode: Option<PostListingMode>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]

@ -93,7 +93,8 @@ 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,
default_post_sort_type: data.default_post_sort_type,
default_comment_sort_type: data.default_comment_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,
@ -193,7 +194,13 @@ 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, SortType};
use lemmy_db_schema::{
source::local_site::LocalSite,
CommentSortType,
ListingType,
PostSortType,
RegistrationMode,
};
use lemmy_utils::error::LemmyErrorType;
#[test]
@ -215,7 +222,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -239,7 +247,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -263,7 +272,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
Some(String::from("(zeta|alpha)")),
None::<bool>,
None::<bool>,
@ -287,7 +297,8 @@ mod tests {
None::<String>,
None::<String>,
Some(ListingType::Subscribed),
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -311,7 +322,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
Some(true),
Some(true),
@ -335,7 +347,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
Some(true),
@ -359,7 +372,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -417,7 +431,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -440,7 +455,8 @@ mod tests {
Some(String::new()),
Some(String::new()),
Some(ListingType::All),
Some(SortType::Active),
Some(PostSortType::Active),
Some(CommentSortType::Hot),
Some(String::new()),
Some(false),
Some(true),
@ -463,7 +479,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
Some(String::new()),
None::<bool>,
None::<bool>,
@ -486,7 +503,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -536,7 +554,8 @@ mod tests {
site_description: Option<String>,
site_sidebar: Option<String>,
site_listing_type: Option<ListingType>,
site_sort_type: Option<SortType>,
site_post_sort_type: Option<PostSortType>,
site_comment_sort_type: Option<CommentSortType>,
site_slur_filter_regex: Option<String>,
site_is_private: Option<bool>,
site_is_federated: Option<bool>,
@ -557,7 +576,8 @@ mod tests {
private_instance: site_is_private,
default_theme: None,
default_post_listing_type: site_listing_type,
default_sort_type: site_sort_type,
default_post_sort_type: site_post_sort_type,
default_comment_sort_type: site_comment_sort_type,
legal_information: None,
application_email_admins: None,
hide_modlog_mod_names: None,

@ -92,7 +92,8 @@ 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,
default_post_sort_type: data.default_post_sort_type,
default_comment_sort_type: data.default_comment_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,
@ -228,7 +229,13 @@ 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, SortType};
use lemmy_db_schema::{
source::local_site::LocalSite,
CommentSortType,
ListingType,
PostSortType,
RegistrationMode,
};
use lemmy_utils::error::LemmyErrorType;
#[test]
@ -249,7 +256,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -272,7 +280,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
Some(String::from("(zeta|alpha)")),
None::<bool>,
None::<bool>,
@ -295,7 +304,8 @@ mod tests {
None::<String>,
None::<String>,
Some(ListingType::Subscribed),
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -318,7 +328,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
Some(true),
Some(true),
@ -341,7 +352,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
Some(true),
@ -364,7 +376,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -418,7 +431,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -440,7 +454,8 @@ mod tests {
Some(String::new()),
Some(String::new()),
Some(ListingType::All),
Some(SortType::Active),
Some(PostSortType::Active),
Some(CommentSortType::Hot),
Some(String::new()),
Some(false),
Some(true),
@ -462,7 +477,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
Some(String::new()),
None::<bool>,
None::<bool>,
@ -484,7 +500,8 @@ mod tests {
None::<String>,
None::<String>,
None::<ListingType>,
None::<SortType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
@ -532,7 +549,8 @@ mod tests {
site_description: Option<String>,
site_sidebar: Option<String>,
site_listing_type: Option<ListingType>,
site_sort_type: Option<SortType>,
site_post_sort_type: Option<PostSortType>,
site_comment_sort_type: Option<CommentSortType>,
site_slur_filter_regex: Option<String>,
site_is_private: Option<bool>,
site_is_federated: Option<bool>,
@ -553,7 +571,8 @@ mod tests {
private_instance: site_is_private,
default_theme: None,
default_post_listing_type: site_listing_type,
default_sort_type: site_sort_type,
default_post_sort_type: site_post_sort_type,
default_comment_sort_type: site_comment_sort_type,
legal_information: None,
application_email_admins: None,
hide_modlog_mod_names: None,

@ -1,3 +1,4 @@
use super::comment_sort_type_with_default;
use crate::{
api::listing_type_with_default,
fetcher::resolve_actor_identifier,
@ -32,7 +33,12 @@ pub async fn list_comments(
} else {
data.community_id
};
let sort = data.sort;
let local_user_ref = local_user_view.as_ref().map(|u| &u.local_user);
let sort = Some(comment_sort_type_with_default(
data.sort,
local_user_ref,
&local_site,
));
let max_depth = data.max_depth;
let saved_only = data.saved_only.unwrap_or_default();

@ -1,5 +1,5 @@
use crate::{
api::{listing_type_with_default, sort_type_with_default},
api::{listing_type_with_default, post_sort_type_with_default},
fetcher::resolve_actor_identifier,
objects::community::ApubCommunity,
};
@ -51,7 +51,7 @@ pub async fn list_posts(
community_id,
));
let sort = Some(sort_type_with_default(
let sort = Some(post_sort_type_with_default(
data.sort,
local_user_ref,
&local_site.local_site,

@ -1,8 +1,9 @@
use lemmy_db_schema::{
newtypes::CommunityId,
source::{local_site::LocalSite, local_user::LocalUser},
CommentSortType,
ListingType,
SortType,
PostSortType,
};
pub mod list_comments;
@ -33,16 +34,30 @@ fn listing_type_with_default(
}
}
/// Returns a default instance-level sort type, if none is given by the user.
/// Returns a default instance-level post 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>,
fn post_sort_type_with_default(
type_: Option<PostSortType>,
local_user: Option<&LocalUser>,
local_site: &LocalSite,
) -> SortType {
) -> PostSortType {
type_.unwrap_or(
local_user
.map(|u| u.default_sort_type)
.unwrap_or(local_site.default_sort_type),
.map(|u| u.default_post_sort_type)
.unwrap_or(local_site.default_post_sort_type),
)
}
/// Returns a default instance-level comment sort type, if none is given by the user.
/// Order is type, local user default, then site default.
fn comment_sort_type_with_default(
type_: Option<CommentSortType>,
local_user: Option<&LocalUser>,
local_site: &LocalSite,
) -> CommentSortType {
type_.unwrap_or(
local_user
.map(|u| u.default_comment_sort_type)
.unwrap_or(local_site.default_comment_sort_type),
)
}

@ -107,7 +107,8 @@ pub async fn import_settings(
let local_user_form = LocalUserUpdateForm {
show_nsfw: data.settings.as_ref().map(|s| s.show_nsfw),
theme: data.settings.clone().map(|s| s.theme.clone()),
default_sort_type: data.settings.as_ref().map(|s| s.default_sort_type),
default_post_sort_type: data.settings.as_ref().map(|s| s.default_post_sort_type),
default_comment_sort_type: data.settings.as_ref().map(|s| s.default_comment_sort_type),
default_listing_type: data.settings.as_ref().map(|s| s.default_listing_type),
interface_language: data.settings.clone().map(|s| s.interface_language),
show_avatars: data.settings.as_ref().map(|s| s.show_avatars),

@ -20,7 +20,7 @@ use lemmy_db_schema::{
},
traits::Crud,
utils::{build_db_pool, get_conn, now},
SortType,
PostSortType,
};
use lemmy_db_views::{post_view::PostQuery, structs::PaginationCursor};
use lemmy_utils::error::{LemmyErrorExt2, LemmyResult};
@ -154,7 +154,7 @@ async fn try_main() -> LemmyResult<()> {
// TODO: include local_user
let post_views = PostQuery {
community_id: community_ids.as_slice().first().cloned(),
sort: Some(SortType::New),
sort: Some(PostSortType::New),
limit: Some(20),
page_after,
..Default::default()

@ -54,13 +54,13 @@ use ts_rs::TS;
#[cfg_attr(feature = "full", derive(DbEnum, TS))]
#[cfg_attr(
feature = "full",
ExistingTypePath = "crate::schema::sql_types::SortTypeEnum"
ExistingTypePath = "crate::schema::sql_types::PostSortTypeEnum"
)]
#[cfg_attr(feature = "full", DbValueStyle = "verbatim")]
#[cfg_attr(feature = "full", ts(export))]
// TODO add the controversial and scaled rankings to the doc below
/// The post sort types. See here for descriptions: https://join-lemmy.org/docs/en/users/03-votes-and-ranking.html
pub enum SortType {
pub enum PostSortType {
#[default]
Active,
Hot,
@ -83,11 +83,19 @@ pub enum SortType {
Scaled,
}
#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[derive(
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Hash,
)]
#[cfg_attr(feature = "full", derive(DbEnum, TS))]
#[cfg_attr(
feature = "full",
ExistingTypePath = "crate::schema::sql_types::CommentSortTypeEnum"
)]
#[cfg_attr(feature = "full", DbValueStyle = "verbatim")]
#[cfg_attr(feature = "full", ts(export))]
/// The comment sort types. See here for descriptions: https://join-lemmy.org/docs/en/users/03-votes-and-ranking.html
pub enum CommentSortType {
#[default]
Hot,
Top,
New,

@ -5,6 +5,10 @@ pub mod sql_types {
#[diesel(postgres_type(name = "actor_type_enum"))]
pub struct ActorTypeEnum;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "comment_sort_type_enum"))]
pub struct CommentSortTypeEnum;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "community_visibility"))]
pub struct CommunityVisibility;
@ -22,12 +26,12 @@ pub mod sql_types {
pub struct PostListingModeEnum;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "registration_mode_enum"))]
pub struct RegistrationModeEnum;
#[diesel(postgres_type(name = "post_sort_type_enum"))]
pub struct PostSortTypeEnum;
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "sort_type_enum"))]
pub struct SortTypeEnum;
#[diesel(postgres_type(name = "registration_mode_enum"))]
pub struct RegistrationModeEnum;
}
diesel::table! {
@ -354,7 +358,8 @@ diesel::table! {
use super::sql_types::ListingTypeEnum;
use super::sql_types::RegistrationModeEnum;
use super::sql_types::PostListingModeEnum;
use super::sql_types::SortTypeEnum;
use super::sql_types::PostSortTypeEnum;
use super::sql_types::CommentSortTypeEnum;
local_site (id) {
id -> Int4,
@ -383,7 +388,8 @@ diesel::table! {
reports_email_admins -> Bool,
federation_signed_fetch -> Bool,
default_post_listing_mode -> PostListingModeEnum,
default_sort_type -> SortTypeEnum,
default_post_sort_type -> PostSortTypeEnum,
default_comment_sort_type -> CommentSortTypeEnum,
}
}
@ -411,9 +417,10 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use super::sql_types::SortTypeEnum;
use super::sql_types::PostSortTypeEnum;
use super::sql_types::ListingTypeEnum;
use super::sql_types::PostListingModeEnum;
use super::sql_types::CommentSortTypeEnum;
local_user (id) {
id -> Int4,
@ -422,7 +429,7 @@ diesel::table! {
email -> Nullable<Text>,
show_nsfw -> Bool,
theme -> Text,
default_sort_type -> SortTypeEnum,
default_post_sort_type -> PostSortTypeEnum,
default_listing_type -> ListingTypeEnum,
#[max_length = 20]
interface_language -> Varchar,
@ -444,6 +451,7 @@ diesel::table! {
enable_keyboard_navigation -> Bool,
enable_animated_images -> Bool,
collapse_bot_comments -> Bool,
default_comment_sort_type -> CommentSortTypeEnum,
}
}

@ -2,10 +2,11 @@
use crate::schema::local_site;
use crate::{
newtypes::{LocalSiteId, SiteId},
CommentSortType,
ListingType,
PostListingMode,
PostSortType,
RegistrationMode,
SortType,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
@ -68,8 +69,10 @@ pub struct LocalSite {
pub federation_signed_fetch: bool,
/// 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,
/// Default value for [LocalUser.post_sort_type]
pub default_post_sort_type: PostSortType,
/// Default value for [LocalUser.comment_sort_type]
pub default_comment_sort_type: CommentSortType,
}
#[derive(Clone, TypedBuilder)]
@ -100,7 +103,8 @@ 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>,
pub default_post_sort_type: Option<PostSortType>,
pub default_comment_sort_type: Option<CommentSortType>,
}
#[derive(Clone, Default)]
@ -129,5 +133,6 @@ 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>,
pub default_post_sort_type: Option<PostSortType>,
pub default_comment_sort_type: Option<CommentSortType>,
}

@ -2,9 +2,10 @@
use crate::schema::local_user;
use crate::{
newtypes::{LocalUserId, PersonId},
CommentSortType,
ListingType,
PostListingMode,
SortType,
PostSortType,
};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
@ -29,7 +30,7 @@ pub struct LocalUser {
/// Whether to show NSFW content.
pub show_nsfw: bool,
pub theme: String,
pub default_sort_type: SortType,
pub default_post_sort_type: PostSortType,
pub default_listing_type: ListingType,
pub interface_language: String,
/// Whether to show avatars.
@ -63,6 +64,7 @@ pub struct LocalUser {
pub enable_animated_images: bool,
/// Whether to auto-collapse bot comments.
pub collapse_bot_comments: bool,
pub default_comment_sort_type: CommentSortType,
}
#[derive(Clone, TypedBuilder)]
@ -77,7 +79,7 @@ pub struct LocalUserInsertForm {
pub email: Option<String>,
pub show_nsfw: Option<bool>,
pub theme: Option<String>,
pub default_sort_type: Option<SortType>,
pub default_post_sort_type: Option<PostSortType>,
pub default_listing_type: Option<ListingType>,
pub interface_language: Option<String>,
pub show_avatars: Option<bool>,
@ -98,6 +100,7 @@ pub struct LocalUserInsertForm {
pub enable_keyboard_navigation: Option<bool>,
pub enable_animated_images: Option<bool>,
pub collapse_bot_comments: Option<bool>,
pub default_comment_sort_type: Option<CommentSortType>,
}
#[derive(Clone, Default)]
@ -108,7 +111,7 @@ pub struct LocalUserUpdateForm {
pub email: Option<Option<String>>,
pub show_nsfw: Option<bool>,
pub theme: Option<String>,
pub default_sort_type: Option<SortType>,
pub default_post_sort_type: Option<PostSortType>,
pub default_listing_type: Option<ListingType>,
pub interface_language: Option<String>,
pub show_avatars: Option<bool>,
@ -129,4 +132,5 @@ pub struct LocalUserUpdateForm {
pub enable_keyboard_navigation: Option<bool>,
pub enable_animated_images: Option<bool>,
pub collapse_bot_comments: Option<bool>,
pub default_comment_sort_type: Option<CommentSortType>,
}

@ -3,7 +3,7 @@ use crate::{
diesel_migrations::MigrationHarness,
newtypes::DbUrl,
CommentSortType,
SortType,
PostSortType,
};
use anyhow::Context;
use chrono::{DateTime, Utc};
@ -421,23 +421,25 @@ pub fn naive_now() -> DateTime<Utc> {
Utc::now()
}
pub fn post_to_comment_sort_type(sort: SortType) -> CommentSortType {
pub fn post_to_comment_sort_type(sort: PostSortType) -> CommentSortType {
match sort {
SortType::Active | SortType::Hot | SortType::Scaled => CommentSortType::Hot,
SortType::New | SortType::NewComments | SortType::MostComments => CommentSortType::New,
SortType::Old => CommentSortType::Old,
SortType::Controversial => CommentSortType::Controversial,
SortType::TopHour
| SortType::TopSixHour
| SortType::TopTwelveHour
| SortType::TopDay
| SortType::TopAll
| SortType::TopWeek
| SortType::TopYear
| SortType::TopMonth
| SortType::TopThreeMonths
| SortType::TopSixMonths
| SortType::TopNineMonths => CommentSortType::Top,
PostSortType::Active | PostSortType::Hot | PostSortType::Scaled => CommentSortType::Hot,
PostSortType::New | PostSortType::NewComments | PostSortType::MostComments => {
CommentSortType::New
}
PostSortType::Old => CommentSortType::Old,
PostSortType::Controversial => CommentSortType::Controversial,
PostSortType::TopHour
| PostSortType::TopSixHour
| PostSortType::TopTwelveHour
| PostSortType::TopDay
| PostSortType::TopAll
| PostSortType::TopWeek
| PostSortType::TopYear
| PostSortType::TopMonth
| PostSortType::TopThreeMonths
| PostSortType::TopSixMonths
| PostSortType::TopNineMonths => CommentSortType::Top,
}
}

@ -56,7 +56,7 @@ use lemmy_db_schema::{
},
CommunityVisibility,
ListingType,
SortType,
PostSortType,
};
use tracing::debug;
@ -480,33 +480,33 @@ fn queries<'a>() -> Queries<
let time = |interval| post_aggregates::published.gt(now() - interval);
// then use the main sort
query = match options.sort.unwrap_or(SortType::Hot) {
SortType::Active => query.then_desc(key::hot_rank_active),
SortType::Hot => query.then_desc(key::hot_rank),
SortType::Scaled => query.then_desc(key::scaled_rank),
SortType::Controversial => query.then_desc(key::controversy_rank),
SortType::New => query.then_desc(key::published),
SortType::Old => query.then_desc(ReverseTimestampKey(key::published)),
SortType::NewComments => query.then_desc(key::newest_comment_time),
SortType::MostComments => query.then_desc(key::comments),
SortType::TopAll => query.then_desc(key::score),
SortType::TopYear => query.then_desc(key::score).filter(time(1.years())),
SortType::TopMonth => query.then_desc(key::score).filter(time(1.months())),
SortType::TopWeek => query.then_desc(key::score).filter(time(1.weeks())),
SortType::TopDay => query.then_desc(key::score).filter(time(1.days())),
SortType::TopHour => query.then_desc(key::score).filter(time(1.hours())),
SortType::TopSixHour => query.then_desc(key::score).filter(time(6.hours())),
SortType::TopTwelveHour => query.then_desc(key::score).filter(time(12.hours())),
SortType::TopThreeMonths => query.then_desc(key::score).filter(time(3.months())),
SortType::TopSixMonths => query.then_desc(key::score).filter(time(6.months())),
SortType::TopNineMonths => query.then_desc(key::score).filter(time(9.months())),
query = match options.sort.unwrap_or(PostSortType::Hot) {
PostSortType::Active => query.then_desc(key::hot_rank_active),
PostSortType::Hot => query.then_desc(key::hot_rank),
PostSortType::Scaled => query.then_desc(key::scaled_rank),
PostSortType::Controversial => query.then_desc(key::controversy_rank),
PostSortType::New => query.then_desc(key::published),
PostSortType::Old => query.then_desc(ReverseTimestampKey(key::published)),
PostSortType::NewComments => query.then_desc(key::newest_comment_time),
PostSortType::MostComments => query.then_desc(key::comments),
PostSortType::TopAll => query.then_desc(key::score),
PostSortType::TopYear => query.then_desc(key::score).filter(time(1.years())),
PostSortType::TopMonth => query.then_desc(key::score).filter(time(1.months())),
PostSortType::TopWeek => query.then_desc(key::score).filter(time(1.weeks())),
PostSortType::TopDay => query.then_desc(key::score).filter(time(1.days())),
PostSortType::TopHour => query.then_desc(key::score).filter(time(1.hours())),
PostSortType::TopSixHour => query.then_desc(key::score).filter(time(6.hours())),
PostSortType::TopTwelveHour => query.then_desc(key::score).filter(time(12.hours())),
PostSortType::TopThreeMonths => query.then_desc(key::score).filter(time(3.months())),
PostSortType::TopSixMonths => query.then_desc(key::score).filter(time(6.months())),
PostSortType::TopNineMonths => query.then_desc(key::score).filter(time(9.months())),
};
// use publish as fallback. especially useful for hot rank which reaches zero after some days.
// necessary because old posts can be fetched over federation and inserted with high post id
query = match options.sort.unwrap_or(SortType::Hot) {
query = match options.sort.unwrap_or(PostSortType::Hot) {
// A second time-based sort would not be very useful
SortType::New | SortType::Old | SortType::NewComments => query,
PostSortType::New | PostSortType::Old | PostSortType::NewComments => query,
_ => query.then_desc(key::published),
};
@ -577,7 +577,7 @@ pub struct PaginationCursorData(PostAggregates);
#[derive(Clone, Default)]
pub struct PostQuery<'a> {
pub listing_type: Option<ListingType>,
pub sort: Option<SortType>,
pub sort: Option<PostSortType>,
pub creator_id: Option<PersonId>,
pub community_id: Option<CommunityId>,
// if true, the query should be handled as if community_id was not given except adding the literal filter
@ -732,7 +732,7 @@ mod tests {
traits::{Blockable, Crud, Joinable, Likeable},
utils::{build_db_pool, build_db_pool_for_tests, DbPool, RANK_DEFAULT},
CommunityVisibility,
SortType,
PostSortType,
SubscribedType,
};
use lemmy_utils::error::LemmyResult;
@ -763,7 +763,7 @@ mod tests {
impl Data {
fn default_post_query(&self) -> PostQuery<'_> {
PostQuery {
sort: Some(SortType::New),
sort: Some(PostSortType::New),
local_user: Some(&self.local_user_view),
..Default::default()
}
@ -1371,7 +1371,7 @@ mod tests {
let options = PostQuery {
community_id: Some(inserted_community.id),
sort: Some(SortType::MostComments),
sort: Some(PostSortType::MostComments),
limit: Some(10),
..Default::default()
};

@ -252,7 +252,8 @@ mod tests {
auto_expand: inserted_sara_local_user.auto_expand,
blur_nsfw: inserted_sara_local_user.blur_nsfw,
theme: inserted_sara_local_user.theme,
default_sort_type: inserted_sara_local_user.default_sort_type,
default_post_sort_type: inserted_sara_local_user.default_post_sort_type,
default_comment_sort_type: inserted_sara_local_user.default_comment_sort_type,
default_listing_type: inserted_sara_local_user.default_listing_type,
interface_language: inserted_sara_local_user.interface_language,
show_avatars: inserted_sara_local_user.show_avatars,

@ -25,7 +25,7 @@ use lemmy_db_schema::{
utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
CommunityVisibility,
ListingType,
SortType,
PostSortType,
};
fn queries<'a>() -> Queries<
@ -106,7 +106,7 @@ fn queries<'a>() -> Queries<
};
let list = move |mut conn: DbConn<'a>, (options, site): (CommunityQuery<'a>, &'a Site)| async move {
use SortType::*;
use PostSortType::*;
let my_person_id = options.local_user.map(|l| l.person_id);
@ -234,7 +234,7 @@ impl CommunityView {
#[derive(Default)]
pub struct CommunityQuery<'a> {
pub listing_type: Option<ListingType>,
pub sort: Option<SortType>,
pub sort: Option<PostSortType>,
pub local_user: Option<&'a LocalUser>,
pub search_term: Option<String>,
pub is_mod_or_admin: bool,

@ -23,7 +23,7 @@ use lemmy_db_schema::{
Queries,
ReadFn,
},
SortType,
PostSortType,
};
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString};
@ -45,12 +45,14 @@ enum PersonSortType {
PostCount,
}
fn post_to_person_sort_type(sort: SortType) -> PersonSortType {
fn post_to_person_sort_type(sort: PostSortType) -> PersonSortType {
match sort {
SortType::Active | SortType::Hot | SortType::Controversial => PersonSortType::CommentScore,
SortType::New | SortType::NewComments => PersonSortType::New,
SortType::MostComments => PersonSortType::MostComments,
SortType::Old => PersonSortType::Old,
PostSortType::Active | PostSortType::Hot | PostSortType::Controversial => {
PersonSortType::CommentScore
}
PostSortType::New | PostSortType::NewComments => PersonSortType::New,
PostSortType::MostComments => PersonSortType::MostComments,
PostSortType::Old => PersonSortType::Old,
_ => PersonSortType::CommentScore,
}
}
@ -139,7 +141,7 @@ impl PersonView {
#[derive(Default)]
pub struct PersonQuery {
pub sort: Option<SortType>,
pub sort: Option<PostSortType>,
pub search_term: Option<String>,
pub page: Option<i64>,
pub limit: Option<i64>,
@ -252,7 +254,7 @@ mod tests {
assert_eq!(read.err(), Some(NotFound));
let list = PersonQuery {
sort: Some(SortType::New),
sort: Some(PostSortType::New),
..Default::default()
}
.list(pool)

@ -9,7 +9,7 @@ use lemmy_db_schema::{
CommentSortType,
CommunityVisibility,
ListingType,
SortType,
PostSortType,
};
use lemmy_db_views::{
post_view::PostQuery,
@ -46,12 +46,12 @@ struct Params {
}
impl Params {
fn sort_type(&self) -> Result<SortType, Error> {
fn sort_type(&self) -> Result<PostSortType, Error> {
let sort_query = self
.sort
.clone()
.unwrap_or_else(|| SortType::Hot.to_string());
SortType::from_str(&sort_query).map_err(ErrorBadRequest)
.unwrap_or_else(|| PostSortType::Hot.to_string());
PostSortType::from_str(&sort_query).map_err(ErrorBadRequest)
}
fn get_limit(&self) -> i64 {
self.limit.unwrap_or(RSS_FETCH_LIMIT)
@ -148,7 +148,7 @@ async fn get_local_feed(
async fn get_feed_data(
context: &LemmyContext,
listing_type: ListingType,
sort_type: SortType,
sort_type: PostSortType,
limit: i64,
page: i64,
) -> Result<HttpResponse, LemmyError> {
@ -252,7 +252,7 @@ async fn get_feed(
#[tracing::instrument(skip_all)]
async fn get_feed_user(
context: &LemmyContext,
sort_type: &SortType,
sort_type: &PostSortType,
limit: &i64,
page: &i64,
user_name: &str,
@ -288,7 +288,7 @@ async fn get_feed_user(
#[tracing::instrument(skip_all)]
async fn get_feed_community(
context: &LemmyContext,
sort_type: &SortType,
sort_type: &PostSortType,
limit: &i64,
page: &i64,
community_name: &str,
@ -331,7 +331,7 @@ async fn get_feed_community(
#[tracing::instrument(skip_all)]
async fn get_feed_front(
context: &LemmyContext,
sort_type: &SortType,
sort_type: &PostSortType,
limit: &i64,
page: &i64,
jwt: &str,

@ -0,0 +1,19 @@
-- This file should undo anything in `up.sql`
-- Rename the post sort enum
ALTER TYPE post_sort_type_enum RENAME TO sort_type_enum;
-- Rename the default post sort columns
ALTER TABLE local_user RENAME COLUMN default_post_sort_type TO default_sort_type;
ALTER TABLE local_site RENAME COLUMN default_post_sort_type TO default_sort_type;
-- Create the comment sort type enum
ALTER TABLE local_user
DROP COLUMN default_comment_sort_type;
ALTER TABLE local_site
DROP COLUMN default_comment_sort_type;
-- Drop the comment enum
DROP TYPE comment_sort_type_enum;

@ -0,0 +1,24 @@
-- Rename the post sort enum
ALTER TYPE sort_type_enum RENAME TO post_sort_type_enum;
-- Rename the default post sort columns
ALTER TABLE local_user RENAME COLUMN default_sort_type TO default_post_sort_type;
ALTER TABLE local_site RENAME COLUMN default_sort_type TO default_post_sort_type;
-- Create the comment sort type enum
CREATE TYPE comment_sort_type_enum AS ENUM (
'Hot',
'Top',
'New',
'Old',
'Controversial'
);
-- Add the new default comment sort columns to local_user and local_site
ALTER TABLE local_user
ADD COLUMN default_comment_sort_type comment_sort_type_enum NOT NULL DEFAULT 'Hot';
ALTER TABLE local_site
ADD COLUMN default_comment_sort_type comment_sort_type_enum NOT NULL DEFAULT 'Hot';
Loading…
Cancel
Save