Adding a community_name option to GetPosts /post/list . Fixes #800 (#942)

This commit is contained in:
Dessalines 2020-07-13 09:50:13 -04:00 committed by GitHub
parent a7c1c472fe
commit 7556f8615f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 4 deletions

View 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>
}
}

View 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));
}

View 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 =

View 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)