From: Lyra Date: Sat, 7 Dec 2019 12:10:10 +0000 (+0100) Subject: Resolved conflict with master X-Git-Url: http://these/git/%22https:/nerdica.net/static/%24%7Bk%5B1%5D.custom_emoji.image_url%7D?a=commitdiff_plain;h=3123d308d092232dc7c099b4285fab94b9cc8dbc;p=lemmy.git Resolved conflict with master --- 3123d308d092232dc7c099b4285fab94b9cc8dbc diff --cc server/src/db/post_view.rs index 23454b6e,e4e3c16d..cd807847 --- a/server/src/db/post_view.rs +++ b/server/src/db/post_view.rs @@@ -75,19 -73,16 +75,20 @@@ 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, ++ for_creator_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, @@@ -123,137 -168,11 +124,148 @@@ .then_order_by(score.desc()), }; - query = query.limit(limit).offset(offset); + 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, ++ for_creator_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.for_creator_id = Some(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()) + }; + ++ // 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)); ++ } else { ++ self.query = self.query ++ .filter(removed.eq(false)) ++ .filter(deleted.eq(false)) ++ .filter(community_removed.eq(false)) ++ .filter(community_deleted.eq(false)); ++ } + - query.load::(conn) + let (limit, offset) = limit_and_offset(self.page, self.limit); + let query = self + .query + .limit(limit) + .offset(offset) + .filter(removed.eq(false)) + .filter(deleted.eq(false)) + .filter(community_removed.eq(false)) + .filter(community_deleted.eq(false)); + + query.load::(self.conn) } +} +impl PostView { pub fn read( conn: &PgConnection, from_post_id: i32,