From d66f4e8ac084dfdaa63e1a62a4efdcfc02dfdd79 Mon Sep 17 00:00:00 2001
From: Dessalines <tyhou13@gmx.com>
Date: Thu, 3 Dec 2020 10:18:17 -0500
Subject: [PATCH] Finishing up user aggregates.

---
 lemmy_db/src/schema.rs                        |  13 ++
 lemmy_db/src/site_aggregates.rs               |   2 +
 .../down.sql                                  |  24 ++--
 .../up.sql                                    | 120 ++++++----------
 .../down.sql                                  |  12 +-
 .../up.sql                                    | 131 +++++++-----------
 6 files changed, 127 insertions(+), 175 deletions(-)

diff --git a/lemmy_db/src/schema.rs b/lemmy_db/src/schema.rs
index ce84c7d5..bc575f4e 100644
--- a/lemmy_db/src/schema.rs
+++ b/lemmy_db/src/schema.rs
@@ -481,6 +481,17 @@ table! {
     }
 }
 
+table! {
+    user_aggregates (id) {
+        id -> Int4,
+        user_id -> Int4,
+        post_count -> Int8,
+        post_score -> Int8,
+        comment_count -> Int8,
+        comment_score -> Int8,
+    }
+}
+
 table! {
     user_ban (id) {
         id -> Int4,
@@ -562,6 +573,7 @@ joinable!(post_report -> post (post_id));
 joinable!(post_saved -> post (post_id));
 joinable!(post_saved -> user_ (user_id));
 joinable!(site -> user_ (creator_id));
+joinable!(user_aggregates -> user_ (user_id));
 joinable!(user_ban -> user_ (user_id));
 joinable!(user_mention -> comment (comment_id));
 joinable!(user_mention -> user_ (recipient_id));
@@ -599,6 +611,7 @@ allow_tables_to_appear_in_same_query!(
   site,
   site_aggregates,
   user_,
+  user_aggregates,
   user_ban,
   user_fast,
   user_mention,
diff --git a/lemmy_db/src/site_aggregates.rs b/lemmy_db/src/site_aggregates.rs
index 488046ac..93a5ba36 100644
--- a/lemmy_db/src/site_aggregates.rs
+++ b/lemmy_db/src/site_aggregates.rs
@@ -17,3 +17,5 @@ impl SiteAggregates {
     site_aggregates::table.first::<Self>(conn)
   }
 }
+
+// TODO add unit tests, to make sure triggers are working
diff --git a/migrations/2020-12-02-152437_create_site_aggregates/down.sql b/migrations/2020-12-02-152437_create_site_aggregates/down.sql
index bd90603d..4bbee761 100644
--- a/migrations/2020-12-02-152437_create_site_aggregates/down.sql
+++ b/migrations/2020-12-02-152437_create_site_aggregates/down.sql
@@ -1,19 +1,11 @@
 -- Site aggregates
 drop table site_aggregates;
-drop trigger site_aggregates_insert_user on user_;
-drop trigger site_aggregates_delete_user on user_;
-drop trigger site_aggregates_insert_post on post;
-drop trigger site_aggregates_delete_post on post;
-drop trigger site_aggregates_insert_comment on comment;
-drop trigger site_aggregates_delete_comment on comment;
-drop trigger site_aggregates_insert_community on community;
-drop trigger site_aggregates_delete_community on community;
+drop trigger site_aggregates_user on user_;
+drop trigger site_aggregates_post on post;
+drop trigger site_aggregates_comment on comment;
+drop trigger site_aggregates_community on community;
 drop function 
-  site_aggregates_user_increment,
-  site_aggregates_user_decrement,
-  site_aggregates_post_increment,
-  site_aggregates_post_decrement,
-  site_aggregates_comment_increment,
-  site_aggregates_comment_decrement,
-  site_aggregates_community_increment,
-  site_aggregates_community_decrement;
+  site_aggregates_user,
+  site_aggregates_post,
+  site_aggregates_comment,
+  site_aggregates_community;
diff --git a/migrations/2020-12-02-152437_create_site_aggregates/up.sql b/migrations/2020-12-02-152437_create_site_aggregates/up.sql
index 7f482268..f66f1003 100644
--- a/migrations/2020-12-02-152437_create_site_aggregates/up.sql
+++ b/migrations/2020-12-02-152437_create_site_aggregates/up.sql
@@ -15,110 +15,78 @@ insert into site_aggregates (users, posts, comments, communities)
 
 -- Add site aggregate triggers
 -- user
-create function site_aggregates_user_increment()
+create function site_aggregates_user()
 returns trigger language plpgsql
 as $$
 begin
-  update site_aggregates 
-  set users = users + 1;
+  IF (TG_OP = 'INSERT') THEN
+    update site_aggregates 
+    set users = users + 1;
+  ELSIF (TG_OP = 'DELETE') THEN
+    update site_aggregates 
+    set users = users - 1;
+  END IF;
   return null;
 end $$;
 
-create trigger site_aggregates_insert_user
-after insert on user_
-execute procedure site_aggregates_user_increment();
-
-create function site_aggregates_user_decrement()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set users = users - 1;
-  return null;
-end $$;
-
-create trigger site_aggregates_delete_user
-after delete on user_
-execute procedure site_aggregates_user_decrement();
+create trigger site_aggregates_user
+after insert or delete on user_
+execute procedure site_aggregates_user();
 
 -- post
-create function site_aggregates_post_increment()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set posts = posts + 1;
-  return null;
-end $$;
-
-create trigger site_aggregates_insert_post
-after insert on post
-execute procedure site_aggregates_post_increment();
-
-create function site_aggregates_post_decrement()
+create function site_aggregates_post()
 returns trigger language plpgsql
 as $$
 begin
-  update site_aggregates 
-  set posts = posts - 1;
+  IF (TG_OP = 'INSERT') THEN
+    update site_aggregates 
+    set posts = posts + 1;
+  ELSIF (TG_OP = 'DELETE') THEN
+    update site_aggregates 
+    set posts = posts - 1;
+  END IF;
   return null;
 end $$;
 
-create trigger site_aggregates_delete_post
-after delete on post
-execute procedure site_aggregates_post_decrement();
+create trigger site_aggregates_post
+after insert or delete on post
+execute procedure site_aggregates_post();
 
 -- comment
-create function site_aggregates_comment_increment()
+create function site_aggregates_comment()
 returns trigger language plpgsql
 as $$
 begin
-  update site_aggregates 
-  set comments = comments + 1;
+  IF (TG_OP = 'INSERT') THEN
+    update site_aggregates 
+    set comments = comments + 1;
+  ELSIF (TG_OP = 'DELETE') THEN
+    update site_aggregates 
+    set comments = comments - 1;
+  END IF;
   return null;
 end $$;
 
-create trigger site_aggregates_insert_comment
-after insert on comment
-execute procedure site_aggregates_comment_increment();
-
-create function site_aggregates_comment_decrement()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set comments = comments - 1;
-  return null;
-end $$;
-
-create trigger site_aggregates_delete_comment
-after delete on comment
-execute procedure site_aggregates_comment_decrement();
+create trigger site_aggregates_comment
+after insert or delete on comment
+execute procedure site_aggregates_comment();
 
 -- community
-create function site_aggregates_community_increment()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set communities = communities + 1;
-  return null;
-end $$;
-
-create trigger site_aggregates_insert_community
-after insert on community
-execute procedure site_aggregates_community_increment();
-
-create function site_aggregates_community_decrement()
+create function site_aggregates_community()
 returns trigger language plpgsql
 as $$
 begin
-  update site_aggregates 
-  set communities = communities - 1;
+  IF (TG_OP = 'INSERT') THEN
+    update site_aggregates 
+    set communities = communities + 1;
+  ELSIF (TG_OP = 'DELETE') THEN
+    update site_aggregates 
+    set communities = communities - 1;
+  END IF;
   return null;
 end $$;
 
-create trigger site_aggregates_delete_community
-after delete on community
-execute procedure site_aggregates_community_decrement();
+create trigger site_aggregates_community
+after insert or delete on community
+execute procedure site_aggregates_community();
 
diff --git a/migrations/2020-12-03-035643_create_user_aggregates/down.sql b/migrations/2020-12-03-035643_create_user_aggregates/down.sql
index 291a97c5..4e3e7fcb 100644
--- a/migrations/2020-12-03-035643_create_user_aggregates/down.sql
+++ b/migrations/2020-12-03-035643_create_user_aggregates/down.sql
@@ -1 +1,11 @@
--- This file should undo anything in `up.sql`
\ No newline at end of file
+-- User aggregates
+drop table user_aggregates;
+drop trigger user_aggregates_post_count on post;
+drop trigger user_aggregates_post_score on post_like;
+drop trigger user_aggregates_comment_count on comment;
+drop trigger user_aggregates_comment_score on comment_like;
+drop function 
+  user_aggregates_post_count,
+  user_aggregates_post_score,
+  user_aggregates_comment_count,
+  user_aggregates_comment_score;
diff --git a/migrations/2020-12-03-035643_create_user_aggregates/up.sql b/migrations/2020-12-03-035643_create_user_aggregates/up.sql
index b2bed949..85a0b675 100644
--- a/migrations/2020-12-03-035643_create_user_aggregates/up.sql
+++ b/migrations/2020-12-03-035643_create_user_aggregates/up.sql
@@ -34,112 +34,79 @@ insert into user_aggregates (user_id, post_count, post_score, comment_count, com
   ) cd on u.id = cd.creator_id;
 
 
--- Add site aggregate triggers
--- user
-create function site_aggregates_user_increment()
+-- Add user aggregate triggers
+-- post count
+create function user_aggregates_post_count()
 returns trigger language plpgsql
 as $$
 begin
-  update site_aggregates 
-  set users = users + 1;
+  IF (TG_OP = 'INSERT') THEN
+    update user_aggregates 
+    set post_count = post_count + 1 where user_id = NEW.user_id;
+  ELSIF (TG_OP = 'DELETE') THEN
+    update user_aggregates 
+    set post_count = post_count - 1 where user_id = OLD.user_id;
+  END IF;
   return null;
 end $$;
 
-create trigger site_aggregates_insert_user
-after insert on user_
-execute procedure site_aggregates_user_increment();
+create trigger user_aggregates_post_count
+after insert or delete on post
+execute procedure user_aggregates_post_count();
 
-create function site_aggregates_user_decrement()
+-- post score
+create function user_aggregates_post_score()
 returns trigger language plpgsql
 as $$
 begin
-  update site_aggregates 
-  set users = users - 1;
+  IF (TG_OP = 'INSERT') THEN
+    update user_aggregates 
+    set post_score = post_score + NEW.score where user_id = NEW.user_id;
+  ELSIF (TG_OP = 'DELETE') THEN
+    update user_aggregates 
+    set post_score = post_score - OLD.score where user_id = OLD.user_id;
+  END IF;
   return null;
 end $$;
 
-create trigger site_aggregates_delete_user
-after delete on user_
-execute procedure site_aggregates_user_decrement();
+create trigger user_aggregates_post_score
+after insert or delete on post_like
+execute procedure user_aggregates_post_score();
 
--- post
-create function site_aggregates_post_increment()
+-- comment count
+create function user_aggregates_comment_count()
 returns trigger language plpgsql
 as $$
 begin
-  update site_aggregates 
-  set posts = posts + 1;
+  IF (TG_OP = 'INSERT') THEN
+    update user_aggregates 
+    set comment_count = comment_count + 1 where user_id = NEW.user_id;
+  ELSIF (TG_OP = 'DELETE') THEN
+    update user_aggregates 
+    set comment_count = comment_count - 1 where user_id = OLD.user_id;
+  END IF;
   return null;
 end $$;
 
-create trigger site_aggregates_insert_post
-after insert on post
-execute procedure site_aggregates_post_increment();
+create trigger user_aggregates_comment_count
+after insert or delete on comment
+execute procedure user_aggregates_comment_count();
 
-create function site_aggregates_post_decrement()
+-- comment score
+create function user_aggregates_comment_score()
 returns trigger language plpgsql
 as $$
 begin
-  update site_aggregates 
-  set posts = posts - 1;
+  IF (TG_OP = 'INSERT') THEN
+    update user_aggregates 
+    set comment_score = comment_score + NEW.score where user_id = NEW.user_id;
+  ELSIF (TG_OP = 'DELETE') THEN
+    update user_aggregates 
+    set comment_score = comment_score - OLD.score where user_id = OLD.user_id;
+  END IF;
   return null;
 end $$;
 
-create trigger site_aggregates_delete_post
-after delete on post
-execute procedure site_aggregates_post_decrement();
-
--- comment
-create function site_aggregates_comment_increment()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set comments = comments + 1;
-  return null;
-end $$;
-
-create trigger site_aggregates_insert_comment
-after insert on comment
-execute procedure site_aggregates_comment_increment();
-
-create function site_aggregates_comment_decrement()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set comments = comments - 1;
-  return null;
-end $$;
-
-create trigger site_aggregates_delete_comment
-after delete on comment
-execute procedure site_aggregates_comment_decrement();
-
--- community
-create function site_aggregates_community_increment()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set communities = communities + 1;
-  return null;
-end $$;
-
-create trigger site_aggregates_insert_community
-after insert on community
-execute procedure site_aggregates_community_increment();
-
-create function site_aggregates_community_decrement()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set communities = communities - 1;
-  return null;
-end $$;
-
-create trigger site_aggregates_delete_community
-after delete on community
-execute procedure site_aggregates_community_decrement();
-
+create trigger user_aggregates_comment_score
+after insert or delete on comment_like
+execute procedure user_aggregates_comment_score();
-- 
2.44.1