Adding hot ranking in sql, post listing view

This commit is contained in:
Dessalines 2019-03-30 17:08:07 -07:00
parent ff0271c5c4
commit a487caaf38
6 changed files with 28 additions and 3 deletions

View file

@ -1,2 +1,4 @@
drop function hot_rank;
drop view post_listing;
drop table post_like; drop table post_like;
drop table post; drop table post;

View file

@ -14,5 +14,7 @@ create table post_like (
post_id int references post on update cascade on delete cascade not null, post_id int references post on update cascade on delete cascade not null,
fedi_user_id text not null, fedi_user_id text not null,
score smallint not null, -- -1, or 1 for dislike, like, no row for no opinion 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)
); );

View file

@ -0,0 +1,2 @@
drop view post_listing;
drop function hot_rank;

View file

@ -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;

View file

@ -152,7 +152,7 @@ impl CommentView {
for like in likes.iter() { for like in likes.iter() {
if like.score == 1 { if like.score == 1 {
upvotes += 1 upvotes += 1;
} else if like.score == -1 { } else if like.score == -1 {
downvotes += 1; downvotes += 1;
} }

View file

@ -424,7 +424,6 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
} }
} }
render() { render() {
return ( return (
<div> <div>