lemmy/server/migrations/2020-01-21-001001_create_private_message/up.sql

91 lines
2.9 KiB
MySQL
Raw Normal View History

Squashed commit of the following: commit f5b75f342bdc35b6c770358b2408a7ffc9ce80ad Merge: bd1fc2b 69389f6 Author: Dessalines <happydooby@gmail.com> Date: Thu Jan 23 19:17:42 2020 -0500 Done merging http-api and private_message commit bd1fc2b80b8c6a42f0df721d91863b8688b339ee Author: Dessalines <happydooby@gmail.com> Date: Thu Jan 23 16:46:07 2020 -0500 Remove danger from private-message.tsx commit 69389f61c9944319b5c71d7fb398294f1f8b8e7c Author: Dessalines <happydooby@gmail.com> Date: Thu Jan 23 11:21:21 2020 -0500 Fixing http curl POST docs. commit 7fdcae4f0739df79cbf8d7ea2138519d87f2aa32 Merge: dbe9ad0 752318f Author: Dessalines <happydooby@gmail.com> Date: Thu Jan 23 11:01:06 2020 -0500 Merge remote-tracking branch 'nutomic/http-api' into dessalines-http-api commit 752318fdf3cf95633744e89bbfe22a761fecc53d Author: Felix <me@nutomic.com> Date: Thu Jan 23 15:22:17 2020 +0100 api fixes commit 9ccff18f23509d309261670b57563b87e8f61f77 Author: Dessalines <happydooby@gmail.com> Date: Wed Jan 22 22:29:11 2020 -0500 Adding a toaster to replace alerts. Fixes #457 commit 5197407dd29a895fff37d4f0982e44d5e727d426 Merge: bacb9ac 58f673a Author: Dessalines <happydooby@gmail.com> Date: Wed Jan 22 21:20:38 2020 -0500 Merge branch 'private_messaging' into dev commit 58f673ab7856d2380cfaedbc46baf944fedfabb5 Author: Dessalines <happydooby@gmail.com> Date: Wed Jan 22 21:09:17 2020 -0500 Adding message to comment node actions. commit bacb9ac59ed3a6d8b4a80f37103ccf9c3ab751c9 Merge: 10c6505 7d3adda Author: Dessalines <happydooby@gmail.com> Date: Wed Jan 22 20:37:08 2020 -0500 Merge branch 'private_messaging' into dev commit 10c65059686ff74fc2ce412572e2d6fc3497c43a Author: Dessalines <happydooby@gmail.com> Date: Wed Jan 22 20:35:20 2020 -0500 Adding correct hello_name to mail. commit 7d3adda0cd4888e70b362cbad1b82f46d79c8d4c Author: Dessalines <happydooby@gmail.com> Date: Wed Jan 22 16:35:29 2020 -0500 Adding private messaging, and matrix user ids. - Fixes #244 commit dbe9ad0998b80d174130d82c11473555c7d6bc0e Author: Dessalines <happydooby@gmail.com> Date: Mon Jan 20 18:49:54 2020 -0500 Fixing last. commit 20c9c54806429395f8a4c1a4e7a6a0f677d4e6b8 Author: Dessalines <happydooby@gmail.com> Date: Sun Jan 19 13:31:37 2020 -0500 Updating API docs. commit dc84ccaac94b9887f8512e3c32aeea0df89520be Merge: 6c61dd2 3edd75e Author: Dessalines <happydooby@gmail.com> Date: Sun Jan 19 10:06:25 2020 -0500 Merge branch 'master' into dessalines-http-api commit 6c61dd266bb6e389312ec56e7fc50f4a6b71b374 Merge: c5eecd0 e518954 Author: Dessalines <happydooby@gmail.com> Date: Sun Jan 19 09:09:00 2020 -0500 Merge remote-tracking branch 'nutomic/websocket-generics' into dessalines-http-api commit e518954bcab2553906cec9f2bcfa6d1b5fdc2f38 Author: Felix <me@nutomic.com> Date: Sun Jan 19 14:25:50 2020 +0100 Use generics to reduce code duplication in websocket commit c5eecd055e79510421aebe26e5d4f173a5916a3e Author: Dessalines <happydooby@gmail.com> Date: Sun Jan 19 00:38:45 2020 -0500 Strongly typing WebsocketJsonResponse. Forgot comment-form.tsx commit 0c5eb471359929c532a4e6a63477a39ab6db67b6 Author: Dessalines <happydooby@gmail.com> Date: Sat Jan 18 23:54:10 2020 -0500 First pass at fixing UI to work with new websocketresponses. commit baf77bb6be88e796683bc21fd16a0359e68c7791 Author: Felix <me@nutomic.com> Date: Sat Jan 18 17:25:45 2020 +0100 simplify json serialization code commit 047ec97e1857888aeeac53629a76a4dc7e2f110b Author: Felix <me@nutomic.com> Date: Sat Jan 18 14:22:25 2020 +0100 rewrite api endpoint urls commit 2fb4900b0cf586674d6212e0aa2c1b852d104a1a Author: Felix <me@nutomic.com> Date: Thu Jan 16 17:04:37 2020 +0100 fix typo commit cba80815797b6578ab817b99fee35575cccef27d Author: Felix <me@nutomic.com> Date: Thu Jan 16 16:47:38 2020 +0100 fix formatting commit d7285d8c25c9a88c33519ec12f3c730d6b45fcf5 Author: Felix <me@nutomic.com> Date: Thu Jan 16 16:09:01 2020 +0100 small fix commit 415040a1e9b90fcb71872a41fbb6e7e7131d966d Author: Felix <me@nutomic.com> Date: Thu Jan 16 15:39:08 2020 +0100 working! commit 7a97c981a0b74cf6f66574690e58917a47508395 Author: Felix <me@nutomic.com> Date: Wed Jan 15 16:48:21 2020 +0100 try to simplify code with higher order functions commit c41082f98f459da2cc48dcfd7d9d5c2ac08865d0 Author: Felix <me@nutomic.com> Date: Wed Jan 15 16:37:25 2020 +0100 Implement HTTP API using generics (fixes #380)
2020-01-24 00:39:59 +00:00
-- 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 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 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 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 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;
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;
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,
-- content text not null,
-- deleted boolean default false not null,
-- 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,
-- recipient_id int references user_ on update cascade on delete cascade not null,
-- read boolean default false not null,
-- published timestamp not null default now(),
-- unique(private_message_id, recipient_id)
-- )