]> Untitled Git - lemmy.git/blob - migrations/2019-09-05-230317_add_mod_ban_views/up.sql
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / migrations / 2019-09-05-230317_add_mod_ban_views / up.sql
1 -- Create post view, adding banned_from_community 
2
3 drop view post_view;
4 create view post_view as
5 with all_post as
6 (
7   select        
8   p.*,
9   (select u.banned from user_ u where p.creator_id = u.id) as banned,
10   (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,
11   (select name from user_ where p.creator_id = user_.id) as creator_name,
12   (select name from community where p.community_id = community.id) as community_name,
13   (select removed from community c where p.community_id = c.id) as community_removed,
14   (select deleted from community c where p.community_id = c.id) as community_deleted,
15   (select nsfw from community c where p.community_id = c.id) as community_nsfw,
16   (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
17   coalesce(sum(pl.score), 0) as score,
18   count (case when pl.score = 1 then 1 else null end) as upvotes,
19   count (case when pl.score = -1 then 1 else null end) as downvotes,
20   hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
21   from post p
22   left join post_like pl on p.id = pl.post_id
23   group by p.id
24 )
25
26 select
27 ap.*,
28 u.id as user_id,
29 coalesce(pl.score, 0) as my_vote,
30 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
31 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
32 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
33 from user_ u
34 cross join all_post ap
35 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
36
37 union all
38
39 select 
40 ap.*,
41 null as user_id,
42 null as my_vote,
43 null as subscribed,
44 null as read,
45 null as saved
46 from all_post ap
47 ;