]> Untitled Git - lemmy.git/commitdiff
Adding hot ranking in sql, post listing view
authorDessalines <tyhou13@gmx.com>
Sun, 31 Mar 2019 00:08:07 +0000 (17:08 -0700)
committerDessalines <tyhou13@gmx.com>
Sun, 31 Mar 2019 00:08:07 +0000 (17:08 -0700)
server/migrations/2019-03-03-163336_create_post/down.sql
server/migrations/2019-03-03-163336_create_post/up.sql
server/migrations/2019-03-30-212058_post_listing/down.sql [new file with mode: 0644]
server/migrations/2019-03-30-212058_post_listing/up.sql [new file with mode: 0644]
server/src/actions/comment.rs
ui/src/components/post.tsx

index acc0b5d174c417a578dab3741a8436d7f9afc598..d3ffa6b92edc5e331eccad9be987a50c78fd0191 100644 (file)
@@ -1,2 +1,4 @@
+drop function hot_rank;
+drop view post_listing;
 drop table post_like;
 drop table post;
index 14294c8f1d65c2d5fd0491e2897b67c95dcae812..2cb8bb010ac3b948c7a46da64a29467c6e2ee413 100644 (file)
@@ -14,5 +14,7 @@ create table post_like (
   post_id int references post on update cascade on delete cascade not null,
   fedi_user_id text not null,
   score smallint not null, -- -1, or 1 for dislike, like, no row for no opinion
-  published timestamp not null default now()
+  published timestamp not null default now(),
+  unique(post_id, fedi_user_id)
 );
+
diff --git a/server/migrations/2019-03-30-212058_post_listing/down.sql b/server/migrations/2019-03-30-212058_post_listing/down.sql
new file mode 100644 (file)
index 0000000..379ce34
--- /dev/null
@@ -0,0 +1,2 @@
+drop view post_listing;
+drop function hot_rank;
diff --git a/server/migrations/2019-03-30-212058_post_listing/up.sql b/server/migrations/2019-03-30-212058_post_listing/up.sql
new file mode 100644 (file)
index 0000000..1796c8f
--- /dev/null
@@ -0,0 +1,20 @@
+-- Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
+create or replace function hot_rank(
+  score numeric,
+  published timestamp without time zone)
+returns numeric as $$
+begin
+  -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
+  return 10000*sign(score)*log(1 + abs(score)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8);
+end; $$
+LANGUAGE plpgsql;
+
+create view post_listing as 
+select post.*, 
+(select count(*) from comment where comment.post_id = post.id) as number_of_comments,
+coalesce(sum(post_like.score),0) as score,
+hot_rank(coalesce(sum(post_like.score),0), post.published) as hot_rank
+from post
+left join post_like
+on post.id = post_like.post_id
+group by post.id;
index 089c384cdc812c486c1ca61ea3975fae1c645fa4..14f6e931cf65e4ec97ba56d15056c26296f0f23c 100644 (file)
@@ -152,7 +152,7 @@ impl CommentView {
 
     for like in likes.iter() {
       if like.score == 1 {
-        upvotes += 1
+        upvotes += 1;
       } else if like.score == -1 {
         downvotes += 1;
       }
index 1d9412fcc5ba2c06396a38534a025b0fddabf451..adb90840083d68faa6cafc2de10378e88f0c3737 100644 (file)
@@ -424,7 +424,6 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
     }  
   }
 
-
   render() {
     return (
       <div>