2023-07-10 15:30:30 +00:00
|
|
|
-- Fix for duplicated decrementations when both `deleted` and `removed` fields are set subsequently
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE OR REPLACE FUNCTION was_removed_or_deleted (TG_OP text, OLD record, NEW record)
|
|
|
|
RETURNS boolean
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
RETURN FALSE;
|
|
|
|
END IF;
|
|
|
|
IF (TG_OP = 'DELETE' AND OLD.deleted = 'f' AND OLD.removed = 'f') THEN
|
|
|
|
RETURN TRUE;
|
|
|
|
END IF;
|
|
|
|
RETURN TG_OP = 'UPDATE'
|
|
|
|
AND OLD.deleted = 'f'
|
|
|
|
AND OLD.removed = 'f'
|
|
|
|
AND (NEW.deleted = 't'
|
|
|
|
OR NEW.removed = 't');
|
|
|
|
END
|
|
|
|
$$;
|
2023-07-10 15:30:30 +00:00
|
|
|
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE OR REPLACE FUNCTION was_restored_or_created (TG_OP text, OLD record, NEW record)
|
2023-07-10 15:30:30 +00:00
|
|
|
RETURNS boolean
|
|
|
|
LANGUAGE plpgsql
|
2023-08-02 16:44:51 +00:00
|
|
|
AS $$
|
|
|
|
BEGIN
|
2023-07-10 15:30:30 +00:00
|
|
|
IF (TG_OP = 'DELETE') THEN
|
2023-08-02 16:44:51 +00:00
|
|
|
RETURN FALSE;
|
|
|
|
END IF;
|
2023-07-10 15:30:30 +00:00
|
|
|
IF (TG_OP = 'INSERT') THEN
|
2023-08-02 16:44:51 +00:00
|
|
|
RETURN TRUE;
|
|
|
|
END IF;
|
|
|
|
RETURN TG_OP = 'UPDATE'
|
|
|
|
AND NEW.deleted = 'f'
|
|
|
|
AND NEW.removed = 'f'
|
|
|
|
AND (OLD.deleted = 't'
|
|
|
|
OR OLD.removed = 't');
|
|
|
|
END
|
|
|
|
$$;
|
2023-07-10 15:30:30 +00:00
|
|
|
|
|
|
|
-- Fix for post's comment count not updating after setting `removed` to 't'
|
2023-08-02 16:44:51 +00:00
|
|
|
DROP TRIGGER IF EXISTS post_aggregates_comment_set_deleted ON comment;
|
|
|
|
|
|
|
|
DROP FUNCTION post_aggregates_comment_deleted ();
|
2023-07-10 15:30:30 +00:00
|
|
|
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
|
|
|
|
RETURNS TRIGGER
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2023-07-10 15:30:30 +00:00
|
|
|
-- Check for post existence - it may not exist anymore
|
|
|
|
IF TG_OP = 'INSERT' OR EXISTS (
|
2023-08-02 16:44:51 +00:00
|
|
|
SELECT
|
|
|
|
1
|
|
|
|
FROM
|
|
|
|
post p
|
|
|
|
WHERE
|
|
|
|
p.id = OLD.post_id) THEN
|
|
|
|
IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
|
|
|
|
UPDATE
|
|
|
|
post_aggregates pa
|
|
|
|
SET
|
|
|
|
comments = comments + 1
|
|
|
|
WHERE
|
|
|
|
pa.post_id = NEW.post_id;
|
|
|
|
ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
|
|
|
|
UPDATE
|
|
|
|
post_aggregates pa
|
|
|
|
SET
|
|
|
|
comments = comments - 1
|
|
|
|
WHERE
|
|
|
|
pa.post_id = OLD.post_id;
|
2023-07-10 15:30:30 +00:00
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
IF TG_OP = 'INSERT' THEN
|
2023-08-02 16:44:51 +00:00
|
|
|
UPDATE
|
|
|
|
post_aggregates pa
|
|
|
|
SET
|
|
|
|
newest_comment_time = NEW.published
|
|
|
|
WHERE
|
|
|
|
pa.post_id = NEW.post_id;
|
2023-07-10 15:30:30 +00:00
|
|
|
-- A 2 day necro-bump limit
|
2023-08-02 16:44:51 +00:00
|
|
|
UPDATE
|
|
|
|
post_aggregates pa
|
|
|
|
SET
|
|
|
|
newest_comment_time_necro = NEW.published
|
|
|
|
FROM
|
|
|
|
post p
|
|
|
|
WHERE
|
|
|
|
pa.post_id = p.id
|
|
|
|
AND pa.post_id = NEW.post_id
|
|
|
|
-- Fix issue with being able to necro-bump your own post
|
|
|
|
AND NEW.creator_id != p.creator_id
|
|
|
|
AND pa.published > ('now'::timestamp - '2 days'::interval);
|
2023-07-10 15:30:30 +00:00
|
|
|
END IF;
|
2023-08-02 16:44:51 +00:00
|
|
|
RETURN NULL;
|
|
|
|
END
|
|
|
|
$$;
|
2023-07-10 15:30:30 +00:00
|
|
|
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE OR REPLACE TRIGGER post_aggregates_comment_count
|
|
|
|
AFTER INSERT OR DELETE OR UPDATE OF removed,
|
|
|
|
deleted ON comment
|
|
|
|
FOR EACH ROW
|
|
|
|
EXECUTE PROCEDURE post_aggregates_comment_count ();
|
2023-07-10 15:30:30 +00:00
|
|
|
|