3 drop view user_mention_view;
4 drop view user_mention_mview;
5 drop view comment_view;
6 drop view comment_mview;
7 drop materialized view comment_aggregates_mview;
8 drop view comment_aggregates_view;
10 -- reply and comment view
11 create view comment_aggregates_view as
14 (select community_id from post p where p.id = c.post_id),
15 (select u.banned from user_ u where c.creator_id = u.id) as banned,
16 (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,
17 (select name from user_ where c.creator_id = user_.id) as creator_name,
18 (select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
19 coalesce(sum(cl.score), 0) as score,
20 count (case when cl.score = 1 then 1 else null end) as upvotes,
21 count (case when cl.score = -1 then 1 else null end) as downvotes
23 left join comment_like cl on c.id = cl.comment_id
26 create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
28 create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
30 create view comment_view as
35 from comment_aggregates_view ca
41 coalesce(cl.score, 0) as my_vote,
42 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
44 cross join all_comment ac
45 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
57 create view comment_mview as
62 from comment_aggregates_mview ca
68 coalesce(cl.score, 0) as my_vote,
69 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
71 cross join all_comment ac
72 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
85 -- Do the reply_view referencing the comment_mview
86 create view reply_view as
90 c2.creator_id as sender_id,
91 c.creator_id as recipient_id
93 inner join comment c2 on c.id = c2.parent_id
94 where c2.creator_id != c.creator_id
95 -- Do union where post is null
99 c.creator_id as sender_id,
100 p.creator_id as recipient_id
101 from comment c, post p
102 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
105 closereply.recipient_id
106 from comment_mview cv, closereply
107 where closereply.id = cv.id
111 create view user_mention_view as
114 um.id as user_mention_id,
126 c.banned_from_community,
136 from user_mention um, comment_view c
137 where um.comment_id = c.id;
140 create view user_mention_mview as
145 from comment_aggregates_mview ca
150 um.id as user_mention_id,
162 ac.banned_from_community,
169 coalesce(cl.score, 0) as my_vote,
170 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
173 cross join all_comment ac
174 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
175 left join user_mention um on um.comment_id = ac.id
181 um.id as user_mention_id,
193 ac.banned_from_community,
204 left join user_mention um on um.comment_id = ac.id