2 -- Adding community name, hot_rank, to comment_view, user_mention_view, and subscribed to comment_view
4 -- Rebuild the comment view
6 drop view user_mention_view;
7 drop view user_mention_mview;
8 drop view comment_view;
9 drop view comment_mview;
10 drop materialized view comment_aggregates_mview;
11 drop view comment_aggregates_view;
13 -- reply and comment view
14 create view comment_aggregates_view as
17 (select community_id from post p where p.id = c.post_id),
18 (select co.name from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_name,
19 (select u.banned from user_ u where c.creator_id = u.id) as banned,
20 (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,
21 (select name from user_ where c.creator_id = user_.id) as creator_name,
22 (select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
23 coalesce(sum(cl.score), 0) as score,
24 count (case when cl.score = 1 then 1 else null end) as upvotes,
25 count (case when cl.score = -1 then 1 else null end) as downvotes,
26 hot_rank(coalesce(sum(cl.score) , 0), c.published) as hot_rank
28 left join comment_like cl on c.id = cl.comment_id
31 create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
33 create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
35 create view comment_view as
40 from comment_aggregates_view ca
46 coalesce(cl.score, 0) as my_vote,
47 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
48 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
50 cross join all_comment ac
51 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
64 create view comment_mview as
69 from comment_aggregates_mview ca
75 coalesce(cl.score, 0) as my_vote,
76 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
77 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
79 cross join all_comment ac
80 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
93 -- Do the reply_view referencing the comment_mview
94 create view reply_view as
98 c2.creator_id as sender_id,
99 c.creator_id as recipient_id
101 inner join comment c2 on c.id = c2.parent_id
102 where c2.creator_id != c.creator_id
103 -- Do union where post is null
107 c.creator_id as sender_id,
108 p.creator_id as recipient_id
109 from comment c, post p
110 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
113 closereply.recipient_id
114 from comment_mview cv, closereply
115 where closereply.id = cv.id
119 create view user_mention_view as
122 um.id as user_mention_id,
135 c.banned_from_community,
146 from user_mention um, comment_view c
147 where um.comment_id = c.id;
150 create view user_mention_mview as
155 from comment_aggregates_mview ca
160 um.id as user_mention_id,
173 ac.banned_from_community,
181 coalesce(cl.score, 0) as my_vote,
182 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
185 cross join all_comment ac
186 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
187 left join user_mention um on um.comment_id = ac.id
193 um.id as user_mention_id,
206 ac.banned_from_community,
218 left join user_mention um on um.comment_id = ac.id