1 -- Drop the dependent views
4 drop materialized view post_aggregates_mview;
5 drop view post_aggregates_view;
6 drop view mod_remove_post_view;
7 drop view mod_sticky_post_view;
8 drop view mod_lock_post_view;
9 drop view mod_remove_comment_view;
11 -- Add the extra post limit
12 alter table post alter column name type varchar(200);
15 create view post_aggregates_view as
18 (select u.banned from user_ u where p.creator_id = u.id) as banned,
19 (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,
20 (select name from user_ where p.creator_id = user_.id) as creator_name,
21 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
22 (select name from community where p.community_id = community.id) as community_name,
23 (select removed from community c where p.community_id = c.id) as community_removed,
24 (select deleted from community c where p.community_id = c.id) as community_deleted,
25 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
26 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
27 coalesce(sum(pl.score), 0) as score,
28 count (case when pl.score = 1 then 1 else null end) as upvotes,
29 count (case when pl.score = -1 then 1 else null end) as downvotes,
30 hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
32 left join post_like pl on p.id = pl.post_id
35 create materialized view post_aggregates_mview as select * from post_aggregates_view;
37 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
39 create view post_view as
43 from post_aggregates_view pa
48 coalesce(pl.score, 0) as my_vote,
49 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
50 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
51 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
53 cross join all_post ap
54 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
68 create view post_mview as
72 from post_aggregates_mview pa
77 coalesce(pl.score, 0) as my_vote,
78 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
79 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
80 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
82 cross join all_post ap
83 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
99 create view mod_remove_post_view as
101 (select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
102 (select name from post p where mrp.post_id = p.id) as post_name,
103 (select c.id from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_id,
104 (select c.name from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_name
105 from mod_remove_post mrp;
107 create view mod_lock_post_view as
109 (select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
110 (select name from post p where mlp.post_id = p.id) as post_name,
111 (select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
112 (select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
113 from mod_lock_post mlp;
115 create view mod_remove_comment_view as
117 (select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
118 (select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
119 (select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
120 (select content from comment c where mrc.comment_id = c.id) as comment_content,
121 (select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
122 (select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
123 (select co.id from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_id,
124 (select co.name from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_name
125 from mod_remove_comment mrc;
127 create view mod_sticky_post_view as
129 (select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
130 (select name from post p where msp.post_id = p.id) as post_name,
131 (select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
132 (select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
133 from mod_sticky_post msp;