2021-02-25 19:04:12 +00:00
|
|
|
-- post_saved
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE post_saved RENAME COLUMN person_id TO user_id;
|
|
|
|
|
|
|
|
ALTER TABLE post_saved RENAME CONSTRAINT post_saved_post_id_person_id_key TO post_saved_post_id_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE post_saved RENAME CONSTRAINT post_saved_person_id_fkey TO post_saved_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- post_read
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE post_read RENAME COLUMN person_id TO user_id;
|
|
|
|
|
|
|
|
ALTER TABLE post_read RENAME CONSTRAINT post_read_post_id_person_id_key TO post_read_post_id_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE post_read RENAME CONSTRAINT post_read_person_id_fkey TO post_read_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- post_like
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE post_like RENAME COLUMN person_id TO user_id;
|
|
|
|
|
|
|
|
ALTER INDEX idx_post_like_person RENAME TO idx_post_like_user;
|
|
|
|
|
|
|
|
ALTER TABLE post_like RENAME CONSTRAINT post_like_post_id_person_id_key TO post_like_post_id_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE post_like RENAME CONSTRAINT post_like_person_id_fkey TO post_like_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- password_reset_request
|
2023-08-02 16:44:51 +00:00
|
|
|
DELETE FROM password_reset_request;
|
|
|
|
|
|
|
|
ALTER TABLE password_reset_request
|
|
|
|
DROP COLUMN local_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE password_reset_request
|
|
|
|
ADD COLUMN user_id integer NOT NULL REFERENCES person (id) ON UPDATE CASCADE ON DELETE CASCADE;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- mod_sticky_post
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE mod_sticky_post RENAME COLUMN mod_person_id TO mod_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_sticky_post RENAME CONSTRAINT mod_sticky_post_mod_person_id_fkey TO mod_sticky_post_mod_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- mod_remove_post
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE mod_remove_post RENAME COLUMN mod_person_id TO mod_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_remove_post RENAME CONSTRAINT mod_remove_post_mod_person_id_fkey TO mod_remove_post_mod_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- mod_remove_community
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE mod_remove_community RENAME COLUMN mod_person_id TO mod_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_remove_community RENAME CONSTRAINT mod_remove_community_mod_person_id_fkey TO mod_remove_community_mod_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- mod_remove_comment
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE mod_remove_comment RENAME COLUMN mod_person_id TO mod_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_remove_comment RENAME CONSTRAINT mod_remove_comment_mod_person_id_fkey TO mod_remove_comment_mod_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- mod_lock_post
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE mod_lock_post RENAME COLUMN mod_person_id TO mod_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_lock_post RENAME CONSTRAINT mod_lock_post_mod_person_id_fkey TO mod_lock_post_mod_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- mod_add_community
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE mod_ban_from_community RENAME COLUMN mod_person_id TO mod_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_ban_from_community RENAME COLUMN other_person_id TO other_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_ban_from_community RENAME CONSTRAINT mod_ban_from_community_mod_person_id_fkey TO mod_ban_from_community_mod_user_id_fkey;
|
|
|
|
|
|
|
|
ALTER TABLE mod_ban_from_community RENAME CONSTRAINT mod_ban_from_community_other_person_id_fkey TO mod_ban_from_community_other_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- mod_ban
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE mod_ban RENAME COLUMN mod_person_id TO mod_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_ban RENAME COLUMN other_person_id TO other_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_ban RENAME CONSTRAINT mod_ban_mod_person_id_fkey TO mod_ban_mod_user_id_fkey;
|
|
|
|
|
|
|
|
ALTER TABLE mod_ban RENAME CONSTRAINT mod_ban_other_person_id_fkey TO mod_ban_other_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- mod_add_community
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE mod_add_community RENAME COLUMN mod_person_id TO mod_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_add_community RENAME COLUMN other_person_id TO other_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_add_community RENAME CONSTRAINT mod_add_community_mod_person_id_fkey TO mod_add_community_mod_user_id_fkey;
|
|
|
|
|
|
|
|
ALTER TABLE mod_add_community RENAME CONSTRAINT mod_add_community_other_person_id_fkey TO mod_add_community_other_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- mod_add
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE mod_add RENAME COLUMN mod_person_id TO mod_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_add RENAME COLUMN other_person_id TO other_user_id;
|
|
|
|
|
|
|
|
ALTER TABLE mod_add RENAME CONSTRAINT mod_add_mod_person_id_fkey TO mod_add_mod_user_id_fkey;
|
|
|
|
|
|
|
|
ALTER TABLE mod_add RENAME CONSTRAINT mod_add_other_person_id_fkey TO mod_add_other_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- community_user_ban
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE community_person_ban RENAME TO community_user_ban;
|
|
|
|
|
|
|
|
ALTER SEQUENCE community_person_ban_id_seq
|
|
|
|
RENAME TO community_user_ban_id_seq;
|
|
|
|
|
|
|
|
ALTER TABLE community_user_ban RENAME COLUMN person_id TO user_id;
|
|
|
|
|
|
|
|
ALTER TABLE community_user_ban RENAME CONSTRAINT community_person_ban_pkey TO community_user_ban_pkey;
|
|
|
|
|
|
|
|
ALTER TABLE community_user_ban RENAME CONSTRAINT community_person_ban_community_id_fkey TO community_user_ban_community_id_fkey;
|
|
|
|
|
|
|
|
ALTER TABLE community_user_ban RENAME CONSTRAINT community_person_ban_community_id_person_id_key TO community_user_ban_community_id_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE community_user_ban RENAME CONSTRAINT community_person_ban_person_id_fkey TO community_user_ban_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- community_moderator
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE community_moderator RENAME COLUMN person_id TO user_id;
|
|
|
|
|
|
|
|
ALTER TABLE community_moderator RENAME CONSTRAINT community_moderator_community_id_person_id_key TO community_moderator_community_id_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE community_moderator RENAME CONSTRAINT community_moderator_person_id_fkey TO community_moderator_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- community_follower
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE community_follower RENAME COLUMN person_id TO user_id;
|
|
|
|
|
|
|
|
ALTER TABLE community_follower RENAME CONSTRAINT community_follower_community_id_person_id_key TO community_follower_community_id_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE community_follower RENAME CONSTRAINT community_follower_person_id_fkey TO community_follower_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- comment_saved
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE comment_saved RENAME COLUMN person_id TO user_id;
|
|
|
|
|
|
|
|
ALTER TABLE comment_saved RENAME CONSTRAINT comment_saved_comment_id_person_id_key TO comment_saved_comment_id_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE comment_saved RENAME CONSTRAINT comment_saved_person_id_fkey TO comment_saved_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
|
|
|
-- comment_like
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE comment_like RENAME COLUMN person_id TO user_id;
|
|
|
|
|
|
|
|
ALTER INDEX idx_comment_like_person RENAME TO idx_comment_like_user;
|
|
|
|
|
|
|
|
ALTER TABLE comment_like RENAME CONSTRAINT comment_like_comment_id_person_id_key TO comment_like_comment_id_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE comment_like RENAME CONSTRAINT comment_like_person_id_fkey TO comment_like_user_id_fkey;
|
2021-02-25 19:04:12 +00:00
|
|
|
|
2021-02-15 19:34:10 +00:00
|
|
|
-- user_ban
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE person_ban RENAME TO user_ban;
|
|
|
|
|
|
|
|
ALTER SEQUENCE person_ban_id_seq
|
|
|
|
RENAME TO user_ban_id_seq;
|
|
|
|
|
|
|
|
ALTER INDEX person_ban_pkey RENAME TO user_ban_pkey;
|
|
|
|
|
|
|
|
ALTER INDEX person_ban_person_id_key RENAME TO user_ban_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE user_ban RENAME COLUMN person_id TO user_id;
|
|
|
|
|
|
|
|
ALTER TABLE user_ban RENAME CONSTRAINT person_ban_person_id_fkey TO user_ban_user_id_fkey;
|
2021-02-15 19:34:10 +00:00
|
|
|
|
|
|
|
-- user_mention
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE person_mention RENAME TO user_mention;
|
|
|
|
|
|
|
|
ALTER SEQUENCE person_mention_id_seq
|
|
|
|
RENAME TO user_mention_id_seq;
|
|
|
|
|
|
|
|
ALTER INDEX person_mention_pkey RENAME TO user_mention_pkey;
|
|
|
|
|
|
|
|
ALTER INDEX person_mention_recipient_id_comment_id_key RENAME TO user_mention_recipient_id_comment_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE user_mention RENAME CONSTRAINT person_mention_comment_id_fkey TO user_mention_comment_id_fkey;
|
|
|
|
|
|
|
|
ALTER TABLE user_mention RENAME CONSTRAINT person_mention_recipient_id_fkey TO user_mention_recipient_id_fkey;
|
2021-02-15 19:34:10 +00:00
|
|
|
|
|
|
|
-- User aggregates table
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE person_aggregates RENAME TO user_aggregates;
|
|
|
|
|
|
|
|
ALTER SEQUENCE person_aggregates_id_seq
|
|
|
|
RENAME TO user_aggregates_id_seq;
|
|
|
|
|
|
|
|
ALTER TABLE user_aggregates RENAME COLUMN person_id TO user_id;
|
2021-02-15 19:34:10 +00:00
|
|
|
|
|
|
|
-- Indexes
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER INDEX person_aggregates_pkey RENAME TO user_aggregates_pkey;
|
|
|
|
|
|
|
|
ALTER INDEX idx_person_aggregates_comment_score RENAME TO idx_user_aggregates_comment_score;
|
|
|
|
|
|
|
|
ALTER INDEX person_aggregates_person_id_key RENAME TO user_aggregates_user_id_key;
|
|
|
|
|
|
|
|
ALTER TABLE user_aggregates RENAME CONSTRAINT person_aggregates_person_id_fkey TO user_aggregates_user_id_fkey;
|
2021-02-15 19:34:10 +00:00
|
|
|
|
|
|
|
-- Redo the user_aggregates table
|
2023-08-02 16:44:51 +00:00
|
|
|
DROP TRIGGER person_aggregates_person ON person;
|
|
|
|
|
|
|
|
DROP TRIGGER person_aggregates_post_count ON post;
|
|
|
|
|
|
|
|
DROP TRIGGER person_aggregates_post_score ON post_like;
|
|
|
|
|
|
|
|
DROP TRIGGER person_aggregates_comment_count ON comment;
|
|
|
|
|
|
|
|
DROP TRIGGER person_aggregates_comment_score ON comment_like;
|
|
|
|
|
|
|
|
DROP FUNCTION person_aggregates_person, person_aggregates_post_count, person_aggregates_post_score, person_aggregates_comment_count, person_aggregates_comment_score;
|
2021-02-15 19:34:10 +00:00
|
|
|
|
2021-02-14 18:46:16 +00:00
|
|
|
-- user_ table
|
|
|
|
-- Drop views
|
2023-08-02 16:44:51 +00:00
|
|
|
DROP VIEW person_alias_1, person_alias_2;
|
2021-02-14 18:46:16 +00:00
|
|
|
|
|
|
|
-- Rename indexes
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER INDEX person__pkey RENAME TO user__pkey;
|
|
|
|
|
|
|
|
ALTER INDEX idx_person_actor_id RENAME TO idx_user_actor_id;
|
|
|
|
|
|
|
|
ALTER INDEX idx_person_inbox_url RENAME TO idx_user_inbox_url;
|
|
|
|
|
|
|
|
ALTER INDEX idx_person_lower_actor_id RENAME TO idx_user_lower_actor_id;
|
|
|
|
|
|
|
|
ALTER INDEX idx_person_published RENAME TO idx_user_published;
|
2021-02-14 18:46:16 +00:00
|
|
|
|
|
|
|
-- Rename triggers
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TRIGGER site_aggregates_person_delete ON person RENAME TO site_aggregates_user_delete;
|
|
|
|
|
|
|
|
ALTER TRIGGER site_aggregates_person_insert ON person RENAME TO site_aggregates_user_insert;
|
2021-02-14 18:46:16 +00:00
|
|
|
|
|
|
|
-- Rename the trigger functions
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER FUNCTION site_aggregates_person_delete () RENAME TO site_aggregates_user_delete;
|
|
|
|
|
|
|
|
ALTER FUNCTION site_aggregates_person_insert () RENAME TO site_aggregates_user_insert;
|
2021-02-14 18:46:16 +00:00
|
|
|
|
|
|
|
-- Rename the table back to user_
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE person RENAME TO user_;
|
|
|
|
|
|
|
|
ALTER SEQUENCE person_id_seq
|
|
|
|
RENAME TO user__id_seq;
|
2021-02-14 18:46:16 +00:00
|
|
|
|
|
|
|
-- Add the columns back in
|
2023-08-02 16:44:51 +00:00
|
|
|
ALTER TABLE user_
|
|
|
|
ADD COLUMN password_encrypted text NOT NULL DEFAULT 'changeme',
|
2024-05-20 02:23:56 +00:00
|
|
|
ADD COLUMN email text UNIQUE,
|
2023-08-02 16:44:51 +00:00
|
|
|
ADD COLUMN admin boolean DEFAULT FALSE NOT NULL,
|
|
|
|
ADD COLUMN show_nsfw boolean DEFAULT FALSE NOT NULL,
|
|
|
|
ADD COLUMN theme character varying(20) DEFAULT 'darkly'::character varying NOT NULL,
|
|
|
|
ADD COLUMN default_sort_type smallint DEFAULT 0 NOT NULL,
|
|
|
|
ADD COLUMN default_listing_type smallint DEFAULT 1 NOT NULL,
|
|
|
|
ADD COLUMN lang character varying(20) DEFAULT 'browser'::character varying NOT NULL,
|
|
|
|
ADD COLUMN show_avatars boolean DEFAULT TRUE NOT NULL,
|
|
|
|
ADD COLUMN send_notifications_to_email boolean DEFAULT FALSE NOT NULL,
|
2024-05-20 02:23:56 +00:00
|
|
|
ADD COLUMN matrix_user_id text UNIQUE;
|
|
|
|
|
|
|
|
-- Default is only for existing rows
|
2024-05-22 20:34:35 +00:00
|
|
|
ALTER TABLE user_
|
|
|
|
ALTER COLUMN password_encrypted DROP DEFAULT;
|
2021-02-14 18:46:16 +00:00
|
|
|
|
|
|
|
-- Update the user_ table with the local_user data
|
2023-08-02 16:44:51 +00:00
|
|
|
UPDATE
|
|
|
|
user_ u
|
|
|
|
SET
|
|
|
|
password_encrypted = lu.password_encrypted,
|
|
|
|
email = lu.email,
|
|
|
|
admin = lu.admin,
|
|
|
|
show_nsfw = lu.show_nsfw,
|
|
|
|
theme = lu.theme,
|
|
|
|
default_sort_type = lu.default_sort_type,
|
|
|
|
default_listing_type = lu.default_listing_type,
|
|
|
|
lang = lu.lang,
|
|
|
|
show_avatars = lu.show_avatars,
|
|
|
|
send_notifications_to_email = lu.send_notifications_to_email,
|
|
|
|
matrix_user_id = lu.matrix_user_id
|
|
|
|
FROM
|
|
|
|
local_user lu
|
|
|
|
WHERE
|
|
|
|
lu.person_id = u.id;
|
|
|
|
|
2024-05-17 20:21:13 +00:00
|
|
|
CREATE UNIQUE INDEX idx_user_email_lower ON user_ (lower(email));
|
|
|
|
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE VIEW user_alias_1 AS
|
|
|
|
SELECT
|
2024-05-27 22:38:03 +00:00
|
|
|
*
|
2023-08-02 16:44:51 +00:00
|
|
|
FROM
|
|
|
|
user_;
|
|
|
|
|
|
|
|
CREATE VIEW user_alias_2 AS
|
|
|
|
SELECT
|
2024-05-27 22:38:03 +00:00
|
|
|
*
|
2023-08-02 16:44:51 +00:00
|
|
|
FROM
|
|
|
|
user_;
|
|
|
|
|
|
|
|
DROP TABLE local_user;
|
2021-02-14 18:46:16 +00:00
|
|
|
|
2021-02-15 19:34:10 +00:00
|
|
|
-- Add the user_aggregates table triggers
|
|
|
|
-- initial user add
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE FUNCTION user_aggregates_user ()
|
|
|
|
RETURNS TRIGGER
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
INSERT INTO user_aggregates (user_id)
|
|
|
|
VALUES (NEW.id);
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
DELETE FROM user_aggregates
|
|
|
|
WHERE user_id = OLD.id;
|
|
|
|
END IF;
|
|
|
|
RETURN NULL;
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
|
|
|
|
CREATE TRIGGER user_aggregates_user
|
|
|
|
AFTER INSERT OR DELETE ON user_
|
|
|
|
FOR EACH ROW
|
|
|
|
EXECUTE PROCEDURE user_aggregates_user ();
|
2021-02-15 19:34:10 +00:00
|
|
|
|
|
|
|
-- post count
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE FUNCTION user_aggregates_post_count ()
|
|
|
|
RETURNS TRIGGER
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
UPDATE
|
|
|
|
user_aggregates
|
|
|
|
SET
|
|
|
|
post_count = post_count + 1
|
|
|
|
WHERE
|
|
|
|
user_id = NEW.creator_id;
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
UPDATE
|
|
|
|
user_aggregates
|
|
|
|
SET
|
|
|
|
post_count = post_count - 1
|
|
|
|
WHERE
|
|
|
|
user_id = OLD.creator_id;
|
|
|
|
-- If the post gets deleted, the score calculation trigger won't fire,
|
|
|
|
-- so you need to re-calculate
|
|
|
|
UPDATE
|
|
|
|
user_aggregates ua
|
|
|
|
SET
|
|
|
|
post_score = pd.score
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
u.id,
|
|
|
|
coalesce(0, sum(pl.score)) AS score
|
|
|
|
-- User join because posts could be empty
|
|
|
|
FROM
|
|
|
|
user_ u
|
|
|
|
LEFT JOIN post p ON u.id = p.creator_id
|
|
|
|
LEFT JOIN post_like pl ON p.id = pl.post_id
|
|
|
|
GROUP BY
|
|
|
|
u.id) pd
|
|
|
|
WHERE
|
|
|
|
ua.user_id = OLD.creator_id;
|
|
|
|
END IF;
|
|
|
|
RETURN NULL;
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
|
|
|
|
CREATE TRIGGER user_aggregates_post_count
|
|
|
|
AFTER INSERT OR DELETE ON post
|
|
|
|
FOR EACH ROW
|
|
|
|
EXECUTE PROCEDURE user_aggregates_post_count ();
|
2021-02-15 19:34:10 +00:00
|
|
|
|
|
|
|
-- post score
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE FUNCTION user_aggregates_post_score ()
|
|
|
|
RETURNS TRIGGER
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
-- Need to get the post creator, not the voter
|
|
|
|
UPDATE
|
|
|
|
user_aggregates ua
|
|
|
|
SET
|
|
|
|
post_score = post_score + NEW.score
|
|
|
|
FROM
|
|
|
|
post p
|
|
|
|
WHERE
|
|
|
|
ua.user_id = p.creator_id
|
|
|
|
AND p.id = NEW.post_id;
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
UPDATE
|
|
|
|
user_aggregates ua
|
|
|
|
SET
|
|
|
|
post_score = post_score - OLD.score
|
|
|
|
FROM
|
|
|
|
post p
|
|
|
|
WHERE
|
|
|
|
ua.user_id = p.creator_id
|
|
|
|
AND p.id = OLD.post_id;
|
|
|
|
END IF;
|
|
|
|
RETURN NULL;
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
|
|
|
|
CREATE TRIGGER user_aggregates_post_score
|
|
|
|
AFTER INSERT OR DELETE ON post_like
|
|
|
|
FOR EACH ROW
|
|
|
|
EXECUTE PROCEDURE user_aggregates_post_score ();
|
2021-02-15 19:34:10 +00:00
|
|
|
|
|
|
|
-- comment count
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE FUNCTION user_aggregates_comment_count ()
|
|
|
|
RETURNS TRIGGER
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
UPDATE
|
|
|
|
user_aggregates
|
|
|
|
SET
|
|
|
|
comment_count = comment_count + 1
|
|
|
|
WHERE
|
|
|
|
user_id = NEW.creator_id;
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
UPDATE
|
|
|
|
user_aggregates
|
|
|
|
SET
|
|
|
|
comment_count = comment_count - 1
|
|
|
|
WHERE
|
|
|
|
user_id = OLD.creator_id;
|
|
|
|
-- If the comment gets deleted, the score calculation trigger won't fire,
|
|
|
|
-- so you need to re-calculate
|
|
|
|
UPDATE
|
|
|
|
user_aggregates ua
|
|
|
|
SET
|
|
|
|
comment_score = cd.score
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
u.id,
|
|
|
|
coalesce(0, sum(cl.score)) AS score
|
|
|
|
-- User join because comments could be empty
|
|
|
|
FROM
|
|
|
|
user_ u
|
|
|
|
LEFT JOIN comment c ON u.id = c.creator_id
|
|
|
|
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
|
|
|
GROUP BY
|
|
|
|
u.id) cd
|
|
|
|
WHERE
|
|
|
|
ua.user_id = OLD.creator_id;
|
|
|
|
END IF;
|
|
|
|
RETURN NULL;
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
|
|
|
|
CREATE TRIGGER user_aggregates_comment_count
|
|
|
|
AFTER INSERT OR DELETE ON comment
|
|
|
|
FOR EACH ROW
|
|
|
|
EXECUTE PROCEDURE user_aggregates_comment_count ();
|
2021-02-15 19:34:10 +00:00
|
|
|
|
|
|
|
-- comment score
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE FUNCTION user_aggregates_comment_score ()
|
|
|
|
RETURNS TRIGGER
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
-- Need to get the post creator, not the voter
|
|
|
|
UPDATE
|
|
|
|
user_aggregates ua
|
|
|
|
SET
|
|
|
|
comment_score = comment_score + NEW.score
|
|
|
|
FROM
|
|
|
|
comment c
|
|
|
|
WHERE
|
|
|
|
ua.user_id = c.creator_id
|
|
|
|
AND c.id = NEW.comment_id;
|
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
|
|
UPDATE
|
|
|
|
user_aggregates ua
|
|
|
|
SET
|
|
|
|
comment_score = comment_score - OLD.score
|
|
|
|
FROM
|
|
|
|
comment c
|
|
|
|
WHERE
|
|
|
|
ua.user_id = c.creator_id
|
|
|
|
AND c.id = OLD.comment_id;
|
|
|
|
END IF;
|
|
|
|
RETURN NULL;
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
|
|
|
|
CREATE TRIGGER user_aggregates_comment_score
|
|
|
|
AFTER INSERT OR DELETE ON comment_like
|
|
|
|
FOR EACH ROW
|
|
|
|
EXECUTE PROCEDURE user_aggregates_comment_score ();
|
2021-02-15 19:34:10 +00:00
|
|
|
|
2021-02-25 19:04:12 +00:00
|
|
|
-- redo site aggregates trigger
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE OR REPLACE FUNCTION site_aggregates_activity (i text)
|
|
|
|
RETURNS integer
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
|
|
|
count_ integer;
|
|
|
|
BEGIN
|
|
|
|
SELECT
|
|
|
|
count(*) INTO count_
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
c.creator_id
|
|
|
|
FROM
|
|
|
|
comment c
|
|
|
|
INNER JOIN user_ u ON c.creator_id = u.id
|
|
|
|
WHERE
|
|
|
|
c.published > ('now'::timestamp - i::interval)
|
|
|
|
AND u.local = TRUE
|
|
|
|
UNION
|
|
|
|
SELECT
|
|
|
|
p.creator_id
|
|
|
|
FROM
|
|
|
|
post p
|
|
|
|
INNER JOIN user_ u ON p.creator_id = u.id
|
|
|
|
WHERE
|
|
|
|
p.published > ('now'::timestamp - i::interval)
|
|
|
|
AND u.local = TRUE) a;
|
|
|
|
RETURN count_;
|
|
|
|
END;
|
2021-02-25 19:04:12 +00:00
|
|
|
$$;
|
2023-08-02 16:44:51 +00:00
|
|
|
|