Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add community alphabetic sorting #5056

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions crates/api_common/src/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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>,
Expand Down
8 changes: 6 additions & 2 deletions crates/apub/src/api/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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,
Expand Down
129 changes: 110 additions & 19 deletions crates/db_views_actor/src/community_view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::structs::{CommunityModeratorView, CommunityView, PersonView};
use crate::structs::{CommunityModeratorView, CommunitySortType, CommunityView, PersonView};
use diesel::{
pg::Pg,
result::Error,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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>,
Expand All @@ -250,7 +278,10 @@ impl<'a> CommunityQuery<'a> {
#[cfg(test)]
mod tests {

use crate::{community_view::CommunityQuery, structs::CommunityView};
use crate::{
community_view::CommunityQuery,
structs::{CommunitySortType, CommunityView},
};
use lemmy_db_schema::{
source::{
community::{Community, CommunityInsertForm, CommunityUpdateForm},
Expand All @@ -270,7 +301,7 @@ mod tests {
struct Data {
inserted_instance: Instance,
local_user: LocalUser,
inserted_community: Community,
inserted_communities: [Community; 3],
site: Site,
}

Expand All @@ -286,13 +317,38 @@ mod tests {
let local_user_form = LocalUserInsertForm::test_form(inserted_person.id);
let local_user = LocalUser::create(pool, &local_user_form, vec![]).await?;

let new_community = CommunityInsertForm::new(
inserted_instance.id,
"test_community_3".to_string(),
"nada".to_owned(),
"pubkey".to_string(),
);
let inserted_community = Community::create(pool, &new_community).await?;
let inserted_communities = [
Community::create(
pool,
&CommunityInsertForm::new(
inserted_instance.id,
"test_community_1".to_string(),
"nada1".to_owned(),
"pubkey".to_string(),
),
)
.await?,
Community::create(
pool,
&CommunityInsertForm::new(
inserted_instance.id,
"test_community_2".to_string(),
"nada2".to_owned(),
"pubkey".to_string(),
),
)
.await?,
Community::create(
pool,
&CommunityInsertForm::new(
inserted_instance.id,
"test_community_3".to_string(),
"nada3".to_owned(),
"pubkey".to_string(),
),
)
.await?,
];

let url = Url::parse("http://example.com")?;
let site = Site {
Expand All @@ -316,13 +372,15 @@ mod tests {
Ok(Data {
inserted_instance,
local_user,
inserted_community,
inserted_communities,
site,
})
}

async fn cleanup(data: Data, pool: &mut DbPool<'_>) -> LemmyResult<()> {
Community::delete(pool, data.inserted_community.id).await?;
for Community { id, .. } in data.inserted_communities {
Community::delete(pool, id).await?;
}
Person::delete(pool, data.local_user.person_id).await?;
Instance::delete(pool, data.inserted_instance.id).await?;

Expand All @@ -338,7 +396,7 @@ mod tests {

Community::update(
pool,
data.inserted_community.id,
data.inserted_communities[0].id,
&CommunityUpdateForm {
visibility: Some(CommunityVisibility::LocalOnly),
..Default::default()
Expand All @@ -351,23 +409,26 @@ mod tests {
}
.list(&data.site, pool)
.await?;
assert_eq!(0, unauthenticated_query.len());
assert_eq!(
data.inserted_communities.len() - 1,
unauthenticated_query.len()
);

let authenticated_query = CommunityQuery {
local_user: Some(&data.local_user),
..Default::default()
}
.list(&data.site, pool)
.await?;
assert_eq!(1, authenticated_query.len());
assert_eq!(data.inserted_communities.len(), authenticated_query.len());

let unauthenticated_community =
CommunityView::read(pool, data.inserted_community.id, None, false).await;
CommunityView::read(pool, data.inserted_communities[0].id, None, false).await;
assert!(unauthenticated_community.is_err());

let authenticated_community = CommunityView::read(
pool,
data.inserted_community.id,
data.inserted_communities[0].id,
Some(&data.local_user),
false,
)
Expand All @@ -376,4 +437,34 @@ mod tests {

cleanup(data, pool).await
}

#[tokio::test]
#[serial]
async fn community_sort_name() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await?;

let query = CommunityQuery {
sort: Some(CommunitySortType::NameAsc),
..Default::default()
};
let communities = query.list(&data.site, pool).await?;
for (i, c) in communities.iter().enumerate().skip(1) {
let prev = communities.get(i - 1).expect("No previous community?");
assert!(c.community.title.cmp(&prev.community.title).is_ge());
}

let query = CommunityQuery {
sort: Some(CommunitySortType::NameDesc),
..Default::default()
};
let communities = query.list(&data.site, pool).await?;
for (i, c) in communities.iter().enumerate().skip(1) {
let prev = communities.get(i - 1).expect("No previous community?");
assert!(c.community.title.cmp(&prev.community.title).is_le());
}

cleanup(data, pool).await
}
}
29 changes: 29 additions & 0 deletions crates/db_views_actor/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down