]> Untitled Git - lemmy.git/blobdiff - crates/db_views_actor/src/comment_reply_view.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_views_actor / src / comment_reply_view.rs
index 611f890644d5ca46ea0471e955b4ae24ae0df61d..39ba74ccb2e6dd50fe7b44c4a9671498b1cef057 100644 (file)
@@ -13,7 +13,6 @@ use lemmy_db_schema::{
     community_follower,
     community_person_ban,
     person,
-    person_alias_1,
     person_block,
     post,
   },
@@ -21,14 +20,15 @@ use lemmy_db_schema::{
     comment::{Comment, CommentSaved},
     comment_reply::CommentReply,
     community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
-    person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
+    person::{Person, PersonSafe},
     person_block::PersonBlock,
     post::Post,
   },
-  traits::{MaybeOptional, ToSafe, ViewToVec},
+  traits::{ToSafe, ViewToVec},
   utils::{functions::hot_rank, limit_and_offset},
   CommentSortType,
 };
+use typed_builder::TypedBuilder;
 
 type CommentReplyViewTuple = (
   CommentReply,
@@ -36,7 +36,7 @@ type CommentReplyViewTuple = (
   PersonSafe,
   Post,
   CommunitySafe,
-  PersonSafeAlias1,
+  PersonSafe,
   CommentAggregates,
   Option<CommunityPersonBan>,
   Option<CommunityFollower>,
@@ -47,10 +47,12 @@ type CommentReplyViewTuple = (
 
 impl CommentReplyView {
   pub fn read(
-    conn: &PgConnection,
+    conn: &mut PgConnection,
     comment_reply_id: CommentReplyId,
     my_person_id: Option<PersonId>,
   ) -> Result<Self, Error> {
+    let person_alias_1 = diesel::alias!(person as person1);
+
     // The left join below will return None in this case
     let person_id_join = my_person_id.unwrap_or(PersonId(-1));
 
@@ -73,7 +75,7 @@ impl CommentReplyView {
       .inner_join(person::table.on(comment::creator_id.eq(person::id)))
       .inner_join(post::table.on(comment::post_id.eq(post::id)))
       .inner_join(community::table.on(post::community_id.eq(community::id)))
-      .inner_join(person_alias_1::table)
+      .inner_join(person_alias_1)
       .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
       .left_join(
         community_person_ban::table.on(
@@ -121,7 +123,7 @@ impl CommentReplyView {
         Person::safe_columns_tuple(),
         post::all_columns,
         Community::safe_columns_tuple(),
-        PersonAlias1::safe_columns_tuple(),
+        person_alias_1.fields(Person::safe_columns_tuple()),
         comment_aggregates::all_columns,
         community_person_ban::all_columns.nullable(),
         community_follower::all_columns.nullable(),
@@ -148,7 +150,7 @@ impl CommentReplyView {
   }
 
   /// Gets the number of unread replies
-  pub fn get_unread_replies(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
+  pub fn get_unread_replies(conn: &mut PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
     use diesel::dsl::*;
 
     comment_reply::table
@@ -159,8 +161,11 @@ impl CommentReplyView {
   }
 }
 
-pub struct CommentReplyQueryBuilder<'a> {
-  conn: &'a PgConnection,
+#[derive(TypedBuilder)]
+#[builder(field_defaults(default))]
+pub struct CommentReplyQuery<'a> {
+  #[builder(!default)]
+  conn: &'a mut PgConnection,
   my_person_id: Option<PersonId>,
   recipient_id: Option<PersonId>,
   sort: Option<CommentSortType>,
@@ -170,58 +175,12 @@ pub struct CommentReplyQueryBuilder<'a> {
   limit: Option<i64>,
 }
 
-impl<'a> CommentReplyQueryBuilder<'a> {
-  pub fn create(conn: &'a PgConnection) -> Self {
-    CommentReplyQueryBuilder {
-      conn,
-      my_person_id: None,
-      recipient_id: None,
-      sort: None,
-      unread_only: None,
-      show_bot_accounts: None,
-      page: None,
-      limit: None,
-    }
-  }
-
-  pub fn sort<T: MaybeOptional<CommentSortType>>(mut self, sort: T) -> Self {
-    self.sort = sort.get_optional();
-    self
-  }
-
-  pub fn unread_only<T: MaybeOptional<bool>>(mut self, unread_only: T) -> Self {
-    self.unread_only = unread_only.get_optional();
-    self
-  }
-
-  pub fn show_bot_accounts<T: MaybeOptional<bool>>(mut self, show_bot_accounts: T) -> Self {
-    self.show_bot_accounts = show_bot_accounts.get_optional();
-    self
-  }
-
-  pub fn recipient_id<T: MaybeOptional<PersonId>>(mut self, recipient_id: T) -> Self {
-    self.recipient_id = recipient_id.get_optional();
-    self
-  }
-
-  pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
-    self.my_person_id = my_person_id.get_optional();
-    self
-  }
-
-  pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
-    self.page = page.get_optional();
-    self
-  }
-
-  pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
-    self.limit = limit.get_optional();
-    self
-  }
-
+impl<'a> CommentReplyQuery<'a> {
   pub fn list(self) -> Result<Vec<CommentReplyView>, Error> {
     use diesel::dsl::*;
 
+    let person_alias_1 = diesel::alias!(person as person1);
+
     // The left join below will return None in this case
     let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
 
@@ -230,7 +189,7 @@ impl<'a> CommentReplyQueryBuilder<'a> {
       .inner_join(person::table.on(comment::creator_id.eq(person::id)))
       .inner_join(post::table.on(comment::post_id.eq(post::id)))
       .inner_join(community::table.on(post::community_id.eq(community::id)))
-      .inner_join(person_alias_1::table)
+      .inner_join(person_alias_1)
       .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
       .left_join(
         community_person_ban::table.on(
@@ -278,7 +237,7 @@ impl<'a> CommentReplyQueryBuilder<'a> {
         Person::safe_columns_tuple(),
         post::all_columns,
         Community::safe_columns_tuple(),
-        PersonAlias1::safe_columns_tuple(),
+        person_alias_1.fields(Person::safe_columns_tuple()),
         comment_aggregates::all_columns,
         community_person_ban::all_columns.nullable(),
         community_follower::all_columns.nullable(),