]> Untitled Git - lemmy.git/commitdiff
Feature add three six and nine months options backend (#3226)
authorc-andy-candies <74613851+c-andy-candies@users.noreply.github.com>
Mon, 26 Jun 2023 19:03:35 +0000 (21:03 +0200)
committerGitHub <noreply@github.com>
Mon, 26 Jun 2023 19:03:35 +0000 (15:03 -0400)
Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
crates/db_schema/src/lib.rs
crates/db_schema/src/utils.rs
crates/db_views/src/post_view.rs
crates/db_views_actor/src/person_view.rs
migrations/2023-06-20-191145_add_listingtype_sorttype_3_6_9_months_enums/down.sql [new file with mode: 0644]
migrations/2023-06-20-191145_add_listingtype_sorttype_3_6_9_months_enums/up.sql [new file with mode: 0644]

index 04ec4e7dab04a40a9009d0ff1d0d2147a2d69bd3..acb069ca7d3711a585b647f32f84c8de46bc37e1 100644 (file)
@@ -63,6 +63,9 @@ pub enum SortType {
   TopHour,
   TopSixHour,
   TopTwelveHour,
+  TopThreeMonths,
+  TopSixMonths,
+  TopNineMonths,
 }
 
 #[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)]
index fdd445637b03f24b4be4af496aa2534202304828..44230d10a8f6f0531b1be7ffd157d3aa18397d20 100644 (file)
@@ -259,7 +259,10 @@ pub fn post_to_comment_sort_type(sort: SortType) -> CommentSortType {
     | SortType::TopAll
     | SortType::TopWeek
     | SortType::TopYear
-    | SortType::TopMonth => CommentSortType::Top,
+    | SortType::TopMonth
+    | SortType::TopThreeMonths
+    | SortType::TopSixMonths
+    | SortType::TopNineMonths => CommentSortType::Top,
   }
 }
 
index 14cf7fe1976e3832b2515d60839de9708997d339..d1e974d8a107290211abbfb034bcde694116ede6 100644 (file)
@@ -430,6 +430,18 @@ impl<'a> PostQuery<'a> {
         .filter(post_aggregates::published.gt(now - 12.hours()))
         .then_order_by(post_aggregates::score.desc())
         .then_order_by(post_aggregates::published.desc()),
+      SortType::TopThreeMonths => query
+        .filter(post_aggregates::published.gt(now - 3.months()))
+        .then_order_by(post_aggregates::score.desc())
+        .then_order_by(post_aggregates::published.desc()),
+      SortType::TopSixMonths => query
+        .filter(post_aggregates::published.gt(now - 6.months()))
+        .then_order_by(post_aggregates::score.desc())
+        .then_order_by(post_aggregates::published.desc()),
+      SortType::TopNineMonths => query
+        .filter(post_aggregates::published.gt(now - 9.months()))
+        .then_order_by(post_aggregates::score.desc())
+        .then_order_by(post_aggregates::published.desc()),
     };
 
     let (limit, offset) = limit_and_offset(self.page, self.limit)?;
index c7876cc1c5e821b1d4291f02a0f5cb0e3d0bd038..2a7a2ce79466a935f731026337c592e4f368e615 100644 (file)
@@ -122,6 +122,15 @@ impl<'a> PersonQuery<'a> {
       SortType::TopTwelveHour => query
         .filter(person::published.gt(now - 12.hours()))
         .order_by(person_aggregates::comment_score.desc()),
+      SortType::TopThreeMonths => query
+        .filter(person::published.gt(now - 3.months()))
+        .order_by(person_aggregates::comment_score.desc()),
+      SortType::TopSixMonths => query
+        .filter(person::published.gt(now - 6.months()))
+        .order_by(person_aggregates::comment_score.desc()),
+      SortType::TopNineMonths => query
+        .filter(person::published.gt(now - 9.months()))
+        .order_by(person_aggregates::comment_score.desc()),
     };
 
     let (limit, offset) = limit_and_offset(self.page, self.limit)?;
diff --git a/migrations/2023-06-20-191145_add_listingtype_sorttype_3_6_9_months_enums/down.sql b/migrations/2023-06-20-191145_add_listingtype_sorttype_3_6_9_months_enums/down.sql
new file mode 100644 (file)
index 0000000..f8493fd
--- /dev/null
@@ -0,0 +1,14 @@
+-- update the default sort type
+update local_user set default_sort_type = 'TopDay' where default_sort_type in ('TopThreeMonths', 'TopSixMonths', 'TopNineMonths');
+
+-- rename the old enum
+alter type sort_type_enum rename to sort_type_enum__;
+-- create the new enum
+CREATE TYPE sort_type_enum AS ENUM ('Active', 'Hot', 'New', 'Old', 'TopDay', 'TopWeek', 'TopMonth', 'TopYear', 'TopAll', 'MostComments', 'NewComments');
+
+-- alter all you enum columns
+alter table local_user
+  alter column default_sort_type type sort_type_enum using default_sort_type::text::sort_type_enum;
+
+-- drop the old enum
+drop type sort_type_enum__;
diff --git a/migrations/2023-06-20-191145_add_listingtype_sorttype_3_6_9_months_enums/up.sql b/migrations/2023-06-20-191145_add_listingtype_sorttype_3_6_9_months_enums/up.sql
new file mode 100644 (file)
index 0000000..85bcfad
--- /dev/null
@@ -0,0 +1,4 @@
+-- Update the enums
+ALTER TYPE sort_type_enum ADD VALUE 'TopThreeMonths';
+ALTER TYPE sort_type_enum ADD VALUE 'TopSixMonths';
+ALTER TYPE sort_type_enum ADD VALUE 'TopNineMonths';