2 drop view user_view cascade;
4 create view user_view as
14 u.send_notifications_to_email,
16 (select count(*) from post p where p.creator_id = u.id) as number_of_posts,
17 (select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
18 (select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
19 (select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
22 create materialized view user_mview as select * from user_view;
24 create unique index idx_user_mview_id on user_mview (id);
27 drop view community_aggregates_view cascade;
28 create view community_aggregates_view as
30 (select name from user_ u where c.creator_id = u.id) as creator_name,
31 (select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
32 (select name from category ct where c.category_id = ct.id) as category_name,
33 (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
34 (select count(*) from post p where p.community_id = c.id) as number_of_posts,
35 (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
36 hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
39 create materialized view community_aggregates_mview as select * from community_aggregates_view;
41 create unique index idx_community_aggregates_mview_id on community_aggregates_mview (id);
43 create view community_view as
48 from community_aggregates_view ca
54 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
56 cross join all_community ac
67 create view community_mview as
72 from community_aggregates_mview ca
78 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
80 cross join all_community ac
92 drop view community_moderator_view;
93 drop view community_follower_view;
94 drop view community_user_ban_view;
96 create view community_moderator_view as
98 (select name from user_ u where cm.user_id = u.id) as user_name,
99 (select avatar from user_ u where cm.user_id = u.id),
100 (select name from community c where cm.community_id = c.id) as community_name
101 from community_moderator cm;
103 create view community_follower_view as
105 (select name from user_ u where cf.user_id = u.id) as user_name,
106 (select avatar from user_ u where cf.user_id = u.id),
107 (select name from community c where cf.community_id = c.id) as community_name
108 from community_follower cf;
110 create view community_user_ban_view as
112 (select name from user_ u where cm.user_id = u.id) as user_name,
113 (select avatar from user_ u where cm.user_id = u.id),
114 (select name from community c where cm.community_id = c.id) as community_name
115 from community_user_ban cm;
119 drop view post_mview;
120 drop materialized view post_aggregates_mview;
121 drop view post_aggregates_view;
124 create view post_aggregates_view as
127 (select u.banned from user_ u where p.creator_id = u.id) as banned,
128 (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
129 (select name from user_ where p.creator_id = user_.id) as creator_name,
130 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
131 (select name from community where p.community_id = community.id) as community_name,
132 (select removed from community c where p.community_id = c.id) as community_removed,
133 (select deleted from community c where p.community_id = c.id) as community_deleted,
134 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
135 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
136 coalesce(sum(pl.score), 0) as score,
137 count (case when pl.score = 1 then 1 else null end) as upvotes,
138 count (case when pl.score = -1 then 1 else null end) as downvotes,
139 hot_rank(coalesce(sum(pl.score) , 0),
141 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
142 else greatest(c.recent_comment_time, p.published)
147 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
148 else greatest(c.recent_comment_time, p.published)
150 ) as newest_activity_time
152 left join post_like pl on p.id = pl.post_id
155 max(published) as recent_comment_time
158 ) c on p.id = c.post_id
159 group by p.id, c.recent_comment_time;
161 create materialized view post_aggregates_mview as select * from post_aggregates_view;
163 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
165 create view post_view as
169 from post_aggregates_view pa
174 coalesce(pl.score, 0) as my_vote,
175 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
176 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
177 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
179 cross join all_post ap
180 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
194 create view post_mview as
198 from post_aggregates_mview pa
203 coalesce(pl.score, 0) as my_vote,
204 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
205 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
206 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
208 cross join all_post ap
209 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
224 -- reply_view, comment_view, user_mention
225 drop view reply_view;
226 drop view user_mention_view;
227 drop view user_mention_mview;
228 drop view comment_view;
229 drop view comment_mview;
230 drop materialized view comment_aggregates_mview;
231 drop view comment_aggregates_view;
233 -- reply and comment view
234 create view comment_aggregates_view as
237 (select community_id from post p where p.id = c.post_id),
238 (select co.name from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_name,
239 (select u.banned from user_ u where c.creator_id = u.id) as banned,
240 (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,
241 (select name from user_ where c.creator_id = user_.id) as creator_name,
242 (select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
243 coalesce(sum(cl.score), 0) as score,
244 count (case when cl.score = 1 then 1 else null end) as upvotes,
245 count (case when cl.score = -1 then 1 else null end) as downvotes,
246 hot_rank(coalesce(sum(cl.score) , 0), c.published) as hot_rank
248 left join comment_like cl on c.id = cl.comment_id
251 create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
253 create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
255 create view comment_view as
260 from comment_aggregates_view ca
266 coalesce(cl.score, 0) as my_vote,
267 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
268 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
270 cross join all_comment ac
271 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
284 create view comment_mview as
289 from comment_aggregates_mview ca
295 coalesce(cl.score, 0) as my_vote,
296 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
297 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
299 cross join all_comment ac
300 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
313 -- Do the reply_view referencing the comment_mview
314 create view reply_view as
318 c2.creator_id as sender_id,
319 c.creator_id as recipient_id
321 inner join comment c2 on c.id = c2.parent_id
322 where c2.creator_id != c.creator_id
323 -- Do union where post is null
327 c.creator_id as sender_id,
328 p.creator_id as recipient_id
329 from comment c, post p
330 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
333 closereply.recipient_id
334 from comment_mview cv, closereply
335 where closereply.id = cv.id
339 create view user_mention_view as
342 um.id as user_mention_id,
355 c.banned_from_community,
366 from user_mention um, comment_view c
367 where um.comment_id = c.id;
370 create view user_mention_mview as
375 from comment_aggregates_mview ca
380 um.id as user_mention_id,
393 ac.banned_from_community,
401 coalesce(cl.score, 0) as my_vote,
402 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
405 cross join all_comment ac
406 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
407 left join user_mention um on um.comment_id = ac.id
413 um.id as user_mention_id,
426 ac.banned_from_community,
438 left join user_mention um on um.comment_id = ac.id