From 9d66314c32841c191231c240b25c0fa472660a56 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 27 Apr 2021 17:11:29 +0200 Subject: [PATCH] Filter comment listing by language --- crates/api_crud/src/comment/read.rs | 4 ++++ crates/api_crud/src/post/read.rs | 4 +--- crates/db_views/src/comment_view.rs | 12 ++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/api_crud/src/comment/read.rs b/crates/api_crud/src/comment/read.rs index 12ccbd5a1..9e6c46098 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -21,6 +21,9 @@ impl PerformCrud for GetComments { let show_bot_accounts = local_user_view .as_ref() .map(|t| t.local_user.show_bot_accounts); + let languages = local_user_view + .as_ref() + .map(|l| l.local_user.discussion_languages.clone()); let person_id = local_user_view.map(|u| u.person.id); let sort: Option = from_opt_str_to_opt_enum(&data.sort); @@ -40,6 +43,7 @@ impl PerformCrud for GetComments { .community_name(community_name) .my_person_id(person_id) .show_bot_accounts(show_bot_accounts) + .languages(languages) .page(page) .limit(limit) .list() diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 87e952120..29a1aff18 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -113,9 +113,7 @@ impl PerformCrud for GetPosts { let community_id = data.community_id; let community_name = data.community_name.to_owned(); let saved_only = data.saved_only; - let languages = local_user_view - .map(|l| l.local_user.discussion_languages) - .unwrap_or_default(); + let languages = local_user_view.map(|l| l.local_user.discussion_languages); let posts = blocking(context.pool(), move |conn| { PostQueryBuilder::create(conn) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 23848fe0a..46e6d53a2 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -34,6 +34,7 @@ use lemmy_db_schema::{ CommunityId, PersonId, PostId, + PrimaryLanguageTag, }; use serde::Serialize; @@ -184,6 +185,7 @@ pub struct CommentQueryBuilder<'a> { saved_only: Option, unread_only: Option, show_bot_accounts: Option, + languages: Option>, page: Option, limit: Option, } @@ -204,6 +206,7 @@ impl<'a> CommentQueryBuilder<'a> { saved_only: None, unread_only: None, show_bot_accounts: None, + languages: None, page: None, limit: None, } @@ -269,6 +272,11 @@ impl<'a> CommentQueryBuilder<'a> { self } + pub fn languages>>(mut self, languages: T) -> Self { + self.languages = languages.get_optional(); + self + } + pub fn page>(mut self, page: T) -> Self { self.page = page.get_optional(); self @@ -392,6 +400,10 @@ impl<'a> CommentQueryBuilder<'a> { query = query.filter(person::bot_account.eq(false)); }; + if let Some(languages) = self.languages { + query = query.filter(comment::language.eq(any(languages))) + } + query = match self.sort.unwrap_or(SortType::New) { SortType::Hot | SortType::Active => query .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())