]> Untitled Git - lemmy.git/commitdiff
Adding a community_name option to GetPosts /post/list . Fixes #800 (#942)
authorDessalines <dessalines@users.noreply.github.com>
Mon, 13 Jul 2020 13:50:13 +0000 (09:50 -0400)
committerGitHub <noreply@github.com>
Mon, 13 Jul 2020 13:50:13 +0000 (09:50 -0400)
docs/src/contributing_websocket_http_api.md
server/lemmy_db/src/post_view.rs
server/lemmy_utils/src/lib.rs
server/src/api/post.rs

index 567f674cd9eb6a8277155e30e855f6c995d316cc..6ed25b98ebe09290185a3d5d8cb48e287ac70a93 100644 (file)
@@ -1149,6 +1149,7 @@ Post listing types are `All, Subscribed, Community`
     page: Option<i64>,
     limit: Option<i64>,
     community_id: Option<i32>,
+    community_name: Option<String>,
     auth: Option<String>
   }
 }
index b55359ea332b0204bd640eb092f0a3cfd0b31244..3e9f8737676891d4cb5a07ce4c69a7379479c1ce 100644 (file)
@@ -158,6 +158,7 @@ pub struct PostQueryBuilder<'a> {
   my_user_id: Option<i32>,
   for_creator_id: Option<i32>,
   for_community_id: Option<i32>,
+  for_community_name: Option<String>,
   search_term: Option<String>,
   url_search: Option<String>,
   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<T: MaybeOptional<String>>(mut self, for_community_name: T) -> Self {
+    self.for_community_name = for_community_name.get_optional();
+    self
+  }
+
   pub fn for_creator_id<T: MaybeOptional<i32>>(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));
     }
index bbee8500a5c42aa4e0c79663cb69469f1e214f47..d88335e2da5cf260b27fcdf91b769067ebfb9749 100644 (file)
@@ -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 =
index cbdb976c6a794ddc1f31facaadec04560acd6f80..6710a2cdf35d54bc239aeac54d40854b3eec946c 100644 (file)
@@ -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<i64>,
   limit: Option<i64>,
   pub community_id: Option<i32>,
+  pub community_name: Option<String>,
   auth: Option<String>,
 }
 
@@ -369,12 +376,14 @@ impl Perform for Oper<GetPosts> {
     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)