]> Untitled Git - lemmy.git/commitdiff
Adding a new comment sort for posts. Fixes #1294 (#1425)
authorDessalines <dessalines@users.noreply.github.com>
Thu, 18 Feb 2021 15:38:25 +0000 (10:38 -0500)
committerGitHub <noreply@github.com>
Thu, 18 Feb 2021 15:38:25 +0000 (10:38 -0500)
* Adding a new comment sort. Fixes #1294

* Fixing a migration comment.

* Adding a comment for newest_comment_time_necro

.rgignore
.rustfmt.toml
crates/db_queries/src/aggregates/post_aggregates.rs
crates/db_queries/src/lib.rs
crates/db_schema/src/schema.rs
crates/db_views/src/comment_view.rs
crates/db_views/src/post_view.rs
crates/db_views_actor/src/user_mention_view.rs
crates/db_views_actor/src/user_view.rs
migrations/2021-02-10-164051_add_new_comments_sort_index/down.sql [new file with mode: 0644]
migrations/2021-02-10-164051_add_new_comments_sort_index/up.sql [new file with mode: 0644]

index d1b811b7de598f2c47eb45e757c4337fc2d3f4b6..eab207b73f6bc78e98c07d57a84276eedeebcde7 100644 (file)
--- a/.rgignore
+++ b/.rgignore
@@ -1 +1 @@
-*.sql
+*.sqldump
index f3efdc308472dcc95439ed2ecb08c6194e22d315..59528c80bfea4a24fcf7b314916d1853ca21c769 100644 (file)
@@ -1,5 +1,5 @@
 tab_spaces = 2
 edition="2018"
 imports_layout="HorizontalVertical"
-merge_imports=true
+imports_granularity="Crate"
 reorder_imports=true
index 1d69fb40e7b9997d03b06343c4c1e5c946c9352e..ffee93bec64dc533f5c50c50e16d694bbc9dca47 100644 (file)
@@ -13,6 +13,7 @@ pub struct PostAggregates {
   pub downvotes: i64,
   pub stickied: bool,
   pub published: chrono::NaiveDateTime,
+  pub newest_comment_time_necro: chrono::NaiveDateTime, // A newest comment time, limited to 2 days, to prevent necrobumping
   pub newest_comment_time: chrono::NaiveDateTime,
 }
 
index ebe7394c9ffed493cd29a1761fcee1ee6713695c..bf0de33662a0f5cfe46f0f2cde416cff213b3236 100644 (file)
@@ -165,6 +165,7 @@ pub enum SortType {
   TopYear,
   TopAll,
   MostComments,
+  NewComments,
 }
 
 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
index e808d77029e61740fe3352fa87959a97dde8c629..71232ebdb86edb71e67b1e80c79edcd3dacd9c20 100644 (file)
@@ -291,6 +291,7 @@ table! {
         downvotes -> Int8,
         stickied -> Bool,
         published -> Timestamp,
+        newest_comment_time_necro -> Timestamp,
         newest_comment_time -> Timestamp,
     }
 }
