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.published as creator_published,
25 u.avatar as creator_avatar,
27 coalesce(cl.total, 0) as score,
28 coalesce(cl.up, 0) as upvotes,
29 coalesce(cl.down, 0) as downvotes,
30 hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
32 left join post p on ct.post_id = p.id
33 left join community c on p.community_id = c.id
34 left join user_ u on ct.creator_id = u.id
35 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
39 sum(l.score) as total,
40 count(case when l.score = 1 then 1 else null end) as up,
41 count(case when l.score = -1 then 1 else null end) as down
44 ) as cl on cl.id = ct.id;
46 create or replace view comment_view as (
49 us.user_id as user_id,
50 us.my_vote as my_vote,
51 us.is_subbed::bool as subscribed,
52 us.is_saved::bool as saved
53 from comment_aggregates_view cav
57 coalesce(cl.score, 0) as my_vote,
58 coalesce(cf.id, 0) as is_subbed,
59 coalesce(cs.id, 0) as is_saved
61 left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
62 left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
63 left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
74 from comment_aggregates_view cav
77 create table comment_aggregates_fast as select * from comment_aggregates_view;
78 alter table comment_aggregates_fast add primary key (id);
80 create view comment_fast_view as
83 us.user_id as user_id,
84 us.my_vote as my_vote,
85 us.is_subbed::bool as subscribed,
86 us.is_saved::bool as saved
87 from comment_aggregates_fast cav
91 coalesce(cl.score, 0) as my_vote,
92 coalesce(cf.id, 0) as is_subbed,
93 coalesce(cs.id, 0) as is_saved
95 left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
96 left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
97 left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
108 from comment_aggregates_fast cav;
110 create view user_mention_view as
113 um.id as user_mention_id,
126 c.community_actor_id,
130 c.banned_from_community,
141 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
142 (select local from user_ u where u.id = um.recipient_id) as recipient_local
143 from user_mention um, comment_view c
144 where um.comment_id = c.id;
146 create view user_mention_fast_view as
149 um.id as user_mention_id,
162 ac.community_actor_id,
166 ac.banned_from_community,
174 coalesce(cl.score, 0) as my_vote,
175 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
177 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
178 (select local from user_ u where u.id = um.recipient_id) as recipient_local
183 from comment_aggregates_fast ca
185 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
186 left join user_mention um on um.comment_id = ac.id
192 um.id as user_mention_id,
205 ac.community_actor_id,
209 ac.banned_from_community,
220 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
221 (select local from user_ u where u.id = um.recipient_id) as recipient_local
222 from comment_aggregates_fast ac
223 left join user_mention um on um.comment_id = ac.id
226 -- Do the reply_view referencing the comment_fast_view
227 create view reply_fast_view as
231 c2.creator_id as sender_id,
232 c.creator_id as recipient_id
234 inner join comment c2 on c.id = c2.parent_id
235 where c2.creator_id != c.creator_id
236 -- Do union where post is null
240 c.creator_id as sender_id,
241 p.creator_id as recipient_id
242 from comment c, post p
243 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
246 closereply.recipient_id
247 from comment_fast_view cv, closereply
248 where closereply.id = cv.id
251 -- add creator_published to the post view
252 drop view post_fast_view;
253 drop table post_aggregates_fast;
255 drop view post_aggregates_view;
257 create view post_aggregates_view as
261 u.actor_id as creator_actor_id,
262 u."local" as creator_local,
263 u."name" as creator_name,
264 u.published as creator_published,
265 u.avatar as creator_avatar,
267 cb.id::bool as banned_from_community,
269 c.actor_id as community_actor_id,
270 c."local" as community_local,
271 c."name" as community_name,
272 c.removed as community_removed,
273 c.deleted as community_deleted,
274 c.nsfw as community_nsfw,
275 -- post score data/comment count
276 coalesce(ct.comments, 0) as number_of_comments,
277 coalesce(pl.score, 0) as score,
278 coalesce(pl.upvotes, 0) as upvotes,
279 coalesce(pl.downvotes, 0) as downvotes,
281 coalesce(pl.score , 0), (
283 when (p.published < ('now'::timestamp - '1 month'::interval))
285 else greatest(ct.recent_comment_time, p.published)
291 when (p.published < ('now'::timestamp - '1 month'::interval))
293 else greatest(ct.recent_comment_time, p.published)
295 ) as newest_activity_time
297 left join user_ u on p.creator_id = u.id
298 left join community_user_ban cb on p.creator_id = cb.user_id and p.community_id = cb.community_id
299 left join community c on p.community_id = c.id
303 count(*) as comments,
304 max(published) as recent_comment_time
307 ) ct on ct.post_id = p.id
312 sum(score) filter (where score = 1) as upvotes,
313 -sum(score) filter (where score = -1) as downvotes
316 ) pl on pl.post_id = p.id
319 create view post_view as
323 us.user_vote as my_vote,
324 us.is_subbed::bool as subscribed,
325 us.is_read::bool as read,
326 us.is_saved::bool as saved
327 from post_aggregates_view pav
331 coalesce(cf.community_id, 0) as is_subbed,
332 coalesce(pr.post_id, 0) as is_read,
333 coalesce(ps.post_id, 0) as is_saved,
334 coalesce(pl.score, 0) as user_vote
336 left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
337 left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
338 left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
339 left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
340 left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
352 from post_aggregates_view pav;
354 create table post_aggregates_fast as select * from post_aggregates_view;
355 alter table post_aggregates_fast add primary key (id);
357 create view post_fast_view as
361 us.user_vote as my_vote,
362 us.is_subbed::bool as subscribed,
363 us.is_read::bool as read,
364 us.is_saved::bool as saved
365 from post_aggregates_fast pav
369 coalesce(cf.community_id, 0) as is_subbed,
370 coalesce(pr.post_id, 0) as is_read,
371 coalesce(ps.post_id, 0) as is_saved,
372 coalesce(pl.score, 0) as user_vote
374 left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
375 left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
376 left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
377 left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
378 left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
390 from post_aggregates_fast pav;