]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/comment.rs
First pass at adding comment trees. (#2362)
[lemmy.git] / crates / db_schema / src / impls / comment.rs
index 908aed86f2148522a654cc01d22b1ceb7b9016a7..f3199c77fedc52ac5f04fa17a675393f608cb23c 100644 (file)
@@ -12,6 +12,7 @@ use crate::{
   utils::naive_now,
 };
 use diesel::{dsl::*, result::Error, *};
+use diesel_ltree::Ltree;
 use url::Url;
 
 impl Comment {
@@ -74,17 +75,6 @@ impl Comment {
       .get_results::<Self>(conn)
   }
 
-  pub fn update_read(
-    conn: &PgConnection,
-    comment_id: CommentId,
-    new_read: bool,
-  ) -> Result<Self, Error> {
-    use crate::schema::comment::dsl::*;
-    diesel::update(comment.find(comment_id))
-      .set(read.eq(new_read))
-      .get_result::<Self>(conn)
-  }
-
   pub fn update_content(
     conn: &PgConnection,
     comment_id: CommentId,
@@ -96,14 +86,71 @@ impl Comment {
       .get_result::<Self>(conn)
   }
 
-  pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Comment, Error> {
+  pub fn create(
+    conn: &PgConnection,
+    comment_form: &CommentForm,
+    parent_path: Option<&Ltree>,
+  ) -> Result<Comment, Error> {
     use crate::schema::comment::dsl::*;
-    insert_into(comment)
+
+    // Insert, to get the id
+    let inserted_comment = insert_into(comment)
       .values(comment_form)
       .on_conflict(ap_id)
       .do_update()
       .set(comment_form)
-      .get_result::<Self>(conn)
+      .get_result::<Self>(conn);
+
+    if let Ok(comment_insert) = inserted_comment {
+      let comment_id = comment_insert.id;
+
+      // You need to update the ltree column
+      let ltree = Ltree(if let Some(parent_path) = parent_path {
+        // The previous parent will already have 0 in it
+        // Append this comment id
+        format!("{}.{}", parent_path.0, comment_id)
+      } else {
+        // '0' is always the first path, append to that
+        format!("{}.{}", 0, comment_id)
+      });
+
+      let updated_comment = diesel::update(comment.find(comment_id))
+        .set(path.eq(ltree))
+        .get_result::<Self>(conn);
+
+      // Update the child count for the parent comment_aggregates
+      // You could do this with a trigger, but since you have to do this manually anyway,
+      // you can just have it here
+      if let Some(parent_path) = parent_path {
+        // You have to update counts for all parents, not just the immediate one
+        // TODO if the performance of this is terrible, it might be better to do this as part of a
+        // scheduled query... although the counts would often be wrong.
+        //
+        // The child_count query for reference:
+        // select c.id, c.path, count(c2.id) as child_count from comment c
+        // left join comment c2 on c2.path <@ c.path and c2.path != c.path
+        // group by c.id
+
+        let top_parent = format!("0.{}", parent_path.0.split('.').collect::<Vec<&str>>()[1]);
+        let update_child_count_stmt = format!(
+          "
+update comment_aggregates ca set child_count = c.child_count
+from (
+  select c.id, c.path, count(c2.id) as child_count from comment c
+  join comment c2 on c2.path <@ c.path and c2.path != c.path
+  and c.path <@ '{}'
+  group by c.id
+) as c
+where ca.comment_id = c.id",
+          top_parent
+        );
+
+        sql_query(update_child_count_stmt).execute(conn)?;
+      }
+      updated_comment
+    } else {
+      inserted_comment
+    }
   }
   pub fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
     use crate::schema::comment::dsl::*;
@@ -116,6 +163,19 @@ impl Comment {
         .map(Into::into),
     )
   }
+
+  pub fn parent_comment_id(&self) -> Option<CommentId> {
+    let mut ltree_split: Vec<&str> = self.path.0.split('.').collect();
+    ltree_split.remove(0); // The first is always 0
+    if ltree_split.len() > 1 {
+      ltree_split[ltree_split.len() - 2]
+        .parse::<i32>()
+        .map(CommentId)
+        .ok()
+    } else {
+      None
+    }
+  }
 }
 
 impl Crud for Comment {
@@ -131,11 +191,9 @@ impl Crud for Comment {
     diesel::delete(comment.find(comment_id)).execute(conn)
   }
 
-  fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
-    use crate::schema::comment::dsl::*;
-    insert_into(comment)
-      .values(comment_form)
-      .get_result::<Self>(conn)
+  /// This is unimplemented, use [[Comment::create]]
+  fn create(_conn: &PgConnection, _comment_form: &CommentForm) -> Result<Self, Error> {
+    unimplemented!();
   }
 
   fn update(
@@ -218,6 +276,7 @@ mod tests {
     traits::{Crud, Likeable, Saveable},
     utils::establish_unpooled_connection,
   };
+  use diesel_ltree::Ltree;
   use serial_test::serial;
 
   #[test]
@@ -258,7 +317,7 @@ mod tests {
       ..CommentForm::default()
     };
 
-    let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
+    let inserted_comment = Comment::create(&conn, &comment_form, None).unwrap();
 
     let expected_comment = Comment {
       id: inserted_comment.id,
@@ -267,8 +326,7 @@ mod tests {
       post_id: inserted_post.id,
       removed: false,
       deleted: false,
-      read: false,
-      parent_id: None,
+      path: Ltree(format!("0.{}", inserted_comment.id)),
       published: inserted_comment.published,
       updated: None,
       ap_id: inserted_comment.ap_id.to_owned(),
@@ -279,11 +337,12 @@ mod tests {
       content: "A child comment".into(),
       creator_id: inserted_person.id,
       post_id: inserted_post.id,
-      parent_id: Some(inserted_comment.id),
+      // path: Some(text2ltree(inserted_comment.id),
       ..CommentForm::default()
     };
 
-    let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
+    let inserted_child_comment =
+      Comment::create(&conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
 
     // Comment Like
     let comment_like_form = CommentLikeForm {
@@ -335,8 +394,8 @@ mod tests {
     assert_eq!(expected_comment_like, inserted_comment_like);
     assert_eq!(expected_comment_saved, inserted_comment_saved);
     assert_eq!(
-      expected_comment.id,
-      inserted_child_comment.parent_id.unwrap()
+      format!("0.{}.{}", expected_comment.id, inserted_child_comment.id),
+      inserted_child_comment.path.0,
     );
     assert_eq!(1, like_removed);
     assert_eq!(1, saved_removed);