This commit is contained in:
Steven Vergenz 2024-09-27 12:08:01 -07:00 committed by GitHub
commit 4fd0803cc0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 73 additions and 8 deletions

View File

@ -3,9 +3,13 @@ use lemmy_db_schema::{
source::site::Site,
CommunityVisibility,
ListingType,
PostSortType,
};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonView};
use lemmy_db_views_actor::structs::{
CommunityModeratorView,
CommunitySortType,
CommunityView,
PersonView,
};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[cfg(feature = "full")]
@ -74,7 +78,7 @@ pub struct CommunityResponse {
/// Fetches a list of communities.
pub struct ListCommunities {
pub type_: Option<ListingType>,
pub sort: Option<PostSortType>,
pub sort: Option<CommunitySortType>,
pub show_nsfw: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,

View File

@ -12,7 +12,11 @@ use lemmy_db_views::{
post_view::PostQuery,
structs::{LocalUserView, SiteView},
};
use lemmy_db_views_actor::{community_view::CommunityQuery, person_view::PersonQuery};
use lemmy_db_views_actor::{
community_view::CommunityQuery,
person_view::PersonQuery,
structs::CommunitySortType,
};
use lemmy_utils::error::LemmyResult;
#[tracing::instrument(skip(context))]
@ -102,7 +106,7 @@ pub async fn search(
};
let community_query = CommunityQuery {
sort,
sort: sort.map(CommunitySortType::from),
listing_type,
search_term: Some(q.clone()),
title_only,

View File

@ -1,4 +1,4 @@
use crate::structs::{CommunityModeratorView, CommunityView, PersonView};
use crate::structs::{CommunityModeratorView, CommunitySortType, CommunityView, PersonView};
use diesel::{
pg::Pg,
result::Error,
@ -103,7 +103,7 @@ fn queries<'a>() -> Queries<
};
let list = move |mut conn: DbConn<'a>, (options, site): (CommunityQuery<'a>, &'a Site)| async move {
use PostSortType::*;
use CommunitySortType::*;
// The left join below will return None in this case
let person_id_join = options.local_user.person_id().unwrap_or(PersonId(-1));
@ -148,6 +148,8 @@ fn queries<'a>() -> Queries<
}
TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
TopWeek => query = query.order_by(community_aggregates::users_active_week.desc()),
NameAsc => query = query.order_by(community::name.asc()),
NameDesc => query = query.order_by(community::name.desc()),
};
if let Some(listing_type) = options.listing_type {
@ -228,10 +230,36 @@ impl CommunityView {
}
}
impl From<PostSortType> for CommunitySortType {
fn from(value: PostSortType) -> Self {
match value {
PostSortType::Active => Self::Active,
PostSortType::Hot => Self::Hot,
PostSortType::New => Self::New,
PostSortType::Old => Self::Old,
PostSortType::TopDay => Self::TopDay,
PostSortType::TopWeek => Self::TopWeek,
PostSortType::TopMonth => Self::TopMonth,
PostSortType::TopYear => Self::TopYear,
PostSortType::TopAll => Self::TopAll,
PostSortType::MostComments => Self::MostComments,
PostSortType::NewComments => Self::NewComments,
PostSortType::TopHour => Self::TopHour,
PostSortType::TopSixHour => Self::TopSixHour,
PostSortType::TopTwelveHour => Self::TopTwelveHour,
PostSortType::TopThreeMonths => Self::TopThreeMonths,
PostSortType::TopSixMonths => Self::TopSixMonths,
PostSortType::TopNineMonths => Self::TopNineMonths,
PostSortType::Controversial => Self::Controversial,
PostSortType::Scaled => Self::Scaled,
}
}
}
#[derive(Default)]
pub struct CommunityQuery<'a> {
pub listing_type: Option<ListingType>,
pub sort: Option<PostSortType>,
pub sort: Option<CommunitySortType>,
pub local_user: Option<&'a LocalUser>,
pub search_term: Option<String>,
pub title_only: Option<bool>,

View File

@ -59,6 +59,35 @@ pub struct CommunityView {
pub banned_from_community: bool,
}
/// The community sort types. See here for descriptions: https://join-lemmy.org/docs/en/users/03-votes-and-ranking.html
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
pub enum CommunitySortType {
#[default]
Active,
Hot,
New,
Old,
TopDay,
TopWeek,
TopMonth,
TopYear,
TopAll,
MostComments,
NewComments,
TopHour,
TopSixHour,
TopTwelveHour,
TopThreeMonths,
TopSixMonths,
TopNineMonths,
Controversial,
Scaled,
NameAsc,
NameDesc,
}
#[skip_serializing_none]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS, Queryable))]