2 drop view user_mention_view;
4 drop view comment_view;
8 create view user_view as
15 (select count(*) from post p where p.creator_id = u.id) as number_of_posts,
16 (select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
17 (select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
18 (select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
24 create view post_view as
29 (select u.banned from user_ u where p.creator_id = u.id) as banned,
30 (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,
31 (select name from user_ where p.creator_id = user_.id) as creator_name,
32 (select name from community where p.community_id = community.id) as community_name,
33 (select removed from community c where p.community_id = c.id) as community_removed,
34 (select deleted from community c where p.community_id = c.id) as community_deleted,
35 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
36 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
37 coalesce(sum(pl.score), 0) as score,
38 count (case when pl.score = 1 then 1 else null end) as upvotes,
39 count (case when pl.score = -1 then 1 else null end) as downvotes,
40 hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
42 left join post_like pl on p.id = pl.post_id
49 coalesce(pl.score, 0) as my_vote,
50 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
51 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
52 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
54 cross join all_post ap
55 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
71 drop view community_view;
72 create view community_view as
76 (select name from user_ u where c.creator_id = u.id) as creator_name,
77 (select name from category ct where c.category_id = ct.id) as category_name,
78 (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
79 (select count(*) from post p where p.community_id = c.id) as number_of_posts,
80 (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
81 hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
88 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
90 cross join all_community ac
101 -- Reply and comment view
102 create view comment_view as
107 (select community_id from post p where p.id = c.post_id),
108 (select u.banned from user_ u where c.creator_id = u.id) as banned,
109 (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
110 (select name from user_ where c.creator_id = user_.id) as creator_name,
111 coalesce(sum(cl.score), 0) as score,
112 count (case when cl.score = 1 then 1 else null end) as upvotes,
113 count (case when cl.score = -1 then 1 else null end) as downvotes
115 left join comment_like cl on c.id = cl.comment_id
122 coalesce(cl.score, 0) as my_vote,
123 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
125 cross join all_comment ac
126 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
138 create view reply_view as
142 c2.creator_id as sender_id,
143 c.creator_id as recipient_id
145 inner join comment c2 on c.id = c2.parent_id
146 where c2.creator_id != c.creator_id
147 -- Do union where post is null
151 c.creator_id as sender_id,
152 p.creator_id as recipient_id
153 from comment c, post p
154 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
157 closereply.recipient_id
158 from comment_view cv, closereply
159 where closereply.id = cv.id
163 create view user_mention_view as
166 um.id as user_mention_id,
178 c.banned_from_community,
187 from user_mention um, comment_view c
188 where um.comment_id = c.id;
191 drop view community_moderator_view;
192 drop view community_follower_view;
193 drop view community_user_ban_view;
196 create view community_moderator_view as
198 (select name from user_ u where cm.user_id = u.id) as user_name,
199 (select name from community c where cm.community_id = c.id) as community_name
200 from community_moderator cm;
202 create view community_follower_view as
204 (select name from user_ u where cf.user_id = u.id) as user_name,
205 (select name from community c where cf.community_id = c.id) as community_name
206 from community_follower cf;
208 create view community_user_ban_view as
210 (select name from user_ u where cm.user_id = u.id) as user_name,
211 (select name from community c where cm.community_id = c.id) as community_name
212 from community_user_ban cm;
214 create view site_view as
216 (select name from user_ u where s.creator_id = u.id) as creator_name,
217 (select count(*) from user_) as number_of_users,
218 (select count(*) from post) as number_of_posts,
219 (select count(*) from comment) as number_of_comments,
220 (select count(*) from community) as number_of_communities
223 alter table user_ rename column avatar to icon;
224 alter table user_ alter column icon type bytea using icon::bytea;