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
|
||||
- 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)
|
||||
check_api_common_default_features:
|
||||
image: *muslrust_image
|
||||
|
@ -145,7 +157,7 @@ pipeline:
|
|||
environment:
|
||||
CARGO_HOME: .cargo
|
||||
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
|
||||
- cargo clippy --workspace --tests --all-targets --features console --
|
||||
-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
|
||||
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||
-- 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_set_updated_at ();
|
||||
|
||||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
|
||||
DROP FUNCTION IF EXISTS diesel_set_updated_at();
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
-- This file was automatically created by Diesel to setup helper functions
|
||||
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||
-- changes will be added to existing projects as new migrations.
|
||||
|
||||
|
||||
|
||||
|
||||
-- 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
|
||||
-- in the modified columns)
|
||||
|
@ -16,21 +12,25 @@
|
|||
--
|
||||
-- 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
|
||||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
||||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
|
||||
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
|
||||
IF (
|
||||
NEW IS DISTINCT FROM OLD AND
|
||||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
|
||||
) THEN
|
||||
NEW.updated_at := current_timestamp;
|
||||
IF (NEW IS DISTINCT FROM OLD AND NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at) THEN
|
||||
NEW.updated_at := CURRENT_TIMESTAMP;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
$$
|
||||
LANGUAGE plpgsql;
|
||||
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
drop table user_ban;
|
||||
drop table user_;
|
||||
DROP TABLE user_ban;
|
||||
|
||||
DROP TABLE user_;
|
||||
|
||||
|
|
|
@ -1,23 +1,25 @@
|
|||
create table user_ (
|
||||
id serial primary key,
|
||||
name varchar(20) not null,
|
||||
fedi_name varchar(40) not null,
|
||||
preferred_username varchar(20),
|
||||
password_encrypted text not null,
|
||||
email text unique,
|
||||
icon bytea,
|
||||
admin boolean default false not null,
|
||||
banned boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp,
|
||||
unique(name, fedi_name)
|
||||
CREATE TABLE user_ (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(20) NOT NULL,
|
||||
fedi_name varchar(40) NOT NULL,
|
||||
preferred_username varchar(20),
|
||||
password_encrypted text NOT NULL,
|
||||
email text UNIQUE,
|
||||
icon bytea,
|
||||
admin boolean DEFAULT FALSE NOT NULL,
|
||||
banned boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp,
|
||||
UNIQUE (name, fedi_name)
|
||||
);
|
||||
|
||||
create table user_ban (
|
||||
id serial primary key,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
unique (user_id)
|
||||
CREATE TABLE user_ban (
|
||||
id serial PRIMARY KEY,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
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 community_user_ban;;
|
||||
drop table community_moderator;
|
||||
drop table community_follower;
|
||||
drop table community;
|
||||
drop table category;
|
||||
DROP TABLE site;
|
||||
|
||||
DROP TABLE community_user_ban;
|
||||
|
||||
;
|
||||
|
||||
DROP TABLE community_moderator;
|
||||
|
||||
DROP TABLE community_follower;
|
||||
|
||||
DROP TABLE community;
|
||||
|
||||
DROP TABLE category;
|
||||
|
||||
|
|
|
@ -1,79 +1,81 @@
|
|||
create table category (
|
||||
id serial primary key,
|
||||
name varchar(100) not null unique
|
||||
CREATE TABLE category (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(100) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
insert into category (name) values
|
||||
('Discussion'),
|
||||
('Humor/Memes'),
|
||||
('Gaming'),
|
||||
('Movies'),
|
||||
('TV'),
|
||||
('Music'),
|
||||
('Literature'),
|
||||
('Comics'),
|
||||
('Photography'),
|
||||
('Art'),
|
||||
('Learning'),
|
||||
('DIY'),
|
||||
('Lifestyle'),
|
||||
('News'),
|
||||
('Politics'),
|
||||
('Society'),
|
||||
('Gender/Identity/Sexuality'),
|
||||
('Race/Colonisation'),
|
||||
('Religion'),
|
||||
('Science/Technology'),
|
||||
('Programming/Software'),
|
||||
('Health/Sports/Fitness'),
|
||||
('Porn'),
|
||||
('Places'),
|
||||
('Meta'),
|
||||
('Other');
|
||||
INSERT INTO category (name)
|
||||
VALUES ('Discussion'),
|
||||
('Humor/Memes'),
|
||||
('Gaming'),
|
||||
('Movies'),
|
||||
('TV'),
|
||||
('Music'),
|
||||
('Literature'),
|
||||
('Comics'),
|
||||
('Photography'),
|
||||
('Art'),
|
||||
('Learning'),
|
||||
('DIY'),
|
||||
('Lifestyle'),
|
||||
('News'),
|
||||
('Politics'),
|
||||
('Society'),
|
||||
('Gender/Identity/Sexuality'),
|
||||
('Race/Colonisation'),
|
||||
('Religion'),
|
||||
('Science/Technology'),
|
||||
('Programming/Software'),
|
||||
('Health/Sports/Fitness'),
|
||||
('Porn'),
|
||||
('Places'),
|
||||
('Meta'),
|
||||
('Other');
|
||||
|
||||
create table community (
|
||||
id serial primary key,
|
||||
name varchar(20) not null unique,
|
||||
title varchar(100) not null,
|
||||
description text,
|
||||
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,
|
||||
removed boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE community (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(20) NOT NULL UNIQUE,
|
||||
title varchar(100) NOT NULL,
|
||||
description text,
|
||||
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,
|
||||
removed boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
create table community_moderator (
|
||||
id serial primary key,
|
||||
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,
|
||||
published timestamp not null default now(),
|
||||
unique (community_id, user_id)
|
||||
CREATE TABLE community_moderator (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (community_id, user_id)
|
||||
);
|
||||
|
||||
create table community_follower (
|
||||
id serial primary key,
|
||||
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,
|
||||
published timestamp not null default now(),
|
||||
unique (community_id, user_id)
|
||||
CREATE TABLE community_follower (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (community_id, user_id)
|
||||
);
|
||||
|
||||
create table community_user_ban (
|
||||
id serial primary key,
|
||||
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,
|
||||
published timestamp not null default now(),
|
||||
unique (community_id, user_id)
|
||||
CREATE TABLE community_user_ban (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
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 (
|
||||
id serial primary key,
|
||||
name varchar(20) not null unique,
|
||||
description text,
|
||||
creator_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE site (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(20) NOT NULL UNIQUE,
|
||||
description text,
|
||||
creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
drop table post_read;
|
||||
drop table post_saved;
|
||||
drop table post_like;
|
||||
drop table post;
|
||||
DROP TABLE post_read;
|
||||
|
||||
DROP TABLE post_saved;
|
||||
|
||||
DROP TABLE post_like;
|
||||
|
||||
DROP TABLE post;
|
||||
|
||||
|
|
|
@ -1,37 +1,38 @@
|
|||
create table post (
|
||||
id serial primary key,
|
||||
name varchar(100) not null,
|
||||
url text, -- These are both optional, a post can just have a title
|
||||
body text,
|
||||
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,
|
||||
removed boolean default false not null,
|
||||
locked boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE post (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(100) NOT NULL,
|
||||
url text, -- These are both optional, a post can just have a title
|
||||
body text,
|
||||
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,
|
||||
removed boolean DEFAULT FALSE NOT NULL,
|
||||
locked boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
create table post_like (
|
||||
id serial primary key,
|
||||
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,
|
||||
score smallint not null, -- -1, or 1 for dislike, like, no row for no opinion
|
||||
published timestamp not null default now(),
|
||||
unique(post_id, user_id)
|
||||
CREATE TABLE post_like (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
score smallint NOT NULL, -- -1, or 1 for dislike, like, no row for no opinion
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (post_id, user_id)
|
||||
);
|
||||
|
||||
create table post_saved (
|
||||
id serial primary key,
|
||||
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,
|
||||
published timestamp not null default now(),
|
||||
unique(post_id, user_id)
|
||||
CREATE TABLE post_saved (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (post_id, user_id)
|
||||
);
|
||||
|
||||
create table post_read (
|
||||
id serial primary key,
|
||||
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,
|
||||
published timestamp not null default now(),
|
||||
unique(post_id, user_id)
|
||||
CREATE TABLE post_read (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (post_id, user_id)
|
||||
);
|
||||
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
drop table comment_saved;
|
||||
drop table comment_like;
|
||||
drop table comment;
|
||||
DROP TABLE comment_saved;
|
||||
|
||||
DROP TABLE comment_like;
|
||||
|
||||
DROP TABLE comment;
|
||||
|
||||
|
|
|
@ -1,29 +1,30 @@
|
|||
create table comment (
|
||||
id serial primary key,
|
||||
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,
|
||||
parent_id int references comment on update cascade on delete cascade,
|
||||
content text not null,
|
||||
removed boolean default false not null,
|
||||
read boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE comment (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
parent_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE,
|
||||
content text NOT NULL,
|
||||
removed boolean DEFAULT FALSE NOT NULL,
|
||||
read boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
create table comment_like (
|
||||
id serial primary key,
|
||||
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,
|
||||
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
|
||||
published timestamp not null default now(),
|
||||
unique(comment_id, user_id)
|
||||
CREATE TABLE comment_like (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
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
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (comment_id, user_id)
|
||||
);
|
||||
|
||||
create table comment_saved (
|
||||
id serial primary key,
|
||||
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,
|
||||
published timestamp not null default now(),
|
||||
unique(comment_id, user_id)
|
||||
CREATE TABLE comment_saved (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (comment_id, user_id)
|
||||
);
|
||||
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
drop view post_view;
|
||||
drop function hot_rank;
|
||||
DROP VIEW post_view;
|
||||
|
||||
DROP FUNCTION hot_rank;
|
||||
|
||||
|
|
|
@ -1,51 +1,107 @@
|
|||
-- Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
|
||||
create or replace function hot_rank(
|
||||
score numeric,
|
||||
published timestamp without time zone)
|
||||
returns integer as $$
|
||||
begin
|
||||
-- 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;
|
||||
end; $$
|
||||
CREATE OR REPLACE FUNCTION hot_rank (score numeric, published timestamp without time zone)
|
||||
RETURNS integer
|
||||
AS $$
|
||||
BEGIN
|
||||
-- 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;
|
||||
END;
|
||||
$$
|
||||
LANGUAGE plpgsql;
|
||||
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id
|
||||
)
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
drop view community_view;
|
||||
drop view community_moderator_view;
|
||||
drop view community_follower_view;
|
||||
drop view community_user_ban_view;
|
||||
drop view site_view;
|
||||
DROP VIEW community_view;
|
||||
|
||||
DROP VIEW community_moderator_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
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
||||
from community c
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
CREATE VIEW community_moderator_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_moderator cm;
|
||||
|
||||
union all
|
||||
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;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
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 community_moderator_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_moderator 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 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
|
||||
from site s;
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
drop view reply_view;
|
||||
drop view comment_view;
|
||||
DROP VIEW reply_view;
|
||||
|
||||
DROP VIEW comment_view;
|
||||
|
||||
|
|
|
@ -1,60 +1,114 @@
|
|||
create view comment_view as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
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,
|
||||
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 VIEW comment_view AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
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,
|
||||
coalesce(sum(cl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN cl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN cl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes
|
||||
FROM
|
||||
comment c
|
||||
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||
GROUP BY
|
||||
c.id
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
||||
from user_ u
|
||||
cross join all_comment ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
|
|
@ -1,8 +1,16 @@
|
|||
drop table mod_remove_post;
|
||||
drop table mod_lock_post;
|
||||
drop table mod_remove_comment;
|
||||
drop table mod_remove_community;
|
||||
drop table mod_ban;
|
||||
drop table mod_ban_from_community;
|
||||
drop table mod_add;
|
||||
drop table mod_add_community;
|
||||
DROP TABLE mod_remove_post;
|
||||
|
||||
DROP TABLE mod_lock_post;
|
||||
|
||||
DROP TABLE mod_remove_comment;
|
||||
|
||||
DROP TABLE mod_remove_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 (
|
||||
id serial primary key,
|
||||
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,
|
||||
reason text,
|
||||
removed boolean default true,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_remove_post (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
reason text,
|
||||
removed boolean DEFAULT TRUE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_lock_post (
|
||||
id serial primary key,
|
||||
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,
|
||||
locked boolean default true,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_lock_post (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
locked boolean DEFAULT TRUE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_remove_comment (
|
||||
id serial primary key,
|
||||
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,
|
||||
reason text,
|
||||
removed boolean default true,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_remove_comment (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
reason text,
|
||||
removed boolean DEFAULT TRUE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_remove_community (
|
||||
id serial primary key,
|
||||
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,
|
||||
reason text,
|
||||
removed boolean default true,
|
||||
expires timestamp,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_remove_community (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
reason text,
|
||||
removed boolean DEFAULT TRUE,
|
||||
expires timestamp,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- TODO make sure you can't ban other mods
|
||||
create table mod_ban_from_community (
|
||||
id serial primary key,
|
||||
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,
|
||||
community_id int references community on update cascade on delete cascade not null,
|
||||
reason text,
|
||||
banned boolean default true,
|
||||
expires timestamp,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_ban_from_community (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
reason text,
|
||||
banned boolean DEFAULT TRUE,
|
||||
expires timestamp,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_ban (
|
||||
id serial primary key,
|
||||
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,
|
||||
reason text,
|
||||
banned boolean default true,
|
||||
expires timestamp,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_ban (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
reason text,
|
||||
banned boolean DEFAULT TRUE,
|
||||
expires timestamp,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_add_community (
|
||||
id serial primary key,
|
||||
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,
|
||||
community_id int references community on update cascade on delete cascade not null,
|
||||
removed boolean default false,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_add_community (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
removed boolean DEFAULT FALSE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- When removed is false that means kicked
|
||||
create table mod_add (
|
||||
id serial primary key,
|
||||
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,
|
||||
removed boolean default false,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_add (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
removed boolean DEFAULT FALSE,
|
||||
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
|
||||
select id,
|
||||
name,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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 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 VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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
|
||||
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_lock_post_view;
|
||||
drop view mod_remove_comment_view;
|
||||
drop view mod_remove_community_view;
|
||||
drop view mod_ban_from_community_view;
|
||||
drop view mod_ban_view;
|
||||
drop view mod_add_community_view;
|
||||
drop view mod_add_view;
|
||||
DROP VIEW mod_remove_post_view;
|
||||
|
||||
DROP VIEW mod_lock_post_view;
|
||||
|
||||
DROP VIEW mod_remove_comment_view;
|
||||
|
||||
DROP VIEW mod_remove_community_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
|
||||
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
|
||||
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_lock_post_view as
|
||||
select mlp.*,
|
||||
(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from post p where mlp.post_id = p.id) as post_name,
|
||||
(select 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
|
||||
SELECT
|
||||
mlp.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mlp.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
mlp.post_id = p.id) AS post_name,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
select mrc.*,
|
||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
||||
(select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
|
||||
(select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
|
||||
(select content from comment c where mrc.comment_id = c.id) as comment_content,
|
||||
(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
|
||||
(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
|
||||
(select co.id from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_id,
|
||||
(select co.name from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_name
|
||||
from mod_remove_comment mrc;
|
||||
CREATE VIEW mod_remove_comment_view AS
|
||||
SELECT
|
||||
mrc.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mrc.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id) AS comment_user_id,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND u.id = c.creator_id) AS comment_user_name,
|
||||
(
|
||||
SELECT
|
||||
content
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id) AS comment_content,
|
||||
(
|
||||
SELECT
|
||||
p.id
|
||||
FROM
|
||||
post p,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id) AS post_id,
|
||||
(
|
||||
SELECT
|
||||
p.name
|
||||
FROM
|
||||
post p,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id) AS post_name,
|
||||
(
|
||||
SELECT
|
||||
co.id
|
||||
FROM
|
||||
comment c,
|
||||
post p,
|
||||
community co
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id
|
||||
AND p.community_id = co.id) AS community_id,
|
||||
(
|
||||
SELECT
|
||||
co.name
|
||||
FROM
|
||||
comment c,
|
||||
post p,
|
||||
community co
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id
|
||||
AND p.community_id = co.id) AS community_name
|
||||
FROM
|
||||
mod_remove_comment mrc;
|
||||
|
||||
create view mod_remove_community_view as
|
||||
select mrc.*,
|
||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
||||
(select c.name from community c where mrc.community_id = c.id) as community_name
|
||||
from mod_remove_community mrc;
|
||||
CREATE VIEW mod_remove_community_view AS
|
||||
SELECT
|
||||
mrc.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mrc.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
c.name
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
mrc.community_id = c.id) AS community_name
|
||||
FROM
|
||||
mod_remove_community mrc;
|
||||
|
||||
create view mod_ban_from_community_view as
|
||||
select mb.*,
|
||||
(select name from user_ u where mb.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from user_ u where mb.other_user_id = u.id) as other_user_name,
|
||||
(select name from community c where mb.community_id = c.id) as community_name
|
||||
from mod_ban_from_community mb;
|
||||
CREATE VIEW mod_ban_from_community_view AS
|
||||
SELECT
|
||||
mb.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mb.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mb.other_user_id = u.id) AS other_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
mb.community_id = c.id) AS community_name
|
||||
FROM
|
||||
mod_ban_from_community mb;
|
||||
|
||||
create view mod_ban_view as
|
||||
select mb.*,
|
||||
(select name from user_ u where mb.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from user_ u where mb.other_user_id = u.id) as other_user_name
|
||||
from mod_ban mb;
|
||||
CREATE VIEW mod_ban_view AS
|
||||
SELECT
|
||||
mb.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mb.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mb.other_user_id = u.id) AS other_user_name
|
||||
FROM
|
||||
mod_ban mb;
|
||||
|
||||
create view mod_add_community_view as
|
||||
select ma.*,
|
||||
(select name from user_ u where ma.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from user_ u where ma.other_user_id = u.id) as other_user_name,
|
||||
(select name from community c where ma.community_id = c.id) as community_name
|
||||
from mod_add_community ma;
|
||||
CREATE VIEW mod_add_community_view AS
|
||||
SELECT
|
||||
ma.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
ma.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
ma.other_user_id = u.id) AS other_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
ma.community_id = c.id) AS community_name
|
||||
FROM
|
||||
mod_add_community ma;
|
||||
|
||||
CREATE VIEW mod_add_view AS
|
||||
SELECT
|
||||
ma.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
ma.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
ma.other_user_id = u.id) AS other_user_name
|
||||
FROM
|
||||
mod_add ma;
|
||||
|
||||
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 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;
|
||||
DROP VIEW reply_view;
|
||||
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
||||
from community c
|
||||
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
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
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.*,
|
||||
(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.*,
|
||||
(select community_id from post p where p.id = c.post_id),
|
||||
(select u.banned from user_ u where c.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
||||
coalesce(sum(cl.score), 0) as score,
|
||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
||||
from comment c
|
||||
left join comment_like cl on c.id = cl.comment_id
|
||||
group by c.id
|
||||
)
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
||||
from user_ u
|
||||
cross join all_comment ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
SELECT
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE OR REPLACE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.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.*,
|
||||
(
|
||||
SELECT
|
||||
community_id
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.id = c.post_id),
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb,
|
||||
post p
|
||||
WHERE
|
||||
c.creator_id = cb.user_id
|
||||
AND p.id = c.post_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
c.creator_id = user_.id) AS creator_name,
|
||||
coalesce(sum(cl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN cl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN cl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes
|
||||
FROM
|
||||
comment c
|
||||
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||
GROUP BY
|
||||
c.id
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
|
|
@ -1,141 +1,301 @@
|
|||
alter table community add column deleted boolean default false not null;
|
||||
alter table post add column deleted boolean default false not null;
|
||||
alter table comment add column deleted boolean default false not null;
|
||||
ALTER TABLE community
|
||||
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
|
||||
drop view community_view;
|
||||
DROP VIEW community_view;
|
||||
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
||||
from community c
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
|
||||
|
||||
drop view post_view;
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select 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.*,
|
||||
(select community_id from post p where p.id = c.post_id),
|
||||
(select u.banned from user_ u where c.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
||||
coalesce(sum(cl.score), 0) as score,
|
||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
||||
from comment c
|
||||
left join comment_like cl on c.id = cl.comment_id
|
||||
group by c.id
|
||||
)
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
||||
from user_ u
|
||||
cross join all_comment ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
SELECT
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
DROP VIEW post_view;
|
||||
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
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 cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.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.*,
|
||||
(
|
||||
SELECT
|
||||
community_id
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.id = c.post_id),
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb,
|
||||
post p
|
||||
WHERE
|
||||
c.creator_id = cb.user_id
|
||||
AND p.id = c.post_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
c.creator_id = user_.id) AS creator_name,
|
||||
coalesce(sum(cl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN cl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN cl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes
|
||||
FROM
|
||||
comment c
|
||||
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||
GROUP BY
|
||||
c.id
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
|
|
@ -1,28 +1,68 @@
|
|||
drop view community_view;
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
||||
from community c
|
||||
DROP VIEW community_view;
|
||||
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
|
|
|
@ -1,29 +1,74 @@
|
|||
drop view community_view;
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
||||
from community c
|
||||
DROP VIEW community_view;
|
||||
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments,
|
||||
hot_rank ((
|
||||
SELECT
|
||||
count(*)
|
||||
FROM community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id), c.published) AS hot_rank
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
|
|
|
@ -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 post_view;
|
||||
alter table community drop column nsfw;
|
||||
alter table post drop column nsfw;
|
||||
alter table user_ drop column show_nsfw;
|
||||
DROP VIEW community_view;
|
||||
|
||||
DROP VIEW post_view;
|
||||
|
||||
ALTER TABLE community
|
||||
DROP COLUMN nsfw;
|
||||
|
||||
ALTER TABLE post
|
||||
DROP COLUMN nsfw;
|
||||
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN show_nsfw;
|
||||
|
||||
-- the views
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
||||
from community c
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments,
|
||||
hot_rank ((
|
||||
SELECT
|
||||
count(*)
|
||||
FROM community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id), c.published) AS hot_rank
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
-- Post view
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select 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
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id
|
||||
)
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
|
|
@ -1,79 +1,197 @@
|
|||
alter table community add column 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;
|
||||
ALTER TABLE community
|
||||
ADD COLUMN 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
|
||||
drop view community_view;
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
||||
from community c
|
||||
DROP VIEW community_view;
|
||||
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments,
|
||||
hot_rank ((
|
||||
SELECT
|
||||
count(*)
|
||||
FROM community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id), c.published) AS hot_rank
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
|
||||
-- Post view
|
||||
drop view post_view;
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select 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
|
||||
DROP VIEW post_view;
|
||||
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
|
|
@ -1,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
|
||||
drop view post_view;
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select 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
|
||||
DROP VIEW post_view;
|
||||
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
|
|
@ -1,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
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select 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 VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
|
|
@ -1,50 +1,134 @@
|
|||
drop view post_view;
|
||||
drop view mod_sticky_post_view;
|
||||
alter table post drop column stickied;
|
||||
DROP VIEW post_view;
|
||||
|
||||
drop table mod_sticky_post;
|
||||
DROP VIEW mod_sticky_post_view;
|
||||
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select 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
|
||||
ALTER TABLE post
|
||||
DROP COLUMN stickied;
|
||||
|
||||
DROP TABLE mod_sticky_post;
|
||||
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
|
|
@ -1,67 +1,180 @@
|
|||
-- 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
|
||||
create table mod_sticky_post (
|
||||
id serial primary key,
|
||||
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,
|
||||
stickied boolean default true,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_sticky_post (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
stickied boolean DEFAULT TRUE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Add mod view
|
||||
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;
|
||||
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;
|
||||
|
||||
-- Recreate the view
|
||||
drop view post_view;
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select 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
|
||||
DROP VIEW post_view;
|
||||
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
|
|
@ -1 +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 table user_mention;
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
DROP TABLE user_mention;
|
||||
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
create table user_mention (
|
||||
id serial primary key,
|
||||
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,
|
||||
read boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
unique(recipient_id, comment_id)
|
||||
CREATE TABLE user_mention (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
read boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (recipient_id, comment_id)
|
||||
);
|
||||
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.post_id,
|
||||
c.parent_id,
|
||||
|
@ -31,5 +31,9 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
um.recipient_id
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.id;
|
||||
FROM
|
||||
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_ drop column default_listing_type;
|
||||
ALTER TABLE user_
|
||||
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_ add column default_listing_type smallint default 1 not null;
|
||||
ALTER TABLE user_
|
||||
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 (
|
||||
id serial primary key,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
token_encrypted text not null,
|
||||
published timestamp not null default now()
|
||||
CREATE TABLE password_reset_request (
|
||||
id serial PRIMARY KEY,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
token_encrypted text NOT NULL,
|
||||
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 view site_view;
|
||||
alter table site drop column enable_downvotes;
|
||||
alter table site drop column open_registration;
|
||||
alter table site drop column enable_nsfw;
|
||||
DROP VIEW site_view;
|
||||
|
||||
ALTER TABLE site
|
||||
DROP COLUMN enable_downvotes;
|
||||
|
||||
ALTER TABLE site
|
||||
DROP COLUMN open_registration;
|
||||
|
||||
ALTER TABLE site
|
||||
DROP COLUMN enable_nsfw;
|
||||
|
||||
-- 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
|
||||
alter table site add column enable_downvotes 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;
|
||||
ALTER TABLE site
|
||||
ADD COLUMN enable_downvotes 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
|
||||
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
|
||||
drop view user_mention_view;
|
||||
drop view reply_view;
|
||||
drop view comment_view;
|
||||
drop view user_view;
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
DROP VIEW reply_view;
|
||||
|
||||
DROP VIEW comment_view;
|
||||
|
||||
DROP VIEW user_view;
|
||||
|
||||
-- user
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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 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 VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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
|
||||
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
|
||||
-- Recreate the view
|
||||
drop view post_view;
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select 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
|
||||
DROP VIEW post_view;
|
||||
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
SELECT
|
||||
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;
|
||||
|
||||
-- community
|
||||
DROP VIEW community_view;
|
||||
|
||||
drop view community_view;
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
||||
from community c
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments,
|
||||
hot_rank ((
|
||||
SELECT
|
||||
count(*)
|
||||
FROM community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id), c.published) AS hot_rank
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
-- Reply and comment view
|
||||
create view comment_view as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
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,
|
||||
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 VIEW comment_view AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
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,
|
||||
coalesce(sum(cl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN cl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN cl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes
|
||||
FROM
|
||||
comment c
|
||||
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||
GROUP BY
|
||||
c.id
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
||||
from user_ u
|
||||
cross join all_comment ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
-- user mention
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.post_id,
|
||||
c.parent_id,
|
||||
|
@ -184,41 +395,117 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
um.recipient_id
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.id;
|
||||
FROM
|
||||
user_mention um,
|
||||
comment_view c
|
||||
WHERE
|
||||
um.comment_id = c.id;
|
||||
|
||||
-- community tables
|
||||
drop view community_moderator_view;
|
||||
drop view community_follower_view;
|
||||
drop view community_user_ban_view;
|
||||
drop view site_view;
|
||||
DROP VIEW community_moderator_view;
|
||||
|
||||
create view community_moderator_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_moderator cm;
|
||||
DROP VIEW community_follower_view;
|
||||
|
||||
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;
|
||||
DROP VIEW community_user_ban_view;
|
||||
|
||||
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;
|
||||
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 community_moderator_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_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
|
||||
alter table user_ rename column icon to avatar;
|
||||
alter table user_ alter column avatar type text;
|
||||
ALTER TABLE user_ RENAME COLUMN icon TO avatar;
|
||||
|
||||
ALTER TABLE user_
|
||||
ALTER COLUMN avatar TYPE text;
|
||||
|
||||
-- Rebuild nearly all the views, to include the creator avatars
|
||||
|
||||
-- user
|
||||
drop view user_view;
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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 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;
|
||||
DROP VIEW user_view;
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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
|
||||
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
|
||||
-- Recreate the view
|
||||
drop view post_view;
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id
|
||||
DROP VIEW post_view;
|
||||
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id
|
||||
)
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
||||
SELECT
|
||||
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;
|
||||
|
||||
-- community
|
||||
drop view community_view;
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select 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
|
||||
DROP VIEW community_view;
|
||||
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
-- reply and comment view
|
||||
drop view reply_view;
|
||||
drop view user_mention_view;
|
||||
drop view comment_view;
|
||||
create view comment_view as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
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
|
||||
DROP VIEW reply_view;
|
||||
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
DROP VIEW comment_view;
|
||||
|
||||
CREATE VIEW comment_view AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
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
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
||||
from user_ u
|
||||
cross join all_comment ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
-- user mention
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.post_id,
|
||||
c.parent_id,
|
||||
|
@ -193,42 +424,136 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
um.recipient_id
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.id;
|
||||
FROM
|
||||
user_mention um,
|
||||
comment_view c
|
||||
WHERE
|
||||
um.comment_id = c.id;
|
||||
|
||||
-- community views
|
||||
drop view community_moderator_view;
|
||||
drop view community_follower_view;
|
||||
drop view community_user_ban_view;
|
||||
drop view site_view;
|
||||
DROP VIEW community_moderator_view;
|
||||
|
||||
create view community_moderator_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_moderator cm;
|
||||
DROP VIEW community_follower_view;
|
||||
|
||||
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;
|
||||
DROP VIEW community_user_ban_view;
|
||||
|
||||
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;
|
||||
DROP VIEW site_view;
|
||||
|
||||
CREATE VIEW community_moderator_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_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
|
||||
drop view user_view;
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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 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;
|
||||
DROP VIEW user_view;
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
|
|
|
@ -1,16 +1,48 @@
|
|||
-- user
|
||||
drop view user_view;
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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 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;
|
||||
DROP VIEW user_view;
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
|
|
|
@ -1,20 +1,55 @@
|
|||
-- Drop the columns
|
||||
drop view user_view;
|
||||
alter table user_ drop column show_avatars;
|
||||
alter table user_ drop column send_notifications_to_email;
|
||||
DROP VIEW user_view;
|
||||
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN show_avatars;
|
||||
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN send_notifications_to_email;
|
||||
|
||||
-- Rebuild the view
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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 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 VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
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
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
|
|
|
@ -1,22 +1,57 @@
|
|||
-- Add columns
|
||||
alter table user_ add column show_avatars boolean default true not null;
|
||||
alter table user_ add column send_notifications_to_email boolean default false not null;
|
||||
ALTER TABLE user_
|
||||
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
|
||||
drop view user_view;
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
show_avatars,
|
||||
send_notifications_to_email,
|
||||
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 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;
|
||||
DROP VIEW user_view;
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
show_avatars,
|
||||
send_notifications_to_email,
|
||||
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
|
||||
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_community;
|
||||
DROP INDEX idx_post_creator;
|
||||
|
||||
drop index idx_post_like_post;
|
||||
drop index idx_post_like_user;
|
||||
DROP INDEX idx_post_community;
|
||||
|
||||
drop index idx_comment_creator;
|
||||
drop index idx_comment_parent;
|
||||
drop index idx_comment_post;
|
||||
DROP INDEX idx_post_like_post;
|
||||
|
||||
drop index idx_comment_like_comment;
|
||||
drop index idx_comment_like_user;
|
||||
drop index idx_comment_like_post;
|
||||
DROP INDEX idx_post_like_user;
|
||||
|
||||
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.
|
||||
create index idx_post_creator on post (creator_id);
|
||||
create index idx_post_community on post (community_id);
|
||||
CREATE INDEX idx_post_creator ON post (creator_id);
|
||||
|
||||
create index idx_post_like_post on post_like (post_id);
|
||||
create index idx_post_like_user on post_like (user_id);
|
||||
CREATE INDEX idx_post_community ON post (community_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_post_like_post ON post_like (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_post_like_user ON post_like (user_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,202 +1,457 @@
|
|||
-- functions and triggers
|
||||
drop trigger refresh_user on user_;
|
||||
drop function refresh_user();
|
||||
drop trigger refresh_post on post;
|
||||
drop function refresh_post();
|
||||
drop trigger refresh_post_like on post_like;
|
||||
drop function refresh_post_like();
|
||||
drop trigger refresh_community on community;
|
||||
drop function refresh_community();
|
||||
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();
|
||||
DROP TRIGGER refresh_user ON user_;
|
||||
|
||||
DROP FUNCTION refresh_user ();
|
||||
|
||||
DROP TRIGGER refresh_post ON post;
|
||||
|
||||
DROP FUNCTION refresh_post ();
|
||||
|
||||
DROP TRIGGER refresh_post_like ON post_like;
|
||||
|
||||
DROP FUNCTION refresh_post_like ();
|
||||
|
||||
DROP TRIGGER refresh_community ON community;
|
||||
|
||||
DROP FUNCTION refresh_community ();
|
||||
|
||||
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
|
||||
-- Recreate the view
|
||||
drop view post_view;
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id
|
||||
DROP VIEW post_view;
|
||||
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id
|
||||
)
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
select
|
||||
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
|
||||
DROP VIEW post_mview;
|
||||
|
||||
union all
|
||||
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||
|
||||
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 post_mview;
|
||||
drop materialized view post_aggregates_mview;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
-- user
|
||||
drop materialized view user_mview;
|
||||
drop view user_view;
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
show_avatars,
|
||||
send_notifications_to_email,
|
||||
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 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;
|
||||
DROP MATERIALIZED VIEW user_mview;
|
||||
|
||||
DROP VIEW user_view;
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
show_avatars,
|
||||
send_notifications_to_email,
|
||||
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
|
||||
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
|
||||
drop view community_mview;
|
||||
drop materialized view community_aggregates_mview;
|
||||
drop view community_view;
|
||||
drop view community_aggregates_view;
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select 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
|
||||
DROP VIEW community_mview;
|
||||
|
||||
DROP MATERIALIZED VIEW community_aggregates_mview;
|
||||
|
||||
DROP VIEW community_view;
|
||||
|
||||
DROP VIEW community_aggregates_view;
|
||||
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
-- reply and comment view
|
||||
drop view reply_view;
|
||||
drop view user_mention_view;
|
||||
drop view comment_view;
|
||||
drop view comment_mview;
|
||||
drop materialized view comment_aggregates_mview;
|
||||
drop view comment_aggregates_view;
|
||||
create view comment_view as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
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
|
||||
DROP VIEW reply_view;
|
||||
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
DROP VIEW comment_view;
|
||||
|
||||
DROP VIEW comment_mview;
|
||||
|
||||
DROP MATERIALIZED VIEW comment_aggregates_mview;
|
||||
|
||||
DROP VIEW comment_aggregates_view;
|
||||
|
||||
CREATE VIEW comment_view AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
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
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
||||
from user_ u
|
||||
cross join all_comment ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
-- user mention
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.post_id,
|
||||
c.parent_id,
|
||||
|
@ -218,6 +473,9 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
um.recipient_id
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.id;
|
||||
FROM
|
||||
user_mention um,
|
||||
comment_view c
|
||||
WHERE
|
||||
um.comment_id = c.id;
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,34 +1,72 @@
|
|||
-- Drop the triggers
|
||||
drop trigger refresh_private_message on private_message;
|
||||
drop function refresh_private_message();
|
||||
DROP TRIGGER refresh_private_message ON private_message;
|
||||
|
||||
DROP FUNCTION refresh_private_message ();
|
||||
|
||||
-- Drop the view and table
|
||||
drop view private_message_view cascade;
|
||||
drop table private_message;
|
||||
DROP VIEW private_message_view CASCADE;
|
||||
|
||||
DROP TABLE private_message;
|
||||
|
||||
-- Rebuild the old views
|
||||
drop view user_view cascade;
|
||||
create view user_view as
|
||||
select
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.fedi_name,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
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 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;
|
||||
DROP VIEW user_view CASCADE;
|
||||
|
||||
create materialized view user_mview as select * from user_view;
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.fedi_name,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
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
|
||||
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 unique index idx_user_mview_id on user_mview (id);
|
||||
CREATE MATERIALIZED VIEW user_mview AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
user_view;
|
||||
|
||||
CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
|
||||
|
||||
-- Drop the columns
|
||||
alter table user_ drop column matrix_user_id;
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN matrix_user_id;
|
||||
|
||||
|
|
|
@ -1,76 +1,117 @@
|
|||
-- Creating private message
|
||||
create table private_message (
|
||||
id serial primary key,
|
||||
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,
|
||||
content text not null,
|
||||
deleted boolean default false not null,
|
||||
read boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE private_message (
|
||||
id serial PRIMARY KEY,
|
||||
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,
|
||||
content text NOT NULL,
|
||||
deleted boolean DEFAULT FALSE NOT NULL,
|
||||
read boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
-- Create the view and materialized view which has the avatar and creator name
|
||||
create view private_message_view as
|
||||
select
|
||||
pm.*,
|
||||
u.name as creator_name,
|
||||
u.avatar as creator_avatar,
|
||||
u2.name as recipient_name,
|
||||
u2.avatar as recipient_avatar
|
||||
from private_message pm
|
||||
inner join user_ u on u.id = pm.creator_id
|
||||
inner join user_ u2 on u2.id = pm.recipient_id;
|
||||
CREATE VIEW private_message_view AS
|
||||
SELECT
|
||||
pm.*,
|
||||
u.name AS creator_name,
|
||||
u.avatar AS creator_avatar,
|
||||
u2.name AS recipient_name,
|
||||
u2.avatar AS recipient_avatar
|
||||
FROM
|
||||
private_message pm
|
||||
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 or replace function refresh_private_message()
|
||||
returns trigger language plpgsql
|
||||
as $$
|
||||
begin
|
||||
refresh materialized view concurrently private_message_mview;
|
||||
return null;
|
||||
end $$;
|
||||
CREATE OR REPLACE FUNCTION refresh_private_message ()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
REFRESH MATERIALIZED VIEW CONCURRENTLY private_message_mview;
|
||||
RETURN NULL;
|
||||
END
|
||||
$$;
|
||||
|
||||
create trigger refresh_private_message
|
||||
after insert or update or delete or truncate
|
||||
on private_message
|
||||
for each statement
|
||||
execute procedure refresh_private_message();
|
||||
CREATE TRIGGER refresh_private_message
|
||||
AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON private_message
|
||||
FOR EACH statement
|
||||
EXECUTE PROCEDURE refresh_private_message ();
|
||||
|
||||
-- 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;
|
||||
create view user_view as
|
||||
select
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.matrix_user_id,
|
||||
u.fedi_name,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
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 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;
|
||||
DROP VIEW user_view CASCADE;
|
||||
|
||||
create materialized view user_mview as select * from user_view;
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.matrix_user_id,
|
||||
u.fedi_name,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
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
|
||||
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 unique index idx_user_mview_id on user_mview (id);
|
||||
CREATE MATERIALIZED VIEW user_mview AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
user_view;
|
||||
|
||||
CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
|
||||
|
||||
-- This is what a group pm table would look like
|
||||
-- Not going to do it now because of the complications
|
||||
--
|
||||
--
|
||||
-- create table private_message (
|
||||
-- id serial primary key,
|
||||
-- creator_id int references user_ on update cascade on delete cascade not null,
|
||||
|
@ -79,7 +120,7 @@ create unique index idx_user_mview_id on user_mview (id);
|
|||
-- published timestamp not null default now(),
|
||||
-- updated timestamp
|
||||
-- );
|
||||
--
|
||||
--
|
||||
-- create table private_message_recipient (
|
||||
-- id serial primary key,
|
||||
-- private_message_id int references private_message on update cascade on delete cascade not null,
|
||||
|
|
|
@ -1,25 +1,37 @@
|
|||
-- Drop the materialized / built views
|
||||
drop view reply_view;
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
DROP VIEW reply_view;
|
||||
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
|
|
@ -1,27 +1,38 @@
|
|||
-- https://github.com/dessalines/lemmy/issues/197
|
||||
drop view reply_view;
|
||||
DROP VIEW reply_view;
|
||||
|
||||
-- Do the reply_view referencing the comment_mview
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_mview cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_mview cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
drop view user_mention_mview;
|
||||
DROP VIEW user_mention_mview;
|
||||
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
create view user_mention_mview as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_mview ca
|
||||
CREATE VIEW user_mention_mview AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
comment_aggregates_mview ca
|
||||
)
|
||||
|
||||
select
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.post_id,
|
||||
ac.parent_id,
|
||||
|
@ -26,20 +25,27 @@ select
|
|||
ac.score,
|
||||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
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,
|
||||
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,
|
||||
um.recipient_id
|
||||
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
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
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
|
||||
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.post_id,
|
||||
ac.parent_id,
|
||||
|
@ -57,11 +63,11 @@ select
|
|||
ac.score,
|
||||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved,
|
||||
um.recipient_id
|
||||
from all_comment ac
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
;
|
||||
FROM
|
||||
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_email_lower;
|
||||
DROP INDEX idx_user_name_lower;
|
||||
|
||||
DROP INDEX idx_user_email_lower;
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
-- Add case insensitive username and email uniqueness
|
||||
|
||||
-- An example of showing the dupes:
|
||||
-- select
|
||||
-- max(id) as id,
|
||||
|
@ -8,22 +7,28 @@
|
|||
-- from user_
|
||||
-- group by lower(name)
|
||||
-- having count(*) > 1;
|
||||
|
||||
-- Delete username dupes, keeping the first one
|
||||
delete
|
||||
from user_
|
||||
where id not in (
|
||||
select min(id)
|
||||
from user_
|
||||
group by lower(name), lower(fedi_name)
|
||||
);
|
||||
DELETE FROM user_
|
||||
WHERE id NOT IN (
|
||||
SELECT
|
||||
min(id)
|
||||
FROM
|
||||
user_
|
||||
GROUP BY
|
||||
lower(name),
|
||||
lower(fedi_name));
|
||||
|
||||
-- The user index
|
||||
create unique index idx_user_name_lower on user_ (lower(name));
|
||||
-- The user index
|
||||
CREATE UNIQUE INDEX idx_user_name_lower ON user_ (lower(name));
|
||||
|
||||
-- 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
|
||||
update user_ set email = null where email = '';
|
||||
UPDATE
|
||||
user_
|
||||
SET
|
||||
email = NULL
|
||||
WHERE
|
||||
email = '';
|
||||
|
||||
|
|
|
@ -1,132 +1,409 @@
|
|||
-- Drop the dependent views
|
||||
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;
|
||||
DROP VIEW post_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
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id;
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
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
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
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;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
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;
|
||||
|
||||
-- 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
|
||||
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_lock_post_view AS
|
||||
SELECT
|
||||
mlp.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mlp.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
mlp.post_id = p.id) AS post_name,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
select mlp.*,
|
||||
(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from post p where mlp.post_id = p.id) as post_name,
|
||||
(select 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
|
||||
SELECT
|
||||
mrc.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mrc.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id) AS comment_user_id,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND u.id = c.creator_id) AS comment_user_name,
|
||||
(
|
||||
SELECT
|
||||
content
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id) AS comment_content,
|
||||
(
|
||||
SELECT
|
||||
p.id
|
||||
FROM
|
||||
post p,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id) AS post_id,
|
||||
(
|
||||
SELECT
|
||||
p.name
|
||||
FROM
|
||||
post p,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id) AS post_name,
|
||||
(
|
||||
SELECT
|
||||
co.id
|
||||
FROM
|
||||
comment c,
|
||||
post p,
|
||||
community co
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id
|
||||
AND p.community_id = co.id) AS community_id,
|
||||
(
|
||||
SELECT
|
||||
co.name
|
||||
FROM
|
||||
comment c,
|
||||
post p,
|
||||
community co
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id
|
||||
AND p.community_id = co.id) AS community_name
|
||||
FROM
|
||||
mod_remove_comment mrc;
|
||||
|
||||
create view mod_remove_comment_view as
|
||||
select mrc.*,
|
||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
||||
(select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
|
||||
(select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
|
||||
(select content from comment c where mrc.comment_id = c.id) as comment_content,
|
||||
(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
|
||||
(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
|
||||
(select co.id from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_id,
|
||||
(select co.name from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_name
|
||||
from mod_remove_comment mrc;
|
||||
CREATE VIEW mod_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;
|
||||
|
||||
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 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;
|
||||
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;
|
||||
|
||||
-- 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
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id;
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
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
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
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;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
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;
|
||||
|
||||
-- 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
|
||||
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_lock_post_view AS
|
||||
SELECT
|
||||
mlp.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mlp.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
mlp.post_id = p.id) AS post_name,
|
||||
(
|
||||
SELECT
|
||||
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
|
||||
select mlp.*,
|
||||
(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from post p where mlp.post_id = p.id) as post_name,
|
||||
(select 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
|
||||
SELECT
|
||||
mrc.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mrc.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id) AS comment_user_id,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND u.id = c.creator_id) AS comment_user_name,
|
||||
(
|
||||
SELECT
|
||||
content
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id) AS comment_content,
|
||||
(
|
||||
SELECT
|
||||
p.id
|
||||
FROM
|
||||
post p,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id) AS post_id,
|
||||
(
|
||||
SELECT
|
||||
p.name
|
||||
FROM
|
||||
post p,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id) AS post_name,
|
||||
(
|
||||
SELECT
|
||||
co.id
|
||||
FROM
|
||||
comment c,
|
||||
post p,
|
||||
community co
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id
|
||||
AND p.community_id = co.id) AS community_id,
|
||||
(
|
||||
SELECT
|
||||
co.name
|
||||
FROM
|
||||
comment c,
|
||||
post p,
|
||||
community co
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id
|
||||
AND p.community_id = co.id) AS community_name
|
||||
FROM
|
||||
mod_remove_comment mrc;
|
||||
|
||||
create view mod_remove_comment_view as
|
||||
select mrc.*,
|
||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
||||
(select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
|
||||
(select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
|
||||
(select content from comment c where mrc.comment_id = c.id) as comment_content,
|
||||
(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
|
||||
(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
|
||||
(select co.id from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_id,
|
||||
(select co.name from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_name
|
||||
from mod_remove_comment mrc;
|
||||
CREATE VIEW mod_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;
|
||||
|
||||
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_mview;
|
||||
drop view comment_view;
|
||||
drop view comment_mview;
|
||||
drop materialized view comment_aggregates_mview;
|
||||
drop view comment_aggregates_view;
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
DROP VIEW user_mention_mview;
|
||||
|
||||
DROP VIEW comment_view;
|
||||
|
||||
DROP VIEW comment_mview;
|
||||
|
||||
DROP MATERIALIZED VIEW comment_aggregates_mview;
|
||||
|
||||
DROP VIEW comment_aggregates_view;
|
||||
|
||||
-- reply and comment view
|
||||
create view comment_aggregates_view as
|
||||
select
|
||||
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 VIEW comment_aggregates_view AS
|
||||
SELECT
|
||||
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 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 UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
|
||||
|
||||
create view comment_view as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_view ca
|
||||
CREATE VIEW comment_view AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
comment_aggregates_view 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
|
||||
SELECT
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
create view comment_mview as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_mview ca
|
||||
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
|
||||
SELECT
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment 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
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_mview cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_mview cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
-- user mention
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.post_id,
|
||||
c.parent_id,
|
||||
|
@ -133,21 +207,22 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
um.recipient_id
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.id;
|
||||
FROM
|
||||
user_mention um,
|
||||
comment_view c
|
||||
WHERE
|
||||
um.comment_id = c.id;
|
||||
|
||||
|
||||
create view user_mention_mview as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_mview ca
|
||||
CREATE VIEW user_mention_mview AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
comment_aggregates_mview ca
|
||||
)
|
||||
|
||||
select
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.post_id,
|
||||
ac.parent_id,
|
||||
|
@ -165,20 +240,27 @@ select
|
|||
ac.score,
|
||||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
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,
|
||||
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,
|
||||
um.recipient_id
|
||||
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
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
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
|
||||
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.post_id,
|
||||
ac.parent_id,
|
||||
|
@ -196,11 +278,11 @@ select
|
|||
ac.score,
|
||||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved,
|
||||
um.recipient_id
|
||||
from all_comment ac
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
;
|
||||
FROM
|
||||
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
|
||||
|
||||
-- Rebuild the comment view
|
||||
drop view reply_view;
|
||||
drop view user_mention_view;
|
||||
drop view user_mention_mview;
|
||||
drop view comment_view;
|
||||
drop view comment_mview;
|
||||
drop materialized view comment_aggregates_mview;
|
||||
drop view comment_aggregates_view;
|
||||
DROP VIEW reply_view;
|
||||
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
DROP VIEW user_mention_mview;
|
||||
|
||||
DROP VIEW comment_view;
|
||||
|
||||
DROP VIEW comment_mview;
|
||||
|
||||
DROP MATERIALIZED VIEW comment_aggregates_mview;
|
||||
|
||||
DROP VIEW comment_aggregates_view;
|
||||
|
||||
-- reply and comment view
|
||||
create view comment_aggregates_view as
|
||||
select
|
||||
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 VIEW comment_aggregates_view AS
|
||||
SELECT
|
||||
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 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 UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
|
||||
|
||||
create view comment_view as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_view ca
|
||||
CREATE VIEW comment_view AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
comment_aggregates_view 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
|
||||
SELECT
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from all_comment 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;
|
||||
|
||||
create view comment_mview as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_mview ca
|
||||
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
|
||||
SELECT
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from all_comment 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
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_mview cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_mview cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
-- user mention
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.post_id,
|
||||
c.parent_id,
|
||||
|
@ -143,21 +239,22 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
um.recipient_id
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.id;
|
||||
FROM
|
||||
user_mention um,
|
||||
comment_view c
|
||||
WHERE
|
||||
um.comment_id = c.id;
|
||||
|
||||
|
||||
create view user_mention_mview as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_mview ca
|
||||
CREATE VIEW user_mention_mview AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
comment_aggregates_mview ca
|
||||
)
|
||||
|
||||
select
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.post_id,
|
||||
ac.parent_id,
|
||||
|
@ -177,20 +274,27 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
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,
|
||||
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,
|
||||
um.recipient_id
|
||||
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
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
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
|
||||
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.post_id,
|
||||
ac.parent_id,
|
||||
|
@ -210,11 +314,11 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved,
|
||||
um.recipient_id
|
||||
from all_comment ac
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
;
|
||||
FROM
|
||||
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_mview;
|
||||
drop materialized view post_aggregates_mview;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_view;
|
||||
|
||||
DROP VIEW post_mview;
|
||||
|
||||
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
-- regen post view
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id;
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
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
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
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;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
|
|
@ -1,106 +1,227 @@
|
|||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||
drop view post_view;
|
||||
drop view post_mview;
|
||||
drop materialized view post_aggregates_mview;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_view;
|
||||
|
||||
DROP VIEW post_mview;
|
||||
|
||||
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
-- regen post view
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0),
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
)
|
||||
) as hot_rank,
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
) as newest_activity_time
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
left join (
|
||||
select post_id,
|
||||
max(published) as recent_comment_time
|
||||
from comment
|
||||
group by 1
|
||||
) c on p.id = c.post_id
|
||||
group by p.id, c.recent_comment_time;
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), (
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END)) AS hot_rank,
|
||||
(
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END) AS newest_activity_time
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
max(published) AS recent_comment_time
|
||||
FROM
|
||||
comment
|
||||
GROUP BY
|
||||
1) c ON p.id = c.post_id
|
||||
GROUP BY
|
||||
p.id,
|
||||
c.recent_comment_time;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
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
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
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;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
|
|
@ -1,112 +1,240 @@
|
|||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||
drop view post_view;
|
||||
drop view post_mview;
|
||||
drop materialized view post_aggregates_mview;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_view;
|
||||
|
||||
DROP VIEW post_mview;
|
||||
|
||||
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
-- Drop the columns
|
||||
alter table post drop column embed_title;
|
||||
alter table post drop column embed_description;
|
||||
alter table post drop column embed_html;
|
||||
alter table post drop column thumbnail_url;
|
||||
ALTER TABLE post
|
||||
DROP COLUMN embed_title;
|
||||
|
||||
ALTER TABLE post
|
||||
DROP COLUMN embed_description;
|
||||
|
||||
ALTER TABLE post
|
||||
DROP COLUMN embed_html;
|
||||
|
||||
ALTER TABLE post
|
||||
DROP COLUMN thumbnail_url;
|
||||
|
||||
-- regen post view
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0),
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
)
|
||||
) as hot_rank,
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
) as newest_activity_time
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
left join (
|
||||
select post_id,
|
||||
max(published) as recent_comment_time
|
||||
from comment
|
||||
group by 1
|
||||
) c on p.id = c.post_id
|
||||
group by p.id, c.recent_comment_time;
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), (
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END)) AS hot_rank,
|
||||
(
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END) AS newest_activity_time
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
max(published) AS recent_comment_time
|
||||
FROM
|
||||
comment
|
||||
GROUP BY
|
||||
1) c ON p.id = c.post_id
|
||||
GROUP BY
|
||||
p.id,
|
||||
c.recent_comment_time;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
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
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
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;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
|
|
@ -1,115 +1,241 @@
|
|||
-- Add the columns
|
||||
alter table post add column embed_title 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;
|
||||
ALTER TABLE post
|
||||
ADD COLUMN embed_title 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
|
||||
|
||||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||
drop view post_view;
|
||||
drop view post_mview;
|
||||
drop materialized view post_aggregates_mview;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_view;
|
||||
|
||||
DROP VIEW post_mview;
|
||||
|
||||
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
-- regen post view
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0),
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
)
|
||||
) as hot_rank,
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
) as newest_activity_time
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
left join (
|
||||
select post_id,
|
||||
max(published) as recent_comment_time
|
||||
from comment
|
||||
group by 1
|
||||
) c on p.id = c.post_id
|
||||
group by p.id, c.recent_comment_time;
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), (
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END)) AS hot_rank,
|
||||
(
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END) AS newest_activity_time
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
max(published) AS recent_comment_time
|
||||
FROM
|
||||
comment
|
||||
GROUP BY
|
||||
1) c ON p.id = c.post_id
|
||||
GROUP BY
|
||||
p.id,
|
||||
c.recent_comment_time;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
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
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
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;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
drop table activity;
|
||||
DROP TABLE activity;
|
||||
|
||||
alter table user_
|
||||
drop column actor_id,
|
||||
drop column private_key,
|
||||
drop column public_key,
|
||||
drop column bio,
|
||||
drop column local,
|
||||
drop column last_refreshed_at;
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN actor_id,
|
||||
DROP COLUMN private_key,
|
||||
DROP COLUMN public_key,
|
||||
DROP COLUMN bio,
|
||||
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;
|
||||
|
||||
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
|
||||
-- All user actions must create a row here.
|
||||
create table activity (
|
||||
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.
|
||||
data jsonb not null,
|
||||
local boolean not null default true,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE activity (
|
||||
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.
|
||||
data jsonb NOT NULL,
|
||||
local boolean NOT NULL DEFAULT TRUE,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
-- 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
|
||||
alter table user_
|
||||
ALTER TABLE user_
|
||||
-- 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 bio text, -- not on community, already has description
|
||||
add column local boolean not null default true,
|
||||
add column private_key text, -- These need to be generated from code
|
||||
add column public_key text,
|
||||
add column last_refreshed_at timestamp not null default now() -- Used to re-fetch federated actor periodically
|
||||
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 local boolean NOT NULL DEFAULT TRUE,
|
||||
ADD COLUMN private_key text, -- These need to be generated from code
|
||||
ADD COLUMN public_key text,
|
||||
ADD COLUMN last_refreshed_at timestamp NOT NULL DEFAULT now() -- Used to re-fetch federated actor periodically
|
||||
;
|
||||
|
||||
-- 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 local boolean not null default true,
|
||||
add column private_key text, -- These need to be generated from code
|
||||
add column public_key text,
|
||||
add column last_refreshed_at timestamp not null default now() -- Used to re-fetch federated actor periodically
|
||||
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 local boolean NOT NULL DEFAULT TRUE,
|
||||
ADD COLUMN private_key text, -- These need to be generated from code
|
||||
ADD COLUMN public_key text,
|
||||
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.
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
alter table post
|
||||
drop column ap_id,
|
||||
drop column local;
|
||||
ALTER TABLE post
|
||||
DROP COLUMN ap_id,
|
||||
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
|
||||
|
||||
alter table post
|
||||
ALTER TABLE post
|
||||
-- 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 local boolean not null default true
|
||||
;
|
||||
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;
|
||||
|
||||
alter table comment
|
||||
ALTER TABLE comment
|
||||
-- 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 local boolean not null default true
|
||||
;
|
||||
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;
|
||||
|
||||
|
|
|
@ -1,36 +1,69 @@
|
|||
-- User table
|
||||
drop view user_view cascade;
|
||||
DROP VIEW user_view CASCADE;
|
||||
|
||||
alter table user_
|
||||
add column fedi_name varchar(40) not null default 'http://fake.com';
|
||||
ALTER TABLE user_
|
||||
ADD COLUMN fedi_name varchar(40) NOT NULL DEFAULT 'http://fake.com';
|
||||
|
||||
alter table user_
|
||||
add constraint user__name_fedi_name_key unique (name, fedi_name);
|
||||
ALTER TABLE user_
|
||||
ADD CONSTRAINT user__name_fedi_name_key UNIQUE (name, fedi_name);
|
||||
|
||||
-- Community
|
||||
alter table community
|
||||
add constraint community_name_key unique (name);
|
||||
ALTER TABLE community
|
||||
ADD CONSTRAINT community_name_key UNIQUE (name);
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.matrix_user_id,
|
||||
u.fedi_name,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
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
|
||||
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 view user_view as
|
||||
select
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.matrix_user_id,
|
||||
u.fedi_name,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
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 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,38 +1,70 @@
|
|||
-- User table
|
||||
|
||||
-- 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
|
||||
alter table user_
|
||||
drop constraint user__name_fedi_name_key;
|
||||
ALTER TABLE user_
|
||||
DROP CONSTRAINT user__name_fedi_name_key;
|
||||
|
||||
alter table user_
|
||||
drop column fedi_name;
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN fedi_name;
|
||||
|
||||
-- Community
|
||||
alter table community
|
||||
drop constraint community_name_key;
|
||||
ALTER TABLE community
|
||||
DROP CONSTRAINT community_name_key;
|
||||
|
||||
create view user_view as
|
||||
select
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.matrix_user_id,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
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 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 VIEW user_view AS
|
||||
SELECT
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.matrix_user_id,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
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
|
||||
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
|
||||
drop index idx_user_name_lower_actor_id;
|
||||
create unique index idx_user_name_lower on user_ (lower(name));
|
||||
DROP INDEX idx_user_name_lower_actor_id;
|
||||
|
||||
CREATE UNIQUE INDEX idx_user_name_lower ON user_ (lower(name));
|
||||
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
drop index idx_user_name_lower;
|
||||
create unique index idx_user_name_lower_actor_id on user_ (lower(name), lower(actor_id));
|
||||
DROP INDEX idx_user_name_lower;
|
||||
|
||||
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 view private_message_view;
|
||||
DROP MATERIALIZED VIEW private_message_mview;
|
||||
|
||||
alter table private_message
|
||||
drop column ap_id,
|
||||
drop column local;
|
||||
DROP VIEW private_message_view;
|
||||
|
||||
create view private_message_view as
|
||||
select
|
||||
pm.*,
|
||||
u.name as creator_name,
|
||||
u.avatar as creator_avatar,
|
||||
u2.name as recipient_name,
|
||||
u2.avatar as recipient_avatar
|
||||
from private_message pm
|
||||
inner join user_ u on u.id = pm.creator_id
|
||||
inner join user_ u2 on u2.id = pm.recipient_id;
|
||||
ALTER TABLE private_message
|
||||
DROP COLUMN ap_id,
|
||||
DROP COLUMN local;
|
||||
|
||||
create materialized view private_message_mview as select * from private_message_view;
|
||||
CREATE VIEW private_message_view AS
|
||||
SELECT
|
||||
pm.*,
|
||||
u.name AS creator_name,
|
||||
u.avatar AS creator_avatar,
|
||||
u2.name AS recipient_name,
|
||||
u2.avatar AS recipient_avatar
|
||||
FROM
|
||||
private_message pm
|
||||
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 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
|
||||
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
|
||||
;
|
||||
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 local boolean NOT NULL DEFAULT TRUE;
|
||||
|
||||
drop materialized view private_message_mview;
|
||||
drop view private_message_view;
|
||||
create view private_message_view as
|
||||
select
|
||||
pm.*,
|
||||
u.name as creator_name,
|
||||
u.avatar as creator_avatar,
|
||||
u.actor_id as creator_actor_id,
|
||||
u.local as creator_local,
|
||||
u2.name as recipient_name,
|
||||
u2.avatar as recipient_avatar,
|
||||
u2.actor_id as recipient_actor_id,
|
||||
u2.local as recipient_local
|
||||
from private_message pm
|
||||
inner join user_ u on u.id = pm.creator_id
|
||||
inner join user_ u2 on u2.id = pm.recipient_id;
|
||||
DROP MATERIALIZED VIEW private_message_mview;
|
||||
|
||||
create materialized view private_message_mview as select * from private_message_view;
|
||||
DROP VIEW private_message_view;
|
||||
|
||||
CREATE VIEW private_message_view AS
|
||||
SELECT
|
||||
pm.*,
|
||||
u.name AS creator_name,
|
||||
u.avatar AS creator_avatar,
|
||||
u.actor_id AS creator_actor_id,
|
||||
u.local AS creator_local,
|
||||
u2.name AS recipient_name,
|
||||
u2.avatar AS recipient_avatar,
|
||||
u2.actor_id AS recipient_actor_id,
|
||||
u2.local AS recipient_local
|
||||
FROM
|
||||
private_message pm
|
||||
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 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 reply_fast_view;
|
||||
drop view comment_fast_view;
|
||||
drop view comment_view;
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
drop view user_mention_fast_view;
|
||||
drop table comment_aggregates_fast;
|
||||
drop view comment_aggregates_view;
|
||||
DROP VIEW reply_fast_view;
|
||||
|
||||
create view comment_aggregates_view as
|
||||
select
|
||||
ct.*,
|
||||
-- community details
|
||||
p.community_id,
|
||||
c.actor_id as community_actor_id,
|
||||
c."local" as community_local,
|
||||
c."name" as community_name,
|
||||
-- creator details
|
||||
u.banned as banned,
|
||||
coalesce(cb.id, 0)::bool as banned_from_community,
|
||||
u.actor_id as creator_actor_id,
|
||||
u.local as creator_local,
|
||||
u.name as creator_name,
|
||||
u.avatar as creator_avatar,
|
||||
-- score details
|
||||
coalesce(cl.total, 0) as score,
|
||||
coalesce(cl.up, 0) as upvotes,
|
||||
coalesce(cl.down, 0) as downvotes,
|
||||
hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
|
||||
from comment ct
|
||||
left join post p on ct.post_id = p.id
|
||||
left join community c on p.community_id = c.id
|
||||
left join user_ u on ct.creator_id = u.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 (
|
||||
select
|
||||
l.comment_id as id,
|
||||
sum(l.score) as total,
|
||||
count(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;
|
||||
DROP VIEW comment_fast_view;
|
||||
|
||||
create or replace view comment_view as (
|
||||
select
|
||||
cav.*,
|
||||
us.user_id as user_id,
|
||||
us.my_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_saved::bool as saved
|
||||
from comment_aggregates_view cav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
coalesce(cf.id, 0) as is_subbed,
|
||||
coalesce(cs.id, 0) as is_saved
|
||||
from user_ u
|
||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
||||
) as us
|
||||
DROP VIEW comment_view;
|
||||
|
||||
union all
|
||||
DROP VIEW user_mention_fast_view;
|
||||
|
||||
select
|
||||
DROP TABLE comment_aggregates_fast;
|
||||
|
||||
DROP VIEW comment_aggregates_view;
|
||||
|
||||
CREATE VIEW comment_aggregates_view AS
|
||||
SELECT
|
||||
ct.*,
|
||||
-- community details
|
||||
p.community_id,
|
||||
c.actor_id AS community_actor_id,
|
||||
c."local" AS community_local,
|
||||
c."name" AS community_name,
|
||||
-- creator details
|
||||
u.banned AS banned,
|
||||
coalesce(cb.id, 0)::bool AS banned_from_community,
|
||||
u.actor_id AS creator_actor_id,
|
||||
u.local AS creator_local,
|
||||
u.name AS creator_name,
|
||||
u.avatar AS creator_avatar,
|
||||
-- score details
|
||||
coalesce(cl.total, 0) AS score,
|
||||
coalesce(cl.up, 0) AS upvotes,
|
||||
coalesce(cl.down, 0) AS downvotes,
|
||||
hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
|
||||
FROM
|
||||
comment ct
|
||||
LEFT JOIN post p ON ct.post_id = p.id
|
||||
LEFT JOIN community c ON p.community_id = c.id
|
||||
LEFT JOIN user_ u ON ct.creator_id = u.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 (
|
||||
SELECT
|
||||
l.comment_id AS id,
|
||||
sum(l.score) AS total,
|
||||
count(
|
||||
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 (
|
||||
SELECT
|
||||
cav.*,
|
||||
us.user_id AS user_id,
|
||||
us.my_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
comment_aggregates_view cav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
coalesce(cf.id, 0) AS is_subbed,
|
||||
coalesce(cs.id, 0) AS is_saved
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND cav.id = cl.comment_id
|
||||
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||
AND cs.comment_id = cav.id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cav.community_id = cf.community_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from comment_aggregates_view cav
|
||||
);
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS saved
|
||||
FROM
|
||||
comment_aggregates_view cav);
|
||||
|
||||
create table comment_aggregates_fast as select * from comment_aggregates_view;
|
||||
alter table comment_aggregates_fast add primary key (id);
|
||||
CREATE TABLE comment_aggregates_fast AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
comment_aggregates_view;
|
||||
|
||||
create view comment_fast_view as
|
||||
select
|
||||
cav.*,
|
||||
us.user_id as user_id,
|
||||
us.my_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_saved::bool as saved
|
||||
from comment_aggregates_fast cav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
coalesce(cf.id, 0) as is_subbed,
|
||||
coalesce(cs.id, 0) as is_saved
|
||||
from user_ u
|
||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
||||
) as us
|
||||
ALTER TABLE comment_aggregates_fast
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
CREATE VIEW comment_fast_view AS
|
||||
SELECT
|
||||
cav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from comment_aggregates_fast cav;
|
||||
us.user_id AS user_id,
|
||||
us.my_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
comment_aggregates_fast cav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
coalesce(cf.id, 0) AS is_subbed,
|
||||
coalesce(cs.id, 0) AS is_saved
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND cav.id = cl.comment_id
|
||||
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||
AND cs.comment_id = cav.id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cav.community_id = cf.community_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cav.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS saved
|
||||
FROM
|
||||
comment_aggregates_fast cav;
|
||||
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.creator_actor_id,
|
||||
c.creator_local,
|
||||
|
@ -137,15 +167,30 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
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
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.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
|
||||
FROM
|
||||
user_mention um,
|
||||
comment_view c
|
||||
WHERE
|
||||
um.comment_id = c.id;
|
||||
|
||||
create view user_mention_fast_view as
|
||||
select
|
||||
CREATE VIEW user_mention_fast_view AS
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.creator_actor_id,
|
||||
ac.creator_local,
|
||||
|
@ -169,26 +214,45 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
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,
|
||||
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,
|
||||
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
|
||||
from user_ u
|
||||
cross join (
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_fast ca
|
||||
) ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
(
|
||||
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
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
comment_aggregates_fast ca) ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.creator_actor_id,
|
||||
ac.creator_local,
|
||||
|
@ -212,177 +276,220 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved,
|
||||
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
|
||||
from comment_aggregates_fast ac
|
||||
left join user_mention um on um.comment_id = ac.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
|
||||
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
|
||||
create view reply_fast_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_fast_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_fast_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_fast_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
-- add creator_published to the post view
|
||||
drop view post_fast_view;
|
||||
drop table post_aggregates_fast;
|
||||
drop view post_view;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_fast_view;
|
||||
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
-- creator details
|
||||
u.actor_id as creator_actor_id,
|
||||
u."local" as creator_local,
|
||||
u."name" as creator_name,
|
||||
u.avatar as creator_avatar,
|
||||
u.banned as banned,
|
||||
cb.id::bool as banned_from_community,
|
||||
-- community details
|
||||
c.actor_id as community_actor_id,
|
||||
c."local" as community_local,
|
||||
c."name" as community_name,
|
||||
c.removed as community_removed,
|
||||
c.deleted as community_deleted,
|
||||
c.nsfw as community_nsfw,
|
||||
-- post score data/comment count
|
||||
coalesce(ct.comments, 0) as number_of_comments,
|
||||
coalesce(pl.score, 0) as score,
|
||||
coalesce(pl.upvotes, 0) as upvotes,
|
||||
coalesce(pl.downvotes, 0) as downvotes,
|
||||
hot_rank(
|
||||
coalesce(pl.score , 0), (
|
||||
case
|
||||
when (p.published < ('now'::timestamp - '1 month'::interval))
|
||||
then p.published
|
||||
else greatest(ct.recent_comment_time, p.published)
|
||||
end
|
||||
)
|
||||
) as hot_rank,
|
||||
(
|
||||
case
|
||||
when (p.published < ('now'::timestamp - '1 month'::interval))
|
||||
then p.published
|
||||
else greatest(ct.recent_comment_time, p.published)
|
||||
end
|
||||
) as newest_activity_time
|
||||
from post p
|
||||
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 c on p.community_id = c.id
|
||||
left join (
|
||||
select
|
||||
post_id,
|
||||
count(*) as comments,
|
||||
max(published) as recent_comment_time
|
||||
from comment
|
||||
group by post_id
|
||||
) ct on ct.post_id = p.id
|
||||
left join (
|
||||
select
|
||||
post_id,
|
||||
sum(score) as score,
|
||||
sum(score) filter (where score = 1) as upvotes,
|
||||
-sum(score) filter (where score = -1) as downvotes
|
||||
from post_like
|
||||
group by post_id
|
||||
) pl on pl.post_id = p.id
|
||||
order by p.id;
|
||||
DROP TABLE post_aggregates_fast;
|
||||
|
||||
create view post_view as
|
||||
select
|
||||
pav.*,
|
||||
us.id as user_id,
|
||||
us.user_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_read::bool as read,
|
||||
us.is_saved::bool as saved
|
||||
from post_aggregates_view pav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id,
|
||||
coalesce(cf.community_id, 0) as is_subbed,
|
||||
coalesce(pr.post_id, 0) as is_read,
|
||||
coalesce(ps.post_id, 0) as is_saved,
|
||||
coalesce(pl.score, 0) as user_vote
|
||||
from user_ u
|
||||
left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
|
||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
|
||||
left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
|
||||
left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
|
||||
left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
|
||||
) as us
|
||||
DROP VIEW post_view;
|
||||
|
||||
union all
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
select
|
||||
pav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from post_aggregates_view pav;
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
-- creator details
|
||||
u.actor_id AS creator_actor_id,
|
||||
u."local" AS creator_local,
|
||||
u."name" AS creator_name,
|
||||
u.avatar AS creator_avatar,
|
||||
u.banned AS banned,
|
||||
cb.id::bool AS banned_from_community,
|
||||
-- community details
|
||||
c.actor_id AS community_actor_id,
|
||||
c."local" AS community_local,
|
||||
c."name" AS community_name,
|
||||
c.removed AS community_removed,
|
||||
c.deleted AS community_deleted,
|
||||
c.nsfw AS community_nsfw,
|
||||
-- post score data/comment count
|
||||
coalesce(ct.comments, 0) AS number_of_comments,
|
||||
coalesce(pl.score, 0) AS score,
|
||||
coalesce(pl.upvotes, 0) AS upvotes,
|
||||
coalesce(pl.downvotes, 0) AS downvotes,
|
||||
hot_rank (coalesce(pl.score, 0), (
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published
|
||||
ELSE
|
||||
greatest (ct.recent_comment_time, p.published)
|
||||
END)) AS hot_rank,
|
||||
(
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published
|
||||
ELSE
|
||||
greatest (ct.recent_comment_time, p.published)
|
||||
END) AS newest_activity_time
|
||||
FROM
|
||||
post p
|
||||
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 c ON p.community_id = c.id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
count(*) AS comments,
|
||||
max(published) AS recent_comment_time
|
||||
FROM
|
||||
comment
|
||||
GROUP BY
|
||||
post_id) ct ON ct.post_id = p.id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
sum(score) AS score,
|
||||
sum(score) FILTER (WHERE score = 1) AS upvotes,
|
||||
- sum(score) FILTER (WHERE score = - 1) AS downvotes
|
||||
FROM
|
||||
post_like
|
||||
GROUP BY
|
||||
post_id) pl ON pl.post_id = p.id
|
||||
ORDER BY
|
||||
p.id;
|
||||
|
||||
create table post_aggregates_fast as select * from post_aggregates_view;
|
||||
alter table post_aggregates_fast add primary key (id);
|
||||
CREATE VIEW post_view AS
|
||||
SELECT
|
||||
pav.*,
|
||||
us.id AS user_id,
|
||||
us.user_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_read::bool AS read,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
post_aggregates_view pav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id,
|
||||
coalesce(cf.community_id, 0) AS is_subbed,
|
||||
coalesce(pr.post_id, 0) AS is_read,
|
||||
coalesce(ps.post_id, 0) AS is_saved,
|
||||
coalesce(pl.score, 0) AS user_vote
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN community_user_ban cb ON u.id = cb.user_id
|
||||
AND cb.community_id = pav.community_id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cf.community_id = pav.community_id
|
||||
LEFT JOIN post_read pr ON u.id = pr.user_id
|
||||
AND pr.post_id = pav.id
|
||||
LEFT JOIN post_saved ps ON u.id = ps.user_id
|
||||
AND ps.post_id = pav.id
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND pav.id = pl.post_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
pav.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
post_aggregates_view pav;
|
||||
|
||||
create view post_fast_view as
|
||||
select
|
||||
pav.*,
|
||||
us.id as user_id,
|
||||
us.user_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_read::bool as read,
|
||||
us.is_saved::bool as saved
|
||||
from post_aggregates_fast pav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id,
|
||||
coalesce(cf.community_id, 0) as is_subbed,
|
||||
coalesce(pr.post_id, 0) as is_read,
|
||||
coalesce(ps.post_id, 0) as is_saved,
|
||||
coalesce(pl.score, 0) as user_vote
|
||||
from user_ u
|
||||
left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
|
||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
|
||||
left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
|
||||
left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
|
||||
left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
|
||||
) as us
|
||||
CREATE TABLE post_aggregates_fast AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
post_aggregates_view;
|
||||
|
||||
union all
|
||||
ALTER TABLE post_aggregates_fast
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
CREATE VIEW post_fast_view AS
|
||||
SELECT
|
||||
pav.*,
|
||||
us.id AS user_id,
|
||||
us.user_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_read::bool AS read,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
post_aggregates_fast pav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id,
|
||||
coalesce(cf.community_id, 0) AS is_subbed,
|
||||
coalesce(pr.post_id, 0) AS is_read,
|
||||
coalesce(ps.post_id, 0) AS is_saved,
|
||||
coalesce(pl.score, 0) AS user_vote
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN community_user_ban cb ON u.id = cb.user_id
|
||||
AND cb.community_id = pav.community_id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cf.community_id = pav.community_id
|
||||
LEFT JOIN post_read pr ON u.id = pr.user_id
|
||||
AND pr.post_id = pav.id
|
||||
LEFT JOIN post_saved ps ON u.id = ps.user_id
|
||||
AND ps.post_id = pav.id
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND pav.id = pl.post_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
pav.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
post_aggregates_fast pav;
|
||||
|
||||
select
|
||||
pav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from post_aggregates_fast pav;
|
|
@ -1,116 +1,146 @@
|
|||
drop view user_mention_view;
|
||||
drop view reply_fast_view;
|
||||
drop view comment_fast_view;
|
||||
drop view comment_view;
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
drop view user_mention_fast_view;
|
||||
drop table comment_aggregates_fast;
|
||||
drop view comment_aggregates_view;
|
||||
DROP VIEW reply_fast_view;
|
||||
|
||||
create view comment_aggregates_view as
|
||||
select
|
||||
ct.*,
|
||||
-- community details
|
||||
p.community_id,
|
||||
c.actor_id as community_actor_id,
|
||||
c."local" as community_local,
|
||||
c."name" as community_name,
|
||||
-- creator details
|
||||
u.banned as banned,
|
||||
coalesce(cb.id, 0)::bool as banned_from_community,
|
||||
u.actor_id as creator_actor_id,
|
||||
u.local as creator_local,
|
||||
u.name as creator_name,
|
||||
u.published as creator_published,
|
||||
u.avatar as creator_avatar,
|
||||
-- score details
|
||||
coalesce(cl.total, 0) as score,
|
||||
coalesce(cl.up, 0) as upvotes,
|
||||
coalesce(cl.down, 0) as downvotes,
|
||||
hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
|
||||
from comment ct
|
||||
left join post p on ct.post_id = p.id
|
||||
left join community c on p.community_id = c.id
|
||||
left join user_ u on ct.creator_id = u.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 (
|
||||
select
|
||||
l.comment_id as id,
|
||||
sum(l.score) as total,
|
||||
count(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;
|
||||
DROP VIEW comment_fast_view;
|
||||
|
||||
create or replace view comment_view as (
|
||||
select
|
||||
cav.*,
|
||||
us.user_id as user_id,
|
||||
us.my_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_saved::bool as saved
|
||||
from comment_aggregates_view cav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
coalesce(cf.id, 0) as is_subbed,
|
||||
coalesce(cs.id, 0) as is_saved
|
||||
from user_ u
|
||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
||||
) as us
|
||||
DROP VIEW comment_view;
|
||||
|
||||
union all
|
||||
DROP VIEW user_mention_fast_view;
|
||||
|
||||
select
|
||||
DROP TABLE comment_aggregates_fast;
|
||||
|
||||
DROP VIEW comment_aggregates_view;
|
||||
|
||||
CREATE VIEW comment_aggregates_view AS
|
||||
SELECT
|
||||
ct.*,
|
||||
-- community details
|
||||
p.community_id,
|
||||
c.actor_id AS community_actor_id,
|
||||
c."local" AS community_local,
|
||||
c."name" AS community_name,
|
||||
-- creator details
|
||||
u.banned AS banned,
|
||||
coalesce(cb.id, 0)::bool AS banned_from_community,
|
||||
u.actor_id AS creator_actor_id,
|
||||
u.local AS creator_local,
|
||||
u.name AS creator_name,
|
||||
u.published AS creator_published,
|
||||
u.avatar AS creator_avatar,
|
||||
-- score details
|
||||
coalesce(cl.total, 0) AS score,
|
||||
coalesce(cl.up, 0) AS upvotes,
|
||||
coalesce(cl.down, 0) AS downvotes,
|
||||
hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
|
||||
FROM
|
||||
comment ct
|
||||
LEFT JOIN post p ON ct.post_id = p.id
|
||||
LEFT JOIN community c ON p.community_id = c.id
|
||||
LEFT JOIN user_ u ON ct.creator_id = u.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 (
|
||||
SELECT
|
||||
l.comment_id AS id,
|
||||
sum(l.score) AS total,
|
||||
count(
|
||||
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 (
|
||||
SELECT
|
||||
cav.*,
|
||||
us.user_id AS user_id,
|
||||
us.my_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
comment_aggregates_view cav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
coalesce(cf.id, 0) AS is_subbed,
|
||||
coalesce(cs.id, 0) AS is_saved
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND cav.id = cl.comment_id
|
||||
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||
AND cs.comment_id = cav.id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cav.community_id = cf.community_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from comment_aggregates_view cav
|
||||
);
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS saved
|
||||
FROM
|
||||
comment_aggregates_view cav);
|
||||
|
||||
create table comment_aggregates_fast as select * from comment_aggregates_view;
|
||||
alter table comment_aggregates_fast add primary key (id);
|
||||
CREATE TABLE comment_aggregates_fast AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
comment_aggregates_view;
|
||||
|
||||
create view comment_fast_view as
|
||||
select
|
||||
cav.*,
|
||||
us.user_id as user_id,
|
||||
us.my_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_saved::bool as saved
|
||||
from comment_aggregates_fast cav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
coalesce(cf.id, 0) as is_subbed,
|
||||
coalesce(cs.id, 0) as is_saved
|
||||
from user_ u
|
||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
||||
) as us
|
||||
ALTER TABLE comment_aggregates_fast
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
CREATE VIEW comment_fast_view AS
|
||||
SELECT
|
||||
cav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from comment_aggregates_fast cav;
|
||||
us.user_id AS user_id,
|
||||
us.my_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
comment_aggregates_fast cav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
coalesce(cf.id, 0) AS is_subbed,
|
||||
coalesce(cs.id, 0) AS is_saved
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND cav.id = cl.comment_id
|
||||
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||
AND cs.comment_id = cav.id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cav.community_id = cf.community_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cav.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS saved
|
||||
FROM
|
||||
comment_aggregates_fast cav;
|
||||
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.creator_actor_id,
|
||||
c.creator_local,
|
||||
|
@ -138,15 +168,30 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
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
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.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
|
||||
FROM
|
||||
user_mention um,
|
||||
comment_view c
|
||||
WHERE
|
||||
um.comment_id = c.id;
|
||||
|
||||
create view user_mention_fast_view as
|
||||
select
|
||||
CREATE VIEW user_mention_fast_view AS
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.creator_actor_id,
|
||||
ac.creator_local,
|
||||
|
@ -170,26 +215,45 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
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,
|
||||
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,
|
||||
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
|
||||
from user_ u
|
||||
cross join (
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_fast ca
|
||||
) ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
(
|
||||
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
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
comment_aggregates_fast ca) ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.creator_actor_id,
|
||||
ac.creator_local,
|
||||
|
@ -213,178 +277,221 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved,
|
||||
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
|
||||
from comment_aggregates_fast ac
|
||||
left join user_mention um on um.comment_id = ac.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
|
||||
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
|
||||
create view reply_fast_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_fast_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_fast_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_fast_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
-- add creator_published to the post view
|
||||
drop view post_fast_view;
|
||||
drop table post_aggregates_fast;
|
||||
drop view post_view;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_fast_view;
|
||||
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
-- creator details
|
||||
u.actor_id as creator_actor_id,
|
||||
u."local" as creator_local,
|
||||
u."name" as creator_name,
|
||||
u.published as creator_published,
|
||||
u.avatar as creator_avatar,
|
||||
u.banned as banned,
|
||||
cb.id::bool as banned_from_community,
|
||||
-- community details
|
||||
c.actor_id as community_actor_id,
|
||||
c."local" as community_local,
|
||||
c."name" as community_name,
|
||||
c.removed as community_removed,
|
||||
c.deleted as community_deleted,
|
||||
c.nsfw as community_nsfw,
|
||||
-- post score data/comment count
|
||||
coalesce(ct.comments, 0) as number_of_comments,
|
||||
coalesce(pl.score, 0) as score,
|
||||
coalesce(pl.upvotes, 0) as upvotes,
|
||||
coalesce(pl.downvotes, 0) as downvotes,
|
||||
hot_rank(
|
||||
coalesce(pl.score , 0), (
|
||||
case
|
||||
when (p.published < ('now'::timestamp - '1 month'::interval))
|
||||
then p.published
|
||||
else greatest(ct.recent_comment_time, p.published)
|
||||
end
|
||||
)
|
||||
) as hot_rank,
|
||||
(
|
||||
case
|
||||
when (p.published < ('now'::timestamp - '1 month'::interval))
|
||||
then p.published
|
||||
else greatest(ct.recent_comment_time, p.published)
|
||||
end
|
||||
) as newest_activity_time
|
||||
from post p
|
||||
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 c on p.community_id = c.id
|
||||
left join (
|
||||
select
|
||||
post_id,
|
||||
count(*) as comments,
|
||||
max(published) as recent_comment_time
|
||||
from comment
|
||||
group by post_id
|
||||
) ct on ct.post_id = p.id
|
||||
left join (
|
||||
select
|
||||
post_id,
|
||||
sum(score) as score,
|
||||
sum(score) filter (where score = 1) as upvotes,
|
||||
-sum(score) filter (where score = -1) as downvotes
|
||||
from post_like
|
||||
group by post_id
|
||||
) pl on pl.post_id = p.id
|
||||
order by p.id;
|
||||
DROP TABLE post_aggregates_fast;
|
||||
|
||||
create view post_view as
|
||||
select
|
||||
pav.*,
|
||||
us.id as user_id,
|
||||
us.user_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_read::bool as read,
|
||||
us.is_saved::bool as saved
|
||||
from post_aggregates_view pav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id,
|
||||
coalesce(cf.community_id, 0) as is_subbed,
|
||||
coalesce(pr.post_id, 0) as is_read,
|
||||
coalesce(ps.post_id, 0) as is_saved,
|
||||
coalesce(pl.score, 0) as user_vote
|
||||
from user_ u
|
||||
left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
|
||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
|
||||
left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
|
||||
left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
|
||||
left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
|
||||
) as us
|
||||
DROP VIEW post_view;
|
||||
|
||||
union all
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
select
|
||||
pav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from post_aggregates_view pav;
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
-- creator details
|
||||
u.actor_id AS creator_actor_id,
|
||||
u."local" AS creator_local,
|
||||
u."name" AS creator_name,
|
||||
u.published AS creator_published,
|
||||
u.avatar AS creator_avatar,
|
||||
u.banned AS banned,
|
||||
cb.id::bool AS banned_from_community,
|
||||
-- community details
|
||||
c.actor_id AS community_actor_id,
|
||||
c."local" AS community_local,
|
||||
c."name" AS community_name,
|
||||
c.removed AS community_removed,
|
||||
c.deleted AS community_deleted,
|
||||
c.nsfw AS community_nsfw,
|
||||
-- post score data/comment count
|
||||
coalesce(ct.comments, 0) AS number_of_comments,
|
||||
coalesce(pl.score, 0) AS score,
|
||||
coalesce(pl.upvotes, 0) AS upvotes,
|
||||
coalesce(pl.downvotes, 0) AS downvotes,
|
||||
hot_rank (coalesce(pl.score, 0), (
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published
|
||||
ELSE
|
||||
greatest (ct.recent_comment_time, p.published)
|
||||
END)) AS hot_rank,
|
||||
(
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published
|
||||
ELSE
|
||||
greatest (ct.recent_comment_time, p.published)
|
||||
END) AS newest_activity_time
|
||||
FROM
|
||||
post p
|
||||
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 c ON p.community_id = c.id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
count(*) AS comments,
|
||||
max(published) AS recent_comment_time
|
||||
FROM
|
||||
comment
|
||||
GROUP BY
|
||||
post_id) ct ON ct.post_id = p.id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
sum(score) AS score,
|
||||
sum(score) FILTER (WHERE score = 1) AS upvotes,
|
||||
- sum(score) FILTER (WHERE score = - 1) AS downvotes
|
||||
FROM
|
||||
post_like
|
||||
GROUP BY
|
||||
post_id) pl ON pl.post_id = p.id
|
||||
ORDER BY
|
||||
p.id;
|
||||
|
||||
create table post_aggregates_fast as select * from post_aggregates_view;
|
||||
alter table post_aggregates_fast add primary key (id);
|
||||
CREATE VIEW post_view AS
|
||||
SELECT
|
||||
pav.*,
|
||||
us.id AS user_id,
|
||||
us.user_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_read::bool AS read,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
post_aggregates_view pav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id,
|
||||
coalesce(cf.community_id, 0) AS is_subbed,
|
||||
coalesce(pr.post_id, 0) AS is_read,
|
||||
coalesce(ps.post_id, 0) AS is_saved,
|
||||
coalesce(pl.score, 0) AS user_vote
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN community_user_ban cb ON u.id = cb.user_id
|
||||
AND cb.community_id = pav.community_id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cf.community_id = pav.community_id
|
||||
LEFT JOIN post_read pr ON u.id = pr.user_id
|
||||
AND pr.post_id = pav.id
|
||||
LEFT JOIN post_saved ps ON u.id = ps.user_id
|
||||
AND ps.post_id = pav.id
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND pav.id = pl.post_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
pav.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
post_aggregates_view pav;
|
||||
|
||||
create view post_fast_view as
|
||||
select
|
||||
pav.*,
|
||||
us.id as user_id,
|
||||
us.user_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_read::bool as read,
|
||||
us.is_saved::bool as saved
|
||||
from post_aggregates_fast pav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id,
|
||||
coalesce(cf.community_id, 0) as is_subbed,
|
||||
coalesce(pr.post_id, 0) as is_read,
|
||||
coalesce(ps.post_id, 0) as is_saved,
|
||||
coalesce(pl.score, 0) as user_vote
|
||||
from user_ u
|
||||
left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
|
||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
|
||||
left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
|
||||
left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
|
||||
left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
|
||||
) as us
|
||||
CREATE TABLE post_aggregates_fast AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
post_aggregates_view;
|
||||
|
||||
union all
|
||||
ALTER TABLE post_aggregates_fast
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
CREATE VIEW post_fast_view AS
|
||||
SELECT
|
||||
pav.*,
|
||||
us.id AS user_id,
|
||||
us.user_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_read::bool AS read,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
post_aggregates_fast pav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id,
|
||||
coalesce(cf.community_id, 0) AS is_subbed,
|
||||
coalesce(pr.post_id, 0) AS is_read,
|
||||
coalesce(ps.post_id, 0) AS is_saved,
|
||||
coalesce(pl.score, 0) AS user_vote
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN community_user_ban cb ON u.id = cb.user_id
|
||||
AND cb.community_id = pav.community_id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cf.community_id = pav.community_id
|
||||
LEFT JOIN post_read pr ON u.id = pr.user_id
|
||||
AND pr.post_id = pav.id
|
||||
LEFT JOIN post_saved ps ON u.id = ps.user_id
|
||||
AND ps.post_id = pav.id
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND pav.id = pl.post_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
pav.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
post_aggregates_fast pav;
|
||||
|
||||
select
|
||||
pav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from post_aggregates_fast pav;
|
|
@ -1,116 +1,146 @@
|
|||
drop view user_mention_view;
|
||||
drop view reply_fast_view;
|
||||
drop view comment_fast_view;
|
||||
drop view comment_view;
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
drop view user_mention_fast_view;
|
||||
drop table comment_aggregates_fast;
|
||||
drop view comment_aggregates_view;
|
||||
DROP VIEW reply_fast_view;
|
||||
|
||||
create view comment_aggregates_view as
|
||||
select
|
||||
ct.*,
|
||||
-- community details
|
||||
p.community_id,
|
||||
c.actor_id as community_actor_id,
|
||||
c."local" as community_local,
|
||||
c."name" as community_name,
|
||||
-- creator details
|
||||
u.banned as banned,
|
||||
coalesce(cb.id, 0)::bool as banned_from_community,
|
||||
u.actor_id as creator_actor_id,
|
||||
u.local as creator_local,
|
||||
u.name as creator_name,
|
||||
u.published as creator_published,
|
||||
u.avatar as creator_avatar,
|
||||
-- score details
|
||||
coalesce(cl.total, 0) as score,
|
||||
coalesce(cl.up, 0) as upvotes,
|
||||
coalesce(cl.down, 0) as downvotes,
|
||||
hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
|
||||
from comment ct
|
||||
left join post p on ct.post_id = p.id
|
||||
left join community c on p.community_id = c.id
|
||||
left join user_ u on ct.creator_id = u.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 (
|
||||
select
|
||||
l.comment_id as id,
|
||||
sum(l.score) as total,
|
||||
count(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;
|
||||
DROP VIEW comment_fast_view;
|
||||
|
||||
create or replace view comment_view as (
|
||||
select
|
||||
cav.*,
|
||||
us.user_id as user_id,
|
||||
us.my_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_saved::bool as saved
|
||||
from comment_aggregates_view cav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
coalesce(cf.id, 0) as is_subbed,
|
||||
coalesce(cs.id, 0) as is_saved
|
||||
from user_ u
|
||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
||||
) as us
|
||||
DROP VIEW comment_view;
|
||||
|
||||
union all
|
||||
DROP VIEW user_mention_fast_view;
|
||||
|
||||
select
|
||||
DROP TABLE comment_aggregates_fast;
|
||||
|
||||
DROP VIEW comment_aggregates_view;
|
||||
|
||||
CREATE VIEW comment_aggregates_view AS
|
||||
SELECT
|
||||
ct.*,
|
||||
-- community details
|
||||
p.community_id,
|
||||
c.actor_id AS community_actor_id,
|
||||
c."local" AS community_local,
|
||||
c."name" AS community_name,
|
||||
-- creator details
|
||||
u.banned AS banned,
|
||||
coalesce(cb.id, 0)::bool AS banned_from_community,
|
||||
u.actor_id AS creator_actor_id,
|
||||
u.local AS creator_local,
|
||||
u.name AS creator_name,
|
||||
u.published AS creator_published,
|
||||
u.avatar AS creator_avatar,
|
||||
-- score details
|
||||
coalesce(cl.total, 0) AS score,
|
||||
coalesce(cl.up, 0) AS upvotes,
|
||||
coalesce(cl.down, 0) AS downvotes,
|
||||
hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
|
||||
FROM
|
||||
comment ct
|
||||
LEFT JOIN post p ON ct.post_id = p.id
|
||||
LEFT JOIN community c ON p.community_id = c.id
|
||||
LEFT JOIN user_ u ON ct.creator_id = u.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 (
|
||||
SELECT
|
||||
l.comment_id AS id,
|
||||
sum(l.score) AS total,
|
||||
count(
|
||||
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 (
|
||||
SELECT
|
||||
cav.*,
|
||||
us.user_id AS user_id,
|
||||
us.my_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
comment_aggregates_view cav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
coalesce(cf.id, 0) AS is_subbed,
|
||||
coalesce(cs.id, 0) AS is_saved
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND cav.id = cl.comment_id
|
||||
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||
AND cs.comment_id = cav.id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cav.community_id = cf.community_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from comment_aggregates_view cav
|
||||
);
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS saved
|
||||
FROM
|
||||
comment_aggregates_view cav);
|
||||
|
||||
create table comment_aggregates_fast as select * from comment_aggregates_view;
|
||||
alter table comment_aggregates_fast add primary key (id);
|
||||
CREATE TABLE comment_aggregates_fast AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
comment_aggregates_view;
|
||||
|
||||
create view comment_fast_view as
|
||||
select
|
||||
cav.*,
|
||||
us.user_id as user_id,
|
||||
us.my_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_saved::bool as saved
|
||||
from comment_aggregates_fast cav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
coalesce(cf.id, 0) as is_subbed,
|
||||
coalesce(cs.id, 0) as is_saved
|
||||
from user_ u
|
||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
||||
) as us
|
||||
ALTER TABLE comment_aggregates_fast
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
CREATE VIEW comment_fast_view AS
|
||||
SELECT
|
||||
cav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from comment_aggregates_fast cav;
|
||||
us.user_id AS user_id,
|
||||
us.my_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
comment_aggregates_fast cav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
coalesce(cf.id, 0) AS is_subbed,
|
||||
coalesce(cs.id, 0) AS is_saved
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND cav.id = cl.comment_id
|
||||
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||
AND cs.comment_id = cav.id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cav.community_id = cf.community_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cav.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS saved
|
||||
FROM
|
||||
comment_aggregates_fast cav;
|
||||
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.creator_actor_id,
|
||||
c.creator_local,
|
||||
|
@ -138,15 +168,30 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
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
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.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
|
||||
FROM
|
||||
user_mention um,
|
||||
comment_view c
|
||||
WHERE
|
||||
um.comment_id = c.id;
|
||||
|
||||
create view user_mention_fast_view as
|
||||
select
|
||||
CREATE VIEW user_mention_fast_view AS
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.creator_actor_id,
|
||||
ac.creator_local,
|
||||
|
@ -170,26 +215,45 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
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,
|
||||
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,
|
||||
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
|
||||
from user_ u
|
||||
cross join (
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_fast ca
|
||||
) ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
(
|
||||
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
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
comment_aggregates_fast ca) ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.creator_actor_id,
|
||||
ac.creator_local,
|
||||
|
@ -213,37 +277,60 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved,
|
||||
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
|
||||
from comment_aggregates_fast ac
|
||||
left join user_mention um on um.comment_id = ac.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
|
||||
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
|
||||
create view reply_fast_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_fast_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_fast_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_fast_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
|
|
@ -1,118 +1,148 @@
|
|||
drop view user_mention_view;
|
||||
drop view reply_fast_view;
|
||||
drop view comment_fast_view;
|
||||
drop view comment_view;
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
drop view user_mention_fast_view;
|
||||
drop table comment_aggregates_fast;
|
||||
drop view comment_aggregates_view;
|
||||
DROP VIEW reply_fast_view;
|
||||
|
||||
create view comment_aggregates_view as
|
||||
select
|
||||
ct.*,
|
||||
-- post details
|
||||
p."name" as post_name,
|
||||
p.community_id,
|
||||
-- community details
|
||||
c.actor_id as community_actor_id,
|
||||
c."local" as community_local,
|
||||
c."name" as community_name,
|
||||
-- creator details
|
||||
u.banned as banned,
|
||||
coalesce(cb.id, 0)::bool as banned_from_community,
|
||||
u.actor_id as creator_actor_id,
|
||||
u.local as creator_local,
|
||||
u.name as creator_name,
|
||||
u.published as creator_published,
|
||||
u.avatar as creator_avatar,
|
||||
-- score details
|
||||
coalesce(cl.total, 0) as score,
|
||||
coalesce(cl.up, 0) as upvotes,
|
||||
coalesce(cl.down, 0) as downvotes,
|
||||
hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
|
||||
from comment ct
|
||||
left join post p on ct.post_id = p.id
|
||||
left join community c on p.community_id = c.id
|
||||
left join user_ u on ct.creator_id = u.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 (
|
||||
select
|
||||
l.comment_id as id,
|
||||
sum(l.score) as total,
|
||||
count(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;
|
||||
DROP VIEW comment_fast_view;
|
||||
|
||||
create or replace view comment_view as (
|
||||
select
|
||||
cav.*,
|
||||
us.user_id as user_id,
|
||||
us.my_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_saved::bool as saved
|
||||
from comment_aggregates_view cav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
coalesce(cf.id, 0) as is_subbed,
|
||||
coalesce(cs.id, 0) as is_saved
|
||||
from user_ u
|
||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
||||
) as us
|
||||
DROP VIEW comment_view;
|
||||
|
||||
union all
|
||||
DROP VIEW user_mention_fast_view;
|
||||
|
||||
select
|
||||
DROP TABLE comment_aggregates_fast;
|
||||
|
||||
DROP VIEW comment_aggregates_view;
|
||||
|
||||
CREATE VIEW comment_aggregates_view AS
|
||||
SELECT
|
||||
ct.*,
|
||||
-- post details
|
||||
p."name" AS post_name,
|
||||
p.community_id,
|
||||
-- community details
|
||||
c.actor_id AS community_actor_id,
|
||||
c."local" AS community_local,
|
||||
c."name" AS community_name,
|
||||
-- creator details
|
||||
u.banned AS banned,
|
||||
coalesce(cb.id, 0)::bool AS banned_from_community,
|
||||
u.actor_id AS creator_actor_id,
|
||||
u.local AS creator_local,
|
||||
u.name AS creator_name,
|
||||
u.published AS creator_published,
|
||||
u.avatar AS creator_avatar,
|
||||
-- score details
|
||||
coalesce(cl.total, 0) AS score,
|
||||
coalesce(cl.up, 0) AS upvotes,
|
||||
coalesce(cl.down, 0) AS downvotes,
|
||||
hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
|
||||
FROM
|
||||
comment ct
|
||||
LEFT JOIN post p ON ct.post_id = p.id
|
||||
LEFT JOIN community c ON p.community_id = c.id
|
||||
LEFT JOIN user_ u ON ct.creator_id = u.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 (
|
||||
SELECT
|
||||
l.comment_id AS id,
|
||||
sum(l.score) AS total,
|
||||
count(
|
||||
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 (
|
||||
SELECT
|
||||
cav.*,
|
||||
us.user_id AS user_id,
|
||||
us.my_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
comment_aggregates_view cav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
coalesce(cf.id, 0) AS is_subbed,
|
||||
coalesce(cs.id, 0) AS is_saved
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND cav.id = cl.comment_id
|
||||
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||
AND cs.comment_id = cav.id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cav.community_id = cf.community_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from comment_aggregates_view cav
|
||||
);
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS saved
|
||||
FROM
|
||||
comment_aggregates_view cav);
|
||||
|
||||
create table comment_aggregates_fast as select * from comment_aggregates_view;
|
||||
alter table comment_aggregates_fast add primary key (id);
|
||||
CREATE TABLE comment_aggregates_fast AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
comment_aggregates_view;
|
||||
|
||||
create view comment_fast_view as
|
||||
select
|
||||
cav.*,
|
||||
us.user_id as user_id,
|
||||
us.my_vote as my_vote,
|
||||
us.is_subbed::bool as subscribed,
|
||||
us.is_saved::bool as saved
|
||||
from comment_aggregates_fast cav
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
coalesce(cf.id, 0) as is_subbed,
|
||||
coalesce(cs.id, 0) as is_saved
|
||||
from user_ u
|
||||
left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
|
||||
left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
|
||||
left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
|
||||
) as us
|
||||
ALTER TABLE comment_aggregates_fast
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
CREATE VIEW comment_fast_view AS
|
||||
SELECT
|
||||
cav.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as saved
|
||||
from comment_aggregates_fast cav;
|
||||
us.user_id AS user_id,
|
||||
us.my_vote AS my_vote,
|
||||
us.is_subbed::bool AS subscribed,
|
||||
us.is_saved::bool AS saved
|
||||
FROM
|
||||
comment_aggregates_fast cav
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
coalesce(cf.id, 0) AS is_subbed,
|
||||
coalesce(cs.id, 0) AS is_saved
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND cav.id = cl.comment_id
|
||||
LEFT JOIN comment_saved cs ON u.id = cs.user_id
|
||||
AND cs.comment_id = cav.id
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cav.community_id = cf.community_id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cav.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS saved
|
||||
FROM
|
||||
comment_aggregates_fast cav;
|
||||
|
||||
create view user_mention_view as
|
||||
select
|
||||
CREATE VIEW user_mention_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
c.creator_id,
|
||||
c.creator_actor_id,
|
||||
c.creator_local,
|
||||
|
@ -141,15 +171,30 @@ select
|
|||
c.my_vote,
|
||||
c.saved,
|
||||
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
|
||||
from user_mention um, comment_view c
|
||||
where um.comment_id = c.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
|
||||
FROM
|
||||
user_mention um,
|
||||
comment_view c
|
||||
WHERE
|
||||
um.comment_id = c.id;
|
||||
|
||||
create view user_mention_fast_view as
|
||||
select
|
||||
CREATE VIEW user_mention_fast_view AS
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.creator_actor_id,
|
||||
ac.creator_local,
|
||||
|
@ -174,26 +219,45 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
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,
|
||||
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,
|
||||
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
|
||||
from user_ u
|
||||
cross join (
|
||||
select
|
||||
ca.*
|
||||
from comment_aggregates_fast ca
|
||||
) ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
left join user_mention um on um.comment_id = ac.id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
(
|
||||
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
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
comment_aggregates_fast ca) ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
LEFT JOIN user_mention um ON um.comment_id = ac.id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.id,
|
||||
um.id as user_mention_id,
|
||||
um.id AS user_mention_id,
|
||||
ac.creator_id,
|
||||
ac.creator_actor_id,
|
||||
ac.creator_local,
|
||||
|
@ -218,37 +282,60 @@ select
|
|||
ac.upvotes,
|
||||
ac.downvotes,
|
||||
ac.hot_rank,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved,
|
||||
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
|
||||
from comment_aggregates_fast ac
|
||||
left join user_mention um on um.comment_id = ac.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
|
||||
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
|
||||
create view reply_fast_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_fast_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_fast_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
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 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';
|
||||
ALTER TABLE community
|
||||
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
|
||||
set actor_id = 'http://fake.com'
|
||||
where actor_id like 'changeme_%';
|
||||
ALTER TABLE user_
|
||||
ALTER COLUMN actor_id SET DEFAULT 'http://fake.com';
|
||||
|
||||
update user_
|
||||
set actor_id = 'http://fake.com'
|
||||
where actor_id like 'changeme_%';
|
||||
DROP FUNCTION generate_unique_changeme;
|
||||
|
||||
drop index idx_user_lower_actor_id;
|
||||
create unique index idx_user_name_lower_actor_id on user_ (lower(name), lower(actor_id));
|
||||
UPDATE
|
||||
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
|
||||
|
||||
-- Creating a unique changeme actor_id
|
||||
create or replace function generate_unique_changeme()
|
||||
returns text language sql
|
||||
as $$
|
||||
select 'changeme_' || string_agg (substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil (random() * 62)::integer, 1), '')
|
||||
from generate_series(1, 20)
|
||||
CREATE OR REPLACE FUNCTION generate_unique_changeme ()
|
||||
RETURNS text
|
||||
LANGUAGE sql
|
||||
AS $$
|
||||
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
|
||||
-- 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);
|
||||
delete from community a using (
|
||||
select min(id) as id, actor_id
|
||||
from community
|
||||
group by actor_id having count(*) > 1
|
||||
) b
|
||||
where a.actor_id = b.actor_id
|
||||
and a.id <> b.id;
|
||||
DELETE FROM community a USING (
|
||||
SELECT
|
||||
min(id) AS id,
|
||||
actor_id
|
||||
FROM
|
||||
community
|
||||
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 (
|
||||
select min(id) as id, actor_id
|
||||
from user_
|
||||
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 (
|
||||
SELECT
|
||||
min(id) AS id,
|
||||
actor_id
|
||||
FROM
|
||||
user_
|
||||
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
|
||||
update community
|
||||
set actor_id = generate_unique_changeme()
|
||||
where actor_id = 'http://fake.com';
|
||||
UPDATE
|
||||
community
|
||||
SET
|
||||
actor_id = generate_unique_changeme ()
|
||||
WHERE
|
||||
actor_id = 'http://fake.com';
|
||||
|
||||
update user_
|
||||
set actor_id = generate_unique_changeme()
|
||||
where actor_id = 'http://fake.com';
|
||||
UPDATE
|
||||
user_
|
||||
SET
|
||||
actor_id = generate_unique_changeme ()
|
||||
WHERE
|
||||
actor_id = 'http://fake.com';
|
||||
|
||||
-- Add the unique indexes
|
||||
alter table community alter column actor_id set not null;
|
||||
alter table community alter column actor_id set default generate_unique_changeme();
|
||||
ALTER TABLE community
|
||||
ALTER COLUMN actor_id SET NOT NULL;
|
||||
|
||||
alter table user_ alter column actor_id set not null;
|
||||
alter table user_ alter column actor_id set default generate_unique_changeme();
|
||||
ALTER TABLE community
|
||||
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
|
||||
drop index idx_user_name_lower_actor_id;
|
||||
create unique index idx_user_lower_actor_id on user_ (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_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 view community_view;
|
||||
drop view community_aggregates_view;
|
||||
drop view community_fast_view;
|
||||
drop table community_aggregates_fast;
|
||||
DROP VIEW community_view;
|
||||
|
||||
create view community_aggregates_view as
|
||||
select
|
||||
DROP VIEW community_aggregates_view;
|
||||
|
||||
DROP VIEW community_fast_view;
|
||||
|
||||
DROP TABLE community_aggregates_fast;
|
||||
|
||||
CREATE VIEW community_aggregates_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
c.name,
|
||||
c.title,
|
||||
|
@ -22,79 +25,96 @@ select
|
|||
c.actor_id,
|
||||
c.local,
|
||||
c.last_refreshed_at,
|
||||
u.actor_id as creator_actor_id,
|
||||
u.local as creator_local,
|
||||
u.name as creator_name,
|
||||
u.preferred_username as creator_preferred_username,
|
||||
u.avatar as creator_avatar,
|
||||
cat.name as category_name,
|
||||
coalesce(cf.subs, 0) as number_of_subscribers,
|
||||
coalesce(cd.posts, 0) as number_of_posts,
|
||||
coalesce(cd.comments, 0) as number_of_comments,
|
||||
hot_rank(cf.subs, c.published) as hot_rank
|
||||
from community c
|
||||
left join user_ u on c.creator_id = u.id
|
||||
left join category cat on c.category_id = cat.id
|
||||
left join (
|
||||
select
|
||||
p.community_id,
|
||||
count(distinct p.id) as posts,
|
||||
count(distinct ct.id) as comments
|
||||
from post p
|
||||
join comment ct on p.id = ct.post_id
|
||||
group by p.community_id
|
||||
) cd on cd.community_id = c.id
|
||||
left join (
|
||||
select
|
||||
community_id,
|
||||
count(*) as subs
|
||||
from community_follower
|
||||
group by community_id
|
||||
) cf on cf.community_id = c.id;
|
||||
u.actor_id AS creator_actor_id,
|
||||
u.local AS creator_local,
|
||||
u.name AS creator_name,
|
||||
u.preferred_username AS creator_preferred_username,
|
||||
u.avatar AS creator_avatar,
|
||||
cat.name AS category_name,
|
||||
coalesce(cf.subs, 0) AS number_of_subscribers,
|
||||
coalesce(cd.posts, 0) AS number_of_posts,
|
||||
coalesce(cd.comments, 0) AS number_of_comments,
|
||||
hot_rank (cf.subs, c.published) AS hot_rank
|
||||
FROM
|
||||
community c
|
||||
LEFT JOIN user_ u ON c.creator_id = u.id
|
||||
LEFT JOIN category cat ON c.category_id = cat.id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
p.community_id,
|
||||
count(DISTINCT p.id) AS posts,
|
||||
count(DISTINCT ct.id) AS comments
|
||||
FROM
|
||||
post p
|
||||
JOIN comment ct ON p.id = ct.post_id
|
||||
GROUP BY
|
||||
p.community_id) cd ON cd.community_id = c.id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
community_id,
|
||||
count(*) AS subs
|
||||
FROM
|
||||
community_follower
|
||||
GROUP BY
|
||||
community_id) cf ON cf.community_id = c.id;
|
||||
|
||||
create view community_view as
|
||||
select
|
||||
CREATE VIEW community_view AS
|
||||
SELECT
|
||||
cv.*,
|
||||
us.user as user_id,
|
||||
us.is_subbed::bool as subscribed
|
||||
from community_aggregates_view cv
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user,
|
||||
coalesce(cf.community_id, 0) as is_subbed
|
||||
from user_ u
|
||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = cv.id
|
||||
) as us
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
us.user AS user_id,
|
||||
us.is_subbed::bool AS subscribed
|
||||
FROM
|
||||
community_aggregates_view cv
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user,
|
||||
coalesce(cf.community_id, 0) AS is_subbed
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cf.community_id = cv.id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cv.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from community_aggregates_view cv;
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
community_aggregates_view cv;
|
||||
|
||||
-- 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 add primary key (id);
|
||||
ALTER TABLE community_aggregates_fast
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
create view community_fast_view as
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join (
|
||||
select
|
||||
ca.*
|
||||
from community_aggregates_fast ca
|
||||
) ac
|
||||
CREATE VIEW community_fast_view AS
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
community_aggregates_fast ca) ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
caf.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
community_aggregates_fast caf;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
caf.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from community_aggregates_fast caf;
|
|
@ -1,11 +1,14 @@
|
|||
-- Drop first
|
||||
drop view community_view;
|
||||
drop view community_aggregates_view;
|
||||
drop view community_fast_view;
|
||||
drop table community_aggregates_fast;
|
||||
DROP VIEW community_view;
|
||||
|
||||
create view community_aggregates_view as
|
||||
select
|
||||
DROP VIEW community_aggregates_view;
|
||||
|
||||
DROP VIEW community_fast_view;
|
||||
|
||||
DROP TABLE community_aggregates_fast;
|
||||
|
||||
CREATE VIEW community_aggregates_view AS
|
||||
SELECT
|
||||
c.id,
|
||||
c.name,
|
||||
c.title,
|
||||
|
@ -22,79 +25,96 @@ select
|
|||
c.actor_id,
|
||||
c.local,
|
||||
c.last_refreshed_at,
|
||||
u.actor_id as creator_actor_id,
|
||||
u.local as creator_local,
|
||||
u.name as creator_name,
|
||||
u.preferred_username as creator_preferred_username,
|
||||
u.avatar as creator_avatar,
|
||||
cat.name as category_name,
|
||||
coalesce(cf.subs, 0) as number_of_subscribers,
|
||||
coalesce(cd.posts, 0) as number_of_posts,
|
||||
coalesce(cd.comments, 0) as number_of_comments,
|
||||
hot_rank(cf.subs, c.published) as hot_rank
|
||||
from community c
|
||||
left join user_ u on c.creator_id = u.id
|
||||
left join category cat on c.category_id = cat.id
|
||||
left join (
|
||||
select
|
||||
p.community_id,
|
||||
count(distinct p.id) as posts,
|
||||
count(distinct ct.id) as comments
|
||||
from post p
|
||||
left join comment ct on p.id = ct.post_id
|
||||
group by p.community_id
|
||||
) cd on cd.community_id = c.id
|
||||
left join (
|
||||
select
|
||||
community_id,
|
||||
count(*) as subs
|
||||
from community_follower
|
||||
group by community_id
|
||||
) cf on cf.community_id = c.id;
|
||||
u.actor_id AS creator_actor_id,
|
||||
u.local AS creator_local,
|
||||
u.name AS creator_name,
|
||||
u.preferred_username AS creator_preferred_username,
|
||||
u.avatar AS creator_avatar,
|
||||
cat.name AS category_name,
|
||||
coalesce(cf.subs, 0) AS number_of_subscribers,
|
||||
coalesce(cd.posts, 0) AS number_of_posts,
|
||||
coalesce(cd.comments, 0) AS number_of_comments,
|
||||
hot_rank (cf.subs, c.published) AS hot_rank
|
||||
FROM
|
||||
community c
|
||||
LEFT JOIN user_ u ON c.creator_id = u.id
|
||||
LEFT JOIN category cat ON c.category_id = cat.id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
p.community_id,
|
||||
count(DISTINCT p.id) AS posts,
|
||||
count(DISTINCT ct.id) AS comments
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN comment ct ON p.id = ct.post_id
|
||||
GROUP BY
|
||||
p.community_id) cd ON cd.community_id = c.id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
community_id,
|
||||
count(*) AS subs
|
||||
FROM
|
||||
community_follower
|
||||
GROUP BY
|
||||
community_id) cf ON cf.community_id = c.id;
|
||||
|
||||
create view community_view as
|
||||
select
|
||||
CREATE VIEW community_view AS
|
||||
SELECT
|
||||
cv.*,
|
||||
us.user as user_id,
|
||||
us.is_subbed::bool as subscribed
|
||||
from community_aggregates_view cv
|
||||
cross join lateral (
|
||||
select
|
||||
u.id as user,
|
||||
coalesce(cf.community_id, 0) as is_subbed
|
||||
from user_ u
|
||||
left join community_follower cf on u.id = cf.user_id and cf.community_id = cv.id
|
||||
) as us
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
us.user AS user_id,
|
||||
us.is_subbed::bool AS subscribed
|
||||
FROM
|
||||
community_aggregates_view cv
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT
|
||||
u.id AS user,
|
||||
coalesce(cf.community_id, 0) AS is_subbed
|
||||
FROM
|
||||
user_ u
|
||||
LEFT JOIN community_follower cf ON u.id = cf.user_id
|
||||
AND cf.community_id = cv.id) AS us
|
||||
UNION ALL
|
||||
SELECT
|
||||
cv.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from community_aggregates_view cv;
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
community_aggregates_view cv;
|
||||
|
||||
-- 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 add primary key (id);
|
||||
ALTER TABLE community_aggregates_fast
|
||||
ADD PRIMARY KEY (id);
|
||||
|
||||
create view community_fast_view as
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join (
|
||||
select
|
||||
ca.*
|
||||
from community_aggregates_fast ca
|
||||
) ac
|
||||
CREATE VIEW community_fast_view AS
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN (
|
||||
SELECT
|
||||
ca.*
|
||||
FROM
|
||||
community_aggregates_fast ca) ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
caf.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
community_aggregates_fast caf;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
caf.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from community_aggregates_fast caf;
|
|
@ -1,27 +1,55 @@
|
|||
-- Drop the uniques
|
||||
alter table private_message drop constraint idx_private_message_ap_id;
|
||||
alter table post drop constraint idx_post_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
|
||||
DROP CONSTRAINT idx_private_message_ap_id;
|
||||
|
||||
alter table private_message alter column ap_id set not null;
|
||||
alter table private_message alter column ap_id set default 'http://fake.com';
|
||||
ALTER TABLE post
|
||||
DROP CONSTRAINT idx_post_ap_id;
|
||||
|
||||
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
|
||||
DROP CONSTRAINT idx_comment_ap_id;
|
||||
|
||||
alter table comment alter column ap_id set not null;
|
||||
alter table comment alter column ap_id set default 'http://fake.com';
|
||||
ALTER TABLE user_
|
||||
DROP CONSTRAINT idx_user_actor_id;
|
||||
|
||||
update private_message
|
||||
set ap_id = 'http://fake.com'
|
||||
where ap_id like 'changeme_%';
|
||||
ALTER TABLE community
|
||||
DROP CONSTRAINT idx_community_actor_id;
|
||||
|
||||
update post
|
||||
set ap_id = 'http://fake.com'
|
||||
where ap_id like 'changeme_%';
|
||||
ALTER TABLE private_message
|
||||
ALTER COLUMN ap_id SET NOT NULL;
|
||||
|
||||
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