1 -- Fix for duplicated decrementations when both `deleted` and `removed` fields are set subsequently
2 CREATE OR REPLACE FUNCTION was_removed_or_deleted (TG_OP text, OLD record, NEW record)
7 IF (TG_OP = 'INSERT') THEN
10 IF (TG_OP = 'DELETE' AND OLD.deleted = 'f' AND OLD.removed = 'f') THEN
13 RETURN TG_OP = 'UPDATE'
16 AND (NEW.deleted = 't'
17 OR NEW.removed = 't');
21 CREATE OR REPLACE FUNCTION was_restored_or_created (TG_OP text, OLD record, NEW record)
26 IF (TG_OP = 'DELETE') THEN
29 IF (TG_OP = 'INSERT') THEN
32 RETURN TG_OP = 'UPDATE'
35 AND (OLD.deleted = 't'
36 OR OLD.removed = 't');
40 -- Fix for post's comment count not updating after setting `removed` to 't'
41 DROP TRIGGER IF EXISTS post_aggregates_comment_set_deleted ON comment;
43 DROP FUNCTION post_aggregates_comment_deleted ();
45 CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
50 -- Check for post existence - it may not exist anymore
51 IF TG_OP = 'INSERT' OR EXISTS (
57 p.id = OLD.post_id) THEN
58 IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
62 comments = comments + 1
64 pa.post_id = NEW.post_id;
65 ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
69 comments = comments - 1
71 pa.post_id = OLD.post_id;
74 IF TG_OP = 'INSERT' THEN
78 newest_comment_time = NEW.published
80 pa.post_id = NEW.post_id;
81 -- A 2 day necro-bump limit
85 newest_comment_time_necro = NEW.published
90 AND pa.post_id = NEW.post_id
91 -- Fix issue with being able to necro-bump your own post
92 AND NEW.creator_id != p.creator_id
93 AND pa.published > ('now'::timestamp - '2 days'::interval);
99 CREATE OR REPLACE TRIGGER post_aggregates_comment_count
100 AFTER INSERT OR DELETE OR UPDATE OF removed,
103 EXECUTE PROCEDURE post_aggregates_comment_count ();