lemmy/migrations/2023-09-01-112158_auto_resolve_report/up.sql
Nutomic a0ea8dbc00
Auto resolve reports trigger (#3871)
* Revert "Automatically resolve report when post/comment is removed (#3850)"

This reverts commit f7f6766650.

* Automatically resolve reports using db trigger

* lint

* use mod log tables

* fix migration

* fix ci

* fix clippy
2023-09-05 05:35:10 -04:00

49 lines
1.1 KiB
PL/PgSQL

-- Automatically resolve all reports for a given post once it is marked as removed
CREATE OR REPLACE FUNCTION post_removed_resolve_reports ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE
post_report
SET
resolved = TRUE,
resolver_id = NEW.mod_person_id,
updated = now()
WHERE
post_report.post_id = NEW.post_id;
RETURN NULL;
END
$$;
CREATE OR REPLACE TRIGGER post_removed_resolve_reports
AFTER INSERT ON mod_remove_post
FOR EACH ROW
WHEN (NEW.removed)
EXECUTE PROCEDURE post_removed_resolve_reports ();
-- Same when comment is marked as removed
CREATE OR REPLACE FUNCTION comment_removed_resolve_reports ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE
comment_report
SET
resolved = TRUE,
resolver_id = NEW.mod_person_id,
updated = now()
WHERE
comment_report.comment_id = NEW.comment_id;
RETURN NULL;
END
$$;
CREATE OR REPLACE TRIGGER comment_removed_resolve_reports
AFTER INSERT ON mod_remove_comment
FOR EACH ROW
WHEN (NEW.removed)
EXECUTE PROCEDURE comment_removed_resolve_reports ();