From 9fc8aacc2d96cce358d98bd3989c0db4785f3512 Mon Sep 17 00:00:00 2001 From: Lyra Date: Sat, 7 Dec 2019 23:54:42 +0100 Subject: [PATCH] Add default options for all parameters and move them out of the constructor. Rename structure to PostQueryBuilder --- server/src/api/post.rs | 5 +- server/src/api/site.rs | 12 ++- server/src/api/user.rs | 24 ++--- server/src/db/post_view.rs | 192 ++++++++++++++++++--------------- server/src/feeds.rs | 8 +- server/src/websocket/server.rs | 17 ++- 6 files changed, 141 insertions(+), 117 deletions(-) diff --git a/server/src/api/post.rs b/server/src/api/post.rs index e507aba3..e1c9b778 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -238,7 +238,10 @@ impl Perform for Oper { 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) diff --git a/server/src/api/site.rs b/server/src/api/site.rs index 8ed5fc25..0e780b38 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -319,7 +319,9 @@ impl Perform for Oper { 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 for Oper { 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 for Oper { 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) diff --git a/server/src/api/user.rs b/server/src/api/user.rs index da077254..8a508e2a 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -366,18 +366,14 @@ impl Perform for Oper { 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 for Oper { } // 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()?; diff --git a/server/src/db/post_view.rs b/server/src/db/post_view.rs index cd807847..cdf75554 100644 --- a/server/src/db/post_view.rs +++ b/server/src/db/post_view.rs @@ -75,80 +75,51 @@ pub struct PostView { pub saved: Option, } -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, for_creator_id: Option, + show_nsfw: bool, + saved_only: bool, + unread_only: bool, page: Option, limit: Option, } -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(); - - match r#type { - ListingType::Subscribed => { - query = query.filter(subscribed.eq(true)); - } - _ => {} - }; + let query = post_view.into_boxed(); - 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, Error> { + pub fn list(self) -> Result, 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_no_user = PostViewQuery::create( - &conn, - ListingType::Community, - &SortType::New, - false, - false, - false, - ) - .for_community_id(inserted_community.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 = 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 = diff --git a/server/src/feeds.rs b/server/src/feeds.rs index 732e85cf..19dd1121 100644 --- a/server/src/feeds.rs +++ b/server/src/feeds.rs @@ -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()?; diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 89172e56..86a929c9 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -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); -- 2.44.1