1 -- Adding community name, hot_rank, to comment_view, user_mention_view, and subscribed to comment_view
2 -- Rebuild the comment view
5 DROP VIEW user_mention_view;
7 DROP VIEW user_mention_mview;
9 DROP VIEW comment_view;
11 DROP VIEW comment_mview;
13 DROP MATERIALIZED VIEW comment_aggregates_mview;
15 DROP VIEW comment_aggregates_view;
17 -- reply and comment view
18 CREATE VIEW comment_aggregates_view AS
35 AND p.community_id = co.id) AS community_name,
42 c.creator_id = u.id) AS banned,
47 community_user_ban cb,
50 c.creator_id = cb.user_id
52 AND p.community_id = cb.community_id) AS banned_from_community,
59 c.creator_id = user_.id) AS creator_name,
66 c.creator_id = user_.id) AS creator_avatar,
67 coalesce(sum(cl.score), 0) AS score,
69 CASE WHEN cl.score = 1 THEN
75 CASE WHEN cl.score = - 1 THEN
80 hot_rank (coalesce(sum(cl.score), 0), c.published) AS hot_rank
83 LEFT JOIN comment_like cl ON c.id = cl.comment_id
87 CREATE MATERIALIZED VIEW comment_aggregates_mview AS
91 comment_aggregates_view;
93 CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
95 CREATE VIEW comment_view AS
100 comment_aggregates_view ca
105 coalesce(cl.score, 0) AS my_vote,
110 community_follower cf
113 AND ac.community_id = cf.community_id) AS subscribed,
121 AND cs.comment_id = ac.id) AS saved
124 CROSS JOIN all_comment ac
125 LEFT JOIN comment_like cl ON u.id = cl.user_id
126 AND ac.id = cl.comment_id
137 CREATE VIEW comment_mview AS
138 with all_comment AS (
142 comment_aggregates_mview ca
147 coalesce(cl.score, 0) AS my_vote,
152 community_follower cf
155 AND ac.community_id = cf.community_id) AS subscribed,
163 AND cs.comment_id = ac.id) AS saved
166 CROSS JOIN all_comment ac
167 LEFT JOIN comment_like cl ON u.id = cl.user_id
168 AND ac.id = cl.comment_id
179 -- Do the reply_view referencing the comment_mview
180 CREATE VIEW reply_view AS
184 c2.creator_id AS sender_id,
185 c.creator_id AS recipient_id
188 INNER JOIN comment c2 ON c.id = c2.parent_id
190 c2.creator_id != c.creator_id
191 -- Do union where post is null
195 c.creator_id AS sender_id,
196 p.creator_id AS recipient_id
202 AND c.parent_id IS NULL
203 AND c.creator_id != p.creator_id
207 closereply.recipient_id
212 closereply.id = cv.id;
215 CREATE VIEW user_mention_view AS
218 um.id AS user_mention_id,
231 c.banned_from_community,
246 um.comment_id = c.id;
248 CREATE VIEW user_mention_mview AS
249 with all_comment AS (
253 comment_aggregates_mview ca
257 um.id AS user_mention_id,
270 ac.banned_from_community,
278 coalesce(cl.score, 0) AS my_vote,
286 AND cs.comment_id = ac.id) AS saved,
290 CROSS JOIN all_comment ac
291 LEFT JOIN comment_like cl ON u.id = cl.user_id
292 AND ac.id = cl.comment_id
293 LEFT JOIN user_mention um ON um.comment_id = ac.id
297 um.id AS user_mention_id,
310 ac.banned_from_community,
323 LEFT JOIN user_mention um ON um.comment_id = ac.id;