mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-25 22:01:19 +00:00
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.
This commit is contained in:
parent
b4380cb548
commit
be1389420b
341 changed files with 22860 additions and 12364 deletions
|
@ -83,6 +83,18 @@ pipeline:
|
||||||
- rustup component add rustfmt --toolchain nightly-2023-07-10
|
- rustup component add rustfmt --toolchain nightly-2023-07-10
|
||||||
- cargo +nightly-2023-07-10 fmt -- --check
|
- cargo +nightly-2023-07-10 fmt -- --check
|
||||||
|
|
||||||
|
sql_fmt:
|
||||||
|
image: alpine:3
|
||||||
|
commands:
|
||||||
|
- apk add bash wget perl make git
|
||||||
|
- wget https://github.com/darold/pgFormatter/archive/refs/tags/v5.5.tar.gz
|
||||||
|
- tar xzf v5.5.tar.gz
|
||||||
|
- cd pgFormatter-5.5
|
||||||
|
- perl Makefile.PL
|
||||||
|
- make && make install
|
||||||
|
- cd ..
|
||||||
|
- ./scripts/./sql_format_check.sh
|
||||||
|
|
||||||
# make sure api builds with default features (used by other crates relying on lemmy api)
|
# make sure api builds with default features (used by other crates relying on lemmy api)
|
||||||
check_api_common_default_features:
|
check_api_common_default_features:
|
||||||
image: *muslrust_image
|
image: *muslrust_image
|
||||||
|
@ -145,7 +157,7 @@ pipeline:
|
||||||
environment:
|
environment:
|
||||||
CARGO_HOME: .cargo
|
CARGO_HOME: .cargo
|
||||||
commands:
|
commands:
|
||||||
# when adding new clippy lints, make sure to also add them in scripts/fix-clippy.sh
|
# when adding new clippy lints, make sure to also add them in scripts/lint.sh
|
||||||
- rustup component add clippy
|
- rustup component add clippy
|
||||||
- cargo clippy --workspace --tests --all-targets --features console --
|
- cargo clippy --workspace --tests --all-targets --features console --
|
||||||
-D warnings -D deprecated -D clippy::perf -D clippy::complexity
|
-D warnings -D deprecated -D clippy::perf -D clippy::complexity
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
-- This file was automatically created by Diesel to setup helper functions
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
-- and other internal bookkeeping. This file is safe to edit, any future
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
-- changes will be added to existing projects as new migrations.
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
DROP FUNCTION IF EXISTS diesel_manage_updated_at (_tbl regclass);
|
DROP FUNCTION IF EXISTS diesel_manage_updated_at (_tbl regclass);
|
||||||
|
|
||||||
DROP FUNCTION IF EXISTS diesel_set_updated_at ();
|
DROP FUNCTION IF EXISTS diesel_set_updated_at ();
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
-- This file was automatically created by Diesel to setup helper functions
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
-- and other internal bookkeeping. This file is safe to edit, any future
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
-- changes will be added to existing projects as new migrations.
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- Sets up a trigger for the given table to automatically set a column called
|
-- Sets up a trigger for the given table to automatically set a column called
|
||||||
-- `updated_at` whenever the row is modified (unless `updated_at` was included
|
-- `updated_at` whenever the row is modified (unless `updated_at` was included
|
||||||
-- in the modified columns)
|
-- in the modified columns)
|
||||||
|
@ -16,21 +12,25 @@
|
||||||
--
|
--
|
||||||
-- SELECT diesel_manage_updated_at('users');
|
-- SELECT diesel_manage_updated_at('users');
|
||||||
-- ```
|
-- ```
|
||||||
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
|
CREATE OR REPLACE FUNCTION diesel_manage_updated_at (_tbl regclass)
|
||||||
|
RETURNS VOID
|
||||||
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
||||||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
|
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql;
|
$$
|
||||||
|
LANGUAGE plpgsql;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
|
CREATE OR REPLACE FUNCTION diesel_set_updated_at ()
|
||||||
|
RETURNS TRIGGER
|
||||||
|
AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
IF (
|
IF (NEW IS DISTINCT FROM OLD AND NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at) THEN
|
||||||
NEW IS DISTINCT FROM OLD AND
|
NEW.updated_at := CURRENT_TIMESTAMP;
|
||||||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
|
|
||||||
) THEN
|
|
||||||
NEW.updated_at := current_timestamp;
|
|
||||||
END IF;
|
END IF;
|
||||||
RETURN NEW;
|
RETURN NEW;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql;
|
$$
|
||||||
|
LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
|
|
@ -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,10 +1,10 @@
|
||||||
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'),
|
||||||
|
@ -31,49 +31,51 @@ insert into category (name) values
|
||||||
('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,51 +1,107 @@
|
||||||
-- Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
|
-- Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
|
||||||
create or replace function hot_rank(
|
CREATE OR REPLACE FUNCTION hot_rank (score numeric, published timestamp without time zone)
|
||||||
score numeric,
|
RETURNS integer
|
||||||
published timestamp without time zone)
|
AS $$
|
||||||
returns integer as $$
|
BEGIN
|
||||||
begin
|
|
||||||
-- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
|
-- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
|
||||||
return floor(10000*log(greatest(1,score+3)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8))::integer;
|
RETURN floor(10000 * log(greatest (1, score + 3)) / power(((EXTRACT(EPOCH FROM (timezone('utc', now()) - published)) / 3600) + 2), 1.8))::integer;
|
||||||
end; $$
|
END;
|
||||||
|
$$
|
||||||
LANGUAGE plpgsql;
|
LANGUAGE plpgsql;
|
||||||
|
|
||||||
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 name from community where p.community_id = community.id) as community_name,
|
SELECT
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
name
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
FROM
|
||||||
coalesce(sum(pl.score), 0) as score,
|
user_
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
WHERE
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
p.creator_id = user_.id) AS creator_name,
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
(
|
||||||
from post p
|
SELECT
|
||||||
left join post_like pl on p.id = pl.post_id
|
name
|
||||||
group by p.id
|
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
|
||||||
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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
|
|
|
@ -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,53 +1,154 @@
|
||||||
create view community_view as
|
CREATE VIEW community_view AS
|
||||||
with all_community as
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
FROM
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
user_ u
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
WHERE
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
c.creator_id = u.id) AS creator_name,
|
||||||
from community c
|
(
|
||||||
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join all_community ac
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
union all
|
community_follower cf
|
||||||
|
WHERE
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from all_community ac
|
FROM
|
||||||
;
|
all_community ac;
|
||||||
|
|
||||||
create view community_moderator_view as
|
CREATE VIEW community_moderator_view AS
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where cm.user_id = u.id) as user_name,
|
*,
|
||||||
(select name from community c where cm.community_id = c.id) as community_name
|
(
|
||||||
from community_moderator cm;
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cm.user_id = u.id) AS user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
cm.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
community_moderator cm;
|
||||||
|
|
||||||
create view community_follower_view as
|
CREATE VIEW community_follower_view AS
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where cf.user_id = u.id) as user_name,
|
*,
|
||||||
(select name from community c where cf.community_id = c.id) as community_name
|
(
|
||||||
from community_follower cf;
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cf.user_id = u.id) AS user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
cf.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
community_follower cf;
|
||||||
|
|
||||||
create view community_user_ban_view as
|
CREATE VIEW community_user_ban_view AS
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where cm.user_id = u.id) as user_name,
|
*,
|
||||||
(select name from community c where cm.community_id = c.id) as community_name
|
(
|
||||||
from community_user_ban cm;
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cm.user_id = u.id) AS user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
cm.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
community_user_ban cm;
|
||||||
|
|
||||||
|
CREATE VIEW site_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
s.creator_id = u.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
user_) AS number_of_users,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment) AS number_of_comments
|
||||||
|
FROM
|
||||||
|
site s;
|
||||||
|
|
||||||
create view site_view as
|
|
||||||
select *,
|
|
||||||
(select name from user_ u where s.creator_id = u.id) as creator_name,
|
|
||||||
(select count(*) from user_) as number_of_users,
|
|
||||||
(select count(*) from post) as number_of_posts,
|
|
||||||
(select count(*) from comment) as number_of_comments
|
|
||||||
from site s;
|
|
||||||
|
|
|
@ -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 u.banned from user_ u where c.creator_id = u.id) as banned,
|
SELECT
|
||||||
(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,
|
community_id
|
||||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
FROM
|
||||||
coalesce(sum(cl.score), 0) as score,
|
post p
|
||||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
WHERE
|
||||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
p.id = c.post_id),
|
||||||
from comment c
|
(
|
||||||
left join comment_like cl on c.id = cl.comment_id
|
SELECT
|
||||||
group by c.id
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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
|
SELECT
|
||||||
cross join all_comment ac
|
cs.id::bool
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
FROM
|
||||||
|
comment_saved cs
|
||||||
union all
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_comment ac
|
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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
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
|
||||||
|
id,
|
||||||
name,
|
name,
|
||||||
fedi_name,
|
fedi_name,
|
||||||
admin,
|
admin,
|
||||||
banned,
|
banned,
|
||||||
published,
|
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;
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
WHERE
|
||||||
|
mrp.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
c.name
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
mrp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_remove_post mrp;
|
||||||
|
|
||||||
create view mod_lock_post_view as
|
CREATE VIEW mod_lock_post_view AS
|
||||||
select mlp.*,
|
SELECT
|
||||||
(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
|
mlp.*,
|
||||||
(select name from post p where mlp.post_id = p.id) as post_name,
|
(
|
||||||
(select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
|
SELECT
|
||||||
(select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
|
name
|
||||||
from mod_lock_post mlp;
|
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
|
||||||
|
c.id
|
||||||
|
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
|
CREATE VIEW mod_remove_comment_view AS
|
||||||
select mrc.*,
|
SELECT
|
||||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
mrc.*,
|
||||||
(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
|
||||||
(select content from comment c where mrc.comment_id = c.id) as comment_content,
|
name
|
||||||
(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
|
FROM
|
||||||
(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
|
user_ u
|
||||||
(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,
|
WHERE
|
||||||
(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
|
mrc.mod_user_id = u.id) AS mod_user_name,
|
||||||
from mod_remove_comment mrc;
|
(
|
||||||
|
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
|
CREATE VIEW mod_remove_community_view AS
|
||||||
select mrc.*,
|
SELECT
|
||||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
mrc.*,
|
||||||
(select c.name from community c where mrc.community_id = c.id) as community_name
|
(
|
||||||
from mod_remove_community 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
|
CREATE VIEW mod_ban_from_community_view AS
|
||||||
select mb.*,
|
SELECT
|
||||||
(select name from user_ u where mb.mod_user_id = u.id) as mod_user_name,
|
mb.*,
|
||||||
(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
|
SELECT
|
||||||
from mod_ban_from_community mb;
|
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
|
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,
|
mb.*,
|
||||||
(select name from user_ u where mb.other_user_id = u.id) as other_user_name
|
(
|
||||||
from mod_ban 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
|
CREATE VIEW mod_add_community_view AS
|
||||||
select ma.*,
|
SELECT
|
||||||
(select name from user_ u where ma.mod_user_id = u.id) as mod_user_name,
|
ma.*,
|
||||||
(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
|
SELECT
|
||||||
from mod_add_community ma;
|
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;
|
||||||
|
|
||||||
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
|
|
||||||
|
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
|
||||||
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
FROM
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
user_ u
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
WHERE
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
c.creator_id = u.id) AS creator_name,
|
||||||
from community c
|
(
|
||||||
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
|
||||||
;
|
|
||||||
|
|
||||||
create or replace view post_view as
|
|
||||||
with all_post as
|
|
||||||
(
|
(
|
||||||
select
|
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;
|
||||||
|
|
||||||
|
CREATE OR REPLACE VIEW post_view AS
|
||||||
|
with all_post AS (
|
||||||
|
SELECT
|
||||||
p.*,
|
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
|
|
||||||
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 comment_view as
|
|
||||||
with all_comment as
|
|
||||||
(
|
(
|
||||||
select
|
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
|
||||||
|
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 comment_view AS
|
||||||
|
with all_comment AS (
|
||||||
|
SELECT
|
||||||
c.*,
|
c.*,
|
||||||
(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
|
||||||
(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,
|
community_id
|
||||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
FROM
|
||||||
coalesce(sum(cl.score), 0) as score,
|
post p
|
||||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
WHERE
|
||||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
p.id = c.post_id),
|
||||||
from comment c
|
(
|
||||||
left join comment_like cl on c.id = cl.comment_id
|
SELECT
|
||||||
group by c.id
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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
|
SELECT
|
||||||
cross join all_comment ac
|
cs.id::bool
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
FROM
|
||||||
|
comment_saved cs
|
||||||
union all
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_comment ac
|
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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
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;
|
|
||||||
|
ALTER TABLE post
|
||||||
|
ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
|
||||||
|
|
||||||
|
ALTER TABLE comment
|
||||||
|
ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
|
||||||
|
|
||||||
-- The views
|
-- The views
|
||||||
drop view community_view;
|
DROP VIEW community_view;
|
||||||
|
|
||||||
create view community_view as
|
CREATE VIEW community_view AS
|
||||||
with all_community as
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
FROM
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
user_ u
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
WHERE
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
c.creator_id = u.id) AS creator_name,
|
||||||
from community c
|
(
|
||||||
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
|
||||||
;
|
|
||||||
|
|
||||||
|
|
||||||
drop view post_view;
|
|
||||||
create view post_view as
|
|
||||||
with all_post as
|
|
||||||
(
|
(
|
||||||
select
|
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;
|
||||||
|
|
||||||
|
DROP VIEW post_view;
|
||||||
|
|
||||||
|
CREATE VIEW post_view AS
|
||||||
|
with all_post AS (
|
||||||
|
SELECT
|
||||||
p.*,
|
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 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
|
|
||||||
;
|
|
||||||
|
|
||||||
drop view reply_view;
|
|
||||||
drop view comment_view;
|
|
||||||
create view comment_view as
|
|
||||||
with all_comment as
|
|
||||||
(
|
(
|
||||||
select
|
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
|
||||||
|
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;
|
||||||
|
|
||||||
|
DROP VIEW reply_view;
|
||||||
|
|
||||||
|
DROP VIEW comment_view;
|
||||||
|
|
||||||
|
CREATE VIEW comment_view AS
|
||||||
|
with all_comment AS (
|
||||||
|
SELECT
|
||||||
c.*,
|
c.*,
|
||||||
(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
|
||||||
(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,
|
community_id
|
||||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
FROM
|
||||||
coalesce(sum(cl.score), 0) as score,
|
post p
|
||||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
WHERE
|
||||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
p.id = c.post_id),
|
||||||
from comment c
|
(
|
||||||
left join comment_like cl on c.id = cl.comment_id
|
SELECT
|
||||||
group by c.id
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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
|
SELECT
|
||||||
cross join all_comment ac
|
cs.id::bool
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
FROM
|
||||||
|
comment_saved cs
|
||||||
union all
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_comment ac
|
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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_view cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,68 @@
|
||||||
drop view community_view;
|
DROP VIEW community_view;
|
||||||
create view community_view as
|
|
||||||
with all_community as
|
CREATE VIEW community_view AS
|
||||||
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
FROM
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
user_ u
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
WHERE
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
c.creator_id = u.id) AS creator_name,
|
||||||
from community c
|
(
|
||||||
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join all_community ac
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
union all
|
community_follower cf
|
||||||
|
WHERE
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from all_community ac
|
FROM
|
||||||
;
|
all_community ac;
|
||||||
|
|
||||||
|
|
|
@ -1,29 +1,74 @@
|
||||||
drop view community_view;
|
DROP VIEW community_view;
|
||||||
create view community_view as
|
|
||||||
with all_community as
|
CREATE VIEW community_view AS
|
||||||
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
FROM
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
user_ u
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
WHERE
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
c.creator_id = u.id) AS creator_name,
|
||||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
(
|
||||||
from community c
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join all_community ac
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
union all
|
community_follower cf
|
||||||
|
WHERE
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from all_community ac
|
FROM
|
||||||
;
|
all_community ac;
|
||||||
|
|
||||||
|
|
|
@ -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;
|
DROP VIEW post_view;
|
||||||
alter table post drop column nsfw;
|
|
||||||
alter table user_ drop column show_nsfw;
|
ALTER TABLE community
|
||||||
|
DROP COLUMN nsfw;
|
||||||
|
|
||||||
|
ALTER TABLE post
|
||||||
|
DROP COLUMN nsfw;
|
||||||
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
DROP COLUMN show_nsfw;
|
||||||
|
|
||||||
-- the views
|
-- the views
|
||||||
create view community_view as
|
CREATE VIEW community_view AS
|
||||||
with all_community as
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
FROM
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
user_ u
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
WHERE
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
c.creator_id = u.id) AS creator_name,
|
||||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
(
|
||||||
from community c
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join all_community ac
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
union all
|
community_follower cf
|
||||||
|
WHERE
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from all_community ac
|
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 name from community where p.community_id = community.id) as community_name,
|
SELECT
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
name
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
FROM
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
user_
|
||||||
coalesce(sum(pl.score), 0) as score,
|
WHERE
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
p.creator_id = user_.id) AS creator_name,
|
||||||
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
|
SELECT
|
||||||
from post p
|
name
|
||||||
left join post_like pl on p.id = pl.post_id
|
FROM
|
||||||
group by p.id
|
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
|
||||||
|
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 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
|
|
|
@ -1,79 +1,197 @@
|
||||||
alter table community add column nsfw boolean default false not null;
|
ALTER TABLE community
|
||||||
alter table post add column nsfw boolean default false not null;
|
ADD COLUMN nsfw boolean DEFAULT FALSE NOT NULL;
|
||||||
alter table user_ add column show_nsfw boolean default false not null;
|
|
||||||
|
ALTER TABLE post
|
||||||
|
ADD COLUMN nsfw boolean DEFAULT FALSE NOT NULL;
|
||||||
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
ADD COLUMN show_nsfw boolean DEFAULT FALSE NOT NULL;
|
||||||
|
|
||||||
-- The views
|
-- The views
|
||||||
drop view community_view;
|
DROP VIEW community_view;
|
||||||
create view community_view as
|
|
||||||
with all_community as
|
CREATE VIEW community_view AS
|
||||||
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
FROM
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
user_ u
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
WHERE
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
c.creator_id = u.id) AS creator_name,
|
||||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
(
|
||||||
from community c
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join all_community ac
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
union all
|
community_follower cf
|
||||||
|
WHERE
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from all_community ac
|
FROM
|
||||||
;
|
all_community ac;
|
||||||
|
|
||||||
-- Post view
|
-- Post view
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
create view post_view as
|
|
||||||
with all_post as
|
CREATE VIEW post_view AS
|
||||||
(
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
name
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
FROM
|
||||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
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
|
||||||
|
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
|
||||||
)
|
)
|
||||||
|
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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,30 @@
|
||||||
drop view site_view;
|
DROP VIEW site_view;
|
||||||
|
|
||||||
|
CREATE VIEW site_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
s.creator_id = u.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
user_) AS number_of_users,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment) AS number_of_comments
|
||||||
|
FROM
|
||||||
|
site s;
|
||||||
|
|
||||||
create view site_view as
|
|
||||||
select *,
|
|
||||||
(select name from user_ u where s.creator_id = u.id) as creator_name,
|
|
||||||
(select count(*) from user_) as number_of_users,
|
|
||||||
(select count(*) from post) as number_of_posts,
|
|
||||||
(select count(*) from comment) as number_of_comments
|
|
||||||
from site s;
|
|
||||||
|
|
|
@ -1,10 +1,35 @@
|
||||||
drop view site_view;
|
DROP VIEW site_view;
|
||||||
|
|
||||||
|
CREATE VIEW site_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
s.creator_id = u.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
user_) AS number_of_users,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment) AS number_of_comments,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
community) AS number_of_communities
|
||||||
|
FROM
|
||||||
|
site s;
|
||||||
|
|
||||||
create view site_view as
|
|
||||||
select *,
|
|
||||||
(select name from user_ u where s.creator_id = u.id) as creator_name,
|
|
||||||
(select count(*) from user_) as number_of_users,
|
|
||||||
(select count(*) from post) as number_of_posts,
|
|
||||||
(select count(*) from comment) as number_of_comments,
|
|
||||||
(select count(*) from community) as number_of_communities
|
|
||||||
from site s;
|
|
||||||
|
|
|
@ -1,44 +1,113 @@
|
||||||
-- Post view
|
-- Post view
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
create view post_view as
|
|
||||||
with all_post as
|
CREATE VIEW post_view AS
|
||||||
(
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
name
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
FROM
|
||||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
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
|
||||||
|
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
|
||||||
)
|
)
|
||||||
|
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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
|
|
|
@ -1,47 +1,128 @@
|
||||||
-- Create post view, adding banned_from_community
|
-- Create post view, adding banned_from_community
|
||||||
|
DROP VIEW post_view;
|
||||||
|
|
||||||
drop 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 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
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
u.banned
|
||||||
(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 nsfw from community c where p.community_id = c.id) as community_nsfw,
|
p.creator_id = u.id) AS banned,
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
(
|
||||||
coalesce(sum(pl.score), 0) as score,
|
SELECT
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
cb.id::bool
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
FROM
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
community_user_ban cb
|
||||||
from post p
|
WHERE
|
||||||
left join post_like pl on p.id = pl.post_id
|
p.creator_id = cb.user_id
|
||||||
group by p.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
|
||||||
|
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
|
||||||
)
|
)
|
||||||
|
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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
|
|
|
@ -1,50 +1,134 @@
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
drop view mod_sticky_post_view;
|
|
||||||
alter table post drop column stickied;
|
|
||||||
|
|
||||||
drop table mod_sticky_post;
|
DROP VIEW mod_sticky_post_view;
|
||||||
|
|
||||||
create view post_view as
|
ALTER TABLE post
|
||||||
with all_post as
|
DROP COLUMN stickied;
|
||||||
(
|
|
||||||
select
|
DROP TABLE mod_sticky_post;
|
||||||
|
|
||||||
|
CREATE VIEW post_view AS
|
||||||
|
with all_post AS (
|
||||||
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
u.banned
|
||||||
(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 nsfw from community c where p.community_id = c.id) as community_nsfw,
|
p.creator_id = u.id) AS banned,
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
(
|
||||||
coalesce(sum(pl.score), 0) as score,
|
SELECT
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
cb.id::bool
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
FROM
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
community_user_ban cb
|
||||||
from post p
|
WHERE
|
||||||
left join post_like pl on p.id = pl.post_id
|
p.creator_id = cb.user_id
|
||||||
group by p.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
|
||||||
|
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
|
||||||
)
|
)
|
||||||
|
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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
|
|
|
@ -1,67 +1,180 @@
|
||||||
-- Add the column
|
-- Add the column
|
||||||
alter table post add column stickied boolean default false not null;
|
ALTER TABLE post
|
||||||
|
ADD COLUMN stickied boolean DEFAULT FALSE NOT NULL;
|
||||||
|
|
||||||
-- Add the mod table
|
-- Add the mod table
|
||||||
create table mod_sticky_post (
|
CREATE TABLE mod_sticky_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,
|
||||||
stickied boolean default true,
|
stickied boolean DEFAULT TRUE,
|
||||||
when_ timestamp not null default now()
|
when_ timestamp NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Add mod view
|
-- Add mod view
|
||||||
create view mod_sticky_post_view as
|
CREATE VIEW mod_sticky_post_view AS
|
||||||
select msp.*,
|
SELECT
|
||||||
(select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
|
msp.*,
|
||||||
(select name from post p where msp.post_id = p.id) as post_name,
|
(
|
||||||
(select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
|
SELECT
|
||||||
(select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
|
name
|
||||||
from mod_sticky_post msp;
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
msp.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
msp.post_id = p.id) AS post_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
c.id
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
msp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
c.name
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
msp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_sticky_post msp;
|
||||||
|
|
||||||
-- Recreate the view
|
-- Recreate the view
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
create view post_view as
|
|
||||||
with all_post as
|
CREATE VIEW post_view AS
|
||||||
(
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
u.banned
|
||||||
(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 nsfw from community c where p.community_id = c.id) as community_nsfw,
|
p.creator_id = u.id) AS banned,
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
(
|
||||||
coalesce(sum(pl.score), 0) as score,
|
SELECT
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
cb.id::bool
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
FROM
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
community_user_ban cb
|
||||||
from post p
|
WHERE
|
||||||
left join post_like pl on p.id = pl.post_id
|
p.creator_id = cb.user_id
|
||||||
group by p.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
|
||||||
|
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
|
||||||
)
|
)
|
||||||
|
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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
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,16 +1,16 @@
|
||||||
create table user_mention (
|
CREATE TABLE user_mention (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
recipient_id int references user_ on update cascade on delete cascade not null,
|
recipient_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,
|
||||||
read boolean default false not null,
|
read boolean DEFAULT FALSE NOT NULL,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
unique(recipient_id, comment_id)
|
UNIQUE (recipient_id, comment_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.post_id,
|
c.post_id,
|
||||||
c.parent_id,
|
c.parent_id,
|
||||||
|
@ -31,5 +31,9 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from user_mention um, comment_view c
|
FROM
|
||||||
where um.comment_id = c.id;
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
|
|
|
@ -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,16 +1,46 @@
|
||||||
-- Drop the columns
|
-- Drop the columns
|
||||||
drop view site_view;
|
DROP VIEW site_view;
|
||||||
alter table site drop column enable_downvotes;
|
|
||||||
alter table site drop column open_registration;
|
ALTER TABLE site
|
||||||
alter table site drop column enable_nsfw;
|
DROP COLUMN enable_downvotes;
|
||||||
|
|
||||||
|
ALTER TABLE site
|
||||||
|
DROP COLUMN open_registration;
|
||||||
|
|
||||||
|
ALTER TABLE site
|
||||||
|
DROP COLUMN enable_nsfw;
|
||||||
|
|
||||||
-- Rebuild the views
|
-- Rebuild the views
|
||||||
|
CREATE VIEW site_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
s.creator_id = u.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
user_) AS number_of_users,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment) AS number_of_comments,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
community) AS number_of_communities
|
||||||
|
FROM
|
||||||
|
site s;
|
||||||
|
|
||||||
create view site_view as
|
|
||||||
select *,
|
|
||||||
(select name from user_ u where s.creator_id = u.id) as creator_name,
|
|
||||||
(select count(*) from user_) as number_of_users,
|
|
||||||
(select count(*) from post) as number_of_posts,
|
|
||||||
(select count(*) from comment) as number_of_comments,
|
|
||||||
(select count(*) from community) as number_of_communities
|
|
||||||
from site s;
|
|
||||||
|
|
|
@ -1,16 +1,46 @@
|
||||||
-- Add the column
|
-- Add the column
|
||||||
alter table site add column enable_downvotes boolean default true not null;
|
ALTER TABLE site
|
||||||
alter table site add column open_registration boolean default true not null;
|
ADD COLUMN enable_downvotes boolean DEFAULT TRUE NOT NULL;
|
||||||
alter table site add column enable_nsfw boolean default true not null;
|
|
||||||
|
ALTER TABLE site
|
||||||
|
ADD COLUMN open_registration boolean DEFAULT TRUE NOT NULL;
|
||||||
|
|
||||||
|
ALTER TABLE site
|
||||||
|
ADD COLUMN enable_nsfw boolean DEFAULT TRUE NOT NULL;
|
||||||
|
|
||||||
-- Reload the view
|
-- Reload the view
|
||||||
drop view site_view;
|
DROP VIEW site_view;
|
||||||
|
|
||||||
|
CREATE VIEW site_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
s.creator_id = u.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
user_) AS number_of_users,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment) AS number_of_comments,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
community) AS number_of_communities
|
||||||
|
FROM
|
||||||
|
site s;
|
||||||
|
|
||||||
create view site_view as
|
|
||||||
select *,
|
|
||||||
(select name from user_ u where s.creator_id = u.id) as creator_name,
|
|
||||||
(select count(*) from user_) as number_of_users,
|
|
||||||
(select count(*) from post) as number_of_posts,
|
|
||||||
(select count(*) from comment) as number_of_comments,
|
|
||||||
(select count(*) from community) as number_of_communities
|
|
||||||
from site s;
|
|
||||||
|
|
|
@ -1,169 +1,380 @@
|
||||||
-- the views
|
-- the views
|
||||||
drop view user_mention_view;
|
DROP VIEW user_mention_view;
|
||||||
drop view reply_view;
|
|
||||||
drop view comment_view;
|
DROP VIEW reply_view;
|
||||||
drop view user_view;
|
|
||||||
|
DROP VIEW comment_view;
|
||||||
|
|
||||||
|
DROP VIEW user_view;
|
||||||
|
|
||||||
-- user
|
-- user
|
||||||
create view user_view as
|
CREATE VIEW user_view AS
|
||||||
select id,
|
SELECT
|
||||||
|
id,
|
||||||
name,
|
name,
|
||||||
fedi_name,
|
fedi_name,
|
||||||
admin,
|
admin,
|
||||||
banned,
|
banned,
|
||||||
published,
|
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;
|
||||||
|
|
||||||
-- post
|
-- post
|
||||||
-- Recreate the view
|
-- Recreate the view
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
create view post_view as
|
|
||||||
with all_post as
|
CREATE VIEW post_view AS
|
||||||
(
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
u.banned
|
||||||
(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 nsfw from community c where p.community_id = c.id) as community_nsfw,
|
p.creator_id = u.id) AS banned,
|
||||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
(
|
||||||
coalesce(sum(pl.score), 0) as score,
|
SELECT
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
cb.id::bool
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
FROM
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
community_user_ban cb
|
||||||
from post p
|
WHERE
|
||||||
left join post_like pl on p.id = pl.post_id
|
p.creator_id = cb.user_id
|
||||||
group by p.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
|
||||||
|
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
|
||||||
)
|
)
|
||||||
|
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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
-- community
|
-- community
|
||||||
|
DROP VIEW community_view;
|
||||||
|
|
||||||
drop view community_view;
|
CREATE VIEW community_view AS
|
||||||
create view community_view as
|
with all_community AS (
|
||||||
with all_community as
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
FROM
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
user_ u
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
WHERE
|
||||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
c.creator_id = u.id) AS creator_name,
|
||||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
(
|
||||||
from community c
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join all_community ac
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
union all
|
community_follower cf
|
||||||
|
WHERE
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from all_community ac
|
FROM
|
||||||
;
|
all_community ac;
|
||||||
|
|
||||||
-- Reply and comment view
|
-- Reply and comment view
|
||||||
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 u.banned from user_ u where c.creator_id = u.id) as banned,
|
SELECT
|
||||||
(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,
|
community_id
|
||||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
FROM
|
||||||
coalesce(sum(cl.score), 0) as score,
|
post p
|
||||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
WHERE
|
||||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
p.id = c.post_id),
|
||||||
from comment c
|
(
|
||||||
left join comment_like cl on c.id = cl.comment_id
|
SELECT
|
||||||
group by c.id
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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
|
SELECT
|
||||||
cross join all_comment ac
|
cs.id::bool
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
FROM
|
||||||
|
comment_saved cs
|
||||||
union all
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_comment ac
|
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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_view cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
-- user mention
|
-- user mention
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.post_id,
|
c.post_id,
|
||||||
c.parent_id,
|
c.parent_id,
|
||||||
|
@ -184,41 +395,117 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from user_mention um, comment_view c
|
FROM
|
||||||
where um.comment_id = c.id;
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
-- community tables
|
-- community tables
|
||||||
drop view community_moderator_view;
|
DROP VIEW community_moderator_view;
|
||||||
drop view community_follower_view;
|
|
||||||
drop view community_user_ban_view;
|
|
||||||
drop view site_view;
|
|
||||||
|
|
||||||
create view community_moderator_view as
|
DROP VIEW community_follower_view;
|
||||||
select *,
|
|
||||||
(select name from user_ u where cm.user_id = u.id) as user_name,
|
|
||||||
(select name from community c where cm.community_id = c.id) as community_name
|
|
||||||
from community_moderator cm;
|
|
||||||
|
|
||||||
create view community_follower_view as
|
DROP VIEW community_user_ban_view;
|
||||||
select *,
|
|
||||||
(select name from user_ u where cf.user_id = u.id) as user_name,
|
|
||||||
(select name from community c where cf.community_id = c.id) as community_name
|
|
||||||
from community_follower cf;
|
|
||||||
|
|
||||||
create view community_user_ban_view as
|
DROP VIEW site_view;
|
||||||
select *,
|
|
||||||
(select name from user_ u where cm.user_id = u.id) as user_name,
|
|
||||||
(select name from community c where cm.community_id = c.id) as community_name
|
|
||||||
from community_user_ban cm;
|
|
||||||
|
|
||||||
create view site_view as
|
CREATE VIEW community_moderator_view AS
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where s.creator_id = u.id) as creator_name,
|
*,
|
||||||
(select count(*) from user_) as number_of_users,
|
(
|
||||||
(select count(*) from post) as number_of_posts,
|
SELECT
|
||||||
(select count(*) from comment) as number_of_comments,
|
name
|
||||||
(select count(*) from community) as number_of_communities
|
FROM
|
||||||
from site s;
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cm.user_id = u.id) AS user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
cm.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
community_moderator cm;
|
||||||
|
|
||||||
|
CREATE VIEW community_follower_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cf.user_id = u.id) AS user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
cf.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
community_follower cf;
|
||||||
|
|
||||||
|
CREATE VIEW community_user_ban_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cm.user_id = u.id) AS user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
cm.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
community_user_ban cm;
|
||||||
|
|
||||||
|
CREATE VIEW site_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
s.creator_id = u.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
user_) AS number_of_users,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment) AS number_of_comments,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
community) AS number_of_communities
|
||||||
|
FROM
|
||||||
|
site s;
|
||||||
|
|
||||||
|
ALTER TABLE user_ RENAME COLUMN avatar TO icon;
|
||||||
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
ALTER COLUMN icon TYPE bytea
|
||||||
|
USING icon::bytea;
|
||||||
|
|
||||||
alter table user_ rename column avatar to icon;
|
|
||||||
alter table user_ alter column icon type bytea using icon::bytea;
|
|
||||||
|
|
|
@ -1,177 +1,408 @@
|
||||||
-- Rename to avatar
|
-- Rename to avatar
|
||||||
alter table user_ rename column icon to avatar;
|
ALTER TABLE user_ RENAME COLUMN icon TO avatar;
|
||||||
alter table user_ alter column avatar type text;
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
ALTER COLUMN avatar TYPE text;
|
||||||
|
|
||||||
-- Rebuild nearly all the views, to include the creator avatars
|
-- Rebuild nearly all the views, to include the creator avatars
|
||||||
|
|
||||||
-- user
|
-- user
|
||||||
drop view user_view;
|
DROP VIEW user_view;
|
||||||
create view user_view as
|
|
||||||
select id,
|
CREATE VIEW user_view AS
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
name,
|
name,
|
||||||
avatar,
|
avatar,
|
||||||
fedi_name,
|
fedi_name,
|
||||||
admin,
|
admin,
|
||||||
banned,
|
banned,
|
||||||
published,
|
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;
|
||||||
|
|
||||||
-- post
|
-- post
|
||||||
-- Recreate the view
|
-- Recreate the view
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
create view post_view as
|
|
||||||
with all_post as
|
CREATE VIEW post_view AS
|
||||||
(
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
u.banned
|
||||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
FROM
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
user_ u
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
WHERE
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
p.creator_id = u.id) AS banned,
|
||||||
(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,
|
SELECT
|
||||||
coalesce(sum(pl.score), 0) as score,
|
cb.id::bool
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
FROM
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
community_user_ban cb
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
WHERE
|
||||||
from post p
|
p.creator_id = cb.user_id
|
||||||
left join post_like pl on p.id = pl.post_id
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
group by p.id
|
(
|
||||||
|
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
|
||||||
)
|
)
|
||||||
|
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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
|
|
||||||
-- community
|
-- community
|
||||||
drop view community_view;
|
DROP VIEW community_view;
|
||||||
create view community_view as
|
|
||||||
with all_community as
|
CREATE VIEW community_view AS
|
||||||
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
|
FROM
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
user_ u
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
WHERE
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
c.creator_id = u.id) AS creator_name,
|
||||||
(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
|
SELECT
|
||||||
from community c
|
avatar
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS creator_avatar,
|
||||||
|
(
|
||||||
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join all_community ac
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
union all
|
community_follower cf
|
||||||
|
WHERE
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from all_community ac
|
FROM
|
||||||
;
|
all_community ac;
|
||||||
|
|
||||||
-- reply and comment view
|
-- reply and comment view
|
||||||
drop view reply_view;
|
DROP VIEW reply_view;
|
||||||
drop view user_mention_view;
|
|
||||||
drop view comment_view;
|
DROP VIEW user_mention_view;
|
||||||
create view comment_view as
|
|
||||||
with all_comment as
|
DROP VIEW comment_view;
|
||||||
(
|
|
||||||
select
|
CREATE VIEW comment_view AS
|
||||||
|
with all_comment AS (
|
||||||
|
SELECT
|
||||||
c.*,
|
c.*,
|
||||||
(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
|
||||||
(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,
|
community_id
|
||||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
FROM
|
||||||
(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
c.creator_id = user_.id) AS creator_avatar,
|
||||||
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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
|
SELECT
|
||||||
cross join all_comment ac
|
cs.id::bool
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
FROM
|
||||||
|
comment_saved cs
|
||||||
union all
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_comment ac
|
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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_view cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
-- user mention
|
-- user mention
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.post_id,
|
c.post_id,
|
||||||
c.parent_id,
|
c.parent_id,
|
||||||
|
@ -193,42 +424,136 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from user_mention um, comment_view c
|
FROM
|
||||||
where um.comment_id = c.id;
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
-- community views
|
-- community views
|
||||||
drop view community_moderator_view;
|
DROP VIEW community_moderator_view;
|
||||||
drop view community_follower_view;
|
|
||||||
drop view community_user_ban_view;
|
|
||||||
drop view site_view;
|
|
||||||
|
|
||||||
create view community_moderator_view as
|
DROP VIEW community_follower_view;
|
||||||
select *,
|
|
||||||
(select name from user_ u where cm.user_id = u.id) as user_name,
|
|
||||||
(select avatar from user_ u where cm.user_id = u.id),
|
|
||||||
(select name from community c where cm.community_id = c.id) as community_name
|
|
||||||
from community_moderator cm;
|
|
||||||
|
|
||||||
create view community_follower_view as
|
DROP VIEW community_user_ban_view;
|
||||||
select *,
|
|
||||||
(select name from user_ u where cf.user_id = u.id) as user_name,
|
|
||||||
(select avatar from user_ u where cf.user_id = u.id),
|
|
||||||
(select name from community c where cf.community_id = c.id) as community_name
|
|
||||||
from community_follower cf;
|
|
||||||
|
|
||||||
create view community_user_ban_view as
|
DROP VIEW site_view;
|
||||||
select *,
|
|
||||||
(select name from user_ u where cm.user_id = u.id) as user_name,
|
CREATE VIEW community_moderator_view AS
|
||||||
(select avatar from user_ u where cm.user_id = u.id),
|
SELECT
|
||||||
(select name from community c where cm.community_id = c.id) as community_name
|
*,
|
||||||
from community_user_ban cm;
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cm.user_id = u.id) AS user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cm.user_id = u.id), (
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
cm.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
community_moderator cm;
|
||||||
|
|
||||||
|
CREATE VIEW community_follower_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cf.user_id = u.id) AS user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cf.user_id = u.id), (
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
cf.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
community_follower cf;
|
||||||
|
|
||||||
|
CREATE VIEW community_user_ban_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cm.user_id = u.id) AS user_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
cm.user_id = u.id), (
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
cm.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
community_user_ban cm;
|
||||||
|
|
||||||
|
CREATE VIEW site_view AS
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
s.creator_id = u.id) AS creator_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
s.creator_id = u.id) AS creator_avatar,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
user_) AS number_of_users,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
post) AS number_of_posts,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
comment) AS number_of_comments,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
count(*)
|
||||||
|
FROM
|
||||||
|
community) AS number_of_communities
|
||||||
|
FROM
|
||||||
|
site s;
|
||||||
|
|
||||||
create view site_view as
|
|
||||||
select *,
|
|
||||||
(select name from user_ u where s.creator_id = u.id) as creator_name,
|
|
||||||
(select avatar from user_ u where s.creator_id = u.id) as creator_avatar,
|
|
||||||
(select count(*) from user_) as number_of_users,
|
|
||||||
(select count(*) from post) as number_of_posts,
|
|
||||||
(select count(*) from comment) as number_of_comments,
|
|
||||||
(select count(*) from community) as number_of_communities
|
|
||||||
from site s;
|
|
||||||
|
|
|
@ -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
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
name,
|
name,
|
||||||
avatar,
|
avatar,
|
||||||
fedi_name,
|
fedi_name,
|
||||||
admin,
|
admin,
|
||||||
banned,
|
banned,
|
||||||
published,
|
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;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
-- user
|
-- user
|
||||||
drop view user_view;
|
DROP VIEW user_view;
|
||||||
create view user_view as
|
|
||||||
select id,
|
CREATE VIEW user_view AS
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
name,
|
name,
|
||||||
avatar,
|
avatar,
|
||||||
email,
|
email,
|
||||||
|
@ -9,8 +11,38 @@ fedi_name,
|
||||||
admin,
|
admin,
|
||||||
banned,
|
banned,
|
||||||
published,
|
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;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
-- 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
|
||||||
|
id,
|
||||||
name,
|
name,
|
||||||
avatar,
|
avatar,
|
||||||
email,
|
email,
|
||||||
|
@ -13,8 +18,38 @@ fedi_name,
|
||||||
admin,
|
admin,
|
||||||
banned,
|
banned,
|
||||||
published,
|
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;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
-- 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
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
name,
|
name,
|
||||||
avatar,
|
avatar,
|
||||||
email,
|
email,
|
||||||
|
@ -15,8 +20,38 @@ banned,
|
||||||
show_avatars,
|
show_avatars,
|
||||||
send_notifications_to_email,
|
send_notifications_to_email,
|
||||||
published,
|
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;
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,24 @@
|
||||||
drop index idx_post_creator;
|
DROP INDEX idx_post_creator;
|
||||||
drop index idx_post_community;
|
|
||||||
|
|
||||||
drop index idx_post_like_post;
|
DROP INDEX idx_post_community;
|
||||||
drop index idx_post_like_user;
|
|
||||||
|
|
||||||
drop index idx_comment_creator;
|
DROP INDEX idx_post_like_post;
|
||||||
drop index idx_comment_parent;
|
|
||||||
drop index idx_comment_post;
|
|
||||||
|
|
||||||
drop index idx_comment_like_comment;
|
DROP INDEX idx_post_like_user;
|
||||||
drop index idx_comment_like_user;
|
|
||||||
drop index idx_comment_like_post;
|
DROP INDEX idx_comment_creator;
|
||||||
|
|
||||||
|
DROP INDEX idx_comment_parent;
|
||||||
|
|
||||||
|
DROP INDEX idx_comment_post;
|
||||||
|
|
||||||
|
DROP INDEX idx_comment_like_comment;
|
||||||
|
|
||||||
|
DROP INDEX idx_comment_like_user;
|
||||||
|
|
||||||
|
DROP INDEX idx_comment_like_post;
|
||||||
|
|
||||||
|
DROP INDEX idx_community_creator;
|
||||||
|
|
||||||
|
DROP INDEX idx_community_category;
|
||||||
|
|
||||||
drop index idx_community_creator;
|
|
||||||
drop index idx_community_category;
|
|
||||||
|
|
|
@ -1,17 +1,25 @@
|
||||||
-- Go through all the tables joins, optimize every view, CTE, etc.
|
-- Go through all the tables joins, optimize every view, CTE, etc.
|
||||||
create index idx_post_creator on post (creator_id);
|
CREATE INDEX idx_post_creator ON post (creator_id);
|
||||||
create index idx_post_community on post (community_id);
|
|
||||||
|
|
||||||
create index idx_post_like_post on post_like (post_id);
|
CREATE INDEX idx_post_community ON post (community_id);
|
||||||
create index idx_post_like_user on post_like (user_id);
|
|
||||||
|
|
||||||
create index idx_comment_creator on comment (creator_id);
|
CREATE INDEX idx_post_like_post ON post_like (post_id);
|
||||||
create index idx_comment_parent on comment (parent_id);
|
|
||||||
create index idx_comment_post on comment (post_id);
|
|
||||||
|
|
||||||
create index idx_comment_like_comment on comment_like (comment_id);
|
CREATE INDEX idx_post_like_user ON post_like (user_id);
|
||||||
create index idx_comment_like_user on comment_like (user_id);
|
|
||||||
create index idx_comment_like_post on comment_like (post_id);
|
CREATE INDEX idx_comment_creator ON comment (creator_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_comment_parent ON comment (parent_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_comment_post ON comment (post_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_comment_like_comment ON comment_like (comment_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_comment_like_user ON comment_like (user_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_comment_like_post ON comment_like (post_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_community_creator ON community (creator_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_community_category ON community (category_id);
|
||||||
|
|
||||||
create index idx_community_creator on community (creator_id);
|
|
||||||
create index idx_community_category on community (category_id);
|
|
||||||
|
|
|
@ -1,79 +1,186 @@
|
||||||
-- functions and triggers
|
-- functions and triggers
|
||||||
drop trigger refresh_user on user_;
|
DROP TRIGGER refresh_user ON user_;
|
||||||
drop function refresh_user();
|
|
||||||
drop trigger refresh_post on post;
|
DROP FUNCTION refresh_user ();
|
||||||
drop function refresh_post();
|
|
||||||
drop trigger refresh_post_like on post_like;
|
DROP TRIGGER refresh_post ON post;
|
||||||
drop function refresh_post_like();
|
|
||||||
drop trigger refresh_community on community;
|
DROP FUNCTION refresh_post ();
|
||||||
drop function refresh_community();
|
|
||||||
drop trigger refresh_community_follower on community_follower;
|
DROP TRIGGER refresh_post_like ON post_like;
|
||||||
drop function refresh_community_follower();
|
|
||||||
drop trigger refresh_community_user_ban on community_user_ban;
|
DROP FUNCTION refresh_post_like ();
|
||||||
drop function refresh_community_user_ban();
|
|
||||||
drop trigger refresh_comment on comment;
|
DROP TRIGGER refresh_community ON community;
|
||||||
drop function refresh_comment();
|
|
||||||
drop trigger refresh_comment_like on comment_like;
|
DROP FUNCTION refresh_community ();
|
||||||
drop function refresh_comment_like();
|
|
||||||
|
DROP TRIGGER refresh_community_follower ON community_follower;
|
||||||
|
|
||||||
|
DROP FUNCTION refresh_community_follower ();
|
||||||
|
|
||||||
|
DROP TRIGGER refresh_community_user_ban ON community_user_ban;
|
||||||
|
|
||||||
|
DROP FUNCTION refresh_community_user_ban ();
|
||||||
|
|
||||||
|
DROP TRIGGER refresh_comment ON comment;
|
||||||
|
|
||||||
|
DROP FUNCTION refresh_comment ();
|
||||||
|
|
||||||
|
DROP TRIGGER refresh_comment_like ON comment_like;
|
||||||
|
|
||||||
|
DROP FUNCTION refresh_comment_like ();
|
||||||
|
|
||||||
-- post
|
-- post
|
||||||
-- Recreate the view
|
-- Recreate the view
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
create view post_view as
|
|
||||||
with all_post as
|
CREATE VIEW post_view AS
|
||||||
(
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
u.banned
|
||||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
FROM
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
user_ u
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
WHERE
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
p.creator_id = u.id) AS banned,
|
||||||
(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,
|
SELECT
|
||||||
coalesce(sum(pl.score), 0) as score,
|
cb.id::bool
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
FROM
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
community_user_ban cb
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
WHERE
|
||||||
from post p
|
p.creator_id = cb.user_id
|
||||||
left join post_like pl on p.id = pl.post_id
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
group by p.id
|
(
|
||||||
|
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
|
||||||
)
|
)
|
||||||
|
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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
drop view post_mview;
|
DROP VIEW post_mview;
|
||||||
drop materialized view post_aggregates_mview;
|
|
||||||
drop view post_aggregates_view;
|
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||||
|
|
||||||
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
-- user
|
-- user
|
||||||
drop materialized view user_mview;
|
DROP MATERIALIZED VIEW user_mview;
|
||||||
drop view user_view;
|
|
||||||
create view user_view as
|
DROP VIEW user_view;
|
||||||
select id,
|
|
||||||
|
CREATE VIEW user_view AS
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
name,
|
name,
|
||||||
avatar,
|
avatar,
|
||||||
email,
|
email,
|
||||||
|
@ -83,120 +190,268 @@ banned,
|
||||||
show_avatars,
|
show_avatars,
|
||||||
send_notifications_to_email,
|
send_notifications_to_email,
|
||||||
published,
|
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;
|
||||||
|
|
||||||
-- community
|
-- community
|
||||||
drop view community_mview;
|
DROP VIEW community_mview;
|
||||||
drop materialized view community_aggregates_mview;
|
|
||||||
drop view community_view;
|
DROP MATERIALIZED VIEW community_aggregates_mview;
|
||||||
drop view community_aggregates_view;
|
|
||||||
create view community_view as
|
DROP VIEW community_view;
|
||||||
with all_community as
|
|
||||||
|
DROP VIEW community_aggregates_view;
|
||||||
|
|
||||||
|
CREATE VIEW community_view AS
|
||||||
|
with all_community AS (
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
(
|
(
|
||||||
select *,
|
SELECT
|
||||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
name
|
||||||
(select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
|
FROM
|
||||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
user_ u
|
||||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
WHERE
|
||||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
c.creator_id = u.id) AS creator_name,
|
||||||
(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
|
SELECT
|
||||||
from community c
|
avatar
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
c.creator_id = u.id) AS creator_avatar,
|
||||||
|
(
|
||||||
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join all_community ac
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
union all
|
community_follower cf
|
||||||
|
WHERE
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from all_community ac
|
FROM
|
||||||
;
|
all_community ac;
|
||||||
|
|
||||||
-- reply and comment view
|
-- reply and comment view
|
||||||
drop view reply_view;
|
DROP VIEW reply_view;
|
||||||
drop view user_mention_view;
|
|
||||||
drop view comment_view;
|
DROP VIEW user_mention_view;
|
||||||
drop view comment_mview;
|
|
||||||
drop materialized view comment_aggregates_mview;
|
DROP VIEW comment_view;
|
||||||
drop view comment_aggregates_view;
|
|
||||||
create view comment_view as
|
DROP VIEW comment_mview;
|
||||||
with all_comment as
|
|
||||||
(
|
DROP MATERIALIZED VIEW comment_aggregates_mview;
|
||||||
select
|
|
||||||
|
DROP VIEW comment_aggregates_view;
|
||||||
|
|
||||||
|
CREATE VIEW comment_view AS
|
||||||
|
with all_comment AS (
|
||||||
|
SELECT
|
||||||
c.*,
|
c.*,
|
||||||
(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
|
||||||
(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,
|
community_id
|
||||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
FROM
|
||||||
(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
c.creator_id = user_.id) AS creator_avatar,
|
||||||
|
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
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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
|
SELECT
|
||||||
cross join all_comment ac
|
cs.id::bool
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
FROM
|
||||||
|
comment_saved cs
|
||||||
union all
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_comment ac
|
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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_view cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
-- user mention
|
-- user mention
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.post_id,
|
c.post_id,
|
||||||
c.parent_id,
|
c.parent_id,
|
||||||
|
@ -218,6 +473,9 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from user_mention um, comment_view c
|
FROM
|
||||||
where um.comment_id = c.id;
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,15 +1,18 @@
|
||||||
-- 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
|
||||||
|
SELECT
|
||||||
u.id,
|
u.id,
|
||||||
u.name,
|
u.name,
|
||||||
u.avatar,
|
u.avatar,
|
||||||
|
@ -20,15 +23,50 @@ 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);
|
||||||
|
|
||||||
-- Drop the columns
|
-- Drop the columns
|
||||||
alter table user_ drop column matrix_user_id;
|
ALTER TABLE user_
|
||||||
|
DROP COLUMN matrix_user_id;
|
||||||
|
|
||||||
|
|
|
@ -1,52 +1,60 @@
|
||||||
-- Creating private message
|
-- Creating private message
|
||||||
create table private_message (
|
CREATE TABLE private_message (
|
||||||
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,
|
||||||
recipient_id int references user_ on update cascade on delete cascade not null,
|
recipient_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||||
content text not null,
|
content text NOT NULL,
|
||||||
deleted boolean default false not null,
|
deleted 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 the view and materialized view which has the avatar and creator name
|
-- Create the view and materialized view which has the avatar and creator name
|
||||||
create view private_message_view as
|
CREATE VIEW private_message_view AS
|
||||||
select
|
SELECT
|
||||||
pm.*,
|
pm.*,
|
||||||
u.name as creator_name,
|
u.name AS creator_name,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
u2.name as recipient_name,
|
u2.name AS recipient_name,
|
||||||
u2.avatar as recipient_avatar
|
u2.avatar AS recipient_avatar
|
||||||
from private_message pm
|
FROM
|
||||||
inner join user_ u on u.id = pm.creator_id
|
private_message pm
|
||||||
inner join user_ u2 on u2.id = pm.recipient_id;
|
INNER JOIN user_ u ON u.id = pm.creator_id
|
||||||
|
INNER JOIN user_ u2 ON u2.id = pm.recipient_id;
|
||||||
|
|
||||||
create materialized view private_message_mview as select * from private_message_view;
|
CREATE MATERIALIZED VIEW private_message_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
private_message_view;
|
||||||
|
|
||||||
create unique index idx_private_message_mview_id on private_message_mview (id);
|
CREATE UNIQUE INDEX idx_private_message_mview_id ON private_message_mview (id);
|
||||||
|
|
||||||
-- Create the triggers
|
-- Create the triggers
|
||||||
create or replace function refresh_private_message()
|
CREATE OR REPLACE FUNCTION refresh_private_message ()
|
||||||
returns trigger language plpgsql
|
RETURNS TRIGGER
|
||||||
as $$
|
LANGUAGE plpgsql
|
||||||
begin
|
AS $$
|
||||||
refresh materialized view concurrently private_message_mview;
|
BEGIN
|
||||||
return null;
|
REFRESH MATERIALIZED VIEW CONCURRENTLY private_message_mview;
|
||||||
end $$;
|
RETURN NULL;
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
|
||||||
create trigger refresh_private_message
|
CREATE TRIGGER refresh_private_message
|
||||||
after insert or update or delete or truncate
|
AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON private_message
|
||||||
on private_message
|
FOR EACH statement
|
||||||
for each statement
|
EXECUTE PROCEDURE refresh_private_message ();
|
||||||
execute procedure refresh_private_message();
|
|
||||||
|
|
||||||
-- Update user to include matrix id
|
-- Update user to include matrix id
|
||||||
alter table user_ add column matrix_user_id text unique;
|
ALTER TABLE user_
|
||||||
|
ADD COLUMN matrix_user_id text UNIQUE;
|
||||||
|
|
||||||
drop view user_view cascade;
|
DROP VIEW user_view CASCADE;
|
||||||
create view user_view as
|
|
||||||
select
|
CREATE VIEW user_view AS
|
||||||
|
SELECT
|
||||||
u.id,
|
u.id,
|
||||||
u.name,
|
u.name,
|
||||||
u.avatar,
|
u.avatar,
|
||||||
|
@ -58,15 +66,48 @@ 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);
|
||||||
|
|
||||||
-- This is what a group pm table would look like
|
-- This is what a group pm table would look like
|
||||||
-- Not going to do it now because of the complications
|
-- Not going to do it now because of the complications
|
||||||
|
|
|
@ -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 (
|
||||||
|
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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_mview cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_mview cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
drop view user_mention_mview;
|
DROP VIEW user_mention_mview;
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
create view user_mention_mview as
|
CREATE VIEW user_mention_mview AS
|
||||||
with all_comment as
|
with all_comment AS (
|
||||||
(
|
SELECT
|
||||||
select
|
|
||||||
ca.*
|
ca.*
|
||||||
from comment_aggregates_mview ca
|
FROM
|
||||||
|
comment_aggregates_mview ca
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
select
|
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.post_id,
|
ac.post_id,
|
||||||
ac.parent_id,
|
ac.parent_id,
|
||||||
|
@ -26,20 +25,27 @@ select
|
||||||
ac.score,
|
ac.score,
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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,
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from user_ u
|
FROM
|
||||||
cross join all_comment ac
|
user_ u
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
CROSS JOIN all_comment ac
|
||||||
left join user_mention um on um.comment_id = ac.id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
|
AND ac.id = cl.comment_id
|
||||||
union all
|
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||||
|
UNION ALL
|
||||||
select
|
SELECT
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.post_id,
|
ac.post_id,
|
||||||
ac.parent_id,
|
ac.parent_id,
|
||||||
|
@ -57,11 +63,11 @@ select
|
||||||
ac.score,
|
ac.score,
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved,
|
NULL AS saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from all_comment ac
|
FROM
|
||||||
left join user_mention um on um.comment_id = ac.id
|
all_comment ac
|
||||||
;
|
LEFT JOIN user_mention um ON um.comment_id = ac.id;
|
||||||
|
|
||||||
|
|
|
@ -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,5 +1,4 @@
|
||||||
-- Add case insensitive username and email uniqueness
|
-- Add case insensitive username and email uniqueness
|
||||||
|
|
||||||
-- An example of showing the dupes:
|
-- An example of showing the dupes:
|
||||||
-- select
|
-- select
|
||||||
-- max(id) as id,
|
-- max(id) as id,
|
||||||
|
@ -8,22 +7,28 @@
|
||||||
-- from user_
|
-- from user_
|
||||||
-- group by lower(name)
|
-- group by lower(name)
|
||||||
-- having count(*) > 1;
|
-- having count(*) > 1;
|
||||||
|
|
||||||
-- Delete username dupes, keeping the first one
|
-- Delete username dupes, keeping the first one
|
||||||
delete
|
DELETE FROM user_
|
||||||
from user_
|
WHERE id NOT IN (
|
||||||
where id not in (
|
SELECT
|
||||||
select min(id)
|
min(id)
|
||||||
from user_
|
FROM
|
||||||
group by lower(name), lower(fedi_name)
|
user_
|
||||||
);
|
GROUP BY
|
||||||
|
lower(name),
|
||||||
|
lower(fedi_name));
|
||||||
|
|
||||||
-- The user index
|
-- The user index
|
||||||
create unique index idx_user_name_lower on user_ (lower(name));
|
CREATE UNIQUE INDEX idx_user_name_lower ON user_ (lower(name));
|
||||||
|
|
||||||
-- Email lower
|
-- Email lower
|
||||||
create unique index idx_user_email_lower on user_ (lower(email));
|
CREATE UNIQUE INDEX idx_user_email_lower ON user_ (lower(email));
|
||||||
|
|
||||||
-- Set empty emails properly to null
|
-- Set empty emails properly to null
|
||||||
update user_ set email = null where email = '';
|
UPDATE
|
||||||
|
user_
|
||||||
|
SET
|
||||||
|
email = NULL
|
||||||
|
WHERE
|
||||||
|
email = '';
|
||||||
|
|
||||||
|
|
|
@ -1,132 +1,409 @@
|
||||||
-- Drop the dependent views
|
-- Drop the dependent views
|
||||||
drop view post_view;
|
DROP VIEW post_view;
|
||||||
drop view post_mview;
|
|
||||||
drop materialized view post_aggregates_mview;
|
|
||||||
drop view post_aggregates_view;
|
|
||||||
drop view mod_remove_post_view;
|
|
||||||
drop view mod_sticky_post_view;
|
|
||||||
drop view mod_lock_post_view;
|
|
||||||
drop view mod_remove_comment_view;
|
|
||||||
|
|
||||||
alter table post alter column name type varchar(100);
|
DROP VIEW post_mview;
|
||||||
|
|
||||||
|
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||||
|
|
||||||
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_remove_post_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_sticky_post_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_lock_post_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_remove_comment_view;
|
||||||
|
|
||||||
|
ALTER TABLE post
|
||||||
|
ALTER COLUMN name TYPE varchar(100);
|
||||||
|
|
||||||
-- regen post view
|
-- regen post view
|
||||||
create view post_aggregates_view as
|
CREATE VIEW post_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
u.banned
|
||||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
FROM
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
user_ u
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
WHERE
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
p.creator_id = u.id) AS banned,
|
||||||
(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,
|
SELECT
|
||||||
coalesce(sum(pl.score), 0) as score,
|
cb.id::bool
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
FROM
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
community_user_ban cb
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
WHERE
|
||||||
from post p
|
p.creator_id = cb.user_id
|
||||||
left join post_like pl on p.id = pl.post_id
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
group by p.id;
|
(
|
||||||
|
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;
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
|
|
||||||
create view post_view as
|
CREATE VIEW post_view AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_view 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
create view post_mview as
|
CREATE VIEW post_mview AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_mview pa
|
FROM
|
||||||
|
post_aggregates_mview 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
-- The mod views
|
-- The mod views
|
||||||
|
CREATE VIEW mod_remove_post_view AS
|
||||||
|
SELECT
|
||||||
|
mrp.*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
mrp.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
c.name
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
mrp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_remove_post mrp;
|
||||||
|
|
||||||
create view mod_remove_post_view as
|
CREATE VIEW mod_lock_post_view AS
|
||||||
select mrp.*,
|
SELECT
|
||||||
(select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
|
mlp.*,
|
||||||
(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
|
||||||
|
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
|
||||||
|
c.id
|
||||||
|
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_lock_post_view as
|
CREATE VIEW mod_remove_comment_view AS
|
||||||
select mlp.*,
|
SELECT
|
||||||
(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
|
mrc.*,
|
||||||
(select name from post p where mlp.post_id = p.id) as post_name,
|
(
|
||||||
(select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
|
SELECT
|
||||||
(select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
|
name
|
||||||
from mod_lock_post mlp;
|
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_comment_view as
|
CREATE VIEW mod_sticky_post_view AS
|
||||||
select mrc.*,
|
SELECT
|
||||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
msp.*,
|
||||||
(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
|
||||||
(select content from comment c where mrc.comment_id = c.id) as comment_content,
|
name
|
||||||
(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
|
FROM
|
||||||
(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
|
user_ u
|
||||||
(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,
|
WHERE
|
||||||
(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
|
msp.mod_user_id = u.id) AS mod_user_name,
|
||||||
from mod_remove_comment mrc;
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
msp.post_id = p.id) AS post_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
c.id
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
msp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
c.name
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
msp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_sticky_post msp;
|
||||||
|
|
||||||
create view mod_sticky_post_view as
|
|
||||||
select msp.*,
|
|
||||||
(select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
|
|
||||||
(select name from post p where msp.post_id = p.id) as post_name,
|
|
||||||
(select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
|
|
||||||
(select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
|
|
||||||
from mod_sticky_post msp;
|
|
||||||
|
|
|
@ -1,133 +1,410 @@
|
||||||
-- Drop the dependent views
|
-- Drop the dependent views
|
||||||
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 view mod_remove_post_view;
|
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||||
drop view mod_sticky_post_view;
|
|
||||||
drop view mod_lock_post_view;
|
DROP VIEW post_aggregates_view;
|
||||||
drop view mod_remove_comment_view;
|
|
||||||
|
DROP VIEW mod_remove_post_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_sticky_post_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_lock_post_view;
|
||||||
|
|
||||||
|
DROP VIEW mod_remove_comment_view;
|
||||||
|
|
||||||
-- Add the extra post limit
|
-- Add the extra post limit
|
||||||
alter table post alter column name type varchar(200);
|
ALTER TABLE post
|
||||||
|
ALTER COLUMN name TYPE varchar(200);
|
||||||
|
|
||||||
-- regen post view
|
-- regen post view
|
||||||
create view post_aggregates_view as
|
CREATE VIEW post_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
u.banned
|
||||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
FROM
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
user_ u
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
WHERE
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
p.creator_id = u.id) AS banned,
|
||||||
(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,
|
SELECT
|
||||||
coalesce(sum(pl.score), 0) as score,
|
cb.id::bool
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
FROM
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
community_user_ban cb
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
WHERE
|
||||||
from post p
|
p.creator_id = cb.user_id
|
||||||
left join post_like pl on p.id = pl.post_id
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
group by p.id;
|
(
|
||||||
|
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;
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
|
|
||||||
create view post_view as
|
CREATE VIEW post_view AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_view 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
create view post_mview as
|
CREATE VIEW post_mview AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_mview pa
|
FROM
|
||||||
|
post_aggregates_mview 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
-- The mod views
|
-- The mod views
|
||||||
|
CREATE VIEW mod_remove_post_view AS
|
||||||
|
SELECT
|
||||||
|
mrp.*,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
mrp.mod_user_id = u.id) AS mod_user_name,
|
||||||
|
(
|
||||||
|
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
|
||||||
|
c.name
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
mrp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_remove_post mrp;
|
||||||
|
|
||||||
create view mod_remove_post_view as
|
CREATE VIEW mod_lock_post_view AS
|
||||||
select mrp.*,
|
SELECT
|
||||||
(select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
|
mlp.*,
|
||||||
(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
|
||||||
|
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
|
||||||
|
c.id
|
||||||
|
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_lock_post_view as
|
CREATE VIEW mod_remove_comment_view AS
|
||||||
select mlp.*,
|
SELECT
|
||||||
(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
|
mrc.*,
|
||||||
(select name from post p where mlp.post_id = p.id) as post_name,
|
(
|
||||||
(select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
|
SELECT
|
||||||
(select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
|
name
|
||||||
from mod_lock_post mlp;
|
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_comment_view as
|
CREATE VIEW mod_sticky_post_view AS
|
||||||
select mrc.*,
|
SELECT
|
||||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
msp.*,
|
||||||
(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
|
||||||
(select content from comment c where mrc.comment_id = c.id) as comment_content,
|
name
|
||||||
(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
|
FROM
|
||||||
(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
|
user_ u
|
||||||
(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,
|
WHERE
|
||||||
(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
|
msp.mod_user_id = u.id) AS mod_user_name,
|
||||||
from mod_remove_comment mrc;
|
(
|
||||||
|
SELECT
|
||||||
|
name
|
||||||
|
FROM
|
||||||
|
post p
|
||||||
|
WHERE
|
||||||
|
msp.post_id = p.id) AS post_name,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
c.id
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
msp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
c.name
|
||||||
|
FROM
|
||||||
|
post p,
|
||||||
|
community c
|
||||||
|
WHERE
|
||||||
|
msp.post_id = p.id
|
||||||
|
AND p.community_id = c.id) AS community_name
|
||||||
|
FROM
|
||||||
|
mod_sticky_post msp;
|
||||||
|
|
||||||
create view mod_sticky_post_view as
|
|
||||||
select msp.*,
|
|
||||||
(select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
|
|
||||||
(select name from post p where msp.post_id = p.id) as post_name,
|
|
||||||
(select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
|
|
||||||
(select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
|
|
||||||
from mod_sticky_post msp;
|
|
||||||
|
|
|
@ -1,117 +1,191 @@
|
||||||
|
DROP VIEW reply_view;
|
||||||
|
|
||||||
drop view reply_view;
|
DROP VIEW user_mention_view;
|
||||||
drop view user_mention_view;
|
|
||||||
drop view user_mention_mview;
|
DROP VIEW user_mention_mview;
|
||||||
drop view comment_view;
|
|
||||||
drop view comment_mview;
|
DROP VIEW comment_view;
|
||||||
drop materialized view comment_aggregates_mview;
|
|
||||||
drop view comment_aggregates_view;
|
DROP VIEW comment_mview;
|
||||||
|
|
||||||
|
DROP MATERIALIZED VIEW comment_aggregates_mview;
|
||||||
|
|
||||||
|
DROP VIEW comment_aggregates_view;
|
||||||
|
|
||||||
-- reply and comment view
|
-- reply and comment view
|
||||||
create view comment_aggregates_view as
|
CREATE VIEW comment_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
c.*,
|
c.*,
|
||||||
(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,
|
|
||||||
(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
|
|
||||||
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;
|
|
||||||
|
|
||||||
create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
|
|
||||||
|
|
||||||
create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
|
|
||||||
|
|
||||||
create view comment_view as
|
|
||||||
with all_comment as
|
|
||||||
(
|
(
|
||||||
select
|
SELECT
|
||||||
ca.*
|
community_id
|
||||||
from comment_aggregates_view ca
|
FROM
|
||||||
)
|
post p
|
||||||
|
WHERE
|
||||||
select
|
p.id = c.post_id), (
|
||||||
ac.*,
|
SELECT
|
||||||
u.id as user_id,
|
u.banned
|
||||||
coalesce(cl.score, 0) as my_vote,
|
FROM
|
||||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
user_ u
|
||||||
from user_ u
|
WHERE
|
||||||
cross join all_comment ac
|
c.creator_id = u.id) AS banned,
|
||||||
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 comment_mview as
|
|
||||||
with all_comment as
|
|
||||||
(
|
(
|
||||||
select
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
c.creator_id = user_.id) AS creator_avatar,
|
||||||
|
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;
|
||||||
|
|
||||||
|
CREATE MATERIALIZED VIEW comment_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
comment_aggregates_view;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
|
||||||
|
|
||||||
|
CREATE VIEW comment_view AS
|
||||||
|
with all_comment AS (
|
||||||
|
SELECT
|
||||||
ca.*
|
ca.*
|
||||||
from comment_aggregates_mview ca
|
FROM
|
||||||
|
comment_aggregates_view ca
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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
|
SELECT
|
||||||
cross join all_comment ac
|
cs.id::bool
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
FROM
|
||||||
|
comment_saved cs
|
||||||
union all
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
select
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_comment ac
|
FROM
|
||||||
;
|
all_comment ac;
|
||||||
|
|
||||||
|
CREATE VIEW comment_mview AS
|
||||||
|
with all_comment AS (
|
||||||
|
SELECT
|
||||||
|
ca.*
|
||||||
|
FROM
|
||||||
|
comment_aggregates_mview ca
|
||||||
|
)
|
||||||
|
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;
|
||||||
|
|
||||||
-- 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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_mview cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_mview cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
-- user mention
|
-- user mention
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.post_id,
|
c.post_id,
|
||||||
c.parent_id,
|
c.parent_id,
|
||||||
|
@ -133,21 +207,22 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from user_mention um, comment_view c
|
FROM
|
||||||
where um.comment_id = c.id;
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
|
CREATE VIEW user_mention_mview AS
|
||||||
create view user_mention_mview as
|
with all_comment AS (
|
||||||
with all_comment as
|
SELECT
|
||||||
(
|
|
||||||
select
|
|
||||||
ca.*
|
ca.*
|
||||||
from comment_aggregates_mview ca
|
FROM
|
||||||
|
comment_aggregates_mview ca
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
select
|
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.post_id,
|
ac.post_id,
|
||||||
ac.parent_id,
|
ac.parent_id,
|
||||||
|
@ -165,20 +240,27 @@ select
|
||||||
ac.score,
|
ac.score,
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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,
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from user_ u
|
FROM
|
||||||
cross join all_comment ac
|
user_ u
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
CROSS JOIN all_comment ac
|
||||||
left join user_mention um on um.comment_id = ac.id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
|
AND ac.id = cl.comment_id
|
||||||
union all
|
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||||
|
UNION ALL
|
||||||
select
|
SELECT
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.post_id,
|
ac.post_id,
|
||||||
ac.parent_id,
|
ac.parent_id,
|
||||||
|
@ -196,11 +278,11 @@ select
|
||||||
ac.score,
|
ac.score,
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved,
|
NULL AS saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from all_comment ac
|
FROM
|
||||||
left join user_mention um on um.comment_id = ac.id
|
all_comment ac
|
||||||
;
|
LEFT JOIN user_mention um ON um.comment_id = ac.id;
|
||||||
|
|
||||||
|
|
|
@ -1,125 +1,221 @@
|
||||||
|
|
||||||
-- Adding community name, hot_rank, to comment_view, user_mention_view, and subscribed to comment_view
|
-- Adding community name, hot_rank, to comment_view, user_mention_view, and subscribed to comment_view
|
||||||
|
|
||||||
-- Rebuild the comment view
|
-- Rebuild the comment view
|
||||||
drop view reply_view;
|
DROP VIEW reply_view;
|
||||||
drop view user_mention_view;
|
|
||||||
drop view user_mention_mview;
|
DROP VIEW user_mention_view;
|
||||||
drop view comment_view;
|
|
||||||
drop view comment_mview;
|
DROP VIEW user_mention_mview;
|
||||||
drop materialized view comment_aggregates_mview;
|
|
||||||
drop view comment_aggregates_view;
|
DROP VIEW comment_view;
|
||||||
|
|
||||||
|
DROP VIEW comment_mview;
|
||||||
|
|
||||||
|
DROP MATERIALIZED VIEW comment_aggregates_mview;
|
||||||
|
|
||||||
|
DROP VIEW comment_aggregates_view;
|
||||||
|
|
||||||
-- reply and comment view
|
-- reply and comment view
|
||||||
create view comment_aggregates_view as
|
CREATE VIEW comment_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
c.*,
|
c.*,
|
||||||
(select community_id from post p where p.id = c.post_id),
|
|
||||||
(select co.name from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_name,
|
|
||||||
(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,
|
|
||||||
(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
|
|
||||||
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,
|
|
||||||
hot_rank(coalesce(sum(cl.score) , 0), c.published) as hot_rank
|
|
||||||
from comment c
|
|
||||||
left join comment_like cl on c.id = cl.comment_id
|
|
||||||
group by c.id;
|
|
||||||
|
|
||||||
create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
|
|
||||||
|
|
||||||
create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
|
|
||||||
|
|
||||||
create view comment_view as
|
|
||||||
with all_comment as
|
|
||||||
(
|
(
|
||||||
select
|
SELECT
|
||||||
ca.*
|
community_id
|
||||||
from comment_aggregates_view ca
|
FROM
|
||||||
)
|
post p
|
||||||
|
WHERE
|
||||||
select
|
p.id = c.post_id), (
|
||||||
ac.*,
|
SELECT
|
||||||
u.id as user_id,
|
co.name
|
||||||
coalesce(cl.score, 0) as my_vote,
|
FROM
|
||||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
|
post p,
|
||||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
community co
|
||||||
from user_ u
|
WHERE
|
||||||
cross join all_comment ac
|
p.id = c.post_id
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
AND p.community_id = co.id) AS community_name,
|
||||||
|
|
||||||
union all
|
|
||||||
|
|
||||||
select
|
|
||||||
ac.*,
|
|
||||||
null as user_id,
|
|
||||||
null as my_vote,
|
|
||||||
null as subscribed,
|
|
||||||
null as saved
|
|
||||||
from all_comment ac
|
|
||||||
;
|
|
||||||
|
|
||||||
create view comment_mview as
|
|
||||||
with all_comment as
|
|
||||||
(
|
(
|
||||||
select
|
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,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
avatar
|
||||||
|
FROM
|
||||||
|
user_
|
||||||
|
WHERE
|
||||||
|
c.creator_id = user_.id) AS creator_avatar,
|
||||||
|
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,
|
||||||
|
hot_rank (coalesce(sum(cl.score), 0), c.published) AS hot_rank
|
||||||
|
FROM
|
||||||
|
comment c
|
||||||
|
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||||
|
GROUP BY
|
||||||
|
c.id;
|
||||||
|
|
||||||
|
CREATE MATERIALIZED VIEW comment_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
comment_aggregates_view;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
|
||||||
|
|
||||||
|
CREATE VIEW comment_view AS
|
||||||
|
with all_comment AS (
|
||||||
|
SELECT
|
||||||
ca.*
|
ca.*
|
||||||
from comment_aggregates_mview ca
|
FROM
|
||||||
|
comment_aggregates_view ca
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
select
|
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
|
(
|
||||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
SELECT
|
||||||
from user_ u
|
cf.id::boolean
|
||||||
cross join all_comment ac
|
FROM
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
community_follower cf
|
||||||
|
WHERE
|
||||||
union all
|
u.id = cf.user_id
|
||||||
|
AND ac.community_id = cf.community_id) AS subscribed,
|
||||||
select
|
(
|
||||||
|
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.*,
|
ac.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_comment ac
|
FROM
|
||||||
;
|
all_comment ac;
|
||||||
|
|
||||||
|
CREATE VIEW comment_mview AS
|
||||||
|
with all_comment AS (
|
||||||
|
SELECT
|
||||||
|
ca.*
|
||||||
|
FROM
|
||||||
|
comment_aggregates_mview ca
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
ac.*,
|
||||||
|
u.id AS user_id,
|
||||||
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
cf.id::boolean
|
||||||
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND ac.community_id = cf.community_id) AS subscribed,
|
||||||
|
(
|
||||||
|
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 subscribed,
|
||||||
|
NULL AS saved
|
||||||
|
FROM
|
||||||
|
all_comment ac;
|
||||||
|
|
||||||
-- 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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_mview cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_mview cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
-- user mention
|
-- user mention
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.post_id,
|
c.post_id,
|
||||||
c.parent_id,
|
c.parent_id,
|
||||||
|
@ -143,21 +239,22 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from user_mention um, comment_view c
|
FROM
|
||||||
where um.comment_id = c.id;
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
|
CREATE VIEW user_mention_mview AS
|
||||||
create view user_mention_mview as
|
with all_comment AS (
|
||||||
with all_comment as
|
SELECT
|
||||||
(
|
|
||||||
select
|
|
||||||
ca.*
|
ca.*
|
||||||
from comment_aggregates_mview ca
|
FROM
|
||||||
|
comment_aggregates_mview ca
|
||||||
)
|
)
|
||||||
|
SELECT
|
||||||
select
|
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.post_id,
|
ac.post_id,
|
||||||
ac.parent_id,
|
ac.parent_id,
|
||||||
|
@ -177,20 +274,27 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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,
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from user_ u
|
FROM
|
||||||
cross join all_comment ac
|
user_ u
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
CROSS JOIN all_comment ac
|
||||||
left join user_mention um on um.comment_id = ac.id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
|
AND ac.id = cl.comment_id
|
||||||
union all
|
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||||
|
UNION ALL
|
||||||
select
|
SELECT
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.post_id,
|
ac.post_id,
|
||||||
ac.parent_id,
|
ac.parent_id,
|
||||||
|
@ -210,11 +314,11 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved,
|
NULL AS saved,
|
||||||
um.recipient_id
|
um.recipient_id
|
||||||
from all_comment ac
|
FROM
|
||||||
left join user_mention um on um.comment_id = ac.id
|
all_comment ac
|
||||||
;
|
LEFT JOIN user_mention um ON um.comment_id = ac.id;
|
||||||
|
|
||||||
|
|
|
@ -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_mview;
|
||||||
drop view post_aggregates_view;
|
|
||||||
|
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||||
|
|
||||||
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
-- regen post view
|
-- regen post view
|
||||||
create view post_aggregates_view as
|
CREATE VIEW post_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
||||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
u.banned
|
||||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
FROM
|
||||||
(select name from community where p.community_id = community.id) as community_name,
|
user_ u
|
||||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
WHERE
|
||||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
p.creator_id = u.id) AS banned,
|
||||||
(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,
|
SELECT
|
||||||
coalesce(sum(pl.score), 0) as score,
|
cb.id::bool
|
||||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
FROM
|
||||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
community_user_ban cb
|
||||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
WHERE
|
||||||
from post p
|
p.creator_id = cb.user_id
|
||||||
left join post_like pl on p.id = pl.post_id
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
group by p.id;
|
(
|
||||||
|
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;
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
|
|
||||||
create view post_view as
|
CREATE VIEW post_view AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_view 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
create view post_mview as
|
CREATE VIEW post_mview AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_mview pa
|
FROM
|
||||||
|
post_aggregates_mview 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
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_mview;
|
||||||
drop view post_aggregates_view;
|
|
||||||
|
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||||
|
|
||||||
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
-- regen post view
|
-- regen post view
|
||||||
create view post_aggregates_view as
|
CREATE VIEW post_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
SELECT
|
||||||
else greatest(c.recent_comment_time, p.published)
|
u.banned
|
||||||
end
|
FROM
|
||||||
)
|
user_ u
|
||||||
) as hot_rank,
|
WHERE
|
||||||
|
p.creator_id = u.id) AS banned,
|
||||||
(
|
(
|
||||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
SELECT
|
||||||
else greatest(c.recent_comment_time, p.published)
|
cb.id::bool
|
||||||
end
|
FROM
|
||||||
) as newest_activity_time
|
community_user_ban cb
|
||||||
from post p
|
WHERE
|
||||||
left join post_like pl on p.id = pl.post_id
|
p.creator_id = cb.user_id
|
||||||
left join (
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
select post_id,
|
(
|
||||||
max(published) as recent_comment_time
|
SELECT
|
||||||
from comment
|
name
|
||||||
group by 1
|
FROM
|
||||||
) c on p.id = c.post_id
|
user_
|
||||||
group by p.id, c.recent_comment_time;
|
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;
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
|
|
||||||
create view post_view as
|
CREATE VIEW post_view AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_view 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
create view post_mview as
|
CREATE VIEW post_mview AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_mview pa
|
FROM
|
||||||
|
post_aggregates_mview 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
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;
|
ALTER TABLE post
|
||||||
|
DROP COLUMN embed_description;
|
||||||
|
|
||||||
|
ALTER TABLE post
|
||||||
|
DROP COLUMN embed_html;
|
||||||
|
|
||||||
|
ALTER TABLE post
|
||||||
|
DROP COLUMN thumbnail_url;
|
||||||
|
|
||||||
-- regen post view
|
-- regen post view
|
||||||
create view post_aggregates_view as
|
CREATE VIEW post_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
SELECT
|
||||||
else greatest(c.recent_comment_time, p.published)
|
u.banned
|
||||||
end
|
FROM
|
||||||
)
|
user_ u
|
||||||
) as hot_rank,
|
WHERE
|
||||||
|
p.creator_id = u.id) AS banned,
|
||||||
(
|
(
|
||||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
SELECT
|
||||||
else greatest(c.recent_comment_time, p.published)
|
cb.id::bool
|
||||||
end
|
FROM
|
||||||
) as newest_activity_time
|
community_user_ban cb
|
||||||
from post p
|
WHERE
|
||||||
left join post_like pl on p.id = pl.post_id
|
p.creator_id = cb.user_id
|
||||||
left join (
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
select post_id,
|
(
|
||||||
max(published) as recent_comment_time
|
SELECT
|
||||||
from comment
|
name
|
||||||
group by 1
|
FROM
|
||||||
) c on p.id = c.post_id
|
user_
|
||||||
group by p.id, c.recent_comment_time;
|
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;
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
|
|
||||||
create view post_view as
|
CREATE VIEW post_view AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_view 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
create view post_mview as
|
CREATE VIEW post_mview AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_mview pa
|
FROM
|
||||||
|
post_aggregates_mview 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
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;
|
ALTER TABLE post
|
||||||
|
ADD COLUMN embed_description text;
|
||||||
|
|
||||||
|
ALTER TABLE post
|
||||||
|
ADD COLUMN embed_html text;
|
||||||
|
|
||||||
|
ALTER TABLE post
|
||||||
|
ADD COLUMN thumbnail_url text;
|
||||||
|
|
||||||
-- Regenerate the views
|
-- Regenerate the views
|
||||||
|
|
||||||
-- 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;
|
||||||
|
|
||||||
-- regen post view
|
-- regen post view
|
||||||
create view post_aggregates_view as
|
CREATE VIEW post_aggregates_view AS
|
||||||
select
|
SELECT
|
||||||
p.*,
|
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
|
SELECT
|
||||||
else greatest(c.recent_comment_time, p.published)
|
u.banned
|
||||||
end
|
FROM
|
||||||
)
|
user_ u
|
||||||
) as hot_rank,
|
WHERE
|
||||||
|
p.creator_id = u.id) AS banned,
|
||||||
(
|
(
|
||||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
SELECT
|
||||||
else greatest(c.recent_comment_time, p.published)
|
cb.id::bool
|
||||||
end
|
FROM
|
||||||
) as newest_activity_time
|
community_user_ban cb
|
||||||
from post p
|
WHERE
|
||||||
left join post_like pl on p.id = pl.post_id
|
p.creator_id = cb.user_id
|
||||||
left join (
|
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||||
select post_id,
|
(
|
||||||
max(published) as recent_comment_time
|
SELECT
|
||||||
from comment
|
name
|
||||||
group by 1
|
FROM
|
||||||
) c on p.id = c.post_id
|
user_
|
||||||
group by p.id, c.recent_comment_time;
|
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;
|
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||||
|
|
||||||
create view post_view as
|
CREATE VIEW post_view AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_view 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
create view post_mview as
|
CREATE VIEW post_mview AS
|
||||||
with all_post as (
|
with all_post AS (
|
||||||
select
|
SELECT
|
||||||
pa.*
|
pa.*
|
||||||
from post_aggregates_mview pa
|
FROM
|
||||||
|
post_aggregates_mview 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
|
||||||
union all
|
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,
|
||||||
|
(
|
||||||
|
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.*,
|
ap.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from all_post ap
|
FROM
|
||||||
;
|
all_post ap;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
drop table activity;
|
DROP TABLE activity;
|
||||||
|
|
||||||
alter table user_
|
ALTER TABLE user_
|
||||||
drop column actor_id,
|
DROP COLUMN actor_id,
|
||||||
drop column private_key,
|
DROP COLUMN private_key,
|
||||||
drop column public_key,
|
DROP COLUMN public_key,
|
||||||
drop column bio,
|
DROP COLUMN bio,
|
||||||
drop column local,
|
DROP COLUMN local,
|
||||||
drop column last_refreshed_at;
|
DROP COLUMN last_refreshed_at;
|
||||||
|
|
||||||
|
ALTER TABLE community
|
||||||
|
DROP COLUMN actor_id,
|
||||||
|
DROP COLUMN private_key,
|
||||||
|
DROP COLUMN public_key,
|
||||||
|
DROP COLUMN local,
|
||||||
|
DROP COLUMN last_refreshed_at;
|
||||||
|
|
||||||
alter table community
|
|
||||||
drop column actor_id,
|
|
||||||
drop column private_key,
|
|
||||||
drop column public_key,
|
|
||||||
drop column local,
|
|
||||||
drop column last_refreshed_at;
|
|
||||||
|
|
|
@ -1,36 +1,35 @@
|
||||||
-- The Activitypub activity table
|
-- The Activitypub activity table
|
||||||
-- All user actions must create a row here.
|
-- All user actions must create a row here.
|
||||||
create table activity (
|
CREATE TABLE activity (
|
||||||
id serial primary key,
|
id serial PRIMARY KEY,
|
||||||
user_id int references user_ on update cascade on delete cascade not null, -- Ensures that the user is set up here.
|
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- Ensures that the user is set up here.
|
||||||
data jsonb not null,
|
data jsonb NOT NULL,
|
||||||
local boolean not null default true,
|
local boolean NOT NULL DEFAULT TRUE,
|
||||||
published timestamp not null default now(),
|
published timestamp NOT NULL DEFAULT now(),
|
||||||
updated timestamp
|
updated timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Making sure that id is unique
|
-- Making sure that id is unique
|
||||||
create unique index idx_activity_unique_apid on activity ((data ->> 'id'::text));
|
CREATE UNIQUE INDEX idx_activity_unique_apid ON activity ((data ->> 'id'::text));
|
||||||
|
|
||||||
-- Add federation columns to the two actor tables
|
-- Add federation columns to the two actor tables
|
||||||
alter table user_
|
ALTER TABLE user_
|
||||||
-- TODO uniqueness constraints should be added on these 3 columns later
|
-- TODO uniqueness constraints should be added on these 3 columns later
|
||||||
add column actor_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 actor_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 bio text, -- not on community, already has description
|
ADD COLUMN bio text, -- not on community, already has description
|
||||||
add column local boolean not null default true,
|
ADD COLUMN local boolean NOT NULL DEFAULT TRUE,
|
||||||
add column private_key text, -- These need to be generated from code
|
ADD COLUMN private_key text, -- These need to be generated from code
|
||||||
add column public_key text,
|
ADD COLUMN public_key text,
|
||||||
add column last_refreshed_at timestamp not null default now() -- Used to re-fetch federated actor periodically
|
ADD COLUMN last_refreshed_at timestamp NOT NULL DEFAULT now() -- Used to re-fetch federated actor periodically
|
||||||
;
|
;
|
||||||
|
|
||||||
-- Community
|
-- Community
|
||||||
alter table community
|
ALTER TABLE community
|
||||||
add column actor_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 actor_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,
|
||||||
add column private_key text, -- These need to be generated from code
|
ADD COLUMN private_key text, -- These need to be generated from code
|
||||||
add column public_key text,
|
ADD COLUMN public_key text,
|
||||||
add column last_refreshed_at timestamp not null default now() -- Used to re-fetch federated actor periodically
|
ADD COLUMN last_refreshed_at timestamp NOT NULL DEFAULT now() -- Used to re-fetch federated actor periodically
|
||||||
;
|
;
|
||||||
|
|
||||||
-- Don't worry about rebuilding the views right now.
|
-- Don't worry about rebuilding the views right now.
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
alter table post
|
ALTER TABLE post
|
||||||
drop column ap_id,
|
DROP COLUMN ap_id,
|
||||||
drop column local;
|
DROP COLUMN local;
|
||||||
|
|
||||||
|
ALTER TABLE comment
|
||||||
|
DROP COLUMN ap_id,
|
||||||
|
DROP COLUMN local;
|
||||||
|
|
||||||
alter table comment
|
|
||||||
drop column ap_id,
|
|
||||||
drop column local;
|
|
||||||
|
|
|
@ -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,19 +1,18 @@
|
||||||
-- User table
|
-- User table
|
||||||
drop view user_view cascade;
|
DROP VIEW user_view CASCADE;
|
||||||
|
|
||||||
alter table user_
|
ALTER TABLE user_
|
||||||
add column fedi_name varchar(40) not null default 'http://fake.com';
|
ADD COLUMN fedi_name varchar(40) NOT NULL DEFAULT 'http://fake.com';
|
||||||
|
|
||||||
alter table user_
|
ALTER TABLE user_
|
||||||
add constraint user__name_fedi_name_key unique (name, fedi_name);
|
ADD CONSTRAINT user__name_fedi_name_key UNIQUE (name, fedi_name);
|
||||||
|
|
||||||
-- Community
|
-- Community
|
||||||
alter table community
|
ALTER TABLE community
|
||||||
add constraint community_name_key unique (name);
|
ADD CONSTRAINT community_name_key UNIQUE (name);
|
||||||
|
|
||||||
|
CREATE VIEW user_view AS
|
||||||
create view user_view as
|
SELECT
|
||||||
select
|
|
||||||
u.id,
|
u.id,
|
||||||
u.name,
|
u.name,
|
||||||
u.avatar,
|
u.avatar,
|
||||||
|
@ -25,12 +24,46 @@ 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);
|
|
||||||
|
|
|
@ -1,21 +1,20 @@
|
||||||
-- 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,
|
||||||
|
@ -26,13 +25,46 @@ 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));
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,28 @@
|
||||||
drop materialized view private_message_mview;
|
DROP MATERIALIZED VIEW private_message_mview;
|
||||||
drop view private_message_view;
|
|
||||||
|
|
||||||
alter table private_message
|
DROP VIEW private_message_view;
|
||||||
drop column ap_id,
|
|
||||||
drop column local;
|
|
||||||
|
|
||||||
create view private_message_view as
|
ALTER TABLE private_message
|
||||||
select
|
DROP COLUMN ap_id,
|
||||||
|
DROP COLUMN local;
|
||||||
|
|
||||||
|
CREATE VIEW private_message_view AS
|
||||||
|
SELECT
|
||||||
pm.*,
|
pm.*,
|
||||||
u.name as creator_name,
|
u.name AS creator_name,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
u2.name as recipient_name,
|
u2.name AS recipient_name,
|
||||||
u2.avatar as recipient_avatar
|
u2.avatar AS recipient_avatar
|
||||||
from private_message pm
|
FROM
|
||||||
inner join user_ u on u.id = pm.creator_id
|
private_message pm
|
||||||
inner join user_ u2 on u2.id = pm.recipient_id;
|
INNER JOIN user_ u ON u.id = pm.creator_id
|
||||||
|
INNER JOIN user_ u2 ON u2.id = pm.recipient_id;
|
||||||
|
|
||||||
create materialized view private_message_mview as select * from private_message_view;
|
CREATE MATERIALIZED VIEW private_message_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
private_message_view;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX idx_private_message_mview_id ON private_message_mview (id);
|
||||||
|
|
||||||
create unique index idx_private_message_mview_id on private_message_mview (id);
|
|
||||||
|
|
|
@ -1,25 +1,32 @@
|
||||||
alter table private_message
|
ALTER TABLE private_message
|
||||||
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;
|
||||||
;
|
|
||||||
|
|
||||||
drop materialized view private_message_mview;
|
DROP MATERIALIZED VIEW private_message_mview;
|
||||||
drop view private_message_view;
|
|
||||||
create view private_message_view as
|
DROP VIEW private_message_view;
|
||||||
select
|
|
||||||
|
CREATE VIEW private_message_view AS
|
||||||
|
SELECT
|
||||||
pm.*,
|
pm.*,
|
||||||
u.name as creator_name,
|
u.name AS creator_name,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
u.actor_id as creator_actor_id,
|
u.actor_id AS creator_actor_id,
|
||||||
u.local as creator_local,
|
u.local AS creator_local,
|
||||||
u2.name as recipient_name,
|
u2.name AS recipient_name,
|
||||||
u2.avatar as recipient_avatar,
|
u2.avatar AS recipient_avatar,
|
||||||
u2.actor_id as recipient_actor_id,
|
u2.actor_id AS recipient_actor_id,
|
||||||
u2.local as recipient_local
|
u2.local AS recipient_local
|
||||||
from private_message pm
|
FROM
|
||||||
inner join user_ u on u.id = pm.creator_id
|
private_message pm
|
||||||
inner join user_ u2 on u2.id = pm.recipient_id;
|
INNER JOIN user_ u ON u.id = pm.creator_id
|
||||||
|
INNER JOIN user_ u2 ON u2.id = pm.recipient_id;
|
||||||
|
|
||||||
create materialized view private_message_mview as select * from private_message_view;
|
CREATE MATERIALIZED VIEW private_message_mview AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
private_message_view;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX idx_private_message_mview_id ON private_message_mview (id);
|
||||||
|
|
||||||
create unique index idx_private_message_mview_id on private_message_mview (id);
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -1,115 +1,145 @@
|
||||||
drop view user_mention_view;
|
DROP VIEW user_mention_view;
|
||||||
drop view reply_fast_view;
|
|
||||||
drop view comment_fast_view;
|
|
||||||
drop view comment_view;
|
|
||||||
|
|
||||||
drop view user_mention_fast_view;
|
DROP VIEW reply_fast_view;
|
||||||
drop table comment_aggregates_fast;
|
|
||||||
drop view comment_aggregates_view;
|
|
||||||
|
|
||||||
create view comment_aggregates_view as
|
DROP VIEW comment_fast_view;
|
||||||
select
|
|
||||||
|
DROP VIEW comment_view;
|
||||||
|
|
||||||
|
DROP VIEW user_mention_fast_view;
|
||||||
|
|
||||||
|
DROP TABLE comment_aggregates_fast;
|
||||||
|
|
||||||
|
DROP VIEW comment_aggregates_view;
|
||||||
|
|
||||||
|
CREATE VIEW comment_aggregates_view AS
|
||||||
|
SELECT
|
||||||
ct.*,
|
ct.*,
|
||||||
-- community details
|
-- community details
|
||||||
p.community_id,
|
p.community_id,
|
||||||
c.actor_id as community_actor_id,
|
c.actor_id AS community_actor_id,
|
||||||
c."local" as community_local,
|
c."local" AS community_local,
|
||||||
c."name" as community_name,
|
c."name" AS community_name,
|
||||||
-- creator details
|
-- creator details
|
||||||
u.banned as banned,
|
u.banned AS banned,
|
||||||
coalesce(cb.id, 0)::bool as banned_from_community,
|
coalesce(cb.id, 0)::bool AS banned_from_community,
|
||||||
u.actor_id as creator_actor_id,
|
u.actor_id AS creator_actor_id,
|
||||||
u.local as creator_local,
|
u.local AS creator_local,
|
||||||
u.name as creator_name,
|
u.name AS creator_name,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
-- score details
|
-- score details
|
||||||
coalesce(cl.total, 0) as score,
|
coalesce(cl.total, 0) AS score,
|
||||||
coalesce(cl.up, 0) as upvotes,
|
coalesce(cl.up, 0) AS upvotes,
|
||||||
coalesce(cl.down, 0) as downvotes,
|
coalesce(cl.down, 0) AS downvotes,
|
||||||
hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
|
hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
|
||||||
from comment ct
|
FROM
|
||||||
left join post p on ct.post_id = p.id
|
comment ct
|
||||||
left join community c on p.community_id = c.id
|
LEFT JOIN post p ON ct.post_id = p.id
|
||||||
left join user_ u on ct.creator_id = u.id
|
LEFT JOIN community c ON p.community_id = c.id
|
||||||
left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
|
LEFT JOIN user_ u ON ct.creator_id = u.id
|
||||||
left join (
|
LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
|
||||||
select
|
AND p.id = ct.post_id
|
||||||
l.comment_id as id,
|
AND p.community_id = cb.community_id
|
||||||
sum(l.score) as total,
|
LEFT JOIN (
|
||||||
count(case when l.score = 1 then 1 else null end) as up,
|
SELECT
|
||||||
count(case when l.score = -1 then 1 else null end) as down
|
l.comment_id AS id,
|
||||||
from comment_like l
|
sum(l.score) AS total,
|
||||||
group by comment_id
|
count(
|
||||||
) as cl on cl.id = ct.id;
|
CASE WHEN l.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS up,
|
||||||
|
count(
|
||||||
|
CASE WHEN l.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS down
|
||||||
|
FROM
|
||||||
|
comment_like l
|
||||||
|
GROUP BY
|
||||||
|
comment_id) AS cl ON cl.id = ct.id;
|
||||||
|
|
||||||
create or replace view comment_view as (
|
CREATE OR REPLACE VIEW comment_view AS (
|
||||||
select
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
us.user_id as user_id,
|
us.user_id AS user_id,
|
||||||
us.my_vote as my_vote,
|
us.my_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from comment_aggregates_view cav
|
FROM
|
||||||
cross join lateral (
|
comment_aggregates_view cav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
u.id AS user_id,
|
||||||
coalesce(cf.id, 0) as is_subbed,
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
coalesce(cs.id, 0) as is_saved
|
coalesce(cf.id, 0) AS is_subbed,
|
||||||
from user_ u
|
coalesce(cs.id, 0) AS is_saved
|
||||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
FROM
|
||||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
) as us
|
AND cav.id = cl.comment_id
|
||||||
|
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||||
union all
|
AND cs.comment_id = cav.id
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
select
|
AND cav.community_id = cf.community_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from comment_aggregates_view cav
|
FROM
|
||||||
);
|
comment_aggregates_view cav);
|
||||||
|
|
||||||
create table comment_aggregates_fast as select * from comment_aggregates_view;
|
CREATE TABLE comment_aggregates_fast AS
|
||||||
alter table comment_aggregates_fast add primary key (id);
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
comment_aggregates_view;
|
||||||
|
|
||||||
create view comment_fast_view as
|
ALTER TABLE comment_aggregates_fast
|
||||||
select
|
ADD PRIMARY KEY (id);
|
||||||
|
|
||||||
|
CREATE VIEW comment_fast_view AS
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
us.user_id as user_id,
|
us.user_id AS user_id,
|
||||||
us.my_vote as my_vote,
|
us.my_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from comment_aggregates_fast cav
|
FROM
|
||||||
cross join lateral (
|
comment_aggregates_fast cav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
u.id AS user_id,
|
||||||
coalesce(cf.id, 0) as is_subbed,
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
coalesce(cs.id, 0) as is_saved
|
coalesce(cf.id, 0) AS is_subbed,
|
||||||
from user_ u
|
coalesce(cs.id, 0) AS is_saved
|
||||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
FROM
|
||||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
) as us
|
AND cav.id = cl.comment_id
|
||||||
|
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||||
union all
|
AND cs.comment_id = cav.id
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
select
|
AND cav.community_id = cf.community_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from comment_aggregates_fast cav;
|
FROM
|
||||||
|
comment_aggregates_fast cav;
|
||||||
|
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.creator_actor_id,
|
c.creator_actor_id,
|
||||||
c.creator_local,
|
c.creator_local,
|
||||||
|
@ -137,15 +167,30 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from user_mention um, comment_view c
|
actor_id
|
||||||
where um.comment_id = c.id;
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
create view user_mention_fast_view as
|
CREATE VIEW user_mention_fast_view AS
|
||||||
select
|
SELECT
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.creator_actor_id,
|
ac.creator_actor_id,
|
||||||
ac.creator_local,
|
ac.creator_local,
|
||||||
|
@ -169,26 +214,45 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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,
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from user_ u
|
actor_id
|
||||||
cross join (
|
FROM
|
||||||
select
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN (
|
||||||
|
SELECT
|
||||||
ca.*
|
ca.*
|
||||||
from comment_aggregates_fast ca
|
FROM
|
||||||
) ac
|
comment_aggregates_fast ca) ac
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
left join user_mention um on um.comment_id = ac.id
|
AND ac.id = cl.comment_id
|
||||||
|
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||||
union all
|
UNION ALL
|
||||||
|
SELECT
|
||||||
select
|
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.creator_actor_id,
|
ac.creator_actor_id,
|
||||||
ac.creator_local,
|
ac.creator_local,
|
||||||
|
@ -212,177 +276,220 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved,
|
NULL AS saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from comment_aggregates_fast ac
|
actor_id
|
||||||
left join user_mention um on um.comment_id = ac.id
|
FROM
|
||||||
;
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
comment_aggregates_fast ac
|
||||||
|
LEFT JOIN user_mention um ON um.comment_id = ac.id;
|
||||||
|
|
||||||
-- Do the reply_view referencing the comment_fast_view
|
-- Do the reply_view referencing the comment_fast_view
|
||||||
create view reply_fast_view as
|
CREATE VIEW reply_fast_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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_fast_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_fast_view cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
-- add creator_published to the post view
|
-- add creator_published to the post view
|
||||||
drop view post_fast_view;
|
DROP VIEW post_fast_view;
|
||||||
drop table post_aggregates_fast;
|
|
||||||
drop view post_view;
|
|
||||||
drop view post_aggregates_view;
|
|
||||||
|
|
||||||
create view post_aggregates_view as
|
DROP TABLE post_aggregates_fast;
|
||||||
select
|
|
||||||
|
DROP VIEW post_view;
|
||||||
|
|
||||||
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
|
CREATE VIEW post_aggregates_view AS
|
||||||
|
SELECT
|
||||||
p.*,
|
p.*,
|
||||||
-- creator details
|
-- creator details
|
||||||
u.actor_id as creator_actor_id,
|
u.actor_id AS creator_actor_id,
|
||||||
u."local" as creator_local,
|
u."local" AS creator_local,
|
||||||
u."name" as creator_name,
|
u."name" AS creator_name,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
u.banned as banned,
|
u.banned AS banned,
|
||||||
cb.id::bool as banned_from_community,
|
cb.id::bool AS banned_from_community,
|
||||||
-- community details
|
-- community details
|
||||||
c.actor_id as community_actor_id,
|
c.actor_id AS community_actor_id,
|
||||||
c."local" as community_local,
|
c."local" AS community_local,
|
||||||
c."name" as community_name,
|
c."name" AS community_name,
|
||||||
c.removed as community_removed,
|
c.removed AS community_removed,
|
||||||
c.deleted as community_deleted,
|
c.deleted AS community_deleted,
|
||||||
c.nsfw as community_nsfw,
|
c.nsfw AS community_nsfw,
|
||||||
-- post score data/comment count
|
-- post score data/comment count
|
||||||
coalesce(ct.comments, 0) as number_of_comments,
|
coalesce(ct.comments, 0) AS number_of_comments,
|
||||||
coalesce(pl.score, 0) as score,
|
coalesce(pl.score, 0) AS score,
|
||||||
coalesce(pl.upvotes, 0) as upvotes,
|
coalesce(pl.upvotes, 0) AS upvotes,
|
||||||
coalesce(pl.downvotes, 0) as downvotes,
|
coalesce(pl.downvotes, 0) AS downvotes,
|
||||||
hot_rank(
|
hot_rank (coalesce(pl.score, 0), (
|
||||||
coalesce(pl.score , 0), (
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
case
|
p.published
|
||||||
when (p.published < ('now'::timestamp - '1 month'::interval))
|
ELSE
|
||||||
then p.published
|
greatest (ct.recent_comment_time, p.published)
|
||||||
else greatest(ct.recent_comment_time, p.published)
|
END)) AS hot_rank,
|
||||||
end
|
|
||||||
)
|
|
||||||
) as hot_rank,
|
|
||||||
(
|
(
|
||||||
case
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
when (p.published < ('now'::timestamp - '1 month'::interval))
|
p.published
|
||||||
then p.published
|
ELSE
|
||||||
else greatest(ct.recent_comment_time, p.published)
|
greatest (ct.recent_comment_time, p.published)
|
||||||
end
|
END) AS newest_activity_time
|
||||||
) as newest_activity_time
|
FROM
|
||||||
from post p
|
post p
|
||||||
left join user_ u on p.creator_id = u.id
|
LEFT JOIN user_ u ON p.creator_id = u.id
|
||||||
left join community_user_ban cb on p.creator_id = cb.user_id and p.community_id = cb.community_id
|
LEFT JOIN community_user_ban cb ON p.creator_id = cb.user_id
|
||||||
left join community c on p.community_id = c.id
|
AND p.community_id = cb.community_id
|
||||||
left join (
|
LEFT JOIN community c ON p.community_id = c.id
|
||||||
select
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
post_id,
|
post_id,
|
||||||
count(*) as comments,
|
count(*) AS comments,
|
||||||
max(published) as recent_comment_time
|
max(published) AS recent_comment_time
|
||||||
from comment
|
FROM
|
||||||
group by post_id
|
comment
|
||||||
) ct on ct.post_id = p.id
|
GROUP BY
|
||||||
left join (
|
post_id) ct ON ct.post_id = p.id
|
||||||
select
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
post_id,
|
post_id,
|
||||||
sum(score) as score,
|
sum(score) AS score,
|
||||||
sum(score) filter (where score = 1) as upvotes,
|
sum(score) FILTER (WHERE score = 1) AS upvotes,
|
||||||
-sum(score) filter (where score = -1) as downvotes
|
- sum(score) FILTER (WHERE score = - 1) AS downvotes
|
||||||
from post_like
|
FROM
|
||||||
group by post_id
|
post_like
|
||||||
) pl on pl.post_id = p.id
|
GROUP BY
|
||||||
order by p.id;
|
post_id) pl ON pl.post_id = p.id
|
||||||
|
ORDER BY
|
||||||
|
p.id;
|
||||||
|
|
||||||
create view post_view as
|
CREATE VIEW post_view AS
|
||||||
select
|
SELECT
|
||||||
pav.*,
|
pav.*,
|
||||||
us.id as user_id,
|
us.id AS user_id,
|
||||||
us.user_vote as my_vote,
|
us.user_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_read::bool as read,
|
us.is_read::bool AS read,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from post_aggregates_view pav
|
FROM
|
||||||
cross join lateral (
|
post_aggregates_view pav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
|
SELECT
|
||||||
u.id,
|
u.id,
|
||||||
coalesce(cf.community_id, 0) as is_subbed,
|
coalesce(cf.community_id, 0) AS is_subbed,
|
||||||
coalesce(pr.post_id, 0) as is_read,
|
coalesce(pr.post_id, 0) AS is_read,
|
||||||
coalesce(ps.post_id, 0) as is_saved,
|
coalesce(ps.post_id, 0) AS is_saved,
|
||||||
coalesce(pl.score, 0) as user_vote
|
coalesce(pl.score, 0) AS user_vote
|
||||||
from user_ u
|
FROM
|
||||||
left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
|
LEFT JOIN community_user_ban cb ON u.id = cb.user_id
|
||||||
left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
|
AND cb.community_id = pav.community_id
|
||||||
left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
|
AND cf.community_id = pav.community_id
|
||||||
) as us
|
LEFT JOIN post_read pr ON u.id = pr.user_id
|
||||||
|
AND pr.post_id = pav.id
|
||||||
union all
|
LEFT JOIN post_saved ps ON u.id = ps.user_id
|
||||||
|
AND ps.post_id = pav.id
|
||||||
select
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND pav.id = pl.post_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
pav.*,
|
pav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from post_aggregates_view pav;
|
FROM
|
||||||
|
post_aggregates_view pav;
|
||||||
|
|
||||||
create table post_aggregates_fast as select * from post_aggregates_view;
|
CREATE TABLE post_aggregates_fast AS
|
||||||
alter table post_aggregates_fast add primary key (id);
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
create view post_fast_view as
|
ALTER TABLE post_aggregates_fast
|
||||||
select
|
ADD PRIMARY KEY (id);
|
||||||
|
|
||||||
|
CREATE VIEW post_fast_view AS
|
||||||
|
SELECT
|
||||||
pav.*,
|
pav.*,
|
||||||
us.id as user_id,
|
us.id AS user_id,
|
||||||
us.user_vote as my_vote,
|
us.user_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_read::bool as read,
|
us.is_read::bool AS read,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from post_aggregates_fast pav
|
FROM
|
||||||
cross join lateral (
|
post_aggregates_fast pav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
|
SELECT
|
||||||
u.id,
|
u.id,
|
||||||
coalesce(cf.community_id, 0) as is_subbed,
|
coalesce(cf.community_id, 0) AS is_subbed,
|
||||||
coalesce(pr.post_id, 0) as is_read,
|
coalesce(pr.post_id, 0) AS is_read,
|
||||||
coalesce(ps.post_id, 0) as is_saved,
|
coalesce(ps.post_id, 0) AS is_saved,
|
||||||
coalesce(pl.score, 0) as user_vote
|
coalesce(pl.score, 0) AS user_vote
|
||||||
from user_ u
|
FROM
|
||||||
left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
|
LEFT JOIN community_user_ban cb ON u.id = cb.user_id
|
||||||
left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
|
AND cb.community_id = pav.community_id
|
||||||
left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
|
AND cf.community_id = pav.community_id
|
||||||
) as us
|
LEFT JOIN post_read pr ON u.id = pr.user_id
|
||||||
|
AND pr.post_id = pav.id
|
||||||
union all
|
LEFT JOIN post_saved ps ON u.id = ps.user_id
|
||||||
|
AND ps.post_id = pav.id
|
||||||
select
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND pav.id = pl.post_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
pav.*,
|
pav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from post_aggregates_fast pav;
|
FROM
|
||||||
|
post_aggregates_fast pav;
|
||||||
|
|
||||||
|
|
|
@ -1,116 +1,146 @@
|
||||||
drop view user_mention_view;
|
DROP VIEW user_mention_view;
|
||||||
drop view reply_fast_view;
|
|
||||||
drop view comment_fast_view;
|
|
||||||
drop view comment_view;
|
|
||||||
|
|
||||||
drop view user_mention_fast_view;
|
DROP VIEW reply_fast_view;
|
||||||
drop table comment_aggregates_fast;
|
|
||||||
drop view comment_aggregates_view;
|
|
||||||
|
|
||||||
create view comment_aggregates_view as
|
DROP VIEW comment_fast_view;
|
||||||
select
|
|
||||||
|
DROP VIEW comment_view;
|
||||||
|
|
||||||
|
DROP VIEW user_mention_fast_view;
|
||||||
|
|
||||||
|
DROP TABLE comment_aggregates_fast;
|
||||||
|
|
||||||
|
DROP VIEW comment_aggregates_view;
|
||||||
|
|
||||||
|
CREATE VIEW comment_aggregates_view AS
|
||||||
|
SELECT
|
||||||
ct.*,
|
ct.*,
|
||||||
-- community details
|
-- community details
|
||||||
p.community_id,
|
p.community_id,
|
||||||
c.actor_id as community_actor_id,
|
c.actor_id AS community_actor_id,
|
||||||
c."local" as community_local,
|
c."local" AS community_local,
|
||||||
c."name" as community_name,
|
c."name" AS community_name,
|
||||||
-- creator details
|
-- creator details
|
||||||
u.banned as banned,
|
u.banned AS banned,
|
||||||
coalesce(cb.id, 0)::bool as banned_from_community,
|
coalesce(cb.id, 0)::bool AS banned_from_community,
|
||||||
u.actor_id as creator_actor_id,
|
u.actor_id AS creator_actor_id,
|
||||||
u.local as creator_local,
|
u.local AS creator_local,
|
||||||
u.name as creator_name,
|
u.name AS creator_name,
|
||||||
u.published as creator_published,
|
u.published AS creator_published,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
-- score details
|
-- score details
|
||||||
coalesce(cl.total, 0) as score,
|
coalesce(cl.total, 0) AS score,
|
||||||
coalesce(cl.up, 0) as upvotes,
|
coalesce(cl.up, 0) AS upvotes,
|
||||||
coalesce(cl.down, 0) as downvotes,
|
coalesce(cl.down, 0) AS downvotes,
|
||||||
hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
|
hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
|
||||||
from comment ct
|
FROM
|
||||||
left join post p on ct.post_id = p.id
|
comment ct
|
||||||
left join community c on p.community_id = c.id
|
LEFT JOIN post p ON ct.post_id = p.id
|
||||||
left join user_ u on ct.creator_id = u.id
|
LEFT JOIN community c ON p.community_id = c.id
|
||||||
left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
|
LEFT JOIN user_ u ON ct.creator_id = u.id
|
||||||
left join (
|
LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
|
||||||
select
|
AND p.id = ct.post_id
|
||||||
l.comment_id as id,
|
AND p.community_id = cb.community_id
|
||||||
sum(l.score) as total,
|
LEFT JOIN (
|
||||||
count(case when l.score = 1 then 1 else null end) as up,
|
SELECT
|
||||||
count(case when l.score = -1 then 1 else null end) as down
|
l.comment_id AS id,
|
||||||
from comment_like l
|
sum(l.score) AS total,
|
||||||
group by comment_id
|
count(
|
||||||
) as cl on cl.id = ct.id;
|
CASE WHEN l.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS up,
|
||||||
|
count(
|
||||||
|
CASE WHEN l.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS down
|
||||||
|
FROM
|
||||||
|
comment_like l
|
||||||
|
GROUP BY
|
||||||
|
comment_id) AS cl ON cl.id = ct.id;
|
||||||
|
|
||||||
create or replace view comment_view as (
|
CREATE OR REPLACE VIEW comment_view AS (
|
||||||
select
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
us.user_id as user_id,
|
us.user_id AS user_id,
|
||||||
us.my_vote as my_vote,
|
us.my_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from comment_aggregates_view cav
|
FROM
|
||||||
cross join lateral (
|
comment_aggregates_view cav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
u.id AS user_id,
|
||||||
coalesce(cf.id, 0) as is_subbed,
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
coalesce(cs.id, 0) as is_saved
|
coalesce(cf.id, 0) AS is_subbed,
|
||||||
from user_ u
|
coalesce(cs.id, 0) AS is_saved
|
||||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
FROM
|
||||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
) as us
|
AND cav.id = cl.comment_id
|
||||||
|
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||||
union all
|
AND cs.comment_id = cav.id
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
select
|
AND cav.community_id = cf.community_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from comment_aggregates_view cav
|
FROM
|
||||||
);
|
comment_aggregates_view cav);
|
||||||
|
|
||||||
create table comment_aggregates_fast as select * from comment_aggregates_view;
|
CREATE TABLE comment_aggregates_fast AS
|
||||||
alter table comment_aggregates_fast add primary key (id);
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
comment_aggregates_view;
|
||||||
|
|
||||||
create view comment_fast_view as
|
ALTER TABLE comment_aggregates_fast
|
||||||
select
|
ADD PRIMARY KEY (id);
|
||||||
|
|
||||||
|
CREATE VIEW comment_fast_view AS
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
us.user_id as user_id,
|
us.user_id AS user_id,
|
||||||
us.my_vote as my_vote,
|
us.my_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from comment_aggregates_fast cav
|
FROM
|
||||||
cross join lateral (
|
comment_aggregates_fast cav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
u.id AS user_id,
|
||||||
coalesce(cf.id, 0) as is_subbed,
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
coalesce(cs.id, 0) as is_saved
|
coalesce(cf.id, 0) AS is_subbed,
|
||||||
from user_ u
|
coalesce(cs.id, 0) AS is_saved
|
||||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
FROM
|
||||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
) as us
|
AND cav.id = cl.comment_id
|
||||||
|
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||||
union all
|
AND cs.comment_id = cav.id
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
select
|
AND cav.community_id = cf.community_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from comment_aggregates_fast cav;
|
FROM
|
||||||
|
comment_aggregates_fast cav;
|
||||||
|
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.creator_actor_id,
|
c.creator_actor_id,
|
||||||
c.creator_local,
|
c.creator_local,
|
||||||
|
@ -138,15 +168,30 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from user_mention um, comment_view c
|
actor_id
|
||||||
where um.comment_id = c.id;
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
create view user_mention_fast_view as
|
CREATE VIEW user_mention_fast_view AS
|
||||||
select
|
SELECT
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.creator_actor_id,
|
ac.creator_actor_id,
|
||||||
ac.creator_local,
|
ac.creator_local,
|
||||||
|
@ -170,26 +215,45 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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,
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from user_ u
|
actor_id
|
||||||
cross join (
|
FROM
|
||||||
select
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN (
|
||||||
|
SELECT
|
||||||
ca.*
|
ca.*
|
||||||
from comment_aggregates_fast ca
|
FROM
|
||||||
) ac
|
comment_aggregates_fast ca) ac
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
left join user_mention um on um.comment_id = ac.id
|
AND ac.id = cl.comment_id
|
||||||
|
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||||
union all
|
UNION ALL
|
||||||
|
SELECT
|
||||||
select
|
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.creator_actor_id,
|
ac.creator_actor_id,
|
||||||
ac.creator_local,
|
ac.creator_local,
|
||||||
|
@ -213,178 +277,221 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved,
|
NULL AS saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from comment_aggregates_fast ac
|
actor_id
|
||||||
left join user_mention um on um.comment_id = ac.id
|
FROM
|
||||||
;
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
comment_aggregates_fast ac
|
||||||
|
LEFT JOIN user_mention um ON um.comment_id = ac.id;
|
||||||
|
|
||||||
-- Do the reply_view referencing the comment_fast_view
|
-- Do the reply_view referencing the comment_fast_view
|
||||||
create view reply_fast_view as
|
CREATE VIEW reply_fast_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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_fast_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_fast_view cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
-- add creator_published to the post view
|
-- add creator_published to the post view
|
||||||
drop view post_fast_view;
|
DROP VIEW post_fast_view;
|
||||||
drop table post_aggregates_fast;
|
|
||||||
drop view post_view;
|
|
||||||
drop view post_aggregates_view;
|
|
||||||
|
|
||||||
create view post_aggregates_view as
|
DROP TABLE post_aggregates_fast;
|
||||||
select
|
|
||||||
|
DROP VIEW post_view;
|
||||||
|
|
||||||
|
DROP VIEW post_aggregates_view;
|
||||||
|
|
||||||
|
CREATE VIEW post_aggregates_view AS
|
||||||
|
SELECT
|
||||||
p.*,
|
p.*,
|
||||||
-- creator details
|
-- creator details
|
||||||
u.actor_id as creator_actor_id,
|
u.actor_id AS creator_actor_id,
|
||||||
u."local" as creator_local,
|
u."local" AS creator_local,
|
||||||
u."name" as creator_name,
|
u."name" AS creator_name,
|
||||||
u.published as creator_published,
|
u.published AS creator_published,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
u.banned as banned,
|
u.banned AS banned,
|
||||||
cb.id::bool as banned_from_community,
|
cb.id::bool AS banned_from_community,
|
||||||
-- community details
|
-- community details
|
||||||
c.actor_id as community_actor_id,
|
c.actor_id AS community_actor_id,
|
||||||
c."local" as community_local,
|
c."local" AS community_local,
|
||||||
c."name" as community_name,
|
c."name" AS community_name,
|
||||||
c.removed as community_removed,
|
c.removed AS community_removed,
|
||||||
c.deleted as community_deleted,
|
c.deleted AS community_deleted,
|
||||||
c.nsfw as community_nsfw,
|
c.nsfw AS community_nsfw,
|
||||||
-- post score data/comment count
|
-- post score data/comment count
|
||||||
coalesce(ct.comments, 0) as number_of_comments,
|
coalesce(ct.comments, 0) AS number_of_comments,
|
||||||
coalesce(pl.score, 0) as score,
|
coalesce(pl.score, 0) AS score,
|
||||||
coalesce(pl.upvotes, 0) as upvotes,
|
coalesce(pl.upvotes, 0) AS upvotes,
|
||||||
coalesce(pl.downvotes, 0) as downvotes,
|
coalesce(pl.downvotes, 0) AS downvotes,
|
||||||
hot_rank(
|
hot_rank (coalesce(pl.score, 0), (
|
||||||
coalesce(pl.score , 0), (
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
case
|
p.published
|
||||||
when (p.published < ('now'::timestamp - '1 month'::interval))
|
ELSE
|
||||||
then p.published
|
greatest (ct.recent_comment_time, p.published)
|
||||||
else greatest(ct.recent_comment_time, p.published)
|
END)) AS hot_rank,
|
||||||
end
|
|
||||||
)
|
|
||||||
) as hot_rank,
|
|
||||||
(
|
(
|
||||||
case
|
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||||
when (p.published < ('now'::timestamp - '1 month'::interval))
|
p.published
|
||||||
then p.published
|
ELSE
|
||||||
else greatest(ct.recent_comment_time, p.published)
|
greatest (ct.recent_comment_time, p.published)
|
||||||
end
|
END) AS newest_activity_time
|
||||||
) as newest_activity_time
|
FROM
|
||||||
from post p
|
post p
|
||||||
left join user_ u on p.creator_id = u.id
|
LEFT JOIN user_ u ON p.creator_id = u.id
|
||||||
left join community_user_ban cb on p.creator_id = cb.user_id and p.community_id = cb.community_id
|
LEFT JOIN community_user_ban cb ON p.creator_id = cb.user_id
|
||||||
left join community c on p.community_id = c.id
|
AND p.community_id = cb.community_id
|
||||||
left join (
|
LEFT JOIN community c ON p.community_id = c.id
|
||||||
select
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
post_id,
|
post_id,
|
||||||
count(*) as comments,
|
count(*) AS comments,
|
||||||
max(published) as recent_comment_time
|
max(published) AS recent_comment_time
|
||||||
from comment
|
FROM
|
||||||
group by post_id
|
comment
|
||||||
) ct on ct.post_id = p.id
|
GROUP BY
|
||||||
left join (
|
post_id) ct ON ct.post_id = p.id
|
||||||
select
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
post_id,
|
post_id,
|
||||||
sum(score) as score,
|
sum(score) AS score,
|
||||||
sum(score) filter (where score = 1) as upvotes,
|
sum(score) FILTER (WHERE score = 1) AS upvotes,
|
||||||
-sum(score) filter (where score = -1) as downvotes
|
- sum(score) FILTER (WHERE score = - 1) AS downvotes
|
||||||
from post_like
|
FROM
|
||||||
group by post_id
|
post_like
|
||||||
) pl on pl.post_id = p.id
|
GROUP BY
|
||||||
order by p.id;
|
post_id) pl ON pl.post_id = p.id
|
||||||
|
ORDER BY
|
||||||
|
p.id;
|
||||||
|
|
||||||
create view post_view as
|
CREATE VIEW post_view AS
|
||||||
select
|
SELECT
|
||||||
pav.*,
|
pav.*,
|
||||||
us.id as user_id,
|
us.id AS user_id,
|
||||||
us.user_vote as my_vote,
|
us.user_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_read::bool as read,
|
us.is_read::bool AS read,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from post_aggregates_view pav
|
FROM
|
||||||
cross join lateral (
|
post_aggregates_view pav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
|
SELECT
|
||||||
u.id,
|
u.id,
|
||||||
coalesce(cf.community_id, 0) as is_subbed,
|
coalesce(cf.community_id, 0) AS is_subbed,
|
||||||
coalesce(pr.post_id, 0) as is_read,
|
coalesce(pr.post_id, 0) AS is_read,
|
||||||
coalesce(ps.post_id, 0) as is_saved,
|
coalesce(ps.post_id, 0) AS is_saved,
|
||||||
coalesce(pl.score, 0) as user_vote
|
coalesce(pl.score, 0) AS user_vote
|
||||||
from user_ u
|
FROM
|
||||||
left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
|
LEFT JOIN community_user_ban cb ON u.id = cb.user_id
|
||||||
left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
|
AND cb.community_id = pav.community_id
|
||||||
left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
|
AND cf.community_id = pav.community_id
|
||||||
) as us
|
LEFT JOIN post_read pr ON u.id = pr.user_id
|
||||||
|
AND pr.post_id = pav.id
|
||||||
union all
|
LEFT JOIN post_saved ps ON u.id = ps.user_id
|
||||||
|
AND ps.post_id = pav.id
|
||||||
select
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND pav.id = pl.post_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
pav.*,
|
pav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from post_aggregates_view pav;
|
FROM
|
||||||
|
post_aggregates_view pav;
|
||||||
|
|
||||||
create table post_aggregates_fast as select * from post_aggregates_view;
|
CREATE TABLE post_aggregates_fast AS
|
||||||
alter table post_aggregates_fast add primary key (id);
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
post_aggregates_view;
|
||||||
|
|
||||||
create view post_fast_view as
|
ALTER TABLE post_aggregates_fast
|
||||||
select
|
ADD PRIMARY KEY (id);
|
||||||
|
|
||||||
|
CREATE VIEW post_fast_view AS
|
||||||
|
SELECT
|
||||||
pav.*,
|
pav.*,
|
||||||
us.id as user_id,
|
us.id AS user_id,
|
||||||
us.user_vote as my_vote,
|
us.user_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_read::bool as read,
|
us.is_read::bool AS read,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from post_aggregates_fast pav
|
FROM
|
||||||
cross join lateral (
|
post_aggregates_fast pav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
|
SELECT
|
||||||
u.id,
|
u.id,
|
||||||
coalesce(cf.community_id, 0) as is_subbed,
|
coalesce(cf.community_id, 0) AS is_subbed,
|
||||||
coalesce(pr.post_id, 0) as is_read,
|
coalesce(pr.post_id, 0) AS is_read,
|
||||||
coalesce(ps.post_id, 0) as is_saved,
|
coalesce(ps.post_id, 0) AS is_saved,
|
||||||
coalesce(pl.score, 0) as user_vote
|
coalesce(pl.score, 0) AS user_vote
|
||||||
from user_ u
|
FROM
|
||||||
left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
|
LEFT JOIN community_user_ban cb ON u.id = cb.user_id
|
||||||
left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
|
AND cb.community_id = pav.community_id
|
||||||
left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
|
AND cf.community_id = pav.community_id
|
||||||
) as us
|
LEFT JOIN post_read pr ON u.id = pr.user_id
|
||||||
|
AND pr.post_id = pav.id
|
||||||
union all
|
LEFT JOIN post_saved ps ON u.id = ps.user_id
|
||||||
|
AND ps.post_id = pav.id
|
||||||
select
|
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||||
|
AND pav.id = pl.post_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
pav.*,
|
pav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as read,
|
NULL AS read,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from post_aggregates_fast pav;
|
FROM
|
||||||
|
post_aggregates_fast pav;
|
||||||
|
|
||||||
|
|
|
@ -1,116 +1,146 @@
|
||||||
drop view user_mention_view;
|
DROP VIEW user_mention_view;
|
||||||
drop view reply_fast_view;
|
|
||||||
drop view comment_fast_view;
|
|
||||||
drop view comment_view;
|
|
||||||
|
|
||||||
drop view user_mention_fast_view;
|
DROP VIEW reply_fast_view;
|
||||||
drop table comment_aggregates_fast;
|
|
||||||
drop view comment_aggregates_view;
|
|
||||||
|
|
||||||
create view comment_aggregates_view as
|
DROP VIEW comment_fast_view;
|
||||||
select
|
|
||||||
|
DROP VIEW comment_view;
|
||||||
|
|
||||||
|
DROP VIEW user_mention_fast_view;
|
||||||
|
|
||||||
|
DROP TABLE comment_aggregates_fast;
|
||||||
|
|
||||||
|
DROP VIEW comment_aggregates_view;
|
||||||
|
|
||||||
|
CREATE VIEW comment_aggregates_view AS
|
||||||
|
SELECT
|
||||||
ct.*,
|
ct.*,
|
||||||
-- community details
|
-- community details
|
||||||
p.community_id,
|
p.community_id,
|
||||||
c.actor_id as community_actor_id,
|
c.actor_id AS community_actor_id,
|
||||||
c."local" as community_local,
|
c."local" AS community_local,
|
||||||
c."name" as community_name,
|
c."name" AS community_name,
|
||||||
-- creator details
|
-- creator details
|
||||||
u.banned as banned,
|
u.banned AS banned,
|
||||||
coalesce(cb.id, 0)::bool as banned_from_community,
|
coalesce(cb.id, 0)::bool AS banned_from_community,
|
||||||
u.actor_id as creator_actor_id,
|
u.actor_id AS creator_actor_id,
|
||||||
u.local as creator_local,
|
u.local AS creator_local,
|
||||||
u.name as creator_name,
|
u.name AS creator_name,
|
||||||
u.published as creator_published,
|
u.published AS creator_published,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
-- score details
|
-- score details
|
||||||
coalesce(cl.total, 0) as score,
|
coalesce(cl.total, 0) AS score,
|
||||||
coalesce(cl.up, 0) as upvotes,
|
coalesce(cl.up, 0) AS upvotes,
|
||||||
coalesce(cl.down, 0) as downvotes,
|
coalesce(cl.down, 0) AS downvotes,
|
||||||
hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
|
hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
|
||||||
from comment ct
|
FROM
|
||||||
left join post p on ct.post_id = p.id
|
comment ct
|
||||||
left join community c on p.community_id = c.id
|
LEFT JOIN post p ON ct.post_id = p.id
|
||||||
left join user_ u on ct.creator_id = u.id
|
LEFT JOIN community c ON p.community_id = c.id
|
||||||
left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
|
LEFT JOIN user_ u ON ct.creator_id = u.id
|
||||||
left join (
|
LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
|
||||||
select
|
AND p.id = ct.post_id
|
||||||
l.comment_id as id,
|
AND p.community_id = cb.community_id
|
||||||
sum(l.score) as total,
|
LEFT JOIN (
|
||||||
count(case when l.score = 1 then 1 else null end) as up,
|
SELECT
|
||||||
count(case when l.score = -1 then 1 else null end) as down
|
l.comment_id AS id,
|
||||||
from comment_like l
|
sum(l.score) AS total,
|
||||||
group by comment_id
|
count(
|
||||||
) as cl on cl.id = ct.id;
|
CASE WHEN l.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS up,
|
||||||
|
count(
|
||||||
|
CASE WHEN l.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS down
|
||||||
|
FROM
|
||||||
|
comment_like l
|
||||||
|
GROUP BY
|
||||||
|
comment_id) AS cl ON cl.id = ct.id;
|
||||||
|
|
||||||
create or replace view comment_view as (
|
CREATE OR REPLACE VIEW comment_view AS (
|
||||||
select
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
us.user_id as user_id,
|
us.user_id AS user_id,
|
||||||
us.my_vote as my_vote,
|
us.my_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from comment_aggregates_view cav
|
FROM
|
||||||
cross join lateral (
|
comment_aggregates_view cav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
u.id AS user_id,
|
||||||
coalesce(cf.id, 0) as is_subbed,
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
coalesce(cs.id, 0) as is_saved
|
coalesce(cf.id, 0) AS is_subbed,
|
||||||
from user_ u
|
coalesce(cs.id, 0) AS is_saved
|
||||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
FROM
|
||||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
) as us
|
AND cav.id = cl.comment_id
|
||||||
|
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||||
union all
|
AND cs.comment_id = cav.id
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
select
|
AND cav.community_id = cf.community_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from comment_aggregates_view cav
|
FROM
|
||||||
);
|
comment_aggregates_view cav);
|
||||||
|
|
||||||
create table comment_aggregates_fast as select * from comment_aggregates_view;
|
CREATE TABLE comment_aggregates_fast AS
|
||||||
alter table comment_aggregates_fast add primary key (id);
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
comment_aggregates_view;
|
||||||
|
|
||||||
create view comment_fast_view as
|
ALTER TABLE comment_aggregates_fast
|
||||||
select
|
ADD PRIMARY KEY (id);
|
||||||
|
|
||||||
|
CREATE VIEW comment_fast_view AS
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
us.user_id as user_id,
|
us.user_id AS user_id,
|
||||||
us.my_vote as my_vote,
|
us.my_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from comment_aggregates_fast cav
|
FROM
|
||||||
cross join lateral (
|
comment_aggregates_fast cav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
u.id AS user_id,
|
||||||
coalesce(cf.id, 0) as is_subbed,
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
coalesce(cs.id, 0) as is_saved
|
coalesce(cf.id, 0) AS is_subbed,
|
||||||
from user_ u
|
coalesce(cs.id, 0) AS is_saved
|
||||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
FROM
|
||||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
) as us
|
AND cav.id = cl.comment_id
|
||||||
|
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||||
union all
|
AND cs.comment_id = cav.id
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
select
|
AND cav.community_id = cf.community_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from comment_aggregates_fast cav;
|
FROM
|
||||||
|
comment_aggregates_fast cav;
|
||||||
|
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.creator_actor_id,
|
c.creator_actor_id,
|
||||||
c.creator_local,
|
c.creator_local,
|
||||||
|
@ -138,15 +168,30 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from user_mention um, comment_view c
|
actor_id
|
||||||
where um.comment_id = c.id;
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
create view user_mention_fast_view as
|
CREATE VIEW user_mention_fast_view AS
|
||||||
select
|
SELECT
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.creator_actor_id,
|
ac.creator_actor_id,
|
||||||
ac.creator_local,
|
ac.creator_local,
|
||||||
|
@ -170,26 +215,45 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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,
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from user_ u
|
actor_id
|
||||||
cross join (
|
FROM
|
||||||
select
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN (
|
||||||
|
SELECT
|
||||||
ca.*
|
ca.*
|
||||||
from comment_aggregates_fast ca
|
FROM
|
||||||
) ac
|
comment_aggregates_fast ca) ac
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
left join user_mention um on um.comment_id = ac.id
|
AND ac.id = cl.comment_id
|
||||||
|
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||||
union all
|
UNION ALL
|
||||||
|
SELECT
|
||||||
select
|
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.creator_actor_id,
|
ac.creator_actor_id,
|
||||||
ac.creator_local,
|
ac.creator_local,
|
||||||
|
@ -213,37 +277,60 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved,
|
NULL AS saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from comment_aggregates_fast ac
|
actor_id
|
||||||
left join user_mention um on um.comment_id = ac.id
|
FROM
|
||||||
;
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
comment_aggregates_fast ac
|
||||||
|
LEFT JOIN user_mention um ON um.comment_id = ac.id;
|
||||||
|
|
||||||
-- Do the reply_view referencing the comment_fast_view
|
-- Do the reply_view referencing the comment_fast_view
|
||||||
create view reply_fast_view as
|
CREATE VIEW reply_fast_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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_fast_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_fast_view cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
|
|
|
@ -1,118 +1,148 @@
|
||||||
drop view user_mention_view;
|
DROP VIEW user_mention_view;
|
||||||
drop view reply_fast_view;
|
|
||||||
drop view comment_fast_view;
|
|
||||||
drop view comment_view;
|
|
||||||
|
|
||||||
drop view user_mention_fast_view;
|
DROP VIEW reply_fast_view;
|
||||||
drop table comment_aggregates_fast;
|
|
||||||
drop view comment_aggregates_view;
|
|
||||||
|
|
||||||
create view comment_aggregates_view as
|
DROP VIEW comment_fast_view;
|
||||||
select
|
|
||||||
|
DROP VIEW comment_view;
|
||||||
|
|
||||||
|
DROP VIEW user_mention_fast_view;
|
||||||
|
|
||||||
|
DROP TABLE comment_aggregates_fast;
|
||||||
|
|
||||||
|
DROP VIEW comment_aggregates_view;
|
||||||
|
|
||||||
|
CREATE VIEW comment_aggregates_view AS
|
||||||
|
SELECT
|
||||||
ct.*,
|
ct.*,
|
||||||
-- post details
|
-- post details
|
||||||
p."name" as post_name,
|
p."name" AS post_name,
|
||||||
p.community_id,
|
p.community_id,
|
||||||
-- community details
|
-- community details
|
||||||
c.actor_id as community_actor_id,
|
c.actor_id AS community_actor_id,
|
||||||
c."local" as community_local,
|
c."local" AS community_local,
|
||||||
c."name" as community_name,
|
c."name" AS community_name,
|
||||||
-- creator details
|
-- creator details
|
||||||
u.banned as banned,
|
u.banned AS banned,
|
||||||
coalesce(cb.id, 0)::bool as banned_from_community,
|
coalesce(cb.id, 0)::bool AS banned_from_community,
|
||||||
u.actor_id as creator_actor_id,
|
u.actor_id AS creator_actor_id,
|
||||||
u.local as creator_local,
|
u.local AS creator_local,
|
||||||
u.name as creator_name,
|
u.name AS creator_name,
|
||||||
u.published as creator_published,
|
u.published AS creator_published,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
-- score details
|
-- score details
|
||||||
coalesce(cl.total, 0) as score,
|
coalesce(cl.total, 0) AS score,
|
||||||
coalesce(cl.up, 0) as upvotes,
|
coalesce(cl.up, 0) AS upvotes,
|
||||||
coalesce(cl.down, 0) as downvotes,
|
coalesce(cl.down, 0) AS downvotes,
|
||||||
hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
|
hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
|
||||||
from comment ct
|
FROM
|
||||||
left join post p on ct.post_id = p.id
|
comment ct
|
||||||
left join community c on p.community_id = c.id
|
LEFT JOIN post p ON ct.post_id = p.id
|
||||||
left join user_ u on ct.creator_id = u.id
|
LEFT JOIN community c ON p.community_id = c.id
|
||||||
left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
|
LEFT JOIN user_ u ON ct.creator_id = u.id
|
||||||
left join (
|
LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
|
||||||
select
|
AND p.id = ct.post_id
|
||||||
l.comment_id as id,
|
AND p.community_id = cb.community_id
|
||||||
sum(l.score) as total,
|
LEFT JOIN (
|
||||||
count(case when l.score = 1 then 1 else null end) as up,
|
SELECT
|
||||||
count(case when l.score = -1 then 1 else null end) as down
|
l.comment_id AS id,
|
||||||
from comment_like l
|
sum(l.score) AS total,
|
||||||
group by comment_id
|
count(
|
||||||
) as cl on cl.id = ct.id;
|
CASE WHEN l.score = 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS up,
|
||||||
|
count(
|
||||||
|
CASE WHEN l.score = - 1 THEN
|
||||||
|
1
|
||||||
|
ELSE
|
||||||
|
NULL
|
||||||
|
END) AS down
|
||||||
|
FROM
|
||||||
|
comment_like l
|
||||||
|
GROUP BY
|
||||||
|
comment_id) AS cl ON cl.id = ct.id;
|
||||||
|
|
||||||
create or replace view comment_view as (
|
CREATE OR REPLACE VIEW comment_view AS (
|
||||||
select
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
us.user_id as user_id,
|
us.user_id AS user_id,
|
||||||
us.my_vote as my_vote,
|
us.my_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from comment_aggregates_view cav
|
FROM
|
||||||
cross join lateral (
|
comment_aggregates_view cav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
u.id AS user_id,
|
||||||
coalesce(cf.id, 0) as is_subbed,
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
coalesce(cs.id, 0) as is_saved
|
coalesce(cf.id, 0) AS is_subbed,
|
||||||
from user_ u
|
coalesce(cs.id, 0) AS is_saved
|
||||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
FROM
|
||||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
) as us
|
AND cav.id = cl.comment_id
|
||||||
|
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||||
union all
|
AND cs.comment_id = cav.id
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
select
|
AND cav.community_id = cf.community_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from comment_aggregates_view cav
|
FROM
|
||||||
);
|
comment_aggregates_view cav);
|
||||||
|
|
||||||
create table comment_aggregates_fast as select * from comment_aggregates_view;
|
CREATE TABLE comment_aggregates_fast AS
|
||||||
alter table comment_aggregates_fast add primary key (id);
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
comment_aggregates_view;
|
||||||
|
|
||||||
create view comment_fast_view as
|
ALTER TABLE comment_aggregates_fast
|
||||||
select
|
ADD PRIMARY KEY (id);
|
||||||
|
|
||||||
|
CREATE VIEW comment_fast_view AS
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
us.user_id as user_id,
|
us.user_id AS user_id,
|
||||||
us.my_vote as my_vote,
|
us.my_vote AS my_vote,
|
||||||
us.is_subbed::bool as subscribed,
|
us.is_subbed::bool AS subscribed,
|
||||||
us.is_saved::bool as saved
|
us.is_saved::bool AS saved
|
||||||
from comment_aggregates_fast cav
|
FROM
|
||||||
cross join lateral (
|
comment_aggregates_fast cav
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user_id,
|
SELECT
|
||||||
coalesce(cl.score, 0) as my_vote,
|
u.id AS user_id,
|
||||||
coalesce(cf.id, 0) as is_subbed,
|
coalesce(cl.score, 0) AS my_vote,
|
||||||
coalesce(cs.id, 0) as is_saved
|
coalesce(cf.id, 0) AS is_subbed,
|
||||||
from user_ u
|
coalesce(cs.id, 0) AS is_saved
|
||||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
FROM
|
||||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
user_ u
|
||||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
) as us
|
AND cav.id = cl.comment_id
|
||||||
|
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||||
union all
|
AND cs.comment_id = cav.id
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
select
|
AND cav.community_id = cf.community_id) AS us
|
||||||
|
UNION ALL
|
||||||
|
SELECT
|
||||||
cav.*,
|
cav.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as subscribed,
|
NULL AS subscribed,
|
||||||
null as saved
|
NULL AS saved
|
||||||
from comment_aggregates_fast cav;
|
FROM
|
||||||
|
comment_aggregates_fast cav;
|
||||||
|
|
||||||
create view user_mention_view as
|
CREATE VIEW user_mention_view AS
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
c.creator_id,
|
c.creator_id,
|
||||||
c.creator_actor_id,
|
c.creator_actor_id,
|
||||||
c.creator_local,
|
c.creator_local,
|
||||||
|
@ -141,15 +171,30 @@ select
|
||||||
c.my_vote,
|
c.my_vote,
|
||||||
c.saved,
|
c.saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from user_mention um, comment_view c
|
actor_id
|
||||||
where um.comment_id = c.id;
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
user_mention um,
|
||||||
|
comment_view c
|
||||||
|
WHERE
|
||||||
|
um.comment_id = c.id;
|
||||||
|
|
||||||
create view user_mention_fast_view as
|
CREATE VIEW user_mention_fast_view AS
|
||||||
select
|
SELECT
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.creator_actor_id,
|
ac.creator_actor_id,
|
||||||
ac.creator_local,
|
ac.creator_local,
|
||||||
|
@ -174,26 +219,45 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
u.id as user_id,
|
u.id AS user_id,
|
||||||
coalesce(cl.score, 0) as my_vote,
|
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,
|
(
|
||||||
|
SELECT
|
||||||
|
cs.id::bool
|
||||||
|
FROM
|
||||||
|
comment_saved cs
|
||||||
|
WHERE
|
||||||
|
u.id = cs.user_id
|
||||||
|
AND cs.comment_id = ac.id) AS saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from user_ u
|
actor_id
|
||||||
cross join (
|
FROM
|
||||||
select
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN (
|
||||||
|
SELECT
|
||||||
ca.*
|
ca.*
|
||||||
from comment_aggregates_fast ca
|
FROM
|
||||||
) ac
|
comment_aggregates_fast ca) ac
|
||||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||||
left join user_mention um on um.comment_id = ac.id
|
AND ac.id = cl.comment_id
|
||||||
|
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||||
union all
|
UNION ALL
|
||||||
|
SELECT
|
||||||
select
|
|
||||||
ac.id,
|
ac.id,
|
||||||
um.id as user_mention_id,
|
um.id AS user_mention_id,
|
||||||
ac.creator_id,
|
ac.creator_id,
|
||||||
ac.creator_actor_id,
|
ac.creator_actor_id,
|
||||||
ac.creator_local,
|
ac.creator_local,
|
||||||
|
@ -218,37 +282,60 @@ select
|
||||||
ac.upvotes,
|
ac.upvotes,
|
||||||
ac.downvotes,
|
ac.downvotes,
|
||||||
ac.hot_rank,
|
ac.hot_rank,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as my_vote,
|
NULL AS my_vote,
|
||||||
null as saved,
|
NULL AS saved,
|
||||||
um.recipient_id,
|
um.recipient_id,
|
||||||
(select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
|
(
|
||||||
(select local from user_ u where u.id = um.recipient_id) as recipient_local
|
SELECT
|
||||||
from comment_aggregates_fast ac
|
actor_id
|
||||||
left join user_mention um on um.comment_id = ac.id
|
FROM
|
||||||
;
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_actor_id,
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
local
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
WHERE
|
||||||
|
u.id = um.recipient_id) AS recipient_local
|
||||||
|
FROM
|
||||||
|
comment_aggregates_fast ac
|
||||||
|
LEFT JOIN user_mention um ON um.comment_id = ac.id;
|
||||||
|
|
||||||
-- Do the reply_view referencing the comment_fast_view
|
-- Do the reply_view referencing the comment_fast_view
|
||||||
create view reply_fast_view as
|
CREATE VIEW reply_fast_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
|
||||||
|
WHERE
|
||||||
|
c2.creator_id != c.creator_id
|
||||||
-- Do union where post is null
|
-- Do union where post is null
|
||||||
union
|
UNION
|
||||||
select
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.creator_id as sender_id,
|
c.creator_id AS sender_id,
|
||||||
p.creator_id as recipient_id
|
p.creator_id AS recipient_id
|
||||||
from comment c, post p
|
FROM
|
||||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
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
|
||||||
|
cv.*,
|
||||||
closereply.recipient_id
|
closereply.recipient_id
|
||||||
from comment_fast_view cv, closereply
|
FROM
|
||||||
where closereply.id = cv.id
|
comment_fast_view cv,
|
||||||
;
|
closereply
|
||||||
|
WHERE
|
||||||
|
closereply.id = cv.id;
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,34 @@
|
||||||
|
ALTER TABLE community
|
||||||
|
ALTER COLUMN actor_id SET NOT NULL;
|
||||||
|
|
||||||
alter table community alter column actor_id set not null;
|
ALTER TABLE community
|
||||||
alter table community alter column actor_id set default 'http://fake.com';
|
ALTER COLUMN actor_id SET DEFAULT 'http://fake.com';
|
||||||
alter table user_ alter column actor_id set not null;
|
|
||||||
alter table user_ alter column actor_id set default 'http://fake.com';
|
|
||||||
|
|
||||||
drop function generate_unique_changeme;
|
ALTER TABLE user_
|
||||||
|
ALTER COLUMN actor_id SET NOT NULL;
|
||||||
|
|
||||||
update community
|
ALTER TABLE user_
|
||||||
set actor_id = 'http://fake.com'
|
ALTER COLUMN actor_id SET DEFAULT 'http://fake.com';
|
||||||
where actor_id like 'changeme_%';
|
|
||||||
|
|
||||||
update user_
|
DROP FUNCTION generate_unique_changeme;
|
||||||
set actor_id = 'http://fake.com'
|
|
||||||
where actor_id like 'changeme_%';
|
|
||||||
|
|
||||||
drop index idx_user_lower_actor_id;
|
UPDATE
|
||||||
create unique index idx_user_name_lower_actor_id on user_ (lower(name), lower(actor_id));
|
community
|
||||||
|
SET
|
||||||
|
actor_id = 'http://fake.com'
|
||||||
|
WHERE
|
||||||
|
actor_id LIKE 'changeme_%';
|
||||||
|
|
||||||
|
UPDATE
|
||||||
|
user_
|
||||||
|
SET
|
||||||
|
actor_id = 'http://fake.com'
|
||||||
|
WHERE
|
||||||
|
actor_id LIKE 'changeme_%';
|
||||||
|
|
||||||
|
DROP INDEX idx_user_lower_actor_id;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX idx_user_name_lower_actor_id ON user_ (lower(name), lower(actor_id));
|
||||||
|
|
||||||
|
DROP INDEX idx_community_lower_actor_id;
|
||||||
|
|
||||||
drop index idx_community_lower_actor_id;
|
|
||||||
|
|
|
@ -1,50 +1,78 @@
|
||||||
-- Following this issue : https://github.com/LemmyNet/lemmy/issues/957
|
-- Following this issue : https://github.com/LemmyNet/lemmy/issues/957
|
||||||
|
|
||||||
-- Creating a unique changeme actor_id
|
-- Creating a unique changeme actor_id
|
||||||
create or replace function generate_unique_changeme()
|
CREATE OR REPLACE FUNCTION generate_unique_changeme ()
|
||||||
returns text language sql
|
RETURNS text
|
||||||
as $$
|
LANGUAGE sql
|
||||||
select 'changeme_' || string_agg (substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil (random() * 62)::integer, 1), '')
|
AS $$
|
||||||
from generate_series(1, 20)
|
SELECT
|
||||||
|
'changeme_' || string_agg(substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil(random() * 62)::integer, 1), '')
|
||||||
|
FROM
|
||||||
|
generate_series(1, 20)
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
-- Need to delete the possible community and user dupes for ones that don't start with the fake one
|
-- Need to delete the possible community and user dupes for ones that don't start with the fake one
|
||||||
-- A few test inserts, to make sure this removes later dupes
|
-- A few test inserts, to make sure this removes later dupes
|
||||||
-- insert into community (name, title, category_id, creator_id) values ('testcom', 'another testcom', 1, 2);
|
-- insert into community (name, title, category_id, creator_id) values ('testcom', 'another testcom', 1, 2);
|
||||||
delete from community a using (
|
DELETE FROM community a USING (
|
||||||
select min(id) as id, actor_id
|
SELECT
|
||||||
from community
|
min(id) AS id,
|
||||||
group by actor_id having count(*) > 1
|
actor_id
|
||||||
) b
|
FROM
|
||||||
where a.actor_id = b.actor_id
|
community
|
||||||
and a.id <> b.id;
|
GROUP BY
|
||||||
|
actor_id
|
||||||
|
HAVING
|
||||||
|
count(*) > 1) b
|
||||||
|
WHERE
|
||||||
|
a.actor_id = b.actor_id
|
||||||
|
AND a.id <> b.id;
|
||||||
|
|
||||||
delete from user_ a using (
|
DELETE FROM user_ a USING (
|
||||||
select min(id) as id, actor_id
|
SELECT
|
||||||
from user_
|
min(id) AS id,
|
||||||
group by actor_id having count(*) > 1
|
actor_id
|
||||||
) b
|
FROM
|
||||||
where a.actor_id = b.actor_id
|
user_
|
||||||
and a.id <> b.id;
|
GROUP BY
|
||||||
|
actor_id
|
||||||
|
HAVING
|
||||||
|
count(*) > 1) b
|
||||||
|
WHERE
|
||||||
|
a.actor_id = b.actor_id
|
||||||
|
AND a.id <> b.id;
|
||||||
|
|
||||||
-- Replacing the current default on the columns, to the unique one
|
-- Replacing the current default on the columns, to the unique one
|
||||||
update community
|
UPDATE
|
||||||
set actor_id = generate_unique_changeme()
|
community
|
||||||
where actor_id = 'http://fake.com';
|
SET
|
||||||
|
actor_id = generate_unique_changeme ()
|
||||||
|
WHERE
|
||||||
|
actor_id = 'http://fake.com';
|
||||||
|
|
||||||
update user_
|
UPDATE
|
||||||
set actor_id = generate_unique_changeme()
|
user_
|
||||||
where actor_id = 'http://fake.com';
|
SET
|
||||||
|
actor_id = generate_unique_changeme ()
|
||||||
|
WHERE
|
||||||
|
actor_id = 'http://fake.com';
|
||||||
|
|
||||||
-- Add the unique indexes
|
-- Add the unique indexes
|
||||||
alter table community alter column actor_id set not null;
|
ALTER TABLE community
|
||||||
alter table community alter column actor_id set default generate_unique_changeme();
|
ALTER COLUMN actor_id SET NOT NULL;
|
||||||
|
|
||||||
alter table user_ alter column actor_id set not null;
|
ALTER TABLE community
|
||||||
alter table user_ alter column actor_id set default generate_unique_changeme();
|
ALTER COLUMN actor_id SET DEFAULT generate_unique_changeme ();
|
||||||
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
ALTER COLUMN actor_id SET NOT NULL;
|
||||||
|
|
||||||
|
ALTER TABLE user_
|
||||||
|
ALTER COLUMN actor_id SET DEFAULT generate_unique_changeme ();
|
||||||
|
|
||||||
-- Add lowercase uniqueness too
|
-- Add lowercase uniqueness too
|
||||||
drop index idx_user_name_lower_actor_id;
|
DROP INDEX idx_user_name_lower_actor_id;
|
||||||
create unique index idx_user_lower_actor_id on user_ (lower(actor_id));
|
|
||||||
|
CREATE UNIQUE INDEX idx_user_lower_actor_id ON user_ (lower(actor_id));
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX idx_community_lower_actor_id ON community (lower(actor_id));
|
||||||
|
|
||||||
create unique index idx_community_lower_actor_id on community (lower(actor_id));
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -1,11 +1,14 @@
|
||||||
-- Drop first
|
-- Drop first
|
||||||
drop view community_view;
|
DROP VIEW community_view;
|
||||||
drop view community_aggregates_view;
|
|
||||||
drop view community_fast_view;
|
|
||||||
drop table community_aggregates_fast;
|
|
||||||
|
|
||||||
create view community_aggregates_view as
|
DROP VIEW community_aggregates_view;
|
||||||
select
|
|
||||||
|
DROP VIEW community_fast_view;
|
||||||
|
|
||||||
|
DROP TABLE community_aggregates_fast;
|
||||||
|
|
||||||
|
CREATE VIEW community_aggregates_view AS
|
||||||
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.name,
|
c.name,
|
||||||
c.title,
|
c.title,
|
||||||
|
@ -22,79 +25,96 @@ select
|
||||||
c.actor_id,
|
c.actor_id,
|
||||||
c.local,
|
c.local,
|
||||||
c.last_refreshed_at,
|
c.last_refreshed_at,
|
||||||
u.actor_id as creator_actor_id,
|
u.actor_id AS creator_actor_id,
|
||||||
u.local as creator_local,
|
u.local AS creator_local,
|
||||||
u.name as creator_name,
|
u.name AS creator_name,
|
||||||
u.preferred_username as creator_preferred_username,
|
u.preferred_username AS creator_preferred_username,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
cat.name as category_name,
|
cat.name AS category_name,
|
||||||
coalesce(cf.subs, 0) as number_of_subscribers,
|
coalesce(cf.subs, 0) AS number_of_subscribers,
|
||||||
coalesce(cd.posts, 0) as number_of_posts,
|
coalesce(cd.posts, 0) AS number_of_posts,
|
||||||
coalesce(cd.comments, 0) as number_of_comments,
|
coalesce(cd.comments, 0) AS number_of_comments,
|
||||||
hot_rank(cf.subs, c.published) as hot_rank
|
hot_rank (cf.subs, c.published) AS hot_rank
|
||||||
from community c
|
FROM
|
||||||
left join user_ u on c.creator_id = u.id
|
community c
|
||||||
left join category cat on c.category_id = cat.id
|
LEFT JOIN user_ u ON c.creator_id = u.id
|
||||||
left join (
|
LEFT JOIN category cat ON c.category_id = cat.id
|
||||||
select
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
p.community_id,
|
p.community_id,
|
||||||
count(distinct p.id) as posts,
|
count(DISTINCT p.id) AS posts,
|
||||||
count(distinct ct.id) as comments
|
count(DISTINCT ct.id) AS comments
|
||||||
from post p
|
FROM
|
||||||
join comment ct on p.id = ct.post_id
|
post p
|
||||||
group by p.community_id
|
JOIN comment ct ON p.id = ct.post_id
|
||||||
) cd on cd.community_id = c.id
|
GROUP BY
|
||||||
left join (
|
p.community_id) cd ON cd.community_id = c.id
|
||||||
select
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
community_id,
|
community_id,
|
||||||
count(*) as subs
|
count(*) AS subs
|
||||||
from community_follower
|
FROM
|
||||||
group by community_id
|
community_follower
|
||||||
) cf on cf.community_id = c.id;
|
GROUP BY
|
||||||
|
community_id) cf ON cf.community_id = c.id;
|
||||||
|
|
||||||
create view community_view as
|
CREATE VIEW community_view AS
|
||||||
select
|
SELECT
|
||||||
cv.*,
|
cv.*,
|
||||||
us.user as user_id,
|
us.user AS user_id,
|
||||||
us.is_subbed::bool as subscribed
|
us.is_subbed::bool AS subscribed
|
||||||
from community_aggregates_view cv
|
FROM
|
||||||
cross join lateral (
|
community_aggregates_view cv
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user,
|
SELECT
|
||||||
coalesce(cf.community_id, 0) as is_subbed
|
u.id AS user,
|
||||||
from user_ u
|
coalesce(cf.community_id, 0) AS is_subbed
|
||||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = cv.id
|
FROM
|
||||||
) as us
|
user_ u
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
union all
|
AND cf.community_id = cv.id) AS us
|
||||||
|
UNION ALL
|
||||||
select
|
SELECT
|
||||||
cv.*,
|
cv.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from community_aggregates_view cv;
|
FROM
|
||||||
|
community_aggregates_view cv;
|
||||||
|
|
||||||
-- The community fast table
|
-- The community fast table
|
||||||
|
CREATE TABLE community_aggregates_fast AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
community_aggregates_view;
|
||||||
|
|
||||||
create table community_aggregates_fast as select * from community_aggregates_view;
|
ALTER TABLE community_aggregates_fast
|
||||||
alter table community_aggregates_fast add primary key (id);
|
ADD PRIMARY KEY (id);
|
||||||
|
|
||||||
create view community_fast_view as
|
CREATE VIEW community_fast_view AS
|
||||||
select
|
SELECT
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join (
|
cf.id::boolean
|
||||||
select
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND ac.id = cf.community_id) AS subscribed
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN (
|
||||||
|
SELECT
|
||||||
ca.*
|
ca.*
|
||||||
from community_aggregates_fast ca
|
FROM
|
||||||
) ac
|
community_aggregates_fast ca) ac
|
||||||
|
UNION ALL
|
||||||
union all
|
SELECT
|
||||||
|
|
||||||
select
|
|
||||||
caf.*,
|
caf.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from community_aggregates_fast caf;
|
FROM
|
||||||
|
community_aggregates_fast caf;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
-- Drop first
|
-- Drop first
|
||||||
drop view community_view;
|
DROP VIEW community_view;
|
||||||
drop view community_aggregates_view;
|
|
||||||
drop view community_fast_view;
|
|
||||||
drop table community_aggregates_fast;
|
|
||||||
|
|
||||||
create view community_aggregates_view as
|
DROP VIEW community_aggregates_view;
|
||||||
select
|
|
||||||
|
DROP VIEW community_fast_view;
|
||||||
|
|
||||||
|
DROP TABLE community_aggregates_fast;
|
||||||
|
|
||||||
|
CREATE VIEW community_aggregates_view AS
|
||||||
|
SELECT
|
||||||
c.id,
|
c.id,
|
||||||
c.name,
|
c.name,
|
||||||
c.title,
|
c.title,
|
||||||
|
@ -22,79 +25,96 @@ select
|
||||||
c.actor_id,
|
c.actor_id,
|
||||||
c.local,
|
c.local,
|
||||||
c.last_refreshed_at,
|
c.last_refreshed_at,
|
||||||
u.actor_id as creator_actor_id,
|
u.actor_id AS creator_actor_id,
|
||||||
u.local as creator_local,
|
u.local AS creator_local,
|
||||||
u.name as creator_name,
|
u.name AS creator_name,
|
||||||
u.preferred_username as creator_preferred_username,
|
u.preferred_username AS creator_preferred_username,
|
||||||
u.avatar as creator_avatar,
|
u.avatar AS creator_avatar,
|
||||||
cat.name as category_name,
|
cat.name AS category_name,
|
||||||
coalesce(cf.subs, 0) as number_of_subscribers,
|
coalesce(cf.subs, 0) AS number_of_subscribers,
|
||||||
coalesce(cd.posts, 0) as number_of_posts,
|
coalesce(cd.posts, 0) AS number_of_posts,
|
||||||
coalesce(cd.comments, 0) as number_of_comments,
|
coalesce(cd.comments, 0) AS number_of_comments,
|
||||||
hot_rank(cf.subs, c.published) as hot_rank
|
hot_rank (cf.subs, c.published) AS hot_rank
|
||||||
from community c
|
FROM
|
||||||
left join user_ u on c.creator_id = u.id
|
community c
|
||||||
left join category cat on c.category_id = cat.id
|
LEFT JOIN user_ u ON c.creator_id = u.id
|
||||||
left join (
|
LEFT JOIN category cat ON c.category_id = cat.id
|
||||||
select
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
p.community_id,
|
p.community_id,
|
||||||
count(distinct p.id) as posts,
|
count(DISTINCT p.id) AS posts,
|
||||||
count(distinct ct.id) as comments
|
count(DISTINCT ct.id) AS comments
|
||||||
from post p
|
FROM
|
||||||
left join comment ct on p.id = ct.post_id
|
post p
|
||||||
group by p.community_id
|
LEFT JOIN comment ct ON p.id = ct.post_id
|
||||||
) cd on cd.community_id = c.id
|
GROUP BY
|
||||||
left join (
|
p.community_id) cd ON cd.community_id = c.id
|
||||||
select
|
LEFT JOIN (
|
||||||
|
SELECT
|
||||||
community_id,
|
community_id,
|
||||||
count(*) as subs
|
count(*) AS subs
|
||||||
from community_follower
|
FROM
|
||||||
group by community_id
|
community_follower
|
||||||
) cf on cf.community_id = c.id;
|
GROUP BY
|
||||||
|
community_id) cf ON cf.community_id = c.id;
|
||||||
|
|
||||||
create view community_view as
|
CREATE VIEW community_view AS
|
||||||
select
|
SELECT
|
||||||
cv.*,
|
cv.*,
|
||||||
us.user as user_id,
|
us.user AS user_id,
|
||||||
us.is_subbed::bool as subscribed
|
us.is_subbed::bool AS subscribed
|
||||||
from community_aggregates_view cv
|
FROM
|
||||||
cross join lateral (
|
community_aggregates_view cv
|
||||||
select
|
CROSS JOIN LATERAL (
|
||||||
u.id as user,
|
SELECT
|
||||||
coalesce(cf.community_id, 0) as is_subbed
|
u.id AS user,
|
||||||
from user_ u
|
coalesce(cf.community_id, 0) AS is_subbed
|
||||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = cv.id
|
FROM
|
||||||
) as us
|
user_ u
|
||||||
|
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||||
union all
|
AND cf.community_id = cv.id) AS us
|
||||||
|
UNION ALL
|
||||||
select
|
SELECT
|
||||||
cv.*,
|
cv.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from community_aggregates_view cv;
|
FROM
|
||||||
|
community_aggregates_view cv;
|
||||||
|
|
||||||
-- The community fast table
|
-- The community fast table
|
||||||
|
CREATE TABLE community_aggregates_fast AS
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
community_aggregates_view;
|
||||||
|
|
||||||
create table community_aggregates_fast as select * from community_aggregates_view;
|
ALTER TABLE community_aggregates_fast
|
||||||
alter table community_aggregates_fast add primary key (id);
|
ADD PRIMARY KEY (id);
|
||||||
|
|
||||||
create view community_fast_view as
|
CREATE VIEW community_fast_view AS
|
||||||
select
|
SELECT
|
||||||
ac.*,
|
ac.*,
|
||||||
u.id as user_id,
|
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
|
SELECT
|
||||||
cross join (
|
cf.id::boolean
|
||||||
select
|
FROM
|
||||||
|
community_follower cf
|
||||||
|
WHERE
|
||||||
|
u.id = cf.user_id
|
||||||
|
AND ac.id = cf.community_id) AS subscribed
|
||||||
|
FROM
|
||||||
|
user_ u
|
||||||
|
CROSS JOIN (
|
||||||
|
SELECT
|
||||||
ca.*
|
ca.*
|
||||||
from community_aggregates_fast ca
|
FROM
|
||||||
) ac
|
community_aggregates_fast ca) ac
|
||||||
|
UNION ALL
|
||||||
union all
|
SELECT
|
||||||
|
|
||||||
select
|
|
||||||
caf.*,
|
caf.*,
|
||||||
null as user_id,
|
NULL AS user_id,
|
||||||
null as subscribed
|
NULL AS subscribed
|
||||||
from community_aggregates_fast caf;
|
FROM
|
||||||
|
community_aggregates_fast caf;
|
||||||
|
|
||||||
|
|
|
@ -1,27 +1,55 @@
|
||||||
-- Drop the uniques
|
-- Drop the uniques
|
||||||
alter table private_message drop constraint idx_private_message_ap_id;
|
ALTER TABLE private_message
|
||||||
alter table post drop constraint idx_post_ap_id;
|
DROP CONSTRAINT idx_private_message_ap_id;
|
||||||
alter table comment drop constraint idx_comment_ap_id;
|
|
||||||
alter table user_ drop constraint idx_user_actor_id;
|
|
||||||
alter table community drop constraint idx_community_actor_id;
|
|
||||||
|
|
||||||
alter table private_message alter column ap_id set not null;
|
ALTER TABLE post
|
||||||
alter table private_message alter column ap_id set default 'http://fake.com';
|
DROP CONSTRAINT idx_post_ap_id;
|
||||||
|
|
||||||
alter table post alter column ap_id set not null;
|
ALTER TABLE comment
|
||||||
alter table post alter column ap_id set default 'http://fake.com';
|
DROP CONSTRAINT idx_comment_ap_id;
|
||||||
|
|
||||||
alter table comment alter column ap_id set not null;
|
ALTER TABLE user_
|
||||||
alter table comment alter column ap_id set default 'http://fake.com';
|
DROP CONSTRAINT idx_user_actor_id;
|
||||||
|
|
||||||
update private_message
|
ALTER TABLE community
|
||||||
set ap_id = 'http://fake.com'
|
DROP CONSTRAINT idx_community_actor_id;
|
||||||
where ap_id like 'changeme_%';
|
|
||||||
|
|
||||||
update post
|
ALTER TABLE private_message
|
||||||
set ap_id = 'http://fake.com'
|
ALTER COLUMN ap_id SET NOT NULL;
|
||||||
where ap_id like 'changeme_%';
|
|
||||||
|
ALTER TABLE private_message
|
||||||
|
ALTER COLUMN ap_id SET DEFAULT 'http://fake.com';
|
||||||
|
|
||||||
|
ALTER TABLE post
|
||||||
|
ALTER COLUMN ap_id SET NOT NULL;
|
||||||
|
|
||||||
|
ALTER TABLE post
|
||||||
|
ALTER COLUMN ap_id SET DEFAULT 'http://fake.com';
|
||||||
|
|
||||||
|
ALTER TABLE comment
|
||||||
|
ALTER COLUMN ap_id SET NOT NULL;
|
||||||
|
|
||||||
|
ALTER TABLE comment
|
||||||
|
ALTER COLUMN ap_id SET DEFAULT 'http://fake.com';
|
||||||
|
|
||||||
|
UPDATE
|
||||||
|
private_message
|
||||||
|
SET
|
||||||
|
ap_id = 'http://fake.com'
|
||||||
|
WHERE
|
||||||
|
ap_id LIKE 'changeme_%';
|
||||||
|
|
||||||
|
UPDATE
|
||||||
|
post
|
||||||
|
SET
|
||||||
|
ap_id = 'http://fake.com'
|
||||||
|
WHERE
|
||||||
|
ap_id LIKE 'changeme_%';
|
||||||
|
|
||||||
|
UPDATE
|
||||||
|
comment
|
||||||
|
SET
|
||||||
|
ap_id = 'http://fake.com'
|
||||||
|
WHERE
|
||||||
|
ap_id LIKE 'changeme_%';
|
||||||
|
|
||||||
update comment
|
|
||||||
set ap_id = 'http://fake.com'
|
|
||||||
where ap_id like 'changeme_%';
|
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue