2 alter table post add column embed_title text;
3 alter table post add column embed_description text;
4 alter table post add column embed_html text;
5 alter table post add column thumbnail_url text;
7 -- Regenerate the views
9 -- Adds a newest_activity_time for the post_views, in order to sort by newest comment
12 drop materialized view post_aggregates_mview;
13 drop view post_aggregates_view;
16 create view post_aggregates_view as
19 (select u.banned from user_ u where p.creator_id = u.id) as banned,
20 (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,
21 (select name from user_ where p.creator_id = user_.id) as creator_name,
22 (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
23 (select name from community where p.community_id = community.id) as community_name,
24 (select removed from community c where p.community_id = c.id) as community_removed,
25 (select deleted from community c where p.community_id = c.id) as community_deleted,
26 (select nsfw from community c where p.community_id = c.id) as community_nsfw,
27 (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
28 coalesce(sum(pl.score), 0) as score,
29 count (case when pl.score = 1 then 1 else null end) as upvotes,
30 count (case when pl.score = -1 then 1 else null end) as downvotes,
31 hot_rank(coalesce(sum(pl.score) , 0),
33 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
34 else greatest(c.recent_comment_time, p.published)
39 case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
40 else greatest(c.recent_comment_time, p.published)
42 ) as newest_activity_time
44 left join post_like pl on p.id = pl.post_id
47 max(published) as recent_comment_time
50 ) c on p.id = c.post_id
51 group by p.id, c.recent_comment_time;
53 create materialized view post_aggregates_mview as select * from post_aggregates_view;
55 create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
57 create view post_view as
61 from post_aggregates_view pa
66 coalesce(pl.score, 0) as my_vote,
67 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
68 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
69 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
71 cross join all_post ap
72 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
86 create view post_mview as
90 from post_aggregates_mview pa
95 coalesce(pl.score, 0) as my_vote,
96 (select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
97 (select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
98 (select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
100 cross join all_post ap
101 left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id