]> Untitled Git - lemmy.git/commitdiff
Add default options for all parameters and move them out of the constructor. Rename...
authorLyra <teromene@teromene.fr>
Sat, 7 Dec 2019 22:54:42 +0000 (23:54 +0100)
committerLyra <teromene@teromene.fr>
Sat, 7 Dec 2019 22:54:42 +0000 (23:54 +0100)
server/src/api/post.rs
server/src/api/site.rs
server/src/api/user.rs
server/src/db/post_view.rs
server/src/feeds.rs
server/src/websocket/server.rs

index e507aba338d1ac2c59d7a0ed34dd86d7fa39c819..e1c9b778f92084d06b220401a60400f54bd33047 100644 (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)
index 8ed5fc257d8223672e4565ce8564c4d6637985bb..0e780b3843265863e6b781d9b3da76d47d72bf9b 100644 (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)
index da0772546766e450827b5e54ead9aa8972e81439..8a508e2a0f25f9aecac6bb8ac656f2c0c061b298 100644 (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()?;
index cd8078475d1876ef0f2fb1e1352212131534b9bf..cdf7555456984a44c80588c6a8d09577f9769c8c 100644 (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();
-
-    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<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_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 =
index 732e85cf54d73f7440913ebc39daaecc952f6116..19dd1121d5527771367a55b648b0690cfa7dfb17 100644 (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()?;
index 89172e560e0957c90c7a41e27c0fb63e1897b187..86a929c94c767c65ffcb1bf344f13907589be4cb 100644 (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);