mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-22 04:11:17 +00:00
Cleanup public api (#4047)
* Convert PersonSortType to purely internal * Remove hot rank and other db optimizations from public api
This commit is contained in:
parent
6cfbb8fc3b
commit
332e698336
7 changed files with 49 additions and 31 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2808,6 +2808,8 @@ dependencies = [
|
|||
"lemmy_db_schema",
|
||||
"serde",
|
||||
"serde_with",
|
||||
"strum",
|
||||
"strum_macros",
|
||||
"ts-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),
|
||||
|
|
|
@ -27,7 +27,9 @@ pub struct CommentAggregates {
|
|||
pub published: DateTime<Utc>,
|
||||
/// 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<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>,
|
||||
/// The time of the newest comment in the post.
|
||||
#[serde(skip)]
|
||||
pub newest_comment_time: DateTime<Utc>,
|
||||
/// 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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
)]
|
||||
|
|
|
@ -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<Regex> = Lazy::new(|| {
|
||||
Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
|
||||
.expect("compile email regex")
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -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 ReadFn<'a, PersonView, PersonId>, 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<PersonSortType>,
|
||||
pub sort: Option<SortType>,
|
||||
pub search_term: Option<String>,
|
||||
pub page: Option<i64>,
|
||||
pub limit: Option<i64>,
|
||||
|
|
Loading…
Reference in a new issue