Add default options for all parameters and move them out of the constructor. Rename structure to PostQueryBuilder
This commit is contained in:
parent
75bced5d49
commit
b14f55a2f5
6 changed files with 140 additions and 116 deletions
|
@ -238,7 +238,10 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
|
||||||
let type_ = ListingType::from_str(&data.type_)?;
|
let type_ = ListingType::from_str(&data.type_)?;
|
||||||
let sort = SortType::from_str(&data.sort)?;
|
let sort = SortType::from_str(&data.sort)?;
|
||||||
|
|
||||||
let posts = match PostViewQuery::create(&conn, type_, &sort, show_nsfw, false, false)
|
let posts = match PostQueryBuilder::create(&conn)
|
||||||
|
.listing_type(type_)
|
||||||
|
.sort(&sort)
|
||||||
|
.show_nsfw(show_nsfw)
|
||||||
.for_community_id_optional(data.community_id)
|
.for_community_id_optional(data.community_id)
|
||||||
.my_user_id_optional(user_id)
|
.my_user_id_optional(user_id)
|
||||||
.page_optional(data.page)
|
.page_optional(data.page)
|
||||||
|
|
|
@ -319,7 +319,9 @@ impl Perform<SearchResponse> for Oper<Search> {
|
||||||
|
|
||||||
match type_ {
|
match type_ {
|
||||||
SearchType::Posts => {
|
SearchType::Posts => {
|
||||||
posts = PostViewQuery::create(&conn, ListingType::All, &sort, true, false, false)
|
posts = PostQueryBuilder::create(&conn)
|
||||||
|
.sort(&sort)
|
||||||
|
.show_nsfw(true)
|
||||||
.for_community_id_optional(data.community_id)
|
.for_community_id_optional(data.community_id)
|
||||||
.search_term(data.q.to_owned())
|
.search_term(data.q.to_owned())
|
||||||
.page_optional(data.page)
|
.page_optional(data.page)
|
||||||
|
@ -354,7 +356,9 @@ impl Perform<SearchResponse> for Oper<Search> {
|
||||||
users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
|
users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
|
||||||
}
|
}
|
||||||
SearchType::All => {
|
SearchType::All => {
|
||||||
posts = PostViewQuery::create(&conn, ListingType::All, &sort, true, false, false)
|
posts = PostQueryBuilder::create(&conn)
|
||||||
|
.sort(&sort)
|
||||||
|
.show_nsfw(true)
|
||||||
.for_community_id_optional(data.community_id)
|
.for_community_id_optional(data.community_id)
|
||||||
.search_term(data.q.to_owned())
|
.search_term(data.q.to_owned())
|
||||||
.page_optional(data.page)
|
.page_optional(data.page)
|
||||||
|
@ -384,7 +388,9 @@ impl Perform<SearchResponse> for Oper<Search> {
|
||||||
users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
|
users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
|
||||||
}
|
}
|
||||||
SearchType::Url => {
|
SearchType::Url => {
|
||||||
posts = PostViewQuery::create(&conn, ListingType::All, &sort, true, false, false)
|
posts = PostQueryBuilder::create(&conn)
|
||||||
|
.sort(&sort)
|
||||||
|
.show_nsfw(true)
|
||||||
.for_community_id_optional(data.community_id)
|
.for_community_id_optional(data.community_id)
|
||||||
.url_search(data.q.to_owned())
|
.url_search(data.q.to_owned())
|
||||||
.page_optional(data.page)
|
.page_optional(data.page)
|
||||||
|
|
|
@ -366,14 +366,10 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
|
||||||
|
|
||||||
let user_view = UserView::read(&conn, user_details_id)?;
|
let user_view = UserView::read(&conn, user_details_id)?;
|
||||||
|
|
||||||
let mut posts_query = PostViewQuery::create(
|
let mut posts_query = PostQueryBuilder::create(&conn)
|
||||||
&conn,
|
.sort(&sort)
|
||||||
ListingType::All,
|
.show_nsfw(show_nsfw)
|
||||||
&sort,
|
.saved_only(data.saved_only)
|
||||||
show_nsfw,
|
|
||||||
data.saved_only,
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
.for_community_id_optional(data.community_id)
|
.for_community_id_optional(data.community_id)
|
||||||
.my_user_id_optional(user_id)
|
.my_user_id_optional(user_id)
|
||||||
.page_optional(data.page)
|
.page_optional(data.page)
|
||||||
|
@ -763,7 +759,9 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Posts
|
// Posts
|
||||||
let posts = PostViewQuery::create(&conn, ListingType::All, &SortType::New, true, false, false)
|
let posts = PostQueryBuilder::create(&conn)
|
||||||
|
.sort(&SortType::New)
|
||||||
|
.show_nsfw(true)
|
||||||
.for_creator_id(user_id)
|
.for_creator_id(user_id)
|
||||||
.limit(std::i64::MAX)
|
.limit(std::i64::MAX)
|
||||||
.list()?;
|
.list()?;
|
||||||
|
|
|
@ -75,80 +75,51 @@ pub struct PostView {
|
||||||
pub saved: Option<bool>,
|
pub saved: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PostViewQuery<'a> {
|
pub struct PostQueryBuilder<'a> {
|
||||||
conn: &'a PgConnection,
|
conn: &'a PgConnection,
|
||||||
query: BoxedQuery<'a, Pg>,
|
query: BoxedQuery<'a, Pg>,
|
||||||
|
listing_type: ListingType,
|
||||||
|
sort: &'a SortType,
|
||||||
my_user_id: Option<i32>,
|
my_user_id: Option<i32>,
|
||||||
for_creator_id: Option<i32>,
|
for_creator_id: Option<i32>,
|
||||||
|
show_nsfw: bool,
|
||||||
|
saved_only: bool,
|
||||||
|
unread_only: bool,
|
||||||
page: Option<i64>,
|
page: Option<i64>,
|
||||||
limit: Option<i64>,
|
limit: Option<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PostViewQuery<'a> {
|
impl<'a> PostQueryBuilder<'a> {
|
||||||
pub fn create(
|
pub fn create(conn: &'a PgConnection) -> Self {
|
||||||
conn: &'a PgConnection,
|
|
||||||
r#type: ListingType,
|
|
||||||
sort: &'a SortType,
|
|
||||||
show_nsfw: bool,
|
|
||||||
saved_only: bool,
|
|
||||||
unread_only: bool,
|
|
||||||
) -> Self {
|
|
||||||
use super::post_view::post_view::dsl::*;
|
use super::post_view::post_view::dsl::*;
|
||||||
|
|
||||||
let mut query = post_view.into_boxed();
|
let query = post_view.into_boxed();
|
||||||
|
|
||||||
match r#type {
|
PostQueryBuilder {
|
||||||
ListingType::Subscribed => {
|
|
||||||
query = query.filter(subscribed.eq(true));
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
};
|
|
||||||
|
|
||||||
query = match sort {
|
|
||||||
SortType::Hot => query
|
|
||||||
.then_order_by(hot_rank.desc())
|
|
||||||
.then_order_by(published.desc()),
|
|
||||||
SortType::New => query.then_order_by(published.desc()),
|
|
||||||
SortType::TopAll => query.then_order_by(score.desc()),
|
|
||||||
SortType::TopYear => query
|
|
||||||
.filter(published.gt(now - 1.years()))
|
|
||||||
.then_order_by(score.desc()),
|
|
||||||
SortType::TopMonth => query
|
|
||||||
.filter(published.gt(now - 1.months()))
|
|
||||||
.then_order_by(score.desc()),
|
|
||||||
SortType::TopWeek => query
|
|
||||||
.filter(published.gt(now - 1.weeks()))
|
|
||||||
.then_order_by(score.desc()),
|
|
||||||
SortType::TopDay => query
|
|
||||||
.filter(published.gt(now - 1.days()))
|
|
||||||
.then_order_by(score.desc()),
|
|
||||||
};
|
|
||||||
|
|
||||||
if !show_nsfw {
|
|
||||||
query = query
|
|
||||||
.filter(nsfw.eq(false))
|
|
||||||
.filter(community_nsfw.eq(false));
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
|
|
||||||
if saved_only {
|
|
||||||
query = query.filter(saved.eq(true));
|
|
||||||
};
|
|
||||||
|
|
||||||
if unread_only {
|
|
||||||
query = query.filter(read.eq(false));
|
|
||||||
};
|
|
||||||
|
|
||||||
PostViewQuery {
|
|
||||||
conn,
|
conn,
|
||||||
query,
|
query,
|
||||||
my_user_id: None,
|
my_user_id: None,
|
||||||
for_creator_id: None,
|
for_creator_id: None,
|
||||||
|
listing_type: ListingType::All,
|
||||||
|
sort: &SortType::Hot,
|
||||||
|
show_nsfw: false,
|
||||||
|
saved_only: false,
|
||||||
|
unread_only: false,
|
||||||
page: None,
|
page: None,
|
||||||
limit: None,
|
limit: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn listing_type(mut self, listing_type: ListingType) -> Self {
|
||||||
|
self.listing_type = listing_type;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sort(mut self, sort: &'a SortType) -> Self {
|
||||||
|
self.sort = sort;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn for_community_id(mut self, for_community_id: i32) -> Self {
|
pub fn for_community_id(mut self, for_community_id: i32) -> Self {
|
||||||
use super::post_view::post_view::dsl::*;
|
use super::post_view::post_view::dsl::*;
|
||||||
self.query = self.query.filter(community_id.eq(for_community_id));
|
self.query = self.query.filter(community_id.eq(for_community_id));
|
||||||
|
@ -211,6 +182,21 @@ impl<'a> PostViewQuery<'a> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
|
||||||
|
self.show_nsfw = show_nsfw;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn saved_only(mut self, saved_only: bool) -> Self {
|
||||||
|
self.saved_only = saved_only;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unread_only(mut self, unread_only: bool) -> Self {
|
||||||
|
self.unread_only = unread_only;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn page(mut self, page: i64) -> Self {
|
pub fn page(mut self, page: i64) -> Self {
|
||||||
self.page = Some(page);
|
self.page = Some(page);
|
||||||
self
|
self
|
||||||
|
@ -231,29 +217,73 @@ impl<'a> PostViewQuery<'a> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list(mut self) -> Result<Vec<PostView>, Error> {
|
pub fn list(self) -> Result<Vec<PostView>, Error> {
|
||||||
use super::post_view::post_view::dsl::*;
|
use super::post_view::post_view::dsl::*;
|
||||||
|
|
||||||
|
let mut query = self.query;
|
||||||
|
|
||||||
|
match self.listing_type {
|
||||||
|
ListingType::Subscribed => {
|
||||||
|
query = query.filter(subscribed.eq(true));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
query = match self.sort {
|
||||||
|
SortType::Hot => query
|
||||||
|
.then_order_by(hot_rank.desc())
|
||||||
|
.then_order_by(published.desc()),
|
||||||
|
SortType::New => query.then_order_by(published.desc()),
|
||||||
|
SortType::TopAll => query.then_order_by(score.desc()),
|
||||||
|
SortType::TopYear => query
|
||||||
|
.filter(published.gt(now - 1.years()))
|
||||||
|
.then_order_by(score.desc()),
|
||||||
|
SortType::TopMonth => query
|
||||||
|
.filter(published.gt(now - 1.months()))
|
||||||
|
.then_order_by(score.desc()),
|
||||||
|
SortType::TopWeek => query
|
||||||
|
.filter(published.gt(now - 1.weeks()))
|
||||||
|
.then_order_by(score.desc()),
|
||||||
|
SortType::TopDay => query
|
||||||
|
.filter(published.gt(now - 1.days()))
|
||||||
|
.then_order_by(score.desc()),
|
||||||
|
};
|
||||||
|
|
||||||
// The view lets you pass a null user_id, if you're not logged in
|
// The view lets you pass a null user_id, if you're not logged in
|
||||||
self.query = if let Some(my_user_id) = self.my_user_id {
|
query = if let Some(my_user_id) = self.my_user_id {
|
||||||
self.query.filter(user_id.eq(my_user_id))
|
query.filter(user_id.eq(my_user_id))
|
||||||
} else {
|
} else {
|
||||||
self.query.filter(user_id.is_null())
|
query.filter(user_id.is_null())
|
||||||
};
|
};
|
||||||
|
|
||||||
// If its for a specific user, show the removed / deleted
|
// If its for a specific user, show the removed / deleted
|
||||||
if let Some(for_creator_id) = self.for_creator_id {
|
if let Some(for_creator_id) = self.for_creator_id {
|
||||||
self.query = self.query.filter(creator_id.eq(for_creator_id));
|
query = query.filter(creator_id.eq(for_creator_id));
|
||||||
} else {
|
} else {
|
||||||
self.query = self.query
|
query = query
|
||||||
.filter(removed.eq(false))
|
.filter(removed.eq(false))
|
||||||
.filter(deleted.eq(false))
|
.filter(deleted.eq(false))
|
||||||
.filter(community_removed.eq(false))
|
.filter(community_removed.eq(false))
|
||||||
.filter(community_deleted.eq(false));
|
.filter(community_deleted.eq(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !self.show_nsfw {
|
||||||
|
query = query
|
||||||
|
.filter(nsfw.eq(false))
|
||||||
|
.filter(community_nsfw.eq(false));
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
|
||||||
|
if self.saved_only {
|
||||||
|
query = query.filter(saved.eq(true));
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.unread_only {
|
||||||
|
query = query.filter(read.eq(false));
|
||||||
|
};
|
||||||
|
|
||||||
let (limit, offset) = limit_and_offset(self.page, self.limit);
|
let (limit, offset) = limit_and_offset(self.page, self.limit);
|
||||||
let query = self
|
query = query
|
||||||
.query
|
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.offset(offset)
|
.offset(offset)
|
||||||
.filter(removed.eq(false))
|
.filter(removed.eq(false))
|
||||||
|
@ -438,27 +468,17 @@ mod tests {
|
||||||
nsfw: false,
|
nsfw: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let read_post_listings_with_user = PostViewQuery::create(
|
let read_post_listings_with_user = PostQueryBuilder::create(&conn)
|
||||||
&conn,
|
.listing_type(ListingType::Community)
|
||||||
ListingType::Community,
|
.sort(&SortType::New)
|
||||||
&SortType::New,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
.for_community_id(inserted_community.id)
|
.for_community_id(inserted_community.id)
|
||||||
.my_user_id(inserted_user.id)
|
.my_user_id(inserted_user.id)
|
||||||
.list()
|
.list()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let read_post_listings_no_user = PostViewQuery::create(
|
let read_post_listings_no_user = PostQueryBuilder::create(&conn)
|
||||||
&conn,
|
.listing_type(ListingType::Community)
|
||||||
ListingType::Community,
|
.sort(&SortType::New)
|
||||||
&SortType::New,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
.for_community_id(inserted_community.id)
|
.for_community_id(inserted_community.id)
|
||||||
.list()
|
.list()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -4,9 +4,9 @@ extern crate rss;
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::db::community::Community;
|
use crate::db::community::Community;
|
||||||
use crate::db::community_view::SiteView;
|
use crate::db::community_view::SiteView;
|
||||||
use crate::db::post_view::PostViewQuery;
|
use crate::db::post_view::PostQueryBuilder;
|
||||||
use crate::db::user::User_;
|
use crate::db::user::User_;
|
||||||
use crate::db::{establish_connection, ListingType, SortType};
|
use crate::db::{establish_connection, SortType};
|
||||||
use crate::Settings;
|
use crate::Settings;
|
||||||
use actix_web::body::Body;
|
use actix_web::body::Body;
|
||||||
use actix_web::{web, HttpResponse, Result};
|
use actix_web::{web, HttpResponse, Result};
|
||||||
|
@ -124,7 +124,9 @@ fn get_feed_internal(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let posts = PostViewQuery::create(&conn, ListingType::All, sort_type, true, false, false)
|
let posts = PostQueryBuilder::create(&conn)
|
||||||
|
.sort(sort_type)
|
||||||
|
.show_nsfw(true)
|
||||||
.for_community_id_optional(community_id)
|
.for_community_id_optional(community_id)
|
||||||
.for_creator_id_optional(creator_id)
|
.for_creator_id_optional(creator_id)
|
||||||
.list()?;
|
.list()?;
|
||||||
|
|
|
@ -135,14 +135,9 @@ impl ChatServer {
|
||||||
use crate::db::*;
|
use crate::db::*;
|
||||||
let conn = establish_connection();
|
let conn = establish_connection();
|
||||||
|
|
||||||
let posts = PostViewQuery::create(
|
let posts = PostQueryBuilder::create(&conn)
|
||||||
&conn,
|
.listing_type(ListingType::Community)
|
||||||
ListingType::Community,
|
.sort(&SortType::New)
|
||||||
&SortType::New,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
.for_community_id(*community_id)
|
.for_community_id(*community_id)
|
||||||
.limit(9999)
|
.limit(9999)
|
||||||
.list()?;
|
.list()?;
|
||||||
|
|
Reference in a new issue