mirror of https://github.com/LemmyNet/lemmy
Adding SQL format checking via `pg_format` / pgFormatter (#3740)
* SQL format checking, 1. * SQL format checking, 2. * SQL format checking, 3. * SQL format checking, 4. * SQL format checking, 5. * Running pg_format * Getting rid of comment. * Upping pg_format version. * Using git ls-files for sql format check. * Fixing sql lints. * Addressing PR comments.pull/3799/head^2
parent
b4380cb548
commit
be1389420b
@ -1,2 +1,4 @@
|
|||||||
drop table user_ban;
|
DROP TABLE user_ban;
|
||||||
drop table user_;
|
|
||||||
|
DROP TABLE user_;
|
||||||
|
|
||||||
|
@ -1,23 +1,25 @@
|
|||||||
create table user_ (
|
CREATE TABLE user_ (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
name varchar(20) not null,
|
name varchar(20) NOT NULL,
|
||||||
fedi_name varchar(40) not null,
|
fedi_name varchar(40) NOT NULL,
|
||||||
preferred_username varchar(20),
|
preferred_username varchar(20),
|
||||||
password_encrypted text not null,
|
password_encrypted text NOT NULL,
|
||||||
email text unique,
|
email text UNIQUE,
|
||||||
icon bytea,
|
icon bytea,
|
||||||
admin boolean default false not null,
|
admin boolean DEFAULT FALSE NOT NULL,
|
||||||
banned boolean default false not null,
|
banned boolean DEFAULT FALSE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
updated timestamp,
|
updated timestamp,
|
||||||
unique(name, fedi_name)
|
UNIQUE (name, fedi_name)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table user_ban (
|
CREATE TABLE user_ban (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique (user_id)
|
UNIQUE (user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
insert into user_ (name, fedi_name, password_encrypted) values ('admin', 'TBD', 'TBD');
|
INSERT INTO user_ (name, fedi_name, password_encrypted)
|
||||||
|
VALUES ('admin', 'TBD', 'TBD');
|
||||||
|
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
drop table site;
|
DROP TABLE site;
|
||||||
drop table community_user_ban;;
|
|
||||||
drop table community_moderator;
|
DROP TABLE community_user_ban;
|
||||||
drop table community_follower;
|
|
||||||
drop table community;
|
;
|
||||||
drop table category;
|
|
||||||
|
DROP TABLE community_moderator;
|
||||||
|
|
||||||
|
DROP TABLE community_follower;
|
||||||
|
|
||||||
|
DROP TABLE community;
|
||||||
|
|
||||||
|
DROP TABLE category;
|
||||||
|
|
||||||
|
@ -1,79 +1,81 @@
|
|||||||
create table category (
|
CREATE TABLE category (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
name varchar(100) not null unique
|
name varchar(100) NOT NULL UNIQUE
|
||||||
);
|
);
|
||||||
|
|
||||||
insert into category (name) values
|
INSERT INTO category (name)
|
||||||
('Discussion'),
|
VALUES ('Discussion'),
|
||||||
('Humor/Memes'),
|
('Humor/Memes'),
|
||||||
('Gaming'),
|
('Gaming'),
|
||||||
('Movies'),
|
('Movies'),
|
||||||
('TV'),
|
('TV'),
|
||||||
('Music'),
|
('Music'),
|
||||||
('Literature'),
|
('Literature'),
|
||||||
('Comics'),
|
('Comics'),
|
||||||
('Photography'),
|
('Photography'),
|
||||||
('Art'),
|
('Art'),
|
||||||
('Learning'),
|
('Learning'),
|
||||||
('DIY'),
|
('DIY'),
|
||||||
('Lifestyle'),
|
('Lifestyle'),
|
||||||
('News'),
|
('News'),
|
||||||
('Politics'),
|
('Politics'),
|
||||||
('Society'),
|
('Society'),
|
||||||
('Gender/Identity/Sexuality'),
|
('Gender/Identity/Sexuality'),
|
||||||
('Race/Colonisation'),
|
('Race/Colonisation'),
|
||||||
('Religion'),
|
('Religion'),
|
||||||
('Science/Technology'),
|
('Science/Technology'),
|
||||||
('Programming/Software'),
|
('Programming/Software'),
|
||||||
('Health/Sports/Fitness'),
|
('Health/Sports/Fitness'),
|
||||||
('Porn'),
|
('Porn'),
|
||||||
('Places'),
|
('Places'),
|
||||||
('Meta'),
|
('Meta'),
|
||||||
('Other');
|
('Other');
|
||||||
|
|
||||||
create table community (
|
CREATE TABLE community (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
name varchar(20) not null unique,
|
name varchar(20) NOT NULL UNIQUE,
|
||||||
title varchar(100) not null,
|
title varchar(100) NOT NULL,
|
||||||
description text,
|
description text,
|
||||||
category_id int references category on update cascade on delete cascade not null,
|
category_id int REFERENCES category ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
creator_id int references user_ on update cascade on delete cascade not null,
|
creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
removed boolean default false not null,
|
removed boolean DEFAULT FALSE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
updated timestamp
|
updated timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
create table community_moderator (
|
CREATE TABLE community_moderator (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
community_id int references community on update cascade on delete cascade not null,
|
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique (community_id, user_id)
|
UNIQUE (community_id, user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table community_follower (
|
CREATE TABLE community_follower (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
community_id int references community on update cascade on delete cascade not null,
|
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique (community_id, user_id)
|
UNIQUE (community_id, user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table community_user_ban (
|
CREATE TABLE community_user_ban (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
community_id int references community on update cascade on delete cascade not null,
|
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique (community_id, user_id)
|
UNIQUE (community_id, user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
insert into community (name, title, category_id, creator_id) values ('main', 'The Default Community', 1, 1);
|
INSERT INTO community (name, title, category_id, creator_id)
|
||||||
|
VALUES ('main', 'The Default Community', 1, 1);
|
||||||
|
|
||||||
create table site (
|
CREATE TABLE site (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
name varchar(20) not null unique,
|
name varchar(20) NOT NULL UNIQUE,
|
||||||
description text,
|
description text,
|
||||||
creator_id int references user_ on update cascade on delete cascade not null,
|
creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
updated timestamp
|
updated timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
drop table post_read;
|
DROP TABLE post_read;
|
||||||
drop table post_saved;
|
|
||||||
drop table post_like;
|
DROP TABLE post_saved;
|
||||||
drop table post;
|
|
||||||
|
DROP TABLE post_like;
|
||||||
|
|
||||||
|
DROP TABLE post;
|
||||||
|
|
||||||
|
@ -1,37 +1,38 @@
|
|||||||
create table post (
|
CREATE TABLE post (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
name varchar(100) not null,
|
name varchar(100) NOT NULL,
|
||||||
url text, -- These are both optional, a post can just have a title
|
url text, -- These are both optional, a post can just have a title
|
||||||
body text,
|
body text,
|
||||||
creator_id int references user_ on update cascade on delete cascade not null,
|
creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
community_id int references community on update cascade on delete cascade not null,
|
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
removed boolean default false not null,
|
removed boolean DEFAULT FALSE NOT NULL,
|
||||||
locked boolean default false not null,
|
locked boolean DEFAULT FALSE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
updated timestamp
|
updated timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
create table post_like (
|
CREATE TABLE post_like (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
post_id int references post on update cascade on delete cascade not null,
|
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
score smallint not null, -- -1, or 1 for dislike, like, no row for no opinion
|
score smallint NOT NULL, -- -1, or 1 for dislike, like, no row for no opinion
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique(post_id, user_id)
|
UNIQUE (post_id, user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table post_saved (
|
CREATE TABLE post_saved (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
post_id int references post on update cascade on delete cascade not null,
|
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique(post_id, user_id)
|
UNIQUE (post_id, user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table post_read (
|
CREATE TABLE post_read (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
post_id int references post on update cascade on delete cascade not null,
|
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique(post_id, user_id)
|
UNIQUE (post_id, user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
drop table comment_saved;
|
DROP TABLE comment_saved;
|
||||||
drop table comment_like;
|
|
||||||
drop table comment;
|
DROP TABLE comment_like;
|
||||||
|
|
||||||
|
DROP TABLE comment;
|
||||||
|
|
||||||
|
@ -1,29 +1,30 @@
|
|||||||
create table comment (
|
CREATE TABLE comment (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
creator_id int references user_ on update cascade on delete cascade not null,
|
creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
post_id int references post on update cascade on delete cascade not null,
|
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
parent_id int references comment on update cascade on delete cascade,
|
parent_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE,
|
||||||
content text not null,
|
content text NOT NULL,
|
||||||
removed boolean default false not null,
|
removed boolean DEFAULT FALSE NOT NULL,
|
||||||
read boolean default false not null,
|
read boolean DEFAULT FALSE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
updated timestamp
|
updated timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
create table comment_like (
|
CREATE TABLE comment_like (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
comment_id int references comment on update cascade on delete cascade not null,
|
comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
post_id int references post on update cascade on delete cascade not null,
|
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
score smallint not null, -- -1, or 1 for dislike, like, no row for no opinion
|
score smallint NOT NULL, -- -1, or 1 for dislike, like, no row for no opinion
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique(comment_id, user_id)
|
UNIQUE (comment_id, user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table comment_saved (
|
CREATE TABLE comment_saved (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
comment_id int references comment on update cascade on delete cascade not null,
|
comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique(comment_id, user_id)
|
UNIQUE (comment_id, user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,2 +1,4 @@
|
|||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
drop function hot_rank;
|
|
||||||
|
DROP FUNCTION hot_rank;
|
||||||
|
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
drop view community_view;
|
DROP VIEW community_view;
|
||||||
drop view community_moderator_view;
|
|
||||||
drop view community_follower_view;
|
DROP VIEW community_moderator_view;
|
||||||
drop view community_user_ban_view;
|
|
||||||
drop view site_view;
|
DROP VIEW community_follower_view;
|
||||||
|
|
||||||
|
DROP VIEW community_user_ban_view;
|
||||||
|
|
||||||
|
DROP VIEW site_view;
|
||||||
|
|
||||||
|
@ -1,2 +1,4 @@
|
|||||||
drop view reply_view;
|
DROP VIEW reply_view;
|
||||||
drop view comment_view;
|
|
||||||
|
DROP VIEW comment_view;
|
||||||
|
|
||||||
|
@ -1,60 +1,114 @@
|
|||||||
create view comment_view as
|
CREATE VIEW comment_view AS
|
||||||
with all_comment as
|
with all_comment AS (
|
||||||
(
|
SELECT
|
||||||
select
|
c.*,
|
||||||
c.*,
|
(
|
||||||
(select community_id from post p where p.id = c.post_id),
|
SELECT
|
||||||
(select u.banned from user_ u where c.creator_id = u.id) as banned,
|
community_id
|
||||||
(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,
|
FROM
|
||||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
post p
|
||||||
coalesce(sum(cl.score), 0) as score,
|
WHERE
|
||||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
p.id = c.post_id),
|
||||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
(
|
||||||
from comment c
|
SELECT
|
||||||
left join comment_like cl on c.id = cl.comment_id
|
u.banned
|
||||||
group by c.id
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS banned,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
c.creator_id = user_.id) AS creator_name,
|
||||||
|
coalesce(sum(cl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN cl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN cl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||||
|
GROUP BY
|
||||||
|
c.id
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_comment ac
|
||||||
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
|
AND ac.id = cl.comment_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_comment ac;
|
||||||
|
|
||||||
select
|
CREATE VIEW reply_view AS
|
||||||
ac.*,
|
with closereply AS (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
c2.id,
|
||||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
c2.creator_id AS sender_id,
|
||||||
from user_ u
|
c.creator_id AS recipient_id
|
||||||
cross join all_comment ac
|
FROM
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
comment c
|
||||||
|
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||||
union all
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
select
|
-- Do union where post is null
|
||||||
ac.*,
|
UNION
|
||||||
null as user_id,
|
SELECT
|
||||||
null as my_vote,
|
c.id,
|
||||||
null as saved
|
c.creator_id AS sender_id,
|
||||||
from all_comment ac
|
p.creator_id AS recipient_id
|
||||||
;
|
FROM
|
||||||
|
comment c,
|
||||||
create view reply_view as
|
post p
|
||||||
with closereply as (
|
WHERE
|
||||||
select
|
c.post_id = p.id
|
||||||
c2.id,
|
AND c.parent_id IS NULL
|
||||||
c2.creator_id as sender_id,
|
AND c.creator_id != p.creator_id
|
||||||
c.creator_id as recipient_id
|
|
||||||
from comment c
|
|
||||||
inner join comment c2 on c.id = c2.parent_id
|
|
||||||
where c2.creator_id != c.creator_id
|
|
||||||
-- Do union where post is null
|
|
||||||
union
|
|
||||||
select
|
|
||||||
c.id,
|
|
||||||
c.creator_id as sender_id,
|
|
||||||
p.creator_id as recipient_id
|
|
||||||
from comment c, post p
|
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
|
||||||
)
|
)
|
||||||
select cv.*,
|
SELECT
|
||||||
closereply.recipient_id
|
cv.*,
|
||||||
from comment_view cv, closereply
|
closereply.recipient_id
|
||||||
where closereply.id = cv.id
|
FROM
|
||||||
;
|
comment_view cv,
|
||||||
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
drop table mod_remove_post;
|
DROP TABLE mod_remove_post;
|
||||||
drop table mod_lock_post;
|
|
||||||
drop table mod_remove_comment;
|
DROP TABLE mod_lock_post;
|
||||||
drop table mod_remove_community;
|
|
||||||
drop table mod_ban;
|
DROP TABLE mod_remove_comment;
|
||||||
drop table mod_ban_from_community;
|
|
||||||
drop table mod_add;
|
DROP TABLE mod_remove_community;
|
||||||
drop table mod_add_community;
|
|
||||||
|
DROP TABLE mod_ban;
|
||||||
|
|
||||||
|
DROP TABLE mod_ban_from_community;
|
||||||
|
|
||||||
|
DROP TABLE mod_add;
|
||||||
|
|
||||||
|
DROP TABLE mod_add_community;
|
||||||
|
|
||||||
|
@ -1,76 +1,76 @@
|
|||||||
create table mod_remove_post (
|
CREATE TABLE mod_remove_post (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
post_id int references post on update cascade on delete cascade not null,
|
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
reason text,
|
reason text,
|
||||||
removed boolean default true,
|
removed boolean DEFAULT TRUE,
|
||||||
when_ timestamp not null default now()
|
when_ timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
create table mod_lock_post (
|
CREATE TABLE mod_lock_post (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
post_id int references post on update cascade on delete cascade not null,
|
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
locked boolean default true,
|
locked boolean DEFAULT TRUE,
|
||||||
when_ timestamp not null default now()
|
when_ timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
create table mod_remove_comment (
|
CREATE TABLE mod_remove_comment (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
comment_id int references comment on update cascade on delete cascade not null,
|
comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
reason text,
|
reason text,
|
||||||
removed boolean default true,
|
removed boolean DEFAULT TRUE,
|
||||||
when_ timestamp not null default now()
|
when_ timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
create table mod_remove_community (
|
CREATE TABLE mod_remove_community (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
community_id int references community on update cascade on delete cascade not null,
|
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
reason text,
|
reason text,
|
||||||
removed boolean default true,
|
removed boolean DEFAULT TRUE,
|
||||||
expires timestamp,
|
expires timestamp,
|
||||||
when_ timestamp not null default now()
|
when_ timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
-- TODO make sure you can't ban other mods
|
-- TODO make sure you can't ban other mods
|
||||||
create table mod_ban_from_community (
|
CREATE TABLE mod_ban_from_community (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
other_user_id int references user_ on update cascade on delete cascade not null,
|
other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
community_id int references community on update cascade on delete cascade not null,
|
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
reason text,
|
reason text,
|
||||||
banned boolean default true,
|
banned boolean DEFAULT TRUE,
|
||||||
expires timestamp,
|
expires timestamp,
|
||||||
when_ timestamp not null default now()
|
when_ timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
create table mod_ban (
|
CREATE TABLE mod_ban (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
other_user_id int references user_ on update cascade on delete cascade not null,
|
other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
reason text,
|
reason text,
|
||||||
banned boolean default true,
|
banned boolean DEFAULT TRUE,
|
||||||
expires timestamp,
|
expires timestamp,
|
||||||
when_ timestamp not null default now()
|
when_ timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
create table mod_add_community (
|
CREATE TABLE mod_add_community (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
other_user_id int references user_ on update cascade on delete cascade not null,
|
other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
community_id int references community on update cascade on delete cascade not null,
|
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
removed boolean default false,
|
removed boolean DEFAULT FALSE,
|
||||||
when_ timestamp not null default now()
|
when_ timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
-- When removed is false that means kicked
|
-- When removed is false that means kicked
|
||||||
create table mod_add (
|
CREATE TABLE mod_add (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
other_user_id int references user_ on update cascade on delete cascade not null,
|
other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
removed boolean default false,
|
removed boolean DEFAULT FALSE,
|
||||||
when_ timestamp not null default now()
|
when_ timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1 +1,2 @@
|
|||||||
drop view user_view;
|
DROP VIEW user_view;
|
||||||
|
|
||||||
|
@ -1,12 +1,43 @@
|
|||||||
create view user_view as
|
CREATE VIEW user_view AS
|
||||||
select id,
|
SELECT
|
||||||
name,
|
id,
|
||||||
fedi_name,
|
name,
|
||||||
admin,
|
fedi_name,
|
||||||
banned,
|
admin,
|
||||||
published,
|
banned,
|
||||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
published,
|
||||||
(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,
|
(
|
||||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
SELECT
|
||||||
(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
|
count(*)
|
||||||
from user_ u;
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.creator_id = u.id) AS number_of_posts,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS number_of_comments,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
FROM
|
||||||
|
user_ u;
|
||||||
|
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
drop view mod_remove_post_view;
|
DROP VIEW mod_remove_post_view;
|
||||||
drop view mod_lock_post_view;
|
|
||||||
drop view mod_remove_comment_view;
|
DROP VIEW mod_lock_post_view;
|
||||||
drop view mod_remove_community_view;
|
|
||||||
drop view mod_ban_from_community_view;
|
DROP VIEW mod_remove_comment_view;
|
||||||
drop view mod_ban_view;
|
|
||||||
drop view mod_add_community_view;
|
DROP VIEW mod_remove_community_view;
|
||||||
drop view mod_add_view;
|
|
||||||
|
DROP VIEW mod_ban_from_community_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_ban_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_add_community_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_add_view;
|
||||||
|
|
||||||
|
@ -1,59 +1,266 @@
|
|||||||
create view mod_remove_post_view as
|
CREATE VIEW mod_remove_post_view AS
|
||||||
select mrp.*,
|
SELECT
|
||||||
(select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
|
mrp.*,
|
||||||
(select name from post p where mrp.post_id = p.id) as post_name,
|
(
|
||||||
(select c.id from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_id,
|
SELECT
|
||||||
(select c.name from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_name
|
name
|
||||||
from mod_remove_post mrp;
|
FROM
|
||||||
|
user_ u
|
||||||
create view mod_lock_post_view as
|
WHERE
|
||||||
select mlp.*,
|
mrp.mod_user_id = u.id) AS mod_user_name,
|
||||||
(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
|
(
|
||||||
(select name from post p where mlp.post_id = p.id) as post_name,
|
SELECT
|
||||||
(select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
|
name
|
||||||
(select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
|
FROM
|
||||||
from mod_lock_post mlp;
|
post p
|
||||||
|
WHERE
|
||||||
create view mod_remove_comment_view as
|
mrp.post_id = p.id) AS post_name,
|
||||||
select mrc.*,
|
(
|
||||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
SELECT
|
||||||
(select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
|
c.id
|
||||||
(select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
|
FROM
|
||||||
(select content from comment c where mrc.comment_id = c.id) as comment_content,
|
post p,
|
||||||
(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
|
community c
|
||||||
(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
|
WHERE
|
||||||
(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,
|
mrp.post_id = p.id
|
||||||
(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
|
AND p.community_id = c.id) AS community_id,
|
||||||
from mod_remove_comment mrc;
|
(
|
||||||
|
SELECT
|
||||||
create view mod_remove_community_view as
|
c.name
|
||||||
select mrc.*,
|
FROM
|
||||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
post p,
|
||||||
(select c.name from community c where mrc.community_id = c.id) as community_name
|
community c
|
||||||
from mod_remove_community mrc;
|
WHERE
|
||||||
|
mrp.post_id = p.id
|
||||||
create view mod_ban_from_community_view as
|
AND p.community_id = c.id) AS community_name
|
||||||
select mb.*,
|
FROM
|
||||||
(select name from user_ u where mb.mod_user_id = u.id) as mod_user_name,
|
mod_remove_post mrp;
|
||||||
(select name from user_ u where mb.other_user_id = u.id) as other_user_name,
|
|
||||||
(select name from community c where mb.community_id = c.id) as community_name
|
CREATE VIEW mod_lock_post_view AS
|
||||||
from mod_ban_from_community mb;
|
SELECT
|
||||||
|
mlp.*,
|
||||||
create view mod_ban_view as
|
(
|
||||||
select mb.*,
|
SELECT
|
||||||
(select name from user_ u where mb.mod_user_id = u.id) as mod_user_name,
|
name
|
||||||
(select name from user_ u where mb.other_user_id = u.id) as other_user_name
|
FROM
|
||||||
from mod_ban mb;
|
user_ u
|
||||||
|
WHERE
|
||||||
create view mod_add_community_view as
|
mlp.mod_user_id = u.id) AS mod_user_name,
|
||||||
select ma.*,
|
(
|
||||||
(select name from user_ u where ma.mod_user_id = u.id) as mod_user_name,
|
SELECT
|
||||||
(select name from user_ u where ma.other_user_id = u.id) as other_user_name,
|
name
|
||||||
(select name from community c where ma.community_id = c.id) as community_name
|
FROM
|
||||||
from mod_add_community ma;
|
post p
|
||||||
|
WHERE
|
||||||
create view mod_add_view as
|
mlp.post_id = p.id) AS post_name,
|
||||||
select ma.*,
|
(
|
||||||
(select name from user_ u where ma.mod_user_id = u.id) as mod_user_name,
|
SELECT
|
||||||
(select name from user_ u where ma.other_user_id = u.id) as other_user_name
|
c.id
|
||||||
from mod_add ma;
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
mlp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
c.name
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
mlp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_lock_post mlp;
|
||||||
|
|
||||||
|
CREATE VIEW mod_remove_comment_view AS
|
||||||
|
SELECT
|
||||||
|
mrc.*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
mrc.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
c.id
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
mrc.comment_id = c.id) AS comment_user_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u,
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
mrc.comment_id = c.id
|
||||||
|
AND u.id = c.creator_id) AS comment_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
content
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
mrc.comment_id = c.id) AS comment_content,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
p.id
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
mrc.comment_id = c.id
|
||||||
|
AND c.post_id = p.id) AS post_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
p.name
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
mrc.comment_id = c.id
|
||||||
|
AND c.post_id = p.id) AS post_name,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
FROM
|
||||||
|
mod_remove_comment mrc;
|
||||||
|
|
||||||
|
CREATE VIEW mod_remove_community_view AS
|
||||||
|
SELECT
|
||||||
|
mrc.*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
mrc.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
c.name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
mrc.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_remove_community mrc;
|
||||||
|
|
||||||
|
CREATE VIEW mod_ban_from_community_view AS
|
||||||
|
SELECT
|
||||||
|
mb.*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
mb.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
mb.other_user_id = u.id) AS other_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
mb.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_ban_from_community mb;
|
||||||
|
|
||||||
|
CREATE VIEW mod_ban_view AS
|
||||||
|
SELECT
|
||||||
|
mb.*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
mb.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
mb.other_user_id = u.id) AS other_user_name
|
||||||
|
FROM
|
||||||
|
mod_ban mb;
|
||||||
|
|
||||||
|
CREATE VIEW mod_add_community_view AS
|
||||||
|
SELECT
|
||||||
|
ma.*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
ma.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
ma.other_user_id = u.id) AS other_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
ma.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_add_community ma;
|
||||||
|
|
||||||
|
CREATE VIEW mod_add_view AS
|
||||||
|
SELECT
|
||||||
|
ma.*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
ma.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
ma.other_user_id = u.id) AS other_user_name
|
||||||
|
FROM
|
||||||
|
mod_add ma;
|
||||||
|
|
||||||
|
@ -1,137 +1,293 @@
|
|||||||
drop view reply_view;
|
DROP VIEW reply_view;
|
||||||
drop view comment_view;
|
|
||||||
drop view community_view;
|
|
||||||
drop view post_view;
|
|
||||||
alter table community drop column deleted;
|
|
||||||
alter table post drop column deleted;
|
|
||||||
alter table comment drop column deleted;
|
|
||||||
|
|
||||||
create view community_view as
|
DROP VIEW comment_view;
|
||||||
with all_community as
|
|
||||||
(
|
|
||||||
select *,
|
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
|
||||||
from community c
|
|
||||||
)
|
|
||||||
|
|
||||||
select
|
|
||||||
ac.*,
|
|
||||||
u.id as user_id,
|
|
||||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
|
||||||
from user_ u
|
|
||||||
cross join all_community ac
|
|
||||||
|
|
||||||
union all
|
DROP VIEW community_view;
|
||||||
|
|
||||||
select
|
DROP VIEW post_view;
|
||||||
ac.*,
|
|
||||||
null as user_id,
|
|
||||||
null as subscribed
|
|
||||||
from all_community ac
|
|
||||||
;
|
|
||||||
|
|
||||||
create or replace view post_view as
|
ALTER TABLE community
|
||||||
with all_post as
|
DROP COLUMN deleted;
|
||||||
(
|
|
||||||
select
|
|
||||||
p.*,
|
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
|
||||||
coalesce(sum(pl.score), 0) as score,
|
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
|
||||||
from post p
|
|
||||||
left join post_like pl on p.id = pl.post_id
|
|
||||||
group by p.id
|
|
||||||
)
|
|
||||||
|
|
||||||
select
|
ALTER TABLE post
|
||||||
ap.*,
|
DROP COLUMN deleted;
|
||||||
u.id as user_id,
|
|
||||||
coalesce(pl.score, 0) as my_vote,
|
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
|
||||||
from user_ u
|
|
||||||
cross join all_post ap
|
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
|
||||||
|
|
||||||
union all
|
ALTER TABLE comment
|
||||||
|
DROP COLUMN deleted;
|
||||||
|
|
||||||
select
|
CREATE VIEW community_view AS
|
||||||
ap.*,
|
with all_community AS (
|
||||||
null as user_id,
|
SELECT
|
||||||
null as my_vote,
|
*,
|
||||||
null as subscribed,
|
(
|
||||||
null as read,
|
SELECT
|
||||||
null as saved
|
name
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
user_ u
|
||||||
|
WHERE
|
||||||
create view comment_view as
|
c.creator_id = u.id) AS creator_name,
|
||||||
with all_comment as
|
(
|
||||||
(
|
SELECT
|
||||||
select
|
name
|
||||||
c.*,
|
FROM
|
||||||
(select community_id from post p where p.id = c.post_id),
|
category ct
|
||||||
(select u.banned from user_ u where c.creator_id = u.id) as banned,
|
WHERE
|
||||||
(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,
|
c.category_id = ct.id) AS category_name,
|
||||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
(
|
||||||
coalesce(sum(cl.score), 0) as score,
|
SELECT
|
||||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
count(*)
|
||||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
FROM
|
||||||
from comment c
|
community_follower cf
|
||||||
left join comment_like cl on c.id = cl.comment_id
|
WHERE
|
||||||
group by c.id
|
cf.community_id = c.id) AS number_of_subscribers,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment co,
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
c.id = p.community_id
|
||||||
|
AND p.id = co.post_id) AS number_of_comments
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND ac.id = cf.community_id) AS subscribed
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_community ac
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS subscribed
|
||||||
|
FROM
|
||||||
|
all_community ac;
|
||||||
|
|
||||||
select
|
CREATE OR REPLACE VIEW post_view AS
|
||||||
ac.*,
|
with all_post AS (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
p.*,
|
||||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
(
|
||||||
from user_ u
|
SELECT
|
||||||
cross join all_comment ac
|
name
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
FROM
|
||||||
|
user_
|
||||||
union all
|
WHERE
|
||||||
|
p.creator_id = user_.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community
|
||||||
|
WHERE
|
||||||
|
p.community_id = community.id) AS community_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
removed
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_removed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
WHERE
|
||||||
|
comment.post_id = p.id) AS number_of_comments,
|
||||||
|
coalesce(sum(pl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes,
|
||||||
|
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||||
|
GROUP BY
|
||||||
|
p.id
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::bool
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
select
|
CREATE VIEW comment_view AS
|
||||||
ac.*,
|
with all_comment AS (
|
||||||
null as user_id,
|
SELECT
|
||||||
null as my_vote,
|
c.*,
|
||||||
null as saved
|
(
|
||||||
from all_comment ac
|
SELECT
|
||||||
;
|
community_id
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.id = c.post_id),
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
u.banned
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS banned,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
c.creator_id = user_.id) AS creator_name,
|
||||||
|
coalesce(sum(cl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN cl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN cl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||||
|
GROUP BY
|
||||||
|
c.id
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_comment ac
|
||||||
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
|
AND ac.id = cl.comment_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_comment ac;
|
||||||
|
|
||||||
create view reply_view as
|
CREATE VIEW reply_view AS
|
||||||
with closereply as (
|
with closereply AS (
|
||||||
select
|
SELECT
|
||||||
c2.id,
|
c2.id,
|
||||||
c2.creator_id as sender_id,
|
c2.creator_id AS sender_id,
|
||||||
c.creator_id as recipient_id
|
c.creator_id AS recipient_id
|
||||||
from comment c
|
FROM
|
||||||
inner join comment c2 on c.id = c2.parent_id
|
comment c
|
||||||
where c2.creator_id != c.creator_id
|
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||||
-- Do union where post is null
|
WHERE
|
||||||
union
|
c2.creator_id != c.creator_id
|
||||||
select
|
-- Do union where post is null
|
||||||
c.id,
|
UNION
|
||||||
c.creator_id as sender_id,
|
SELECT
|
||||||
p.creator_id as recipient_id
|
c.id,
|
||||||
from comment c, post p
|
c.creator_id AS sender_id,
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
p.creator_id AS recipient_id
|
||||||
|
FROM
|
||||||
|
comment c,
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
c.post_id = p.id
|
||||||
|
AND c.parent_id IS NULL
|
||||||
|
AND c.creator_id != p.creator_id
|
||||||
)
|
)
|
||||||
select cv.*,
|
SELECT
|
||||||
closereply.recipient_id
|
cv.*,
|
||||||
from comment_view cv, closereply
|
closereply.recipient_id
|
||||||
where closereply.id = cv.id
|
FROM
|
||||||
;
|
comment_view cv,
|
||||||
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
|
@ -1,141 +1,301 @@
|
|||||||
alter table community add column deleted boolean default false not null;
|
ALTER TABLE community
|
||||||
alter table post add column deleted boolean default false not null;
|
ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
|
||||||
alter table comment add column deleted boolean default false not null;
|
|
||||||
|
|
||||||
-- The views
|
ALTER TABLE post
|
||||||
drop view community_view;
|
ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
|
||||||
|
|
||||||
create view community_view as
|
|
||||||
with all_community as
|
|
||||||
(
|
|
||||||
select *,
|
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
|
||||||
from community c
|
|
||||||
)
|
|
||||||
|
|
||||||
select
|
ALTER TABLE comment
|
||||||
ac.*,
|
ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
|
||||||
u.id as user_id,
|
|
||||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
|
||||||
from user_ u
|
|
||||||
cross join all_community ac
|
|
||||||
|
|
||||||
union all
|
|
||||||
|
|
||||||
select
|
|
||||||
ac.*,
|
|
||||||
null as user_id,
|
|
||||||
null as subscribed
|
|
||||||
from all_community ac
|
|
||||||
;
|
|
||||||
|
|
||||||
|
-- The views
|
||||||
|
DROP VIEW community_view;
|
||||||
|
|
||||||
drop view post_view;
|
CREATE VIEW community_view AS
|
||||||
create view post_view as
|
with all_community AS (
|
||||||
with all_post as
|
SELECT
|
||||||
(
|
*,
|
||||||
select
|
(
|
||||||
p.*,
|
SELECT
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
name
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
FROM
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
user_ u
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
WHERE
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
c.creator_id = u.id) AS creator_name,
|
||||||
coalesce(sum(pl.score), 0) as score,
|
(
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
SELECT
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
name
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
FROM
|
||||||
from post p
|
category ct
|
||||||
left join post_like pl on p.id = pl.post_id
|
WHERE
|
||||||
group by p.id
|
c.category_id = ct.id) AS category_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
cf.community_id = c.id) AS number_of_subscribers,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment co,
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
c.id = p.community_id
|
||||||
|
AND p.id = co.post_id) AS number_of_comments
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND ac.id = cf.community_id) AS subscribed
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_community ac
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS subscribed
|
||||||
|
FROM
|
||||||
|
all_community ac;
|
||||||
|
|
||||||
select
|
DROP VIEW post_view;
|
||||||
ap.*,
|
|
||||||
u.id as user_id,
|
|
||||||
coalesce(pl.score, 0) as my_vote,
|
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
|
||||||
from user_ u
|
|
||||||
cross join all_post ap
|
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
|
||||||
|
|
||||||
union all
|
|
||||||
|
|
||||||
select
|
|
||||||
ap.*,
|
|
||||||
null as user_id,
|
|
||||||
null as my_vote,
|
|
||||||
null as subscribed,
|
|
||||||
null as read,
|
|
||||||
null as saved
|
|
||||||
from all_post ap
|
|
||||||
;
|
|
||||||
|
|
||||||
drop view reply_view;
|
CREATE VIEW post_view AS
|
||||||
drop view comment_view;
|
with all_post AS (
|
||||||
create view comment_view as
|
SELECT
|
||||||
with all_comment as
|
p.*,
|
||||||
(
|
(
|
||||||
select
|
SELECT
|
||||||
c.*,
|
name
|
||||||
(select community_id from post p where p.id = c.post_id),
|
FROM
|
||||||
(select u.banned from user_ u where c.creator_id = u.id) as banned,
|
user_
|
||||||
(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,
|
WHERE
|
||||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
p.creator_id = user_.id) AS creator_name,
|
||||||
coalesce(sum(cl.score), 0) as score,
|
(
|
||||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
SELECT
|
||||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
name
|
||||||
from comment c
|
FROM
|
||||||
left join comment_like cl on c.id = cl.comment_id
|
community
|
||||||
group by c.id
|
WHERE
|
||||||
|
p.community_id = community.id) AS community_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
removed
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_removed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_deleted,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
WHERE
|
||||||
|
comment.post_id = p.id) AS number_of_comments,
|
||||||
|
coalesce(sum(pl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes,
|
||||||
|
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||||
|
GROUP BY
|
||||||
|
p.id
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::bool
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
select
|
DROP VIEW reply_view;
|
||||||
ac.*,
|
|
||||||
u.id as user_id,
|
|
||||||
coalesce(cl.score, 0) as my_vote,
|
|
||||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
|
||||||
from user_ u
|
|
||||||
cross join all_comment ac
|
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
|
||||||
|
|
||||||
union all
|
DROP VIEW comment_view;
|
||||||
|
|
||||||
select
|
CREATE VIEW comment_view AS
|
||||||
ac.*,
|
with all_comment AS (
|
||||||
null as user_id,
|
SELECT
|
||||||
null as my_vote,
|
c.*,
|
||||||
null as saved
|
(
|
||||||
from all_comment ac
|
SELECT
|
||||||
;
|
community_id
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.id = c.post_id),
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
u.banned
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS banned,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
c.creator_id = user_.id) AS creator_name,
|
||||||
|
coalesce(sum(cl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN cl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN cl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||||
|
GROUP BY
|
||||||
|
c.id
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_comment ac
|
||||||
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
|
AND ac.id = cl.comment_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_comment ac;
|
||||||
|
|
||||||
create view reply_view as
|
CREATE VIEW reply_view AS
|
||||||
with closereply as (
|
with closereply AS (
|
||||||
select
|
SELECT
|
||||||
c2.id,
|
c2.id,
|
||||||
c2.creator_id as sender_id,
|
c2.creator_id AS sender_id,
|
||||||
c.creator_id as recipient_id
|
c.creator_id AS recipient_id
|
||||||
from comment c
|
FROM
|
||||||
inner join comment c2 on c.id = c2.parent_id
|
comment c
|
||||||
where c2.creator_id != c.creator_id
|
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||||
-- Do union where post is null
|
WHERE
|
||||||
union
|
c2.creator_id != c.creator_id
|
||||||
select
|
-- Do union where post is null
|
||||||
c.id,
|
UNION
|
||||||
c.creator_id as sender_id,
|
SELECT
|
||||||
p.creator_id as recipient_id
|
c.id,
|
||||||
from comment c, post p
|
c.creator_id AS sender_id,
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
p.creator_id AS recipient_id
|
||||||
|
FROM
|
||||||
|
comment c,
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
c.post_id = p.id
|
||||||
|
AND c.parent_id IS NULL
|
||||||
|
AND c.creator_id != p.creator_id
|
||||||
)
|
)
|
||||||
select cv.*,
|
SELECT
|
||||||
closereply.recipient_id
|
cv.*,
|
||||||
from comment_view cv, closereply
|
closereply.recipient_id
|
||||||
where closereply.id = cv.id
|
FROM
|
||||||
;
|
comment_view cv,
|
||||||
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
|
@ -1 +1,3 @@
|
|||||||
insert into user_ (name, fedi_name, password_encrypted) values ('admin', 'TBD', 'TBD');
|
INSERT INTO user_ (name, fedi_name, password_encrypted)
|
||||||
|
VALUES ('admin', 'TBD', 'TBD');
|
||||||
|
|
||||||
|
@ -1 +1,3 @@
|
|||||||
delete from user_ where name like 'admin';
|
DELETE FROM user_
|
||||||
|
WHERE name LIKE 'admin';
|
||||||
|
|
||||||
|
@ -1,80 +1,190 @@
|
|||||||
drop view community_view;
|
DROP VIEW community_view;
|
||||||
drop view post_view;
|
|
||||||
alter table community drop column nsfw;
|
|
||||||
alter table post drop column nsfw;
|
|
||||||
alter table user_ drop column show_nsfw;
|
|
||||||
|
|
||||||
-- the views
|
DROP VIEW post_view;
|
||||||
create view community_view as
|
|
||||||
with all_community as
|
|
||||||
(
|
|
||||||
select *,
|
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
|
||||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
|
||||||
from community c
|
|
||||||
)
|
|
||||||
|
|
||||||
select
|
ALTER TABLE community
|
||||||
ac.*,
|
DROP COLUMN nsfw;
|
||||||
u.id as user_id,
|
|
||||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
|
||||||
from user_ u
|
|
||||||
cross join all_community ac
|
|
||||||
|
|
||||||
union all
|
ALTER TABLE post
|
||||||
|
DROP COLUMN nsfw;
|
||||||
|
|
||||||
select
|
ALTER TABLE user_
|
||||||
ac.*,
|
DROP COLUMN show_nsfw;
|
||||||
null as user_id,
|
|
||||||
null as subscribed
|
|
||||||
from all_community ac
|
|
||||||
;
|
|
||||||
|
|
||||||
|
-- the views
|
||||||
|
CREATE VIEW community_view AS
|
||||||
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
category ct
|
||||||
|
WHERE
|
||||||
|
c.category_id = ct.id) AS category_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
cf.community_id = c.id) AS number_of_subscribers,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment co,
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
c.id = p.community_id
|
||||||
|
AND p.id = co.post_id) AS number_of_comments,
|
||||||
|
hot_rank ((
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM community_follower cf
|
||||||
|
WHERE
|
||||||
|
cf.community_id = c.id), c.published) AS hot_rank
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND ac.id = cf.community_id) AS subscribed
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_community ac
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS subscribed
|
||||||
|
FROM
|
||||||
|
all_community ac;
|
||||||
|
|
||||||
-- Post view
|
-- Post view
|
||||||
create view post_view as
|
CREATE VIEW post_view AS
|
||||||
with all_post as
|
with all_post AS (
|
||||||
(
|
SELECT
|
||||||
select
|
p.*,
|
||||||
p.*,
|
(
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
SELECT
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
name
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
FROM
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
user_
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
WHERE
|
||||||
coalesce(sum(pl.score), 0) as score,
|
p.creator_id = user_.id) AS creator_name,
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
(
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
SELECT
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
name
|
||||||
from post p
|
FROM
|
||||||
left join post_like pl on p.id = pl.post_id
|
community
|
||||||
group by p.id
|
WHERE
|
||||||
|
p.community_id = community.id) AS community_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
removed
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_removed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_deleted,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
WHERE
|
||||||
|
comment.post_id = p.id) AS number_of_comments,
|
||||||
|
coalesce(sum(pl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes,
|
||||||
|
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||||
|
GROUP BY
|
||||||
|
p.id
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
select
|
ap.*,
|
||||||
ap.*,
|
u.id AS user_id,
|
||||||
u.id as user_id,
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
coalesce(pl.score, 0) as my_vote,
|
(
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
SELECT
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
cf.id::bool
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
FROM
|
||||||
from user_ u
|
community_follower cf
|
||||||
cross join all_post ap
|
WHERE
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
union all
|
(
|
||||||
|
SELECT
|
||||||
select
|
pr.id::bool
|
||||||
ap.*,
|
FROM
|
||||||
null as user_id,
|
post_read pr
|
||||||
null as my_vote,
|
WHERE
|
||||||
null as subscribed,
|
u.id = pr.user_id
|
||||||
null as read,
|
AND pr.post_id = ap.id) AS read,
|
||||||
null as saved
|
(
|
||||||
from all_post ap
|
SELECT
|
||||||
;
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
|
@ -1 +1,3 @@
|
|||||||
alter table user_ drop column theme;
|
ALTER TABLE user_
|
||||||
|
DROP COLUMN theme;
|
||||||
|
|
||||||
|
@ -1 +1,3 @@
|
|||||||
alter table user_ add column theme varchar(20) default 'darkly' not null;
|
ALTER TABLE user_
|
||||||
|
ADD COLUMN theme varchar(20) DEFAULT 'darkly' NOT NULL;
|
||||||
|
|
||||||
|
@ -1,2 +1,4 @@
|
|||||||
drop view user_mention_view;
|
DROP VIEW user_mention_view;
|
||||||
drop table user_mention;
|
|
||||||
|
DROP TABLE user_mention;
|
||||||
|
|
||||||
|
@ -1,2 +1,6 @@
|
|||||||
alter table user_ drop column default_sort_type;
|
ALTER TABLE user_
|
||||||
alter table user_ drop column default_listing_type;
|
DROP COLUMN default_sort_type;
|
||||||
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
DROP COLUMN default_listing_type;
|
||||||
|
|
||||||
|
@ -1,2 +1,6 @@
|
|||||||
alter table user_ add column default_sort_type smallint default 0 not null;
|
ALTER TABLE user_
|
||||||
alter table user_ add column default_listing_type smallint default 1 not null;
|
ADD COLUMN default_sort_type smallint DEFAULT 0 NOT NULL;
|
||||||
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
ADD COLUMN default_listing_type smallint DEFAULT 1 NOT NULL;
|
||||||
|
|
||||||
|
@ -1 +1,2 @@
|
|||||||
drop table password_reset_request;
|
DROP TABLE password_reset_request;
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
create table password_reset_request (
|
CREATE TABLE password_reset_request (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
user_id int references user_ on update cascade on delete cascade not null,
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
token_encrypted text not null,
|
token_encrypted text NOT NULL,
|
||||||
published timestamp not null default now()
|
published timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1 +1,3 @@
|
|||||||
alter table user_ drop column lang;
|
ALTER TABLE user_
|
||||||
|
DROP COLUMN lang;
|
||||||
|
|
||||||
|
@ -1 +1,3 @@
|
|||||||
alter table user_ add column lang varchar(20) default 'browser' not null;
|
ALTER TABLE user_
|
||||||
|
ADD COLUMN lang varchar(20) DEFAULT 'browser' NOT NULL;
|
||||||
|
|
||||||
|
@ -1,15 +1,47 @@
|
|||||||
-- user
|
-- user
|
||||||
drop view user_view;
|
DROP VIEW user_view;
|
||||||
create view user_view as
|
|
||||||
select id,
|
CREATE VIEW user_view AS
|
||||||
name,
|
SELECT
|
||||||
avatar,
|
id,
|
||||||
fedi_name,
|
name,
|
||||||
admin,
|
avatar,
|
||||||
banned,
|
fedi_name,
|
||||||
published,
|
admin,
|
||||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
banned,
|
||||||
(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,
|
published,
|
||||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
(
|
||||||
(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
|
SELECT
|
||||||
from user_ u;
|
count(*)
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.creator_id = u.id) AS number_of_posts,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS number_of_comments,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
FROM
|
||||||
|
user_ u;
|
||||||
|
|
||||||
|
@ -1,16 +1,48 @@
|
|||||||
-- user
|
-- user
|
||||||
drop view user_view;
|
DROP VIEW user_view;
|
||||||
create view user_view as
|
|
||||||
select id,
|
CREATE VIEW user_view AS
|
||||||
name,
|
SELECT
|
||||||
avatar,
|
id,
|
||||||
email,
|
name,
|
||||||
fedi_name,
|
avatar,
|
||||||
admin,
|
email,
|
||||||
banned,
|
fedi_name,
|
||||||
published,
|
admin,
|
||||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
banned,
|
||||||
(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,
|
published,
|
||||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
(
|
||||||
(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
|
SELECT
|
||||||
from user_ u;
|
count(*)
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.creator_id = u.id) AS number_of_posts,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS number_of_comments,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
FROM
|
||||||
|
user_ u;
|
||||||
|
|
||||||
|
@ -1,20 +1,55 @@
|
|||||||
-- Drop the columns
|
-- Drop the columns
|
||||||
drop view user_view;
|
DROP VIEW user_view;
|
||||||
alter table user_ drop column show_avatars;
|
|
||||||
alter table user_ drop column send_notifications_to_email;
|
ALTER TABLE user_
|
||||||
|
DROP COLUMN show_avatars;
|
||||||
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
DROP COLUMN send_notifications_to_email;
|
||||||
|
|
||||||
-- Rebuild the view
|
-- Rebuild the view
|
||||||
create view user_view as
|
CREATE VIEW user_view AS
|
||||||
select id,
|
SELECT
|
||||||
name,
|
id,
|
||||||
avatar,
|
name,
|
||||||
email,
|
avatar,
|
||||||
fedi_name,
|
email,
|
||||||
admin,
|
fedi_name,
|
||||||
banned,
|
admin,
|
||||||
published,
|
banned,
|
||||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
published,
|
||||||
(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,
|
(
|
||||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
SELECT
|
||||||
(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
|
count(*)
|
||||||
from user_ u;
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.creator_id = u.id) AS number_of_posts,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS number_of_comments,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
FROM
|
||||||
|
user_ u;
|
||||||
|
|
||||||
|
@ -1,22 +1,57 @@
|
|||||||
-- Add columns
|
-- Add columns
|
||||||
alter table user_ add column show_avatars boolean default true not null;
|
ALTER TABLE user_
|
||||||
alter table user_ add column send_notifications_to_email boolean default false not null;
|
ADD COLUMN show_avatars boolean DEFAULT TRUE NOT NULL;
|
||||||
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
ADD COLUMN send_notifications_to_email boolean DEFAULT FALSE NOT NULL;
|
||||||
|
|
||||||
-- Rebuild the user_view
|
-- Rebuild the user_view
|
||||||
drop view user_view;
|
DROP VIEW user_view;
|
||||||
create view user_view as
|
|
||||||
select id,
|
CREATE VIEW user_view AS
|
||||||
name,
|
SELECT
|
||||||
avatar,
|
id,
|
||||||
email,
|
name,
|
||||||
fedi_name,
|
avatar,
|
||||||
admin,
|
email,
|
||||||
banned,
|
fedi_name,
|
||||||
show_avatars,
|
admin,
|
||||||
send_notifications_to_email,
|
banned,
|
||||||
published,
|
show_avatars,
|
||||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
send_notifications_to_email,
|
||||||
(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,
|
published,
|
||||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
(
|
||||||
(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
|
SELECT
|
||||||
from user_ u;
|
count(*)
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
p.creator_id = u.id) AS number_of_posts,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS number_of_comments,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
FROM
|
||||||
|
user_ u;
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,34 +1,72 @@
|
|||||||
-- Drop the triggers
|
-- Drop the triggers
|
||||||
drop trigger refresh_private_message on private_message;
|
DROP TRIGGER refresh_private_message ON private_message;
|
||||||
drop function refresh_private_message();
|
|
||||||
|
DROP FUNCTION refresh_private_message ();
|
||||||
|
|
||||||
-- Drop the view and table
|
-- Drop the view and table
|
||||||
drop view private_message_view cascade;
|
DROP VIEW private_message_view CASCADE;
|
||||||
drop table private_message;
|
|
||||||
|
DROP TABLE private_message;
|
||||||
|
|
||||||
-- Rebuild the old views
|
-- Rebuild the old views
|
||||||
drop view user_view cascade;
|
DROP VIEW user_view CASCADE;
|
||||||
create view user_view as
|
|
||||||
select
|
CREATE VIEW user_view AS
|
||||||
u.id,
|
SELECT
|
||||||
u.name,
|
u.id,
|
||||||
u.avatar,
|
u.name,
|
||||||
u.email,
|
u.avatar,
|
||||||
u.fedi_name,
|
u.email,
|
||||||
u.admin,
|
u.fedi_name,
|
||||||
u.banned,
|
u.admin,
|
||||||
u.show_avatars,
|
u.banned,
|
||||||
u.send_notifications_to_email,
|
u.show_avatars,
|
||||||
u.published,
|
u.send_notifications_to_email,
|
||||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
u.published,
|
||||||
(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,
|
(
|
||||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
SELECT
|
||||||
(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
|
count(*)
|
||||||
from user_ u;
|
FROM
|
||||||
|
post p
|
||||||
create materialized view user_mview as select * from user_view;
|
WHERE
|
||||||
|
p.creator_id = u.id) AS number_of_posts,
|
||||||
create unique index idx_user_mview_id on user_mview (id);
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS number_of_comments,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
FROM
|
||||||
|
user_ u;
|
||||||
|
|
||||||
|
CREATE MATERIALIZED VIEW user_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
user_view;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
|
||||||
|
|
||||||
-- Drop the columns
|
-- Drop the columns
|
||||||
alter table user_ drop column matrix_user_id;
|
ALTER TABLE user_
|
||||||
|
DROP COLUMN matrix_user_id;
|
||||||
|
|
||||||
|
@ -1,25 +1,37 @@
|
|||||||
-- Drop the materialized / built views
|
-- Drop the materialized / built views
|
||||||
drop view reply_view;
|
DROP VIEW reply_view;
|
||||||
create view reply_view as
|
|
||||||
with closereply as (
|
CREATE VIEW reply_view AS
|
||||||
select
|
with closereply AS (
|
||||||
c2.id,
|
SELECT
|
||||||
c2.creator_id as sender_id,
|
c2.id,
|
||||||
c.creator_id as recipient_id
|
c2.creator_id AS sender_id,
|
||||||
from comment c
|
c.creator_id AS recipient_id
|
||||||
inner join comment c2 on c.id = c2.parent_id
|
FROM
|
||||||
where c2.creator_id != c.creator_id
|
comment c
|
||||||
-- Do union where post is null
|
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||||
union
|
WHERE
|
||||||
select
|
c2.creator_id != c.creator_id
|
||||||
c.id,
|
-- Do union where post is null
|
||||||
c.creator_id as sender_id,
|
UNION
|
||||||
p.creator_id as recipient_id
|
SELECT
|
||||||
from comment c, post p
|
c.id,
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
c.creator_id AS sender_id,
|
||||||
|
p.creator_id AS recipient_id
|
||||||
|
FROM
|
||||||
|
comment c,
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
c.post_id = p.id
|
||||||
|
AND c.parent_id IS NULL
|
||||||
|
AND c.creator_id != p.creator_id
|
||||||
)
|
)
|
||||||
select cv.*,
|
SELECT
|
||||||
closereply.recipient_id
|
cv.*,
|
||||||
from comment_view cv, closereply
|
closereply.recipient_id
|
||||||
where closereply.id = cv.id
|
FROM
|
||||||
;
|
comment_view cv,
|
||||||
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
|
@ -1,27 +1,38 @@
|
|||||||
-- https://github.com/dessalines/lemmy/issues/197
|
-- https://github.com/dessalines/lemmy/issues/197
|
||||||
drop view reply_view;
|
DROP VIEW reply_view;
|
||||||
|
|
||||||
-- Do the reply_view referencing the comment_mview
|
-- Do the reply_view referencing the comment_mview
|
||||||
create view reply_view as
|
CREATE VIEW reply_view AS
|
||||||
with closereply as (
|
with closereply AS (
|
||||||
select
|
SELECT
|
||||||
c2.id,
|
c2.id,
|
||||||
c2.creator_id as sender_id,
|
c2.creator_id AS sender_id,
|
||||||
c.creator_id as recipient_id
|
c.creator_id AS recipient_id
|
||||||
from comment c
|
FROM
|
||||||
inner join comment c2 on c.id = c2.parent_id
|
comment c
|
||||||
where c2.creator_id != c.creator_id
|
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||||
-- Do union where post is null
|
WHERE
|
||||||
union
|
c2.creator_id != c.creator_id
|
||||||
select
|
-- Do union where post is null
|
||||||
c.id,
|
UNION
|
||||||
c.creator_id as sender_id,
|
SELECT
|
||||||
p.creator_id as recipient_id
|
c.id,
|
||||||
from comment c, post p
|
c.creator_id AS sender_id,
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
p.creator_id AS recipient_id
|
||||||
|
FROM
|
||||||
|
comment c,
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
c.post_id = p.id
|
||||||
|
AND c.parent_id IS NULL
|
||||||
|
AND c.creator_id != p.creator_id
|
||||||
)
|
)
|
||||||
select cv.*,
|
SELECT
|
||||||
closereply.recipient_id
|
cv.*,
|
||||||
from comment_mview cv, closereply
|
closereply.recipient_id
|
||||||
where closereply.id = cv.id
|
FROM
|
||||||
;
|
comment_mview cv,
|
||||||
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
|
@ -1 +1,2 @@
|
|||||||
drop view user_mention_mview;
|
DROP VIEW user_mention_mview;
|
||||||
|
|
||||||
|
@ -1,2 +1,4 @@
|
|||||||
drop index idx_user_name_lower;
|
DROP INDEX idx_user_name_lower;
|
||||||
drop index idx_user_email_lower;
|
|
||||||
|
DROP INDEX idx_user_email_lower;
|
||||||
|
|
||||||
|
@ -1,88 +1,206 @@
|
|||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
drop view post_mview;
|
|
||||||
drop materialized view post_aggregates_mview;
|
|
||||||
drop view post_aggregates_view;
|
|
||||||
|
|
||||||
-- regen post view
|
DROP VIEW post_mview;
|
||||||
create view post_aggregates_view as
|
|
||||||
select
|
|
||||||
p.*,
|
|
||||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
|
||||||
(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,
|
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
|
||||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
|
||||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
|
||||||
coalesce(sum(pl.score), 0) as score,
|
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
|
||||||
from post p
|
|
||||||
left join post_like pl on p.id = pl.post_id
|
|
||||||
group by p.id;
|
|
||||||
|
|
||||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
create view post_view as
|
-- regen post view
|
||||||
with all_post as (
|
CREATE VIEW post_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
pa.*
|
p.*,
|
||||||
from post_aggregates_view pa
|
(
|
||||||
)
|
SELECT
|
||||||
select
|
u.banned
|
||||||
ap.*,
|
FROM
|
||||||
u.id as user_id,
|
user_ u
|
||||||
coalesce(pl.score, 0) as my_vote,
|
WHERE
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
p.creator_id = u.id) AS banned,
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
(
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
SELECT
|
||||||
from user_ u
|
cb.id::bool
|
||||||
cross join all_post ap
|
FROM
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
community_user_ban cb
|
||||||
|
WHERE
|
||||||
|
p.creator_id = cb.user_id
|
||||||
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
p.creator_id = user_.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
p.creator_id = user_.id) AS creator_avatar,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community
|
||||||
|
WHERE
|
||||||
|
p.community_id = community.id) AS community_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
removed
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_removed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_deleted,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
nsfw
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_nsfw,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
WHERE
|
||||||
|
comment.post_id = p.id) AS number_of_comments,
|
||||||
|
coalesce(sum(pl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes,
|
||||||
|
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||||
|
GROUP BY
|
||||||
|
p.id;
|
||||||
|
|
||||||
union all
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
select
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
ap.*,
|
|
||||||
null as user_id,
|
|
||||||
null as my_vote,
|
|
||||||
null as subscribed,
|
|
||||||
null as read,
|
|
||||||
null as saved
|
|
||||||
from all_post ap
|
|
||||||
;
|
|
||||||
|
|
||||||
create view post_mview as
|
CREATE VIEW post_view AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_mview pa
|
FROM
|
||||||
|
post_aggregates_view pa
|
||||||
)
|
)
|
||||||
select
|
SELECT
|
||||||
ap.*,
|
ap.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(pl.score, 0) as my_vote,
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
(
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
SELECT
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
cf.id::bool
|
||||||
from user_ u
|
FROM
|
||||||
cross join all_post ap
|
community_follower cf
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
union all
|
CREATE VIEW post_mview AS
|
||||||
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
ap.*,
|
pa.*
|
||||||
null as user_id,
|
FROM
|
||||||
null as my_vote,
|
post_aggregates_mview pa
|
||||||
null as subscribed,
|
)
|
||||||
null as read,
|
SELECT
|
||||||
null as saved
|
ap.*,
|
||||||
from all_post ap
|
u.id AS user_id,
|
||||||
;
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::bool
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
|
@ -1,106 +1,227 @@
|
|||||||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
drop view post_mview;
|
|
||||||
drop materialized view post_aggregates_mview;
|
|
||||||
drop view post_aggregates_view;
|
|
||||||
|
|
||||||
-- regen post view
|
DROP VIEW post_mview;
|
||||||
create view post_aggregates_view as
|
|
||||||
select
|
|
||||||
p.*,
|
|
||||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
|
||||||
(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,
|
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
|
||||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
|
||||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
|
||||||
coalesce(sum(pl.score), 0) as score,
|
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
|
||||||
hot_rank(coalesce(sum(pl.score) , 0),
|
|
||||||
(
|
|
||||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
|
||||||
else greatest(c.recent_comment_time, p.published)
|
|
||||||
end
|
|
||||||
)
|
|
||||||
) as hot_rank,
|
|
||||||
(
|
|
||||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
|
||||||
else greatest(c.recent_comment_time, p.published)
|
|
||||||
end
|
|
||||||
) as newest_activity_time
|
|
||||||
from post p
|
|
||||||
left join post_like pl on p.id = pl.post_id
|
|
||||||
left join (
|
|
||||||
select post_id,
|
|
||||||
max(published) as recent_comment_time
|
|
||||||
from comment
|
|
||||||
group by 1
|
|
||||||
) c on p.id = c.post_id
|
|
||||||
group by p.id, c.recent_comment_time;
|
|
||||||
|
|
||||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
create view post_view as
|
-- regen post view
|
||||||
with all_post as (
|
CREATE VIEW post_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
pa.*
|
p.*,
|
||||||
from post_aggregates_view pa
|
(
|
||||||
)
|
SELECT
|
||||||
select
|
u.banned
|
||||||
ap.*,
|
FROM
|
||||||
u.id as user_id,
|
user_ u
|
||||||
coalesce(pl.score, 0) as my_vote,
|
WHERE
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
p.creator_id = u.id) AS banned,
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
(
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
SELECT
|
||||||
from user_ u
|
cb.id::bool
|
||||||
cross join all_post ap
|
FROM
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
community_user_ban cb
|
||||||
|
WHERE
|
||||||
|
p.creator_id = cb.user_id
|
||||||
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
p.creator_id = user_.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
p.creator_id = user_.id) AS creator_avatar,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community
|
||||||
|
WHERE
|
||||||
|
p.community_id = community.id) AS community_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
removed
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_removed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_deleted,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
nsfw
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_nsfw,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
WHERE
|
||||||
|
comment.post_id = p.id) AS number_of_comments,
|
||||||
|
coalesce(sum(pl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes,
|
||||||
|
hot_rank (coalesce(sum(pl.score), 0), (
|
||||||
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
|
p.published -- Prevents necro-bumps
|
||||||
|
ELSE
|
||||||
|
greatest (c.recent_comment_time, p.published)
|
||||||
|
END)) AS hot_rank,
|
||||||
|
(
|
||||||
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
|
p.published -- Prevents necro-bumps
|
||||||
|
ELSE
|
||||||
|
greatest (c.recent_comment_time, p.published)
|
||||||
|
END) AS newest_activity_time
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
|
post_id,
|
||||||
|
max(published) AS recent_comment_time
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
GROUP BY
|
||||||
|
1) c ON p.id = c.post_id
|
||||||
|
GROUP BY
|
||||||
|
p.id,
|
||||||
|
c.recent_comment_time;
|
||||||
|
|
||||||
union all
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
select
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
ap.*,
|
|
||||||
null as user_id,
|
|
||||||
null as my_vote,
|
|
||||||
null as subscribed,
|
|
||||||
null as read,
|
|
||||||
null as saved
|
|
||||||
from all_post ap
|
|
||||||
;
|
|
||||||
|
|
||||||
create view post_mview as
|
CREATE VIEW post_view AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_mview pa
|
FROM
|
||||||
|
post_aggregates_view pa
|
||||||
)
|
)
|
||||||
select
|
SELECT
|
||||||
ap.*,
|
ap.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(pl.score, 0) as my_vote,
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
(
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
SELECT
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
cf.id::bool
|
||||||
from user_ u
|
FROM
|
||||||
cross join all_post ap
|
community_follower cf
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
union all
|
CREATE VIEW post_mview AS
|
||||||
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
ap.*,
|
pa.*
|
||||||
null as user_id,
|
FROM
|
||||||
null as my_vote,
|
post_aggregates_mview pa
|
||||||
null as subscribed,
|
)
|
||||||
null as read,
|
SELECT
|
||||||
null as saved
|
ap.*,
|
||||||
from all_post ap
|
u.id AS user_id,
|
||||||
;
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::bool
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
|
@ -1,112 +1,240 @@
|
|||||||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
drop view post_mview;
|
|
||||||
drop materialized view post_aggregates_mview;
|
DROP VIEW post_mview;
|
||||||
drop view post_aggregates_view;
|
|
||||||
|
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||||
|
|
||||||
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
-- Drop the columns
|
-- Drop the columns
|
||||||
alter table post drop column embed_title;
|
ALTER TABLE post
|
||||||
alter table post drop column embed_description;
|
DROP COLUMN embed_title;
|
||||||
alter table post drop column embed_html;
|
|
||||||
alter table post drop column thumbnail_url;
|
|
||||||
|
|
||||||
-- regen post view
|
ALTER TABLE post
|
||||||
create view post_aggregates_view as
|
DROP COLUMN embed_description;
|
||||||
select
|
|
||||||
p.*,
|
|
||||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
|
||||||
(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,
|
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
|
||||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
|
||||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
|
||||||
coalesce(sum(pl.score), 0) as score,
|
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
|
||||||
hot_rank(coalesce(sum(pl.score) , 0),
|
|
||||||
(
|
|
||||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
|
||||||
else greatest(c.recent_comment_time, p.published)
|
|
||||||
end
|
|
||||||
)
|
|
||||||
) as hot_rank,
|
|
||||||
(
|
|
||||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
|
||||||
else greatest(c.recent_comment_time, p.published)
|
|
||||||
end
|
|
||||||
) as newest_activity_time
|
|
||||||
from post p
|
|
||||||
left join post_like pl on p.id = pl.post_id
|
|
||||||
left join (
|
|
||||||
select post_id,
|
|
||||||
max(published) as recent_comment_time
|
|
||||||
from comment
|
|
||||||
group by 1
|
|
||||||
) c on p.id = c.post_id
|
|
||||||
group by p.id, c.recent_comment_time;
|
|
||||||
|
|
||||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
ALTER TABLE post
|
||||||
|
DROP COLUMN embed_html;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
ALTER TABLE post
|
||||||
|
DROP COLUMN thumbnail_url;
|
||||||
|
|
||||||
create view post_view as
|
-- regen post view
|
||||||
with all_post as (
|
CREATE VIEW post_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
pa.*
|
p.*,
|
||||||
from post_aggregates_view pa
|
(
|
||||||
)
|
SELECT
|
||||||
select
|
u.banned
|
||||||
ap.*,
|
FROM
|
||||||
u.id as user_id,
|
user_ u
|
||||||
coalesce(pl.score, 0) as my_vote,
|
WHERE
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
p.creator_id = u.id) AS banned,
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
(
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
SELECT
|
||||||
from user_ u
|
cb.id::bool
|
||||||
cross join all_post ap
|
FROM
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
community_user_ban cb
|
||||||
|
WHERE
|
||||||
|
p.creator_id = cb.user_id
|
||||||
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
p.creator_id = user_.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
p.creator_id = user_.id) AS creator_avatar,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community
|
||||||
|
WHERE
|
||||||
|
p.community_id = community.id) AS community_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
removed
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_removed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_deleted,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
nsfw
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_nsfw,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
WHERE
|
||||||
|
comment.post_id = p.id) AS number_of_comments,
|
||||||
|
coalesce(sum(pl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes,
|
||||||
|
hot_rank (coalesce(sum(pl.score), 0), (
|
||||||
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
|
p.published -- Prevents necro-bumps
|
||||||
|
ELSE
|
||||||
|
greatest (c.recent_comment_time, p.published)
|
||||||
|
END)) AS hot_rank,
|
||||||
|
(
|
||||||
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
|
p.published -- Prevents necro-bumps
|
||||||
|
ELSE
|
||||||
|
greatest (c.recent_comment_time, p.published)
|
||||||
|
END) AS newest_activity_time
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
|
post_id,
|
||||||
|
max(published) AS recent_comment_time
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
GROUP BY
|
||||||
|
1) c ON p.id = c.post_id
|
||||||
|
GROUP BY
|
||||||
|
p.id,
|
||||||
|
c.recent_comment_time;
|
||||||
|
|
||||||
union all
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
select
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
ap.*,
|
|
||||||
null as user_id,
|
|
||||||
null as my_vote,
|
|
||||||
null as subscribed,
|
|
||||||
null as read,
|
|
||||||
null as saved
|
|
||||||
from all_post ap
|
|
||||||
;
|
|
||||||
|
|
||||||
create view post_mview as
|
CREATE VIEW post_view AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_mview pa
|
FROM
|
||||||
|
post_aggregates_view pa
|
||||||
)
|
)
|
||||||
select
|
SELECT
|
||||||
ap.*,
|
ap.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(pl.score, 0) as my_vote,
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
(
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
SELECT
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
cf.id::bool
|
||||||
from user_ u
|
FROM
|
||||||
cross join all_post ap
|
community_follower cf
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
union all
|
CREATE VIEW post_mview AS
|
||||||
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
ap.*,
|
pa.*
|
||||||
null as user_id,
|
FROM
|
||||||
null as my_vote,
|
post_aggregates_mview pa
|
||||||
null as subscribed,
|
)
|
||||||
null as read,
|
SELECT
|
||||||
null as saved
|
ap.*,
|
||||||
from all_post ap
|
u.id AS user_id,
|
||||||
;
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::bool
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
|
@ -1,115 +1,241 @@
|
|||||||
-- Add the columns
|
-- Add the columns
|
||||||
alter table post add column embed_title text;
|
ALTER TABLE post
|
||||||
alter table post add column embed_description text;
|
ADD COLUMN embed_title text;
|
||||||
alter table post add column embed_html text;
|
|
||||||
alter table post add column thumbnail_url text;
|
|
||||||
|
|
||||||
-- Regenerate the views
|
ALTER TABLE post
|
||||||
|
ADD COLUMN embed_description text;
|
||||||
|
|
||||||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
ALTER TABLE post
|
||||||
drop view post_view;
|
ADD COLUMN embed_html text;
|
||||||
drop view post_mview;
|
|
||||||
drop materialized view post_aggregates_mview;
|
|
||||||
drop view post_aggregates_view;
|
|
||||||
|
|
||||||
-- regen post view
|
ALTER TABLE post
|
||||||
create view post_aggregates_view as
|
ADD COLUMN thumbnail_url text;
|
||||||
select
|
|
||||||
p.*,
|
|
||||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
|
||||||
(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,
|
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
|
||||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
|
||||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
|
||||||
coalesce(sum(pl.score), 0) as score,
|
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
|
||||||
hot_rank(coalesce(sum(pl.score) , 0),
|
|
||||||
(
|
|
||||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
|
||||||
else greatest(c.recent_comment_time, p.published)
|
|
||||||
end
|
|
||||||
)
|
|
||||||
) as hot_rank,
|
|
||||||
(
|
|
||||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
|
||||||
else greatest(c.recent_comment_time, p.published)
|
|
||||||
end
|
|
||||||
) as newest_activity_time
|
|
||||||
from post p
|
|
||||||
left join post_like pl on p.id = pl.post_id
|
|
||||||
left join (
|
|
||||||
select post_id,
|
|
||||||
max(published) as recent_comment_time
|
|
||||||
from comment
|
|
||||||
group by 1
|
|
||||||
) c on p.id = c.post_id
|
|
||||||
group by p.id, c.recent_comment_time;
|
|
||||||
|
|
||||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
-- Regenerate the views
|
||||||
|
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||||
|
DROP VIEW post_view;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
DROP VIEW post_mview;
|
||||||
|
|
||||||
create view post_view as
|
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||||
with all_post as (
|
|
||||||
select
|
|
||||||
pa.*
|
|
||||||
from post_aggregates_view pa
|
|
||||||
)
|
|
||||||
select
|
|
||||||
ap.*,
|
|
||||||
u.id as user_id,
|
|
||||||
coalesce(pl.score, 0) as my_vote,
|
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
|
||||||
from user_ u
|
|
||||||
cross join all_post ap
|
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
|
||||||
|
|
||||||
union all
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
select
|
-- regen post view
|
||||||
ap.*,
|
CREATE VIEW post_aggregates_view AS
|
||||||
null as user_id,
|
SELECT
|
||||||
null as my_vote,
|
p.*,
|
||||||
null as subscribed,
|
(
|
||||||
null as read,
|
SELECT
|
||||||
null as saved
|
u.banned
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
user_ u
|
||||||
|
WHERE
|
||||||
|
p.creator_id = u.id) AS banned,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
p.creator_id = user_.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
p.creator_id = user_.id) AS creator_avatar,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community
|
||||||
|
WHERE
|
||||||
|
p.community_id = community.id) AS community_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
removed
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_removed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_deleted,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
nsfw
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
p.community_id = c.id) AS community_nsfw,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
WHERE
|
||||||
|
comment.post_id = p.id) AS number_of_comments,
|
||||||
|
coalesce(sum(pl.score), 0) AS score,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS upvotes,
|
||||||
|
count(
|
||||||
|
CASE WHEN pl.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS downvotes,
|
||||||
|
hot_rank (coalesce(sum(pl.score), 0), (
|
||||||
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
|
p.published -- Prevents necro-bumps
|
||||||
|
ELSE
|
||||||
|
greatest (c.recent_comment_time, p.published)
|
||||||
|
END)) AS hot_rank,
|
||||||
|
(
|
||||||
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
|
p.published -- Prevents necro-bumps
|
||||||
|
ELSE
|
||||||
|
greatest (c.recent_comment_time, p.published)
|
||||||
|
END) AS newest_activity_time
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
|
post_id,
|
||||||
|
max(published) AS recent_comment_time
|
||||||
|
FROM
|
||||||
|
comment
|
||||||
|
GROUP BY
|
||||||
|
1) c ON p.id = c.post_id
|
||||||
|
GROUP BY
|
||||||
|
p.id,
|
||||||
|
c.recent_comment_time;
|
||||||
|
|
||||||
create view post_mview as
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
with all_post as (
|
SELECT
|
||||||
select
|
*
|
||||||
pa.*
|
FROM
|
||||||
from post_aggregates_mview pa
|
post_aggregates_view;
|
||||||
)
|
|
||||||
select
|
|
||||||
ap.*,
|
|
||||||
u.id as user_id,
|
|
||||||
coalesce(pl.score, 0) as my_vote,
|
|
||||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
|
||||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
|
||||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
|
||||||
from user_ u
|
|
||||||
cross join all_post ap
|
|
||||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
|
||||||
|
|
||||||
union all
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
|
|
||||||
select
|
CREATE VIEW post_view AS
|
||||||
ap.*,
|
with all_post AS (
|
||||||
null as user_id,
|
SELECT
|
||||||
null as my_vote,
|
pa.*
|
||||||
null as subscribed,
|
FROM
|
||||||
null as read,
|
post_aggregates_view pa
|
||||||
null as saved
|
)
|
||||||
from all_post ap
|
SELECT
|
||||||
;
|
ap.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::bool
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
|
CREATE VIEW post_mview AS
|
||||||
|
with all_post AS (
|
||||||
|
SELECT
|
||||||
|
pa.*
|
||||||
|
FROM
|
||||||
|
post_aggregates_mview pa
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
coalesce(pl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::bool
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND cf.community_id = ap.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
pr.id::bool
|
||||||
|
FROM
|
||||||
|
post_read pr
|
||||||
|
WHERE
|
||||||
|
u.id = pr.user_id
|
||||||
|
AND pr.post_id = ap.id) AS read,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
ps.id::bool
|
||||||
|
FROM
|
||||||
|
post_saved ps
|
||||||
|
WHERE
|
||||||
|
u.id = ps.user_id
|
||||||
|
AND ps.post_id = ap.id) AS saved
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN all_post ap
|
||||||
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND ap.id = pl.post_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
|
ap.*,
|
||||||
|
NULL AS user_id,
|
||||||
|
NULL AS my_vote,
|
||||||
|
NULL AS subscribed,
|
||||||
|
NULL AS read,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_post ap;
|
||||||
|
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
-- Add federation columns to post, comment
|
-- Add federation columns to post, comment
|
||||||
|
ALTER TABLE post
|
||||||
alter table post
|
|
||||||
-- TODO uniqueness constraints should be added on these 3 columns later
|
-- TODO uniqueness constraints should be added on these 3 columns later
|
||||||
add column ap_id character varying(255) not null default 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
|
ADD COLUMN ap_id character varying(255) NOT NULL DEFAULT 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
|
||||||
add column local boolean not null default true
|
ADD COLUMN local boolean NOT NULL DEFAULT TRUE;
|
||||||
;
|
|
||||||
|
|
||||||
alter table comment
|
ALTER TABLE comment
|
||||||
-- TODO uniqueness constraints should be added on these 3 columns later
|
-- TODO uniqueness constraints should be added on these 3 columns later
|
||||||
add column ap_id character varying(255) not null default 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
|
ADD COLUMN ap_id character varying(255) NOT NULL DEFAULT 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
|
||||||
add column local boolean not null default true
|
ADD COLUMN local boolean NOT NULL DEFAULT TRUE;
|
||||||
;
|
|
||||||
|
|
||||||
|
@ -1,38 +1,70 @@
|
|||||||
-- User table
|
-- User table
|
||||||
|
|
||||||
-- Need to regenerate user_view, user_mview
|
-- Need to regenerate user_view, user_mview
|
||||||
drop view user_view cascade;
|
DROP VIEW user_view CASCADE;
|
||||||
|
|
||||||
-- Remove the fedi_name constraint, drop that useless column
|
-- Remove the fedi_name constraint, drop that useless column
|
||||||
alter table user_
|
ALTER TABLE user_
|
||||||
drop constraint user__name_fedi_name_key;
|
DROP CONSTRAINT user__name_fedi_name_key;
|
||||||
|
|
||||||
alter table user_
|
ALTER TABLE user_
|
||||||
drop column fedi_name;
|
DROP COLUMN fedi_name;
|
||||||
|
|
||||||
-- Community
|
-- Community
|
||||||
alter table community
|
ALTER TABLE community
|
||||||
drop constraint community_name_key;
|
DROP CONSTRAINT community_name_key;
|
||||||
|
|
||||||
create view user_view as
|
CREATE VIEW user_view AS
|
||||||
select
|
SELECT
|
||||||
u.id,
|
u.id,
|
||||||
u.name,
|
u.name,
|
||||||
u.avatar,
|
u.avatar,
|
||||||
u.email,
|
u.email,
|
||||||
u.matrix_user_id,
|
u.matrix_user_id,
|
||||||
u.admin,
|
u.admin,
|
||||||
u.banned,
|
u.banned,
|
||||||
u.show_avatars,
|
u.show_avatars,
|
||||||
u.send_notifications_to_email,
|
u.send_notifications_to_email,
|
||||||
u.published,
|
u.published,
|
||||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
(
|
||||||
(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,
|
SELECT
|
||||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
count(*)
|
||||||
(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
|
FROM
|
||||||
from user_ u;
|
post p
|
||||||
|
WHERE
|
||||||
|
p.creator_id = u.id) AS number_of_posts,
|
||||||
|
(
|
||||||
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS number_of_comments,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
FROM
|
||||||
|
user_ u;
|
||||||
|
|
||||||
create materialized view user_mview as select * from user_view;
|
CREATE MATERIALIZED VIEW user_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
user_view;
|
||||||
|
|
||||||
create unique index idx_user_mview_id on user_mview (id);
|
CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,5 @@
|
|||||||
-- The username index
|
-- The username index
|
||||||
drop index idx_user_name_lower_actor_id;
|
DROP INDEX idx_user_name_lower_actor_id;
|
||||||
create unique index idx_user_name_lower on user_ (lower(name));
|
|
||||||
|
CREATE UNIQUE INDEX idx_user_name_lower ON user_ (lower(name));
|
||||||
|
|
||||||
|
@ -1,2 +1,4 @@
|
|||||||
drop index idx_user_name_lower;
|
DROP INDEX idx_user_name_lower;
|
||||||
create unique index idx_user_name_lower_actor_id on user_ (lower(name), lower(actor_id));
|
|
||||||
|
CREATE UNIQUE INDEX idx_user_name_lower_actor_id ON user_ (lower(name), lower(actor_id));
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue