]> Untitled Git - lemmy.git/blob - server/migrations/2020-03-06-202329_add_post_iframely_data/up.sql
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / server / migrations / 2020-03-06-202329_add_post_iframely_data / up.sql
1 -- Add the columns
2 alter table post add column embed_title text;
3 alter table post add column embed_description text;
4 alter table post add column embed_html text;
5 alter table post add column thumbnail_url text;
6
7 -- Regenerate the views
8
9 -- Adds a newest_activity_time for the post_views, in order to sort by newest comment
10 drop view post_view;
11 drop view post_mview;
12 drop materialized view post_aggregates_mview;
13 drop view post_aggregates_view;
14
15 -- regen post view
16 create view post_aggregates_view as
17 select        
18 p.*,
19 (select u.banned from user_ u where p.creator_id = u.id) as banned,
20 (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,
21 (select name from user_ where p.creator_id = user_.id) as creator_name,
22 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
23 (select name from community where p.community_id = community.id) as community_name,
24 (select removed from community c where p.community_id = c.id) as community_removed,
25 (select deleted from community c where p.community_id = c.id) as community_deleted,
26 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
27 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
28 coalesce(sum(pl.score), 0) as score,
29 count (case when pl.score = 1 then 1 else null end) as upvotes,
30 count (case when pl.score = -1 then 1 else null end) as downvotes,
31 hot_rank(coalesce(sum(pl.score) , 0), 
32   (
33     case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
34     else greatest(c.recent_comment_time, p.published)
35     end
36   )
37 ) as hot_rank,
38 (
39   case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
40   else greatest(c.recent_comment_time, p.published)
41   end
42 ) as newest_activity_time
43 from post p
44 left join post_like pl on p.id = pl.post_id
45 left join (
46   select post_id, 
47   max(published) as recent_comment_time
48   from comment
49   group by 1
50 ) c on p.id = c.post_id
51 group by p.id, c.recent_comment_time;
52
53 create materialized view post_aggregates_mview as select * from post_aggregates_view;
54
55 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
56
57 create view post_view as 
58 with all_post as (
59   select
60   pa.*
61   from post_aggregates_view pa
62 )
63 select
64 ap.*,
65 u.id as user_id,
66 coalesce(pl.score, 0) as my_vote,
67 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
68 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
69 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
70 from user_ u
71 cross join all_post ap
72 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
73
74 union all
75
76 select 
77 ap.*,
78 null as user_id,
79 null as my_vote,
80 null as subscribed,
81 null as read,
82 null as saved
83 from all_post ap
84 ;
85
86 create view post_mview as 
87 with all_post as (
88   select
89   pa.*
90   from post_aggregates_mview pa
91 )
92 select
93 ap.*,
94 u.id as user_id,
95 coalesce(pl.score, 0) as my_vote,
96 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
97 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
98 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
99 from user_ u
100 cross join all_post ap
101 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
102
103 union all
104
105 select 
106 ap.*,
107 null as user_id,
108 null as my_vote,
109 null as subscribed,
110 null as read,
111 null as saved
112 from all_post ap
113 ;
114
115