]> Untitled Git - lemmy.git/blob - server/migrations/2019-08-11-000918_add_nsfw_columns/up.sql
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / server / migrations / 2019-08-11-000918_add_nsfw_columns / up.sql
1 alter table community add column nsfw boolean default false not null;
2 alter table post add column nsfw boolean default false not null;
3 alter table user_ add column show_nsfw boolean default false not null;
4
5 -- The views
6 drop view community_view;
7 create view community_view as 
8 with all_community as
9 (
10   select *,
11   (select name from user_ u where c.creator_id = u.id) as creator_name,
12   (select name from category ct where c.category_id = ct.id) as category_name,
13   (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
14   (select count(*) from post p where p.community_id = c.id) as number_of_posts,
15   (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
16   hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
17   from community c
18 )
19
20 select
21 ac.*,
22 u.id as user_id,
23 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
24 from user_ u
25 cross join all_community ac
26
27 union all
28
29 select 
30 ac.*,
31 null as user_id,
32 null as subscribed
33 from all_community ac
34 ;
35  
36 -- Post view
37 drop view post_view;
38 create view post_view as
39 with all_post as
40 (
41   select        
42   p.*,
43   (select name from user_ where p.creator_id = user_.id) as creator_name,
44   (select name from community where p.community_id = community.id) as community_name,
45   (select removed from community c where p.community_id = c.id) as community_removed,
46   (select deleted from community c where p.community_id = c.id) as community_deleted,
47   (select nsfw from community c where p.community_id = c.id) as community_nsfw,
48   (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
49   coalesce(sum(pl.score), 0) as score,
50   count (case when pl.score = 1 then 1 else null end) as upvotes,
51   count (case when pl.score = -1 then 1 else null end) as downvotes,
52   hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
53   from post p
54   left join post_like pl on p.id = pl.post_id
55   group by p.id
56 )
57
58 select
59 ap.*,
60 u.id as user_id,
61 coalesce(pl.score, 0) as my_vote,
62 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
63 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
64 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
65 from user_ u
66 cross join all_post ap
67 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
68
69 union all
70
71 select 
72 ap.*,
73 null as user_id,
74 null as my_vote,
75 null as subscribed,
76 null as read,
77 null as saved
78 from all_post ap
79 ;