]> Untitled Git - lemmy.git/blob - server/migrations/2019-12-29-164820_add_avatar/up.sql
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / server / migrations / 2019-12-29-164820_add_avatar / up.sql
1 -- Rename to avatar
2 alter table user_ rename column icon to avatar;
3 alter table user_ alter column avatar type text;
4
5 -- Rebuild nearly all the views, to include the creator avatars
6
7 -- user
8 drop view user_view;
9 create view user_view as 
10 select id,
11 name,
12 avatar,
13 fedi_name,
14 admin,
15 banned,
16 published,
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
21 from user_ u;
22
23 -- post
24 -- Recreate the view
25 drop view post_view;
26 create view post_view as
27 with all_post as
28 (
29   select        
30   p.*,
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
44   from post p
45   left join post_like pl on p.id = pl.post_id
46   group by p.id
47 )
48
49 select
50 ap.*,
51 u.id as user_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
56 from user_ u
57 cross join all_post ap
58 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
59
60 union all
61
62 select 
63 ap.*,
64 null as user_id,
65 null as my_vote,
66 null as subscribed,
67 null as read,
68 null as saved
69 from all_post ap
70 ;
71
72
73 -- community
74 drop view community_view;
75 create view community_view as 
76 with all_community as
77 (
78   select *,
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
86   from community c
87 )
88
89 select
90 ac.*,
91 u.id as user_id,
92 (select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
93 from user_ u
94 cross join all_community ac
95
96 union all
97
98 select 
99 ac.*,
100 null as user_id,
101 null as subscribed
102 from all_community ac
103 ;
104
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
110 with all_comment as
111 (
112   select        
113   c.*,
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
122   from comment c
123   left join comment_like cl on c.id = cl.comment_id
124   group by c.id
125 )
126
127 select
128 ac.*,
129 u.id as user_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
132 from user_ u
133 cross join all_comment ac
134 left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
135
136 union all
137
138 select 
139     ac.*,
140     null as user_id, 
141     null as my_vote,
142     null as saved
143 from all_comment ac
144 ;
145
146 create view reply_view as 
147 with closereply as (
148     select 
149     c2.id, 
150     c2.creator_id as sender_id, 
151     c.creator_id as recipient_id
152     from comment c
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
156     union
157     select
158     c.id,
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
163 )
164 select cv.*,
165 closereply.recipient_id
166 from comment_view cv, closereply
167 where closereply.id = cv.id
168 ;
169
170 -- user mention
171 create view user_mention_view as
172 select 
173     c.id,
174     um.id as user_mention_id,
175     c.creator_id,
176     c.post_id,
177     c.parent_id,
178     c.content,
179     c.removed,
180     um.read,
181     c.published,
182     c.updated,
183     c.deleted,
184     c.community_id,
185     c.banned,
186     c.banned_from_community,
187     c.creator_name,
188     c.creator_avatar,
189     c.score,
190     c.upvotes,
191     c.downvotes,
192     c.user_id,
193     c.my_vote,
194     c.saved,
195     um.recipient_id
196 from user_mention um, comment_view c
197 where um.comment_id = c.id;
198
199 -- community views
200 drop view community_moderator_view;
201 drop view community_follower_view;
202 drop view community_user_ban_view;
203 drop view site_view;
204
205 create view community_moderator_view as 
206 select *,
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;
211
212 create view community_follower_view as 
213 select *,
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;
218
219 create view community_user_ban_view as 
220 select *,
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;
225
226 create view site_view as 
227 select *,
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
234 from site s;