1 drop view user_mention_view;
2 drop view reply_fast_view;
3 drop view comment_fast_view;
4 drop view comment_view;
6 drop view user_mention_fast_view;
7 drop table comment_aggregates_fast;
8 drop view comment_aggregates_view;
10 create view comment_aggregates_view as
15 c.actor_id as community_actor_id,
16 c."local" as community_local,
17 c."name" as community_name,
20 coalesce(cb.id, 0)::bool as banned_from_community,
21 u.actor_id as creator_actor_id,
22 u.local as creator_local,
23 u.name as creator_name,
24 u.avatar as creator_avatar,
26 coalesce(cl.total, 0) as score,
27 coalesce(cl.up, 0) as upvotes,
28 coalesce(cl.down, 0) as downvotes,
29 hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
31 left join post p on ct.post_id = p.id
32 left join community c on p.community_id = c.id
33 left join user_ u on ct.creator_id = u.id
34 left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
38 sum(l.score) as total,
39 count(case when l.score = 1 then 1 else null end) as up,
40 count(case when l.score = -1 then 1 else null end) as down
43 ) as cl on cl.id = ct.id;
45 create or replace view comment_view as (
48 us.user_id as user_id,
49 us.my_vote as my_vote,
50 us.is_subbed::bool as subscribed,
51 us.is_saved::bool as saved
52 from comment_aggregates_view cav
56 coalesce(cl.score, 0) as my_vote,
57 coalesce(cf.id, 0) as is_subbed,
58 coalesce(cs.id, 0) as is_saved
60 left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
61 left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
62 left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
73 from comment_aggregates_view cav
76 create table comment_aggregates_fast as select * from comment_aggregates_view;
77 alter table comment_aggregates_fast add primary key (id);
79 create view comment_fast_view as
82 us.user_id as user_id,
83 us.my_vote as my_vote,
84 us.is_subbed::bool as subscribed,
85 us.is_saved::bool as saved
86 from comment_aggregates_fast cav
90 coalesce(cl.score, 0) as my_vote,
91 coalesce(cf.id, 0) as is_subbed,
92 coalesce(cs.id, 0) as is_saved
94 left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
95 left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
96 left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
107 from comment_aggregates_fast cav;
109 create view user_mention_view as
112 um.id as user_mention_id,
125 c.community_actor_id,
129 c.banned_from_community,
140 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
141 (select local from user_ u where u.id = um.recipient_id) as recipient_local
142 from user_mention um, comment_view c
143 where um.comment_id = c.id;
145 create view user_mention_fast_view as
148 um.id as user_mention_id,
161 ac.community_actor_id,
165 ac.banned_from_community,
173 coalesce(cl.score, 0) as my_vote,
174 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
176 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
177 (select local from user_ u where u.id = um.recipient_id) as recipient_local
182 from comment_aggregates_fast ca
184 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
185 left join user_mention um on um.comment_id = ac.id
191 um.id as user_mention_id,
204 ac.community_actor_id,
208 ac.banned_from_community,
219 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
220 (select local from user_ u where u.id = um.recipient_id) as recipient_local
221 from comment_aggregates_fast ac
222 left join user_mention um on um.comment_id = ac.id
225 -- Do the reply_view referencing the comment_fast_view
226 create view reply_fast_view as
230 c2.creator_id as sender_id,
231 c.creator_id as recipient_id
233 inner join comment c2 on c.id = c2.parent_id
234 where c2.creator_id != c.creator_id
235 -- Do union where post is null
239 c.creator_id as sender_id,
240 p.creator_id as recipient_id
241 from comment c, post p
242 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
245 closereply.recipient_id
246 from comment_fast_view cv, closereply
247 where closereply.id = cv.id
250 -- add creator_published to the post view
251 drop view post_fast_view;
252 drop table post_aggregates_fast;
254 drop view post_aggregates_view;
256 create view post_aggregates_view as
260 u.actor_id as creator_actor_id,
261 u."local" as creator_local,
262 u."name" as creator_name,
263 u.avatar as creator_avatar,
265 cb.id::bool as banned_from_community,
267 c.actor_id as community_actor_id,
268 c."local" as community_local,
269 c."name" as community_name,
270 c.removed as community_removed,
271 c.deleted as community_deleted,
272 c.nsfw as community_nsfw,
273 -- post score data/comment count
274 coalesce(ct.comments, 0) as number_of_comments,
275 coalesce(pl.score, 0) as score,
276 coalesce(pl.upvotes, 0) as upvotes,
277 coalesce(pl.downvotes, 0) as downvotes,
279 coalesce(pl.score , 0), (
281 when (p.published < ('now'::timestamp - '1 month'::interval))
283 else greatest(ct.recent_comment_time, p.published)
289 when (p.published < ('now'::timestamp - '1 month'::interval))
291 else greatest(ct.recent_comment_time, p.published)
293 ) as newest_activity_time
295 left join user_ u on p.creator_id = u.id
296 left join community_user_ban cb on p.creator_id = cb.user_id and p.community_id = cb.community_id
297 left join community c on p.community_id = c.id
301 count(*) as comments,
302 max(published) as recent_comment_time
305 ) ct on ct.post_id = p.id
310 sum(score) filter (where score = 1) as upvotes,
311 -sum(score) filter (where score = -1) as downvotes
314 ) pl on pl.post_id = p.id
317 create view post_view as
321 us.user_vote as my_vote,
322 us.is_subbed::bool as subscribed,
323 us.is_read::bool as read,
324 us.is_saved::bool as saved
325 from post_aggregates_view pav
329 coalesce(cf.community_id, 0) as is_subbed,
330 coalesce(pr.post_id, 0) as is_read,
331 coalesce(ps.post_id, 0) as is_saved,
332 coalesce(pl.score, 0) as user_vote
334 left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
335 left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
336 left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
337 left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
338 left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
350 from post_aggregates_view pav;
352 create table post_aggregates_fast as select * from post_aggregates_view;
353 alter table post_aggregates_fast add primary key (id);
355 create view post_fast_view as
359 us.user_vote as my_vote,
360 us.is_subbed::bool as subscribed,
361 us.is_read::bool as read,
362 us.is_saved::bool as saved
363 from post_aggregates_fast pav
367 coalesce(cf.community_id, 0) as is_subbed,
368 coalesce(pr.post_id, 0) as is_read,
369 coalesce(ps.post_id, 0) as is_saved,
370 coalesce(pl.score, 0) as user_vote
372 left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
373 left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
374 left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
375 left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
376 left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
388 from post_aggregates_fast pav;