1 -- Adds a newest_activity_time for the post_views, in order to sort by newest comment
4 drop materialized view post_aggregates_mview;
5 drop view post_aggregates_view;
8 create view post_aggregates_view as
11 (select u.banned from user_ u where p.creator_id = u.id) as banned,
12 (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
13 (select name from user_ where p.creator_id = user_.id) as creator_name,
14 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
15 (select name from community where p.community_id = community.id) as community_name,
16 (select removed from community c where p.community_id = c.id) as community_removed,
17 (select deleted from community c where p.community_id = c.id) as community_deleted,
18 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
19 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
20 coalesce(sum(pl.score), 0) as score,
21 count (case when pl.score = 1 then 1 else null end) as upvotes,
22 count (case when pl.score = -1 then 1 else null end) as downvotes,
23 hot_rank(coalesce(sum(pl.score) , 0),
25 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
26 else greatest(c.recent_comment_time, p.published)
31 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
32 else greatest(c.recent_comment_time, p.published)
34 ) as newest_activity_time
36 left join post_like pl on p.id = pl.post_id
39 max(published) as recent_comment_time
42 ) c on p.id = c.post_id
43 group by p.id, c.recent_comment_time;
45 create materialized view post_aggregates_mview as select * from post_aggregates_view;
47 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
49 create view post_view as
53 from post_aggregates_view pa
58 coalesce(pl.score, 0) as my_vote,
59 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
60 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
61 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
63 cross join all_post ap
64 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
78 create view post_mview as
82 from post_aggregates_mview pa
87 coalesce(pl.score, 0) as my_vote,
88 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
89 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
90 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
92 cross join all_post ap
93 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id