3 create materialized view post_view as
8 (select u.banned from user_ u where p.creator_id = u.id) as banned,
9 (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,
10 (select name from user_ where p.creator_id = user_.id) as creator_name,
11 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
12 (select name from community where p.community_id = community.id) as community_name,
13 (select removed from community c where p.community_id = c.id) as community_removed,
14 (select deleted from community c where p.community_id = c.id) as community_deleted,
15 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
16 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
17 coalesce(sum(pl.score), 0) as score,
18 count (case when pl.score = 1 then 1 else null end) as upvotes,
19 count (case when pl.score = -1 then 1 else null end) as downvotes,
20 hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
22 left join post_like pl on p.id = pl.post_id
29 coalesce(pl.score, 0) as my_vote,
30 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
31 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
32 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
34 cross join all_post ap
35 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
50 create unique index idx_post_view_unique on post_view (id, user_id);
51 create index idx_post_view_user_id on post_view (user_id);
52 create index idx_post_view_hot_rank_published on post_view (hot_rank desc, published desc);
53 create index idx_post_view_published on post_view (published desc);
54 create index idx_post_view_score on post_view (score desc);
58 create materialized view user_view as
67 send_notifications_to_email,
69 (select count(*) from post p where p.creator_id = u.id) as number_of_posts,
70 (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,
71 (select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
72 (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
75 create unique index idx_user_view_unique on user_view (id);
76 create index idx_user_view_comment_published on user_view (comment_score desc, published desc);
77 create index idx_user_view_admin on user_view (admin);
78 create index idx_user_view_banned on user_view (banned);
81 drop view community_view;
82 create materialized view community_view as
86 (select name from user_ u where c.creator_id = u.id) as creator_name,
87 (select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
88 (select name from category ct where c.category_id = ct.id) as category_name,
89 (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
90 (select count(*) from post p where p.community_id = c.id) as number_of_posts,
91 (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
92 hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
99 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
101 cross join all_community ac
109 from all_community ac
112 create unique index idx_community_view_unique on community_view (id, user_id);
113 create index idx_community_view_user_id on community_view (user_id);
114 create index idx_community_view_hot_rank_subscribed on community_view (hot_rank desc, number_of_subscribers desc);
117 -- reply and comment view
118 drop view reply_view;
119 drop view user_mention_view;
120 drop view comment_view;
121 create materialized view comment_view as
126 (select community_id from post p where p.id = c.post_id),
127 (select u.banned from user_ u where c.creator_id = u.id) as banned,
128 (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,
129 (select name from user_ where c.creator_id = user_.id) as creator_name,
130 (select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
131 coalesce(sum(cl.score), 0) as score,
132 count (case when cl.score = 1 then 1 else null end) as upvotes,
133 count (case when cl.score = -1 then 1 else null end) as downvotes
135 left join comment_like cl on c.id = cl.comment_id
142 coalesce(cl.score, 0) as my_vote,
143 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
145 cross join all_comment ac
146 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
158 create unique index idx_comment_view_unique on comment_view (id, user_id);
159 create index idx_comment_view_user_id on comment_view (user_id);
160 create index idx_comment_view_creator_id on comment_view (creator_id);
161 create index idx_comment_view_post_id on comment_view (post_id);
162 create index idx_comment_view_score on comment_view (score desc);
164 create view reply_view as
168 c2.creator_id as sender_id,
169 c.creator_id as recipient_id
171 inner join comment c2 on c.id = c2.parent_id
172 where c2.creator_id != c.creator_id
173 -- Do union where post is null
177 c.creator_id as sender_id,
178 p.creator_id as recipient_id
179 from comment c, post p
180 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
183 closereply.recipient_id
184 from comment_view cv, closereply
185 where closereply.id = cv.id
189 create view user_mention_view as
192 um.id as user_mention_id,
204 c.banned_from_community,
214 from user_mention um, comment_view c
215 where um.comment_id = c.id;
218 create or replace function refresh_user()
219 returns trigger language plpgsql
222 refresh materialized view concurrently comment_view; -- cause of bans
223 refresh materialized view concurrently post_view;
227 create trigger refresh_user
228 after insert or update or delete or truncate
231 execute procedure refresh_user();
234 create or replace function refresh_post()
235 returns trigger language plpgsql
238 refresh materialized view concurrently post_view;
242 create trigger refresh_post
243 after insert or update or delete or truncate
246 execute procedure refresh_post();
249 create or replace function refresh_post_like()
250 returns trigger language plpgsql
253 refresh materialized view concurrently post_view;
257 create trigger refresh_post_like
258 after insert or update or delete or truncate
261 execute procedure refresh_post_like();
264 create or replace function refresh_community()
265 returns trigger language plpgsql
268 refresh materialized view concurrently post_view;
269 refresh materialized view concurrently community_view;
273 create trigger refresh_community
274 after insert or update or delete or truncate
277 execute procedure refresh_community();
279 -- community_follower
280 create or replace function refresh_community_follower()
281 returns trigger language plpgsql
284 refresh materialized view concurrently community_view;
285 refresh materialized view concurrently post_view;
289 create trigger refresh_community_follower
290 after insert or update or delete or truncate
291 on community_follower
293 execute procedure refresh_community_follower();
296 create or replace function refresh_comment()
297 returns trigger language plpgsql
300 refresh materialized view concurrently post_view;
301 refresh materialized view concurrently comment_view;
305 create trigger refresh_comment
306 after insert or update or delete or truncate
309 execute procedure refresh_comment();
312 create or replace function refresh_comment_like()
313 returns trigger language plpgsql
316 refresh materialized view concurrently comment_view;
320 create trigger refresh_comment_like
321 after insert or update or delete or truncate
324 execute procedure refresh_comment_like();