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
14 p."name" as post_name,
17 c.actor_id as community_actor_id,
18 c."local" as community_local,
19 c."name" as community_name,
22 coalesce(cb.id, 0)::bool as banned_from_community,
23 u.actor_id as creator_actor_id,
24 u.local as creator_local,
25 u.name as creator_name,
26 u.published as creator_published,
27 u.avatar as creator_avatar,
29 coalesce(cl.total, 0) as score,
30 coalesce(cl.up, 0) as upvotes,
31 coalesce(cl.down, 0) as downvotes,
32 hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
34 left join post p on ct.post_id = p.id
35 left join community c on p.community_id = c.id
36 left join user_ u on ct.creator_id = u.id
37 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
41 sum(l.score) as total,
42 count(case when l.score = 1 then 1 else null end) as up,
43 count(case when l.score = -1 then 1 else null end) as down
46 ) as cl on cl.id = ct.id;
48 create or replace view comment_view as (
51 us.user_id as user_id,
52 us.my_vote as my_vote,
53 us.is_subbed::bool as subscribed,
54 us.is_saved::bool as saved
55 from comment_aggregates_view cav
59 coalesce(cl.score, 0) as my_vote,
60 coalesce(cf.id, 0) as is_subbed,
61 coalesce(cs.id, 0) as is_saved
63 left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
64 left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
65 left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
76 from comment_aggregates_view cav
79 create table comment_aggregates_fast as select * from comment_aggregates_view;
80 alter table comment_aggregates_fast add primary key (id);
82 create view comment_fast_view as
85 us.user_id as user_id,
86 us.my_vote as my_vote,
87 us.is_subbed::bool as subscribed,
88 us.is_saved::bool as saved
89 from comment_aggregates_fast cav
93 coalesce(cl.score, 0) as my_vote,
94 coalesce(cf.id, 0) as is_subbed,
95 coalesce(cs.id, 0) as is_saved
97 left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
98 left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
99 left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
110 from comment_aggregates_fast cav;
112 create view user_mention_view as
115 um.id as user_mention_id,
129 c.community_actor_id,
133 c.banned_from_community,
144 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
145 (select local from user_ u where u.id = um.recipient_id) as recipient_local
146 from user_mention um, comment_view c
147 where um.comment_id = c.id;
149 create view user_mention_fast_view as
152 um.id as user_mention_id,
166 ac.community_actor_id,
170 ac.banned_from_community,
178 coalesce(cl.score, 0) as my_vote,
179 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
181 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
182 (select local from user_ u where u.id = um.recipient_id) as recipient_local
187 from comment_aggregates_fast ca
189 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
190 left join user_mention um on um.comment_id = ac.id
196 um.id as user_mention_id,
210 ac.community_actor_id,
214 ac.banned_from_community,
225 (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
226 (select local from user_ u where u.id = um.recipient_id) as recipient_local
227 from comment_aggregates_fast ac
228 left join user_mention um on um.comment_id = ac.id
231 -- Do the reply_view referencing the comment_fast_view
232 create view reply_fast_view as
236 c2.creator_id as sender_id,
237 c.creator_id as recipient_id
239 inner join comment c2 on c.id = c2.parent_id
240 where c2.creator_id != c.creator_id
241 -- Do union where post is null
245 c.creator_id as sender_id,
246 p.creator_id as recipient_id
247 from comment c, post p
248 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
251 closereply.recipient_id
252 from comment_fast_view cv, closereply
253 where closereply.id = cv.id