Add default options for all parameters and move them out of the constructor. Rename structure to PostQueryBuilder

This commit is contained in:
Lyra 2019-12-07 23:54:42 +01:00
parent 75bced5d49
commit b14f55a2f5
6 changed files with 140 additions and 116 deletions

View file

@ -238,7 +238,10 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
let type_ = ListingType::from_str(&data.type_)?;
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)
.my_user_id_optional(user_id)
.page_optional(data.page)

View file

@ -319,7 +319,9 @@ impl Perform<SearchResponse> for Oper<Search> {
match type_ {
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)
.search_term(data.q.to_owned())
.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)?;
}
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)
.search_term(data.q.to_owned())
.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)?;
}
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)
.url_search(data.q.to_owned())
.page_optional(data.page)

View file

@ -366,18 +366,14 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
let user_view = UserView::read(&conn, user_details_id)?;
let mut posts_query = PostViewQuery::create(
&conn,
ListingType::All,
&sort,
show_nsfw,
data.saved_only,
false,
)
.for_community_id_optional(data.community_id)
.my_user_id_optional(user_id)
.page_optional(data.page)
.limit_optional(data.limit);
let mut posts_query = PostQueryBuilder::create(&conn)
.sort(&sort)
.show_nsfw(show_nsfw)
.saved_only(data.saved_only)
.for_community_id_optional(data.community_id)
.my_user_id_optional(user_id)
.page_optional(data.page)
.limit_optional(data.limit);
// If its saved only, you don't care what creator it was
if !data.saved_only {
@ -763,7 +759,9 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
}
// 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)
.limit(std::i64::MAX)
.list()?;

View file

@ -75,80 +75,51 @@ pub struct PostView {
pub saved: Option<bool>,
}
pub struct PostViewQuery<'a> {
pub struct PostQueryBuilder<'a> {
conn: &'a PgConnection,
query: BoxedQuery<'a, Pg>,
listing_type: ListingType,
sort: &'a SortType,
my_user_id: Option<i32>,
for_creator_id: Option<i32>,
show_nsfw: bool,
saved_only: bool,
unread_only: bool,
page: Option<i64>,
limit: Option<i64>,
}
impl<'a> PostViewQuery<'a> {
pub fn create(
conn: &'a PgConnection,
r#type: ListingType,
sort: &'a SortType,
show_nsfw: bool,
saved_only: bool,
unread_only: bool,
) -> Self {
impl<'a> PostQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection) -> Self {
use super::post_view::post_view::dsl::*;
let mut query = post_view.into_boxed();
let query = post_view.into_boxed();
match r#type {
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 {
PostQueryBuilder {
conn,
query,
my_user_id: None,
for_creator_id: None,
listing_type: ListingType::All,
sort: &SortType::Hot,
show_nsfw: false,
saved_only: false,
unread_only: false,
page: 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 {
use super::post_view::post_view::dsl::*;
self.query = self.query.filter(community_id.eq(for_community_id));
@ -211,6 +182,21 @@ impl<'a> PostViewQuery<'a> {
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 {
self.page = Some(page);
self
@ -231,29 +217,73 @@ impl<'a> PostViewQuery<'a> {
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::*;
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
self.query = if let Some(my_user_id) = self.my_user_id {
self.query.filter(user_id.eq(my_user_id))
query = if let Some(my_user_id) = self.my_user_id {
query.filter(user_id.eq(my_user_id))
} 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 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 {
self.query = self.query
query = query
.filter(removed.eq(false))
.filter(deleted.eq(false))
.filter(community_removed.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 query = self
.query
query = query
.limit(limit)
.offset(offset)
.filter(removed.eq(false))
@ -438,30 +468,20 @@ mod tests {
nsfw: false,
};
let read_post_listings_with_user = PostViewQuery::create(
&conn,
ListingType::Community,
&SortType::New,
false,
false,
false,
)
.for_community_id(inserted_community.id)
.my_user_id(inserted_user.id)
.list()
.unwrap();
let read_post_listings_with_user = PostQueryBuilder::create(&conn)
.listing_type(ListingType::Community)
.sort(&SortType::New)
.for_community_id(inserted_community.id)
.my_user_id(inserted_user.id)
.list()
.unwrap();
let read_post_listings_no_user = PostViewQuery::create(
&conn,
ListingType::Community,
&SortType::New,
false,
false,
false,
)
.for_community_id(inserted_community.id)
.list()
.unwrap();
let read_post_listings_no_user = PostQueryBuilder::create(&conn)
.listing_type(ListingType::Community)
.sort(&SortType::New)
.for_community_id(inserted_community.id)
.list()
.unwrap();
let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
let read_post_listing_with_user =

View file

@ -4,9 +4,9 @@ extern crate rss;
use super::*;
use crate::db::community::Community;
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::{establish_connection, ListingType, SortType};
use crate::db::{establish_connection, SortType};
use crate::Settings;
use actix_web::body::Body;
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_creator_id_optional(creator_id)
.list()?;

View file

@ -135,17 +135,12 @@ impl ChatServer {
use crate::db::*;
let conn = establish_connection();
let posts = PostViewQuery::create(
&conn,
ListingType::Community,
&SortType::New,
false,
false,
false,
)
.for_community_id(*community_id)
.limit(9999)
.list()?;
let posts = PostQueryBuilder::create(&conn)
.listing_type(ListingType::Community)
.sort(&SortType::New)
.for_community_id(*community_id)
.limit(9999)
.list()?;
for post in posts {
self.send_room_message(&post.id, message, skip_id);