]> Untitled Git - lemmy.git/blob - server/migrations/2019-05-02-051656_community_view_hot_rank/up.sql
e7e753665f6d99aa9859d127e557d563762fac77
[lemmy.git] / server / migrations / 2019-05-02-051656_community_view_hot_rank / up.sql
1 drop view community_view;
2 create view community_view as 
3 with all_community as
4 (
5   select *,
6   (select name from user_ u where c.creator_id = u.id) as creator_name,
7   (select name from category ct where c.category_id = ct.id) as category_name,
8   (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
9   (select count(*) from post p where p.community_id = c.id) as number_of_posts,
10   (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
11   hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
12   from community c
13 )
14
15 select
16 ac.*,
17 u.id as user_id,
18 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
19 from user_ u
20 cross join all_community ac
21
22 union all
23
24 select 
25 ac.*,
26 null as user_id,
27 null as subscribed
28 from all_community ac
29 ;