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