]> Untitled Git - lemmy.git/commitdiff
Adding hot rank function, possibly fixing views.
authorDessalines <tyhou13@gmx.com>
Sun, 6 Dec 2020 21:44:36 +0000 (16:44 -0500)
committerDessalines <tyhou13@gmx.com>
Sun, 6 Dec 2020 21:44:36 +0000 (16:44 -0500)
lemmy_db/src/lib.rs
lemmy_db/src/views/community_view.rs
migrations/2020-12-03-035643_create_user_aggregates/up.sql
migrations/2020-12-04-183345_create_community_aggregates/up.sql

index 9fb43d6e4959f68d7f8e17477ecc2d236d8f5d16..b5348a6ca29c9d3aaed30146fc45f4cac4eee3ff 100644 (file)
@@ -217,6 +217,14 @@ lazy_static! {
     Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
 }
 
+pub(crate) mod functions {
+  use diesel::sql_types::*;
+
+  sql_function! {
+    fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
+  }
+}
+
 #[cfg(test)]
 mod tests {
   use super::fuzzy_search;
index 2ab351f400090cca7c93da39584c4602ddbae142..cbb90a4276b836fe7e6746863ef7987778b5dac4 100644 (file)
@@ -2,6 +2,7 @@ use crate::{
   aggregates::community_aggregates::CommunityAggregates,
   category::Category,
   community::{Community, CommunityFollower, CommunitySafe},
+  functions::hot_rank,
   fuzzy_search,
   limit_and_offset,
   schema::{category, community, community_aggregates, community_follower, user_},
@@ -253,8 +254,8 @@ impl<'a> CommunityQueryBuilder<'a> {
       // Covers all other sorts, including hot
       _ => {
         query = query
-          // TODO do custom sql function for hot_rank
-          // .order_by(hot_rank.desc())
+          // TODO do custom sql function for hot_rank, make sure this works
+          .order_by(hot_rank(community_aggregates::subscribers, community::published).desc())
           .then_order_by(community_aggregates::subscribers.desc())
       }
     };
index 8d46fbfbee915bb2284655e4002555fc694250c7..bc7e6394d11f2210b00257ca6e9f6981bd7a4a9e 100644 (file)
@@ -42,10 +42,10 @@ as $$
 begin
   IF (TG_OP = 'INSERT') THEN
     update user_aggregates 
-    set post_count = post_count + 1 where user_id = NEW.user_id;
+    set post_count = post_count + 1 where user_id = NEW.creator_id;
   ELSIF (TG_OP = 'DELETE') THEN
     update user_aggregates 
-    set post_count = post_count - 1 where user_id = OLD.user_id;
+    set post_count = post_count - 1 where user_id = OLD.creator_id;
   END IF;
   return null;
 end $$;
@@ -86,10 +86,10 @@ as $$
 begin
   IF (TG_OP = 'INSERT') THEN
     update user_aggregates 
-    set comment_count = comment_count + 1 where user_id = NEW.user_id;
+    set comment_count = comment_count + 1 where user_id = NEW.creator_id;
   ELSIF (TG_OP = 'DELETE') THEN
     update user_aggregates 
-    set comment_count = comment_count - 1 where user_id = OLD.user_id;
+    set comment_count = comment_count - 1 where user_id = OLD.creator_id;
   END IF;
   return null;
 end $$;
index 8af0159757e77bfb217df84b7795f626a913e162..ec95181917b57dedc232c603aa0adbb508d4d366 100644 (file)
@@ -59,10 +59,10 @@ as $$
 begin
   IF (TG_OP = 'INSERT') THEN
     update community_aggregates 
-    set comments = comments + 1 where community_id = NEW.community_id;
+    set comments = comments + 1 from comment c join post p on p.id = c.post_id and p.id = NEW.post_id;
   ELSIF (TG_OP = 'DELETE') THEN
     update community_aggregates 
-    set comments = comments - 1 where community_id = OLD.community_id;
+    set comments = comments - 1 from comment c join post p on p.id = c.post_id and p.id = OLD.post_id;
   END IF;
   return null;
 end $$;