index a262b7c1fc44deca941939860586b3d9f65caad2..b2a9902a4e976b75930f781d9480f778354a7fc3 100644 (file)
@@ -380,7 +380,9 @@ impl<'a> CommentQueryBuilder<'a> {
       SortType::Hot | SortType::Active => query
         .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
         .then_order_by(comment_aggregates::published.desc()),
-      SortType::New | SortType::MostComments => query.order_by(comment::published.desc()),
+      SortType::New | SortType::MostComments | SortType::NewComments => {
+        query.order_by(comment::published.desc())
+      }
       SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
       SortType::TopYear => query
         .filter(comment::published.gt(now - 1.years()))
index 29e4c3579ef72c90a540edc638ce1b067cc742d3..54e28de1736ff87a2eb9b3aae375cc33dbd7cae9 100644 (file)
@@ -356,14 +356,19 @@ impl<'a> PostQueryBuilder<'a> {
     query = match self.sort {
       SortType::Active => query
         .then_order_by(
-          hot_rank(post_aggregates::score, post_aggregates::newest_comment_time).desc(),
+          hot_rank(
+            post_aggregates::score,
+            post_aggregates::newest_comment_time_necro,
+          )
+          .desc(),
         )
-        .then_order_by(post_aggregates::newest_comment_time.desc()),
+        .then_order_by(post_aggregates::newest_comment_time_necro.desc()),
       SortType::Hot => query
         .then_order_by(hot_rank(post_aggregates::score, post_aggregates::published).desc())
         .then_order_by(post_aggregates::published.desc()),
       SortType::New => query.then_order_by(post_aggregates::published.desc()),
       SortType::MostComments => query.then_order_by(post_aggregates::comments.desc()),
+      SortType::NewComments => query.then_order_by(post_aggregates::newest_comment_time.desc()),
       SortType::TopAll => query.then_order_by(post_aggregates::score.desc()),
       SortType::TopYear => query
         .filter(post::published.gt(now - 1.years()))
@@ -623,6 +628,7 @@ mod tests {
         downvotes: 0,
         stickied: false,
         published: agg.published,
+        newest_comment_time_necro: inserted_post.published,
         newest_comment_time: inserted_post.published,
       },
       subscribed: false,
index 811af6e75945ad81a49739dc3285bc6a6be96133..ffdbe0300637cdfce990350dcb5b9665c292f4e4 100644 (file)
@@ -270,7 +270,9 @@ impl<'a> UserMentionQueryBuilder<'a> {
       SortType::Hot | SortType::Active => query
         .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
         .then_order_by(comment_aggregates::published.desc()),
-      SortType::New | SortType::MostComments => query.order_by(comment::published.desc()),
+      SortType::New | SortType::MostComments | SortType::NewComments => {
+        query.order_by(comment::published.desc())
+      }
       SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
       SortType::TopYear => query
         .filter(comment::published.gt(now - 1.years()))
index 2b85a63ce59ab3d2d90b071bdfb72d3c2a7dab91..bbe889a2584d59f7a6cf2a4651d3445d8d645aec 100644 (file)
@@ -110,7 +110,9 @@ impl<'a> UserQueryBuilder<'a> {
       SortType::Active => query
         .order_by(user_aggregates::comment_score.desc())
         .then_order_by(user_::published.desc()),
-      SortType::New | SortType::MostComments => query.order_by(user_::published.desc()),
+      SortType::New | SortType::MostComments | SortType::NewComments => {
+        query.order_by(user_::published.desc())
+      }
       SortType::TopAll => query.order_by(user_aggregates::comment_score.desc()),
       SortType::TopYear => query
         .filter(user_::published.gt(now - 1.years()))
diff --git a/migrations/2021-02-10-164051_add_new_comments_sort_index/down.sql b/migrations/2021-02-10-164051_add_new_comments_sort_index/down.sql
new file mode 100644 (file)
index 0000000..75f9aea
--- /dev/null
@@ -0,0 +1,33 @@
+drop index idx_post_aggregates_newest_comment_time,
+idx_post_aggregates_stickied_newest_comment_time,
+idx_post_aggregates_stickied_comments;
+
+alter table post_aggregates drop column newest_comment_time;
+
+alter table post_aggregates rename column newest_comment_time_necro to newest_comment_time;
+
+create or replace function post_aggregates_comment_count()
+returns trigger language plpgsql
+as $$
+begin
+  IF (TG_OP = 'INSERT') THEN
+    update post_aggregates pa
+    set comments = comments + 1
+    where pa.post_id = NEW.post_id;
+
+    -- A 2 day necro-bump limit
+    update post_aggregates pa
+    set newest_comment_time = NEW.published
+    where pa.post_id = NEW.post_id
+    and published > ('now'::timestamp - '2 days'::interval);
+  ELSIF (TG_OP = 'DELETE') THEN
+    -- Join to post because that post may not exist anymore
+    update post_aggregates pa
+    set comments = comments - 1
+    from post p
+    where pa.post_id = p.id
+    and pa.post_id = OLD.post_id;
+  END IF;
+  return null;
+end $$;
+
diff --git a/migrations/2021-02-10-164051_add_new_comments_sort_index/up.sql b/migrations/2021-02-10-164051_add_new_comments_sort_index/up.sql
new file mode 100644 (file)
index 0000000..5452fba
--- /dev/null
@@ -0,0 +1,43 @@
+-- First rename current newest comment time to newest_comment_time_necro
+-- necro means that time is limited to 2 days, whereas newest_comment_time ignores that.
+alter table post_aggregates rename column newest_comment_time to newest_comment_time_necro;
+
+-- Add the newest_comment_time column
+alter table post_aggregates add column newest_comment_time timestamp not null default now();
+
+-- Set the current newest_comment_time based on the old ones
+update post_aggregates set newest_comment_time = newest_comment_time_necro;
+
+-- Add the indexes for this new column
+create index idx_post_aggregates_newest_comment_time on post_aggregates (newest_comment_time desc);
+create index idx_post_aggregates_stickied_newest_comment_time on post_aggregates (stickied desc, newest_comment_time desc);
+
+-- Forgot to add index w/ stickied first for most comments:
+create index idx_post_aggregates_stickied_comments on post_aggregates (stickied desc, comments desc);
+
+-- Alter the comment trigger to set the newest_comment_time, and newest_comment_time_necro
+create or replace function post_aggregates_comment_count()
+returns trigger language plpgsql
+as $$
+begin
+  IF (TG_OP = 'INSERT') THEN
+    update post_aggregates pa
+    set comments = comments + 1,
+    newest_comment_time = NEW.published
+    where pa.post_id = NEW.post_id;
+
+    -- A 2 day necro-bump limit
+    update post_aggregates pa
+    set newest_comment_time_necro = NEW.published
+    where pa.post_id = NEW.post_id
+    and published > ('now'::timestamp - '2 days'::interval);
+  ELSIF (TG_OP = 'DELETE') THEN
+    -- Join to post because that post may not exist anymore
+    update post_aggregates pa
+    set comments = comments - 1
+    from post p
+    where pa.post_id = p.id
+    and pa.post_id = OLD.post_id;
+  END IF;
+  return null;
+end $$;