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