From bff3183a2701257ce085a76d9d5d327f80ccec37 Mon Sep 17 00:00:00 2001 From: Lyra Date: Sat, 7 Dec 2019 13:03:03 +0100 Subject: [PATCH 1/2] Use a dedicated structure in order to search posts --- server/src/api/post.rs | 22 ++-- server/src/api/site.rs | 64 +++------- server/src/api/user.rs | 71 ++++------- server/src/db/post_view.rs | 221 ++++++++++++++++++++++----------- server/src/feeds.rs | 21 +--- server/src/websocket/server.rs | 16 ++- 6 files changed, 209 insertions(+), 206 deletions(-) diff --git a/server/src/api/post.rs b/server/src/api/post.rs index 5df42990a..e507aba33 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -238,21 +238,13 @@ impl Perform for Oper { let type_ = ListingType::from_str(&data.type_)?; let sort = SortType::from_str(&data.sort)?; - let posts = match PostView::list( - &conn, - type_, - &sort, - data.community_id, - None, - None, - None, - user_id, - show_nsfw, - false, - false, - data.page, - data.limit, - ) { + let posts = match PostViewQuery::create(&conn, type_, &sort, show_nsfw, false, false) + .for_community_id_optional(data.community_id) + .my_user_id_optional(user_id) + .page_optional(data.page) + .limit_optional(data.limit) + .list() + { Ok(posts) => posts, Err(_e) => return Err(APIError::err(&self.op, "couldnt_get_posts"))?, }; diff --git a/server/src/api/site.rs b/server/src/api/site.rs index 618295fca..8ed5fc257 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -319,21 +319,12 @@ impl Perform for Oper { match type_ { SearchType::Posts => { - posts = PostView::list( - &conn, - ListingType::All, - &sort, - data.community_id, - None, - Some(data.q.to_owned()), - None, - None, - true, - false, - false, - data.page, - data.limit, - )?; + posts = PostViewQuery::create(&conn, ListingType::All, &sort, true, false, false) + .for_community_id_optional(data.community_id) + .search_term(data.q.to_owned()) + .page_optional(data.page) + .limit_optional(data.limit) + .list()?; } SearchType::Comments => { comments = CommentView::list( @@ -363,21 +354,13 @@ impl Perform for Oper { users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?; } SearchType::All => { - posts = PostView::list( - &conn, - ListingType::All, - &sort, - data.community_id, - None, - Some(data.q.to_owned()), - None, - None, - true, - false, - false, - data.page, - data.limit, - )?; + posts = PostViewQuery::create(&conn, ListingType::All, &sort, true, false, false) + .for_community_id_optional(data.community_id) + .search_term(data.q.to_owned()) + .page_optional(data.page) + .limit_optional(data.limit) + .list()?; + comments = CommentView::list( &conn, &sort, @@ -401,21 +384,12 @@ impl Perform for Oper { users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?; } SearchType::Url => { - posts = PostView::list( - &conn, - ListingType::All, - &sort, - data.community_id, - None, - None, - Some(data.q.to_owned()), - None, - true, - false, - false, - data.page, - data.limit, - )?; + posts = PostViewQuery::create(&conn, ListingType::All, &sort, true, false, false) + .for_community_id_optional(data.community_id) + .url_search(data.q.to_owned()) + .page_optional(data.page) + .limit_optional(data.limit) + .list()?; } }; diff --git a/server/src/api/user.rs b/server/src/api/user.rs index e15563b0c..da0772546 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -366,40 +366,26 @@ 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); + // If its saved only, you don't care what creator it was - let posts = if data.saved_only { - PostView::list( - &conn, - ListingType::All, - &sort, - data.community_id, - None, - None, - None, - Some(user_details_id), - show_nsfw, - data.saved_only, - false, - data.page, - data.limit, - )? - } else { - PostView::list( - &conn, - ListingType::All, - &sort, - data.community_id, - Some(user_details_id), - None, - None, - user_id, - show_nsfw, - data.saved_only, - false, - data.page, - data.limit, - )? - }; + if !data.saved_only { + posts_query = posts_query.for_creator_id(user_details_id); + } + + let posts = posts_query.list()?; + let comments = if data.saved_only { CommentView::list( &conn, @@ -777,21 +763,10 @@ impl Perform for Oper { } // Posts - let posts = PostView::list( - &conn, - ListingType::All, - &SortType::New, - None, - Some(user_id), - None, - None, - None, - true, - false, - false, - None, - Some(std::i64::MAX), - )?; + let posts = PostViewQuery::create(&conn, ListingType::All, &SortType::New, true, false, false) + .for_creator_id(user_id) + .limit(std::i64::MAX) + .list()?; for post in &posts { let post_form = PostForm { diff --git a/server/src/db/post_view.rs b/server/src/db/post_view.rs index 51dea027f..23454b6e6 100644 --- a/server/src/db/post_view.rs +++ b/server/src/db/post_view.rs @@ -1,4 +1,6 @@ +use super::post_view::post_view::BoxedQuery; use super::*; +use diesel::pg::Pg; // The faked schema since diesel doesn't do views table! { @@ -73,74 +75,34 @@ pub struct PostView { pub saved: Option, } -impl PostView { - pub fn list( - conn: &PgConnection, - type_: ListingType, - sort: &SortType, - for_community_id: Option, - for_creator_id: Option, - search_term: Option, - url_search: Option, - my_user_id: Option, +pub struct PostViewQuery<'a> { + conn: &'a PgConnection, + query: BoxedQuery<'a, Pg>, + my_user_id: Option, + 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, - page: Option, - limit: Option, - ) -> Result, Error> { + ) -> Self { use super::post_view::post_view::dsl::*; - let (limit, offset) = limit_and_offset(page, limit); - let mut query = post_view.into_boxed(); - if let Some(for_creator_id) = for_creator_id { - query = query.filter(creator_id.eq(for_creator_id)); - }; - - if let Some(search_term) = search_term { - query = query.filter(name.ilike(fuzzy_search(&search_term))); - }; - - if let Some(url_search) = url_search { - query = query.filter(url.eq(url_search)); - }; - - if let Some(for_community_id) = for_community_id { - query = query.filter(community_id.eq(for_community_id)); - query = query.then_order_by(stickied.desc()); - }; - - // 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)); - }; - - match type_ { + match r#type { ListingType::Subscribed => { query = query.filter(subscribed.eq(true)); } _ => {} }; - // The view lets you pass a null user_id, if you're not logged in - if let Some(my_user_id) = my_user_id { - query = query.filter(user_id.eq(my_user_id)); - } else { - query = query.filter(user_id.is_null()); - } - - if !show_nsfw { - query = query - .filter(nsfw.eq(false)) - .filter(community_nsfw.eq(false)); - }; - query = match sort { SortType::Hot => query .then_order_by(hot_rank.desc()) @@ -161,7 +123,125 @@ impl PostView { .then_order_by(score.desc()), }; - query = query + 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, + query, + my_user_id: None, + page: None, + limit: None, + } + } + + 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)); + self.query = self.query.then_order_by(stickied.desc()); + self + } + + pub fn for_community_id_optional(self, for_community_id: Option) -> Self { + match for_community_id { + Some(for_community_id) => self.for_community_id(for_community_id), + None => self, + } + } + + pub fn for_creator_id(mut self, for_creator_id: i32) -> Self { + use super::post_view::post_view::dsl::*; + self.query = self.query.filter(creator_id.eq(for_creator_id)); + self + } + + pub fn for_creator_id_optional(self, for_creator_id: Option) -> Self { + match for_creator_id { + Some(for_creator_id) => self.for_creator_id(for_creator_id), + None => self, + } + } + + pub fn search_term(mut self, search_term: String) -> Self { + use super::post_view::post_view::dsl::*; + self.query = self.query.filter(name.ilike(fuzzy_search(&search_term))); + self + } + + pub fn search_term_optional(self, search_term: Option) -> Self { + match search_term { + Some(search_term) => self.search_term(search_term), + None => self, + } + } + + pub fn url_search(mut self, url_search: String) -> Self { + use super::post_view::post_view::dsl::*; + self.query = self.query.filter(url.eq(url_search)); + self + } + + pub fn url_search_optional(self, url_search: Option) -> Self { + match url_search { + Some(url_search) => self.url_search(url_search), + None => self, + } + } + + pub fn my_user_id(mut self, my_user_id: i32) -> Self { + self.my_user_id = Some(my_user_id); + self + } + + pub fn my_user_id_optional(mut self, my_user_id: Option) -> Self { + self.my_user_id = my_user_id; + self + } + + pub fn page(mut self, page: i64) -> Self { + self.page = Some(page); + self + } + + pub fn page_optional(mut self, page: Option) -> Self { + self.page = page; + self + } + + pub fn limit(mut self, limit: i64) -> Self { + self.limit = Some(limit); + self + } + + pub fn limit_optional(mut self, limit: Option) -> Self { + self.limit = limit; + self + } + + pub fn list(mut self) -> Result, Error> { + use super::post_view::post_view::dsl::*; + // 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)) + } else { + self.query.filter(user_id.is_null()) + }; + + let (limit, offset) = limit_and_offset(self.page, self.limit); + let query = self + .query .limit(limit) .offset(offset) .filter(removed.eq(false)) @@ -169,9 +249,11 @@ impl PostView { .filter(community_removed.eq(false)) .filter(community_deleted.eq(false)); - query.load::(conn) + query.load::(self.conn) } +} +impl PostView { pub fn read( conn: &PgConnection, from_post_id: i32, @@ -344,38 +426,31 @@ mod tests { nsfw: false, }; - let read_post_listings_with_user = PostView::list( + let read_post_listings_with_user = PostViewQuery::create( &conn, ListingType::Community, &SortType::New, - Some(inserted_community.id), - None, - None, - None, - Some(inserted_user.id), false, false, false, - None, - None, ) + .for_community_id(inserted_community.id) + .my_user_id(inserted_user.id) + .list() .unwrap(); - let read_post_listings_no_user = PostView::list( + + let read_post_listings_no_user = PostViewQuery::create( &conn, ListingType::Community, &SortType::New, - Some(inserted_community.id), - None, - None, - None, - None, false, false, false, - None, - None, ) + .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 = PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap(); diff --git a/server/src/feeds.rs b/server/src/feeds.rs index 8a1db28bc..732e85cf5 100644 --- a/server/src/feeds.rs +++ b/server/src/feeds.rs @@ -4,7 +4,7 @@ extern crate rss; use super::*; use crate::db::community::Community; use crate::db::community_view::SiteView; -use crate::db::post_view::PostView; +use crate::db::post_view::PostViewQuery; use crate::db::user::User_; use crate::db::{establish_connection, ListingType, SortType}; use crate::Settings; @@ -124,21 +124,10 @@ fn get_feed_internal( } } - let posts = PostView::list( - &conn, - ListingType::All, - sort_type, - community_id, - creator_id, - None, - None, - None, - true, - false, - false, - None, - None, - )?; + let posts = PostViewQuery::create(&conn, ListingType::All, sort_type, true, false, false) + .for_community_id_optional(community_id) + .for_creator_id_optional(creator_id) + .list()?; let mut items: Vec = Vec::new(); diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 5bcca2976..89172e560 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -134,21 +134,19 @@ impl ChatServer { use crate::db::post_view::*; use crate::db::*; let conn = establish_connection(); - let posts = PostView::list( + + let posts = PostViewQuery::create( &conn, ListingType::Community, &SortType::New, - Some(*community_id), - None, - None, - None, - None, false, false, false, - None, - Some(9999), - )?; + ) + .for_community_id(*community_id) + .limit(9999) + .list()?; + for post in posts { self.send_room_message(&post.id, message, skip_id); } From 9fc8aacc2d96cce358d98bd3989c0db4785f3512 Mon Sep 17 00:00:00 2001 From: Lyra Date: Sat, 7 Dec 2019 23:54:42 +0100 Subject: [PATCH 2/2] 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 | 190 ++++++++++++++++++--------------- server/src/feeds.rs | 8 +- server/src/websocket/server.rs | 17 ++- 6 files changed, 140 insertions(+), 116 deletions(-) diff --git a/server/src/api/post.rs b/server/src/api/post.rs index e507aba33..e1c9b778f 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 8ed5fc257..0e780b384 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 da0772546..8a508e2a0 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 cd8078475..cdf755545 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(); + 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, 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_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 = diff --git a/server/src/feeds.rs b/server/src/feeds.rs index 732e85cf5..19dd1121d 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 89172e560..86a929c94 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);