From 332e6983366fbb2cda2c8e9ecb88a0bd37b48ec4 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Tue, 17 Oct 2023 01:37:28 +0200 Subject: [PATCH] Cleanup public api (#4047) * Convert PersonSortType to purely internal * Remove hot rank and other db optimizations from public api --- Cargo.lock | 2 ++ crates/apub/src/api/search.rs | 6 ++--- crates/db_schema/src/aggregates/structs.rs | 16 +++++++++++- crates/db_schema/src/lib.rs | 13 ---------- crates/db_schema/src/utils.rs | 11 -------- crates/db_views_actor/Cargo.toml | 2 ++ crates/db_views_actor/src/person_view.rs | 30 +++++++++++++++++++--- 7 files changed, 49 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 36bbfce8e..5cf23c6a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2808,6 +2808,8 @@ dependencies = [ "lemmy_db_schema", "serde", "serde_with", + "strum", + "strum_macros", "ts-rs", ] diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index 0c7231e8f..b854a91d1 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -8,7 +8,7 @@ use lemmy_api_common::{ }; use lemmy_db_schema::{ source::{community::Community, local_site::LocalSite}, - utils::{post_to_comment_sort_type, post_to_person_sort_type}, + utils::post_to_comment_sort_type, SearchType, }; use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery, structs::LocalUserView}; @@ -101,7 +101,7 @@ pub async fn search( } SearchType::Users => { users = PersonQuery { - sort: (sort.map(post_to_person_sort_type)), + sort, search_term: (Some(q)), page: (page), limit: (limit), @@ -171,7 +171,7 @@ pub async fn search( vec![] } else { PersonQuery { - sort: (sort.map(post_to_person_sort_type)), + sort, search_term: (Some(q)), page: (page), limit: (limit), diff --git a/crates/db_schema/src/aggregates/structs.rs b/crates/db_schema/src/aggregates/structs.rs index 03ff9a640..641ca3b3d 100644 --- a/crates/db_schema/src/aggregates/structs.rs +++ b/crates/db_schema/src/aggregates/structs.rs @@ -27,7 +27,9 @@ pub struct CommentAggregates { pub published: DateTime, /// The total number of children in this comment branch. pub child_count: i32, + #[serde(skip)] pub hot_rank: f64, + #[serde(skip)] pub controversy_rank: f64, } @@ -55,6 +57,7 @@ pub struct CommunityAggregates { pub users_active_month: i64, /// The number of users with any activity in the last year. pub users_active_half_year: i64, + #[serde(skip)] pub hot_rank: f64, } @@ -87,21 +90,32 @@ pub struct PostAggregates { pub upvotes: i64, pub downvotes: i64, pub published: DateTime, - /// A newest comment time, limited to 2 days, to prevent necrobumping + #[serde(skip)] + /// A newest comment time, limited to 2 days, to prevent necrobumping pub newest_comment_time_necro: DateTime, /// The time of the newest comment in the post. + #[serde(skip)] pub newest_comment_time: DateTime, /// If the post is featured on the community. + #[serde(skip)] pub featured_community: bool, /// If the post is featured on the site / to local. + #[serde(skip)] pub featured_local: bool, + #[serde(skip)] pub hot_rank: f64, + #[serde(skip)] pub hot_rank_active: f64, + #[serde(skip)] pub community_id: CommunityId, + #[serde(skip)] pub creator_id: PersonId, + #[serde(skip)] pub controversy_rank: f64, + #[serde(skip)] pub instance_id: InstanceId, /// A rank that amplifies smaller communities + #[serde(skip)] pub scaled_rank: f64, } diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index 80fc9ffb7..5b908f356 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -91,19 +91,6 @@ pub enum CommentSortType { Controversial, } -#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)] -#[cfg_attr(feature = "full", derive(TS))] -#[cfg_attr(feature = "full", ts(export))] -/// The person sort types. See here for descriptions: https://join-lemmy.org/docs/en/users/03-votes-and-ranking.html -pub enum PersonSortType { - New, - Old, - MostComments, - CommentScore, - PostScore, - PostCount, -} - #[derive( EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, )] diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index 7593cfd41..7e83569a7 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -3,7 +3,6 @@ use crate::{ diesel_migrations::MigrationHarness, newtypes::DbUrl, CommentSortType, - PersonSortType, SortType, }; use activitypub_federation::{fetch::object_id::ObjectId, traits::Object}; @@ -365,16 +364,6 @@ pub fn post_to_comment_sort_type(sort: SortType) -> CommentSortType { } } -pub fn post_to_person_sort_type(sort: SortType) -> 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, - _ => PersonSortType::CommentScore, - } -} - static EMAIL_REGEX: Lazy = Lazy::new(|| { Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$") .expect("compile email regex") diff --git a/crates/db_views_actor/Cargo.toml b/crates/db_views_actor/Cargo.toml index 20b69e56c..358bf0cab 100644 --- a/crates/db_views_actor/Cargo.toml +++ b/crates/db_views_actor/Cargo.toml @@ -29,3 +29,5 @@ serde = { workspace = true } serde_with = { workspace = true } ts-rs = { workspace = true, optional = true } chrono.workspace = true +strum = { workspace = true } +strum_macros = { workspace = true } diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 042b04767..d06654f98 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -14,8 +14,10 @@ use lemmy_db_schema::{ schema, schema::{local_user, person, person_aggregates}, utils::{fuzzy_search, get_conn, limit_and_offset, now, DbConn, DbPool, ListFn, Queries, ReadFn}, - PersonSortType, + SortType, }; +use serde::{Deserialize, Serialize}; +use strum_macros::{Display, EnumString}; enum ListMode { Admins, @@ -23,6 +25,27 @@ enum ListMode { Query(PersonQuery), } +#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)] +/// The person sort types. Converted automatically from `SortType` +enum PersonSortType { + New, + Old, + MostComments, + CommentScore, + PostScore, + PostCount, +} + +fn post_to_person_sort_type(sort: SortType) -> 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, + _ => PersonSortType::CommentScore, + } +} + fn queries<'a>( ) -> Queries, impl ListFn<'a, PersonView, ListMode>> { let all_joins = |query: person::BoxedQuery<'a, Pg>| { @@ -66,7 +89,8 @@ fn queries<'a>( .or_filter(person::display_name.ilike(searcher)); } - query = match options.sort.unwrap_or(PersonSortType::CommentScore) { + let sort = options.sort.map(post_to_person_sort_type); + query = match sort.unwrap_or(PersonSortType::CommentScore) { PersonSortType::New => query.order_by(person::published.desc()), PersonSortType::Old => query.order_by(person::published.asc()), PersonSortType::MostComments => query.order_by(person_aggregates::comment_count.desc()), @@ -116,7 +140,7 @@ impl PersonView { #[derive(Default)] pub struct PersonQuery { - pub sort: Option, + pub sort: Option, pub search_term: Option, pub page: Option, pub limit: Option,