]> Untitled Git - lemmy.git/commitdiff
Feature add hours as sorting options backend (#3161)
authorIvo Barros <L3v3L@users.noreply.github.com>
Tue, 20 Jun 2023 14:05:43 +0000 (15:05 +0100)
committerGitHub <noreply@github.com>
Tue, 20 Jun 2023 14:05:43 +0000 (16:05 +0200)
* add hours as sorting options

* ran cargo +nightly fmt

* woodpicker trigger

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-17-175955_add_listingtype_sorttype_hour_enums/down.sql [new file with mode: 0644]
migrations/2023-06-17-175955_add_listingtype_sorttype_hour_enums/up.sql [new file with mode: 0644]

index 4799720662d0b1dd01e6b64ecc5a1832bbacd195..4ab26981bc18d3b8a72a8f85b222506641955698 100644 (file)
@@ -59,6 +59,9 @@ pub enum SortType {
   TopAll,
   MostComments,
   NewComments,
+  TopHour,
+  TopSixHour,
+  TopTwelveHour,
 }
 
 #[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)]
index bba1d810b262ce3040cf1c034432af9c0442512b..98d3952abc1c5d2b96ad9de1eaf8ad73cfd9f663 100644 (file)
@@ -196,7 +196,10 @@ pub fn post_to_comment_sort_type(sort: SortType) -> CommentSortType {
     SortType::Active | SortType::Hot => CommentSortType::Hot,
     SortType::New | SortType::NewComments | SortType::MostComments => CommentSortType::New,
     SortType::Old => CommentSortType::Old,
-    SortType::TopDay
+    SortType::TopHour
+    | SortType::TopSixHour
+    | SortType::TopTwelveHour
+    | SortType::TopDay
     | SortType::TopAll
     | SortType::TopWeek
     | SortType::TopYear
index 2c1b9d1df76e3fcbbcfe640a3ebc9833822f6a27..4c65f56872cae600c8e27b18e11d3ced2207c288 100644 (file)
@@ -414,6 +414,18 @@ impl<'a> PostQuery<'a> {
         .filter(post_aggregates::published.gt(now - 1.days()))
         .then_order_by(post_aggregates::score.desc())
         .then_order_by(post_aggregates::published.desc()),
+      SortType::TopHour => query
+        .filter(post_aggregates::published.gt(now - 1.hours()))
+        .then_order_by(post_aggregates::score.desc())
+        .then_order_by(post_aggregates::published.desc()),
+      SortType::TopSixHour => query
+        .filter(post_aggregates::published.gt(now - 6.hours()))
+        .then_order_by(post_aggregates::score.desc())
+        .then_order_by(post_aggregates::published.desc()),
+      SortType::TopTwelveHour => query
+        .filter(post_aggregates::published.gt(now - 12.hours()))
+        .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 43f99dfd42da4e04d3a10c6c955ad5c992617693..c7876cc1c5e821b1d4291f02a0f5cb0e3d0bd038 100644 (file)
@@ -113,6 +113,15 @@ impl<'a> PersonQuery<'a> {
       SortType::TopDay => query
         .filter(person::published.gt(now - 1.days()))
         .order_by(person_aggregates::comment_score.desc()),
+      SortType::TopHour => query
+        .filter(person::published.gt(now - 1.hours()))
+        .order_by(person_aggregates::comment_score.desc()),
+      SortType::TopSixHour => query
+        .filter(person::published.gt(now - 6.hours()))
+        .order_by(person_aggregates::comment_score.desc()),
+      SortType::TopTwelveHour => query
+        .filter(person::published.gt(now - 12.hours()))
+        .order_by(person_aggregates::comment_score.desc()),
     };
 
     let (limit, offset) = limit_and_offset(self.page, self.limit)?;
diff --git a/migrations/2023-06-17-175955_add_listingtype_sorttype_hour_enums/down.sql b/migrations/2023-06-17-175955_add_listingtype_sorttype_hour_enums/down.sql
new file mode 100644 (file)
index 0000000..006bb10
--- /dev/null
@@ -0,0 +1,14 @@
+-- update the default sort type
+update local_user set default_sort_type = 'TopDay' where default_sort_type in ('TopHour', 'TopSixHour', 'TopTwelveHour');
+
+-- 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__;
\ No newline at end of file
diff --git a/migrations/2023-06-17-175955_add_listingtype_sorttype_hour_enums/up.sql b/migrations/2023-06-17-175955_add_listingtype_sorttype_hour_enums/up.sql
new file mode 100644 (file)
index 0000000..7e4b6fb
--- /dev/null
@@ -0,0 +1,4 @@
+-- Update the enums
+ALTER TYPE sort_type_enum ADD VALUE 'TopHour';
+ALTER TYPE sort_type_enum ADD VALUE 'TopSixHour';
+ALTER TYPE sort_type_enum ADD VALUE 'TopTwelveHour';
\ No newline at end of file