]> Untitled Git - lemmy.git/blob - server/migrations/2020-02-08-145624_add_post_newest_activity_time/up.sql
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / server / migrations / 2020-02-08-145624_add_post_newest_activity_time / up.sql
1 -- Adds a newest_activity_time for the post_views, in order to sort by newest comment
2 drop view post_view;
3 drop view post_mview;
4 drop materialized view post_aggregates_mview;
5 drop view post_aggregates_view;
6
7 -- regen post view
8 create view post_aggregates_view as
9 select        
10 p.*,
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), 
24   (
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)
27     end
28   )
29 ) as hot_rank,
30 (
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)
33   end
34 ) as newest_activity_time 
35 from post p
36 left join post_like pl on p.id = pl.post_id
37 left join (
38   select post_id, 
39   max(published) as recent_comment_time
40   from comment
41   group by 1
42 ) c on p.id = c.post_id
43 group by p.id, c.recent_comment_time;
44
45 create materialized view post_aggregates_mview as select * from post_aggregates_view;
46
47 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
48
49 create view post_view as 
50 with all_post as (
51   select
52   pa.*
53   from post_aggregates_view pa
54 )
55 select
56 ap.*,
57 u.id as user_id,
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
62 from user_ u
63 cross join all_post ap
64 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
65
66 union all
67
68 select 
69 ap.*,
70 null as user_id,
71 null as my_vote,
72 null as subscribed,
73 null as read,
74 null as saved
75 from all_post ap
76 ;
77
78 create view post_mview as 
79 with all_post as (
80   select
81   pa.*
82   from post_aggregates_mview pa
83 )
84 select
85 ap.*,
86 u.id as user_id,
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
91 from user_ u
92 cross join all_post ap
93 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
94
95 union all
96
97 select 
98 ap.*,
99 null as user_id,
100 null as my_vote,
101 null as subscribed,
102 null as read,
103 null as saved
104 from all_post ap
105 ;
106