2 drop view user_view cascade;
4 create view user_view as
17 u.send_notifications_to_email,
19 (select count(*) from post p where p.creator_id = u.id) as number_of_posts,
20 (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,
21 (select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
22 (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
25 create materialized view user_mview as select * from user_view;
27 create unique index idx_user_mview_id on user_mview (id);
30 drop view community_aggregates_view cascade;
31 create view community_aggregates_view as
32 -- Now that there's public and private keys, you have to be explicit here
47 (select actor_id from user_ u where c.creator_id = u.id) as creator_actor_id,
48 (select local from user_ u where c.creator_id = u.id) as creator_local,
49 (select name from user_ u where c.creator_id = u.id) as creator_name,
50 (select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
51 (select name from category ct where c.category_id = ct.id) as category_name,
52 (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
53 (select count(*) from post p where p.community_id = c.id) as number_of_posts,
54 (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
55 hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
58 create materialized view community_aggregates_mview as select * from community_aggregates_view;
60 create unique index idx_community_aggregates_mview_id on community_aggregates_mview (id);
62 create view community_view as
67 from community_aggregates_view ca
73 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
75 cross join all_community ac
86 create view community_mview as
91 from community_aggregates_mview ca
97 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
99 cross join all_community ac
107 from all_community ac
111 drop view community_moderator_view;
112 drop view community_follower_view;
113 drop view community_user_ban_view;
115 create view community_moderator_view as
117 (select actor_id from user_ u where cm.user_id = u.id) as user_actor_id,
118 (select local from user_ u where cm.user_id = u.id) as user_local,
119 (select name from user_ u where cm.user_id = u.id) as user_name,
120 (select avatar from user_ u where cm.user_id = u.id),
121 (select actor_id from community c where cm.community_id = c.id) as community_actor_id,
122 (select local from community c where cm.community_id = c.id) as community_local,
123 (select name from community c where cm.community_id = c.id) as community_name
124 from community_moderator cm;
126 create view community_follower_view as
128 (select actor_id from user_ u where cf.user_id = u.id) as user_actor_id,
129 (select local from user_ u where cf.user_id = u.id) as user_local,
130 (select name from user_ u where cf.user_id = u.id) as user_name,
131 (select avatar from user_ u where cf.user_id = u.id),
132 (select actor_id from community c where cf.community_id = c.id) as community_actor_id,
133 (select local from community c where cf.community_id = c.id) as community_local,
134 (select name from community c where cf.community_id = c.id) as community_name
135 from community_follower cf;
137 create view community_user_ban_view as
139 (select actor_id from user_ u where cm.user_id = u.id) as user_actor_id,
140 (select local from user_ u where cm.user_id = u.id) as user_local,
141 (select name from user_ u where cm.user_id = u.id) as user_name,
142 (select avatar from user_ u where cm.user_id = u.id),
143 (select actor_id from community c where cm.community_id = c.id) as community_actor_id,
144 (select local from community c where cm.community_id = c.id) as community_local,
145 (select name from community c where cm.community_id = c.id) as community_name
146 from community_user_ban cm;
150 drop view post_mview;
151 drop materialized view post_aggregates_mview;
152 drop view post_aggregates_view;
155 create view post_aggregates_view as
158 (select u.banned from user_ u where p.creator_id = u.id) as banned,
159 (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,
160 (select actor_id from user_ where p.creator_id = user_.id) as creator_actor_id,
161 (select local from user_ where p.creator_id = user_.id) as creator_local,
162 (select name from user_ where p.creator_id = user_.id) as creator_name,
163 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
164 (select actor_id from community where p.community_id = community.id) as community_actor_id,
165 (select local from community where p.community_id = community.id) as community_local,
166 (select name from community where p.community_id = community.id) as community_name,
167 (select removed from community c where p.community_id = c.id) as community_removed,
168 (select deleted from community c where p.community_id = c.id) as community_deleted,
169 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
170 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
171 coalesce(sum(pl.score), 0) as score,
172 count (case when pl.score = 1 then 1 else null end) as upvotes,
173 count (case when pl.score = -1 then 1 else null end) as downvotes,
174 hot_rank(coalesce(sum(pl.score) , 0),
176 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
177 else greatest(c.recent_comment_time, p.published)
182 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
183 else greatest(c.recent_comment_time, p.published)
185 ) as newest_activity_time
187 left join post_like pl on p.id = pl.post_id
190 max(published) as recent_comment_time
193 ) c on p.id = c.post_id
194 group by p.id, c.recent_comment_time;
196 create materialized view post_aggregates_mview as select * from post_aggregates_view;
198 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
200 create view post_view as
204 from post_aggregates_view pa
209 coalesce(pl.score, 0) as my_vote,
210 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
211 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
212 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
214 cross join all_post ap
215 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
229 create view post_mview as
233 from post_aggregates_mview pa
238 coalesce(pl.score, 0) as my_vote,
239 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
240 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
241 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
243 cross join all_post ap
244 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
259 -- reply_view, comment_view, user_mention
260 drop view reply_view;
261 drop view user_mention_view;
262 drop view user_mention_mview;
263 drop view comment_view;
264 drop view comment_mview;
265 drop materialized view comment_aggregates_mview;
266 drop view comment_aggregates_view;
268 -- reply and comment view
269 create view comment_aggregates_view as
272 (select community_id from post p where p.id = c.post_id),
273 (select co.actor_id from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_actor_id,
274 (select co.local from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_local,
275 (select co.name from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_name,
276 (select u.banned from user_ u where c.creator_id = u.id) as banned,
277 (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,
278 (select actor_id from user_ where c.creator_id = user_.id) as creator_actor_id,
279 (select local from user_ where c.creator_id = user_.id) as creator_local,
280 (select name from user_ where c.creator_id = user_.id) as creator_name,
281 (select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
282 coalesce(sum(cl.score), 0) as score,
283 count (case when cl.score = 1 then 1 else null end) as upvotes,
284 count (case when cl.score = -1 then 1 else null end) as downvotes,
285 hot_rank(coalesce(sum(cl.score) , 0), c.published) as hot_rank
287 left join comment_like cl on c.id = cl.comment_id
290 create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
292 create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
294 create view comment_view as
299 from comment_aggregates_view ca
305 coalesce(cl.score, 0) as my_vote,
306 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
307 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
309 cross join all_comment ac
310 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
323 create view comment_mview as
328 from comment_aggregates_mview ca
334 coalesce(cl.score, 0) as my_vote,
335 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
336 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
338 cross join all_comment ac
339 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
352 -- Do the reply_view referencing the comment_mview
353 create view reply_view as
357 c2.creator_id as sender_id,
358 c.creator_id as recipient_id
360 inner join comment c2 on c.id = c2.parent_id
361 where c2.creator_id != c.creator_id
362 -- Do union where post is null
366 c.creator_id as sender_id,
367 p.creator_id as recipient_id
368 from comment c, post p
369 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
372 closereply.recipient_id
373 from comment_mview cv, closereply
374 where closereply.id = cv.id
378 create view user_mention_view as
381 um.id as user_mention_id,
394 c.community_actor_id,
398 c.banned_from_community,
409 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
410 (select local from user_ u where u.id = um.recipient_id) as recipient_local
411 from user_mention um, comment_view c
412 where um.comment_id = c.id;
415 create view user_mention_mview as
420 from comment_aggregates_mview ca
425 um.id as user_mention_id,
438 ac.community_actor_id,
442 ac.banned_from_community,
450 coalesce(cl.score, 0) as my_vote,
451 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
453 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
454 (select local from user_ u where u.id = um.recipient_id) as recipient_local
456 cross join all_comment ac
457 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
458 left join user_mention um on um.comment_id = ac.id
464 um.id as user_mention_id,
477 ac.community_actor_id,
481 ac.banned_from_community,
492 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
493 (select local from user_ u where u.id = um.recipient_id) as recipient_local
495 left join user_mention um on um.comment_id = ac.id