Cleanup public api (#4047)

* Convert PersonSortType to purely internal

* Remove hot rank and other db optimizations from public api
This commit is contained in:
Nutomic 2023-10-17 01:37:28 +02:00 committed by GitHub
parent 6cfbb8fc3b
commit 332e698336
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 31 deletions

2
Cargo.lock generated
View file

@ -2808,6 +2808,8 @@ dependencies = [
"lemmy_db_schema", "lemmy_db_schema",
"serde", "serde",
"serde_with", "serde_with",
"strum",
"strum_macros",
"ts-rs", "ts-rs",
] ]

View file

@ -8,7 +8,7 @@ use lemmy_api_common::{
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{community::Community, local_site::LocalSite}, source::{community::Community, local_site::LocalSite},
utils::{post_to_comment_sort_type, post_to_person_sort_type}, utils::post_to_comment_sort_type,
SearchType, SearchType,
}; };
use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery, structs::LocalUserView}; use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery, structs::LocalUserView};
@ -101,7 +101,7 @@ pub async fn search(
} }
SearchType::Users => { SearchType::Users => {
users = PersonQuery { users = PersonQuery {
sort: (sort.map(post_to_person_sort_type)), sort,
search_term: (Some(q)), search_term: (Some(q)),
page: (page), page: (page),
limit: (limit), limit: (limit),
@ -171,7 +171,7 @@ pub async fn search(
vec![] vec![]
} else { } else {
PersonQuery { PersonQuery {
sort: (sort.map(post_to_person_sort_type)), sort,
search_term: (Some(q)), search_term: (Some(q)),
page: (page), page: (page),
limit: (limit), limit: (limit),

View file

@ -27,7 +27,9 @@ pub struct CommentAggregates {
pub published: DateTime<Utc>, pub published: DateTime<Utc>,
/// The total number of children in this comment branch. /// The total number of children in this comment branch.
pub child_count: i32, pub child_count: i32,
#[serde(skip)]
pub hot_rank: f64, pub hot_rank: f64,
#[serde(skip)]
pub controversy_rank: f64, pub controversy_rank: f64,
} }
@ -55,6 +57,7 @@ pub struct CommunityAggregates {
pub users_active_month: i64, pub users_active_month: i64,
/// The number of users with any activity in the last year. /// The number of users with any activity in the last year.
pub users_active_half_year: i64, pub users_active_half_year: i64,
#[serde(skip)]
pub hot_rank: f64, pub hot_rank: f64,
} }
@ -87,21 +90,32 @@ pub struct PostAggregates {
pub upvotes: i64, pub upvotes: i64,
pub downvotes: i64, pub downvotes: i64,
pub published: DateTime<Utc>, pub published: DateTime<Utc>,
/// 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<Utc>, pub newest_comment_time_necro: DateTime<Utc>,
/// The time of the newest comment in the post. /// The time of the newest comment in the post.
#[serde(skip)]
pub newest_comment_time: DateTime<Utc>, pub newest_comment_time: DateTime<Utc>,
/// If the post is featured on the community. /// If the post is featured on the community.
#[serde(skip)]
pub featured_community: bool, pub featured_community: bool,
/// If the post is featured on the site / to local. /// If the post is featured on the site / to local.
#[serde(skip)]
pub featured_local: bool, pub featured_local: bool,
#[serde(skip)]
pub hot_rank: f64, pub hot_rank: f64,
#[serde(skip)]
pub hot_rank_active: f64, pub hot_rank_active: f64,
#[serde(skip)]
pub community_id: CommunityId, pub community_id: CommunityId,
#[serde(skip)]
pub creator_id: PersonId, pub creator_id: PersonId,
#[serde(skip)]
pub controversy_rank: f64, pub controversy_rank: f64,
#[serde(skip)]
pub instance_id: InstanceId, pub instance_id: InstanceId,
/// A rank that amplifies smaller communities /// A rank that amplifies smaller communities
#[serde(skip)]
pub scaled_rank: f64, pub scaled_rank: f64,
} }

View file

@ -91,19 +91,6 @@ pub enum CommentSortType {
Controversial, 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( #[derive(
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default,
)] )]

View file

@ -3,7 +3,6 @@ use crate::{
diesel_migrations::MigrationHarness, diesel_migrations::MigrationHarness,
newtypes::DbUrl, newtypes::DbUrl,
CommentSortType, CommentSortType,
PersonSortType,
SortType, SortType,
}; };
use activitypub_federation::{fetch::object_id::ObjectId, traits::Object}; 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<Regex> = Lazy::new(|| { static EMAIL_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$") Regex::new(r"^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
.expect("compile email regex") .expect("compile email regex")

View file

@ -29,3 +29,5 @@ serde = { workspace = true }
serde_with = { workspace = true } serde_with = { workspace = true }
ts-rs = { workspace = true, optional = true } ts-rs = { workspace = true, optional = true }
chrono.workspace = true chrono.workspace = true
strum = { workspace = true }
strum_macros = { workspace = true }

View file

@ -14,8 +14,10 @@ use lemmy_db_schema::{
schema, schema,
schema::{local_user, person, person_aggregates}, schema::{local_user, person, person_aggregates},
utils::{fuzzy_search, get_conn, limit_and_offset, now, DbConn, DbPool, ListFn, Queries, ReadFn}, 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 { enum ListMode {
Admins, Admins,
@ -23,6 +25,27 @@ enum ListMode {
Query(PersonQuery), 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>( fn queries<'a>(
) -> Queries<impl ReadFn<'a, PersonView, PersonId>, impl ListFn<'a, PersonView, ListMode>> { ) -> Queries<impl ReadFn<'a, PersonView, PersonId>, impl ListFn<'a, PersonView, ListMode>> {
let all_joins = |query: person::BoxedQuery<'a, Pg>| { let all_joins = |query: person::BoxedQuery<'a, Pg>| {
@ -66,7 +89,8 @@ fn queries<'a>(
.or_filter(person::display_name.ilike(searcher)); .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::New => query.order_by(person::published.desc()),
PersonSortType::Old => query.order_by(person::published.asc()), PersonSortType::Old => query.order_by(person::published.asc()),
PersonSortType::MostComments => query.order_by(person_aggregates::comment_count.desc()), PersonSortType::MostComments => query.order_by(person_aggregates::comment_count.desc()),
@ -116,7 +140,7 @@ impl PersonView {
#[derive(Default)] #[derive(Default)]
pub struct PersonQuery { pub struct PersonQuery {
pub sort: Option<PersonSortType>, pub sort: Option<SortType>,
pub search_term: Option<String>, pub search_term: Option<String>,
pub page: Option<i64>, pub page: Option<i64>,
pub limit: Option<i64>, pub limit: Option<i64>,