2 alter table user_ rename column icon to avatar;
3 alter table user_ alter column avatar type text;
5 -- Rebuild nearly all the views, to include the creator avatars
9 create view user_view as
17 (select count(*) from post p where p.creator_id = u.id) as number_of_posts,
18 (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,
19 (select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
20 (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
26 create view post_view as
31 (select u.banned from user_ u where p.creator_id = u.id) as banned,
32 (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,
33 (select name from user_ where p.creator_id = user_.id) as creator_name,
34 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
35 (select name from community where p.community_id = community.id) as community_name,
36 (select removed from community c where p.community_id = c.id) as community_removed,
37 (select deleted from community c where p.community_id = c.id) as community_deleted,
38 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
39 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
40 coalesce(sum(pl.score), 0) as score,
41 count (case when pl.score = 1 then 1 else null end) as upvotes,
42 count (case when pl.score = -1 then 1 else null end) as downvotes,
43 hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
45 left join post_like pl on p.id = pl.post_id
52 coalesce(pl.score, 0) as my_vote,
53 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
54 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
55 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
57 cross join all_post ap
58 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
74 drop view community_view;
75 create view community_view as
79 (select name from user_ u where c.creator_id = u.id) as creator_name,
80 (select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
81 (select name from category ct where c.category_id = ct.id) as category_name,
82 (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
83 (select count(*) from post p where p.community_id = c.id) as number_of_posts,
84 (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
85 hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
92 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
94 cross join all_community ac
102 from all_community ac
105 -- reply and comment view
106 drop view reply_view;
107 drop view user_mention_view;
108 drop view comment_view;
109 create view comment_view as
114 (select community_id from post p where p.id = c.post_id),
115 (select u.banned from user_ u where c.creator_id = u.id) as banned,
116 (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,
117 (select name from user_ where c.creator_id = user_.id) as creator_name,
118 (select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
119 coalesce(sum(cl.score), 0) as score,
120 count (case when cl.score = 1 then 1 else null end) as upvotes,
121 count (case when cl.score = -1 then 1 else null end) as downvotes
123 left join comment_like cl on c.id = cl.comment_id
130 coalesce(cl.score, 0) as my_vote,
131 (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
133 cross join all_comment ac
134 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
146 create view reply_view as
150 c2.creator_id as sender_id,
151 c.creator_id as recipient_id
153 inner join comment c2 on c.id = c2.parent_id
154 where c2.creator_id != c.creator_id
155 -- Do union where post is null
159 c.creator_id as sender_id,
160 p.creator_id as recipient_id
161 from comment c, post p
162 where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
165 closereply.recipient_id
166 from comment_view cv, closereply
167 where closereply.id = cv.id
171 create view user_mention_view as
174 um.id as user_mention_id,
186 c.banned_from_community,
196 from user_mention um, comment_view c
197 where um.comment_id = c.id;
200 drop view community_moderator_view;
201 drop view community_follower_view;
202 drop view community_user_ban_view;
205 create view community_moderator_view as
207 (select name from user_ u where cm.user_id = u.id) as user_name,
208 (select avatar from user_ u where cm.user_id = u.id),
209 (select name from community c where cm.community_id = c.id) as community_name
210 from community_moderator cm;
212 create view community_follower_view as
214 (select name from user_ u where cf.user_id = u.id) as user_name,
215 (select avatar from user_ u where cf.user_id = u.id),
216 (select name from community c where cf.community_id = c.id) as community_name
217 from community_follower cf;
219 create view community_user_ban_view as
221 (select name from user_ u where cm.user_id = u.id) as user_name,
222 (select avatar from user_ u where cm.user_id = u.id),
223 (select name from community c where cm.community_id = c.id) as community_name
224 from community_user_ban cm;
226 create view site_view as
228 (select name from user_ u where s.creator_id = u.id) as creator_name,
229 (select avatar from user_ u where s.creator_id = u.id) as creator_avatar,
230 (select count(*) from user_) as number_of_users,
231 (select count(*) from post) as number_of_posts,
232 (select count(*) from comment) as number_of_comments,
233 (select count(*) from community) as number_of_communities