]> Untitled Git - lemmy.git/blob - migrations/2019-04-03-155309_create_comment_view/up.sql
Isomorphic docker (#1124)
[lemmy.git] / migrations / 2019-04-03-155309_create_comment_view / up.sql
1 create view comment_view as
2 with all_comment as
3 (
4   select        
5   c.*,
6   (select community_id from post p where p.id = c.post_id),
7   (select u.banned from user_ u where c.creator_id = u.id) as banned,
8   (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
9   (select name from user_ where c.creator_id = user_.id) as creator_name,
10   coalesce(sum(cl.score), 0) as score,
11   count (case when cl.score = 1 then 1 else null end) as upvotes,
12   count (case when cl.score = -1 then 1 else null end) as downvotes
13   from comment c
14   left join comment_like cl on c.id = cl.comment_id
15   group by c.id
16 )
17
18 select
19 ac.*,
20 u.id as user_id,
21 coalesce(cl.score, 0) as my_vote,
22 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
23 from user_ u
24 cross join all_comment ac
25 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
26
27 union all
28
29 select 
30     ac.*,
31     null as user_id, 
32     null as my_vote,
33     null as saved
34 from all_comment ac
35 ;
36
37 create view reply_view as 
38 with closereply as (
39     select 
40     c2.id, 
41     c2.creator_id as sender_id, 
42     c.creator_id as recipient_id
43     from comment c
44     inner join comment c2 on c.id = c2.parent_id
45     where c2.creator_id != c.creator_id
46     -- Do union where post is null
47     union
48     select
49     c.id,
50     c.creator_id as sender_id,
51     p.creator_id as recipient_id
52     from comment c, post p
53     where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
54 )
55 select cv.*,
56 closereply.recipient_id
57 from comment_view cv, closereply
58 where closereply.id = cv.id
59 ;
60