3 DROP VIEW comment_view;
5 DROP VIEW community_view;
18 CREATE VIEW community_view AS
19 with all_community AS (
28 c.creator_id = u.id) AS creator_name,
35 c.category_id = ct.id) AS category_name,
42 cf.community_id = c.id) AS number_of_subscribers,
49 p.community_id = c.id) AS number_of_posts,
58 AND p.id = co.post_id) AS number_of_comments
72 AND ac.id = cf.community_id) AS subscribed
75 CROSS JOIN all_community ac
84 CREATE OR REPLACE VIEW post_view AS
94 p.creator_id = user_.id) AS creator_name,
101 p.community_id = community.id) AS community_name,
108 p.community_id = c.id) AS community_removed,
115 comment.post_id = p.id) AS number_of_comments,
116 coalesce(sum(pl.score), 0) AS score,
118 CASE WHEN pl.score = 1 THEN
124 CASE WHEN pl.score = - 1 THEN
129 hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
132 LEFT JOIN post_like pl ON p.id = pl.post_id
139 coalesce(pl.score, 0) AS my_vote,
144 community_follower cf
147 AND cf.community_id = ap.community_id) AS subscribed,
155 AND pr.post_id = ap.id) AS read,
163 AND ps.post_id = ap.id) AS saved
166 CROSS JOIN all_post ap
167 LEFT JOIN post_like pl ON u.id = pl.user_id
168 AND ap.id = pl.post_id
180 CREATE VIEW comment_view AS
181 with all_comment AS (
197 c.creator_id = u.id) AS banned,
202 community_user_ban cb,
205 c.creator_id = cb.user_id
207 AND p.community_id = cb.community_id) AS banned_from_community,
214 c.creator_id = user_.id) AS creator_name,
215 coalesce(sum(cl.score), 0) AS score,
217 CASE WHEN cl.score = 1 THEN
223 CASE WHEN cl.score = - 1 THEN
230 LEFT JOIN comment_like cl ON c.id = cl.comment_id
237 coalesce(cl.score, 0) AS my_vote,
245 AND cs.comment_id = ac.id) AS saved
248 CROSS JOIN all_comment ac
249 LEFT JOIN comment_like cl ON u.id = cl.user_id
250 AND ac.id = cl.comment_id
260 CREATE VIEW reply_view AS
264 c2.creator_id AS sender_id,
265 c.creator_id AS recipient_id
268 INNER JOIN comment c2 ON c.id = c2.parent_id
270 c2.creator_id != c.creator_id
271 -- Do union where post is null
275 c.creator_id AS sender_id,
276 p.creator_id AS recipient_id
282 AND c.parent_id IS NULL
283 AND c.creator_id != p.creator_id
287 closereply.recipient_id
292 closereply.id = cv.id;