From: Dessalines Date: Mon, 13 Jul 2020 13:50:13 +0000 (-0400) Subject: Adding a community_name option to GetPosts /post/list . Fixes #800 (#942) X-Git-Url: http://these/git/%7B%60%24%7BwebArchiveUrl%7D/%22%7B%7D/%22https:/hacktivis.me/readmes/README.zh.hant.md?a=commitdiff_plain;h=7556f8615fb859a400595960484da11da3d2ce41;p=lemmy.git Adding a community_name option to GetPosts /post/list . Fixes #800 (#942) --- diff --git a/docs/src/contributing_websocket_http_api.md b/docs/src/contributing_websocket_http_api.md index 567f674c..6ed25b98 100644 --- a/docs/src/contributing_websocket_http_api.md +++ b/docs/src/contributing_websocket_http_api.md @@ -1149,6 +1149,7 @@ Post listing types are `All, Subscribed, Community` page: Option, limit: Option, community_id: Option, + community_name: Option, auth: Option } } diff --git a/server/lemmy_db/src/post_view.rs b/server/lemmy_db/src/post_view.rs index b55359ea..3e9f8737 100644 --- a/server/lemmy_db/src/post_view.rs +++ b/server/lemmy_db/src/post_view.rs @@ -158,6 +158,7 @@ pub struct PostQueryBuilder<'a> { my_user_id: Option, for_creator_id: Option, for_community_id: Option, + for_community_name: Option, search_term: Option, url_search: Option, show_nsfw: bool, @@ -181,6 +182,7 @@ impl<'a> PostQueryBuilder<'a> { my_user_id: None, for_creator_id: None, for_community_id: None, + for_community_name: None, search_term: None, url_search: None, show_nsfw: true, @@ -206,6 +208,11 @@ impl<'a> PostQueryBuilder<'a> { self } + pub fn for_community_name>(mut self, for_community_name: T) -> Self { + self.for_community_name = for_community_name.get_optional(); + self + } + pub fn for_creator_id>(mut self, for_creator_id: T) -> Self { self.for_creator_id = for_creator_id.get_optional(); self @@ -265,6 +272,11 @@ impl<'a> PostQueryBuilder<'a> { query = query.then_order_by(stickied.desc()); } + if let Some(for_community_name) = self.for_community_name { + query = query.filter(community_name.eq(for_community_name)); + query = query.then_order_by(stickied.desc()); + } + if let Some(url_search) = self.url_search { query = query.filter(url.eq(url_search)); } diff --git a/server/lemmy_utils/src/lib.rs b/server/lemmy_utils/src/lib.rs index bbee8500..d88335e2 100644 --- a/server/lemmy_utils/src/lib.rs +++ b/server/lemmy_utils/src/lib.rs @@ -167,8 +167,8 @@ mod tests { use crate::{ is_email_regex, is_valid_community_name, - is_valid_username, is_valid_post_title, + is_valid_username, remove_slurs, scrape_text_for_mentions, slur_check, @@ -216,8 +216,6 @@ mod tests { assert!(!is_valid_post_title("\n \n \n \n ")); // tabs/spaces/newlines } - - #[test] fn test_slur_filter() { let test = diff --git a/server/src/api/post.rs b/server/src/api/post.rs index cbdb976c..6710a2cd 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -28,7 +28,13 @@ use lemmy_db::{ Saveable, SortType, }; -use lemmy_utils::{is_valid_post_title, make_apub_endpoint, slur_check, slurs_vec_to_str, EndpointType}; +use lemmy_utils::{ + is_valid_post_title, + make_apub_endpoint, + slur_check, + slurs_vec_to_str, + EndpointType, +}; use serde::{Deserialize, Serialize}; use std::str::FromStr; @@ -70,6 +76,7 @@ pub struct GetPosts { page: Option, limit: Option, pub community_id: Option, + pub community_name: Option, auth: Option, } @@ -369,12 +376,14 @@ impl Perform for Oper { let page = data.page; let limit = data.limit; let community_id = data.community_id; + let community_name = data.community_name.to_owned(); let posts = match blocking(pool, move |conn| { PostQueryBuilder::create(conn) .listing_type(type_) .sort(&sort) .show_nsfw(show_nsfw) .for_community_id(community_id) + .for_community_name(community_name) .my_user_id(user_id) .page(page) .limit(limit)