From 1f647374a8cdf3bc5c2d29ff8be277b027925c8c Mon Sep 17 00:00:00 2001
From: Freya Murphy <freya@freyacat.org>
Date: Sat, 30 Mar 2024 16:36:54 -0400
Subject: post comments, refactor post loading, hide load more btn

---
 db/rest/comment/api_comment_update.sql | 50 ++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 db/rest/comment/api_comment_update.sql

(limited to 'db/rest/comment/api_comment_update.sql')

diff --git a/db/rest/comment/api_comment_update.sql b/db/rest/comment/api_comment_update.sql
new file mode 100644
index 0000000..d6b4aca
--- /dev/null
+++ b/db/rest/comment/api_comment_update.sql
@@ -0,0 +1,50 @@
+CREATE FUNCTION _api.comment_update()
+RETURNS TRIGGER
+LANGUAGE plpgsql VOLATILE
+AS $BODY$
+DECLARE
+	_user_id INTEGER;
+	_changed BOOLEAN;
+BEGIN
+	_user_id = _api.get_user_id();
+	_changed = FALSE;
+
+	IF OLD.user_id <> _user_id THEN
+		PERFORM _api.raise_deny();
+	END IF;
+
+	NEW.content = COALESCE(NEW.content, OLD.content);
+	NEW.content := _api.trim(NEW.content);
+	PERFORM _api.validate_text(
+		_text => NEW.content,
+		_column => 'content',
+		_min => 1,
+		_max => 1024
+	);
+
+	IF NEW.content IS DISTINCT FROM OLD.content THEN
+		_changed = TRUE;
+	END IF;
+
+	IF _changed THEN
+		UPDATE admin.comment
+			SET content = NEW.content
+			WHERE id = OLD.id;
+	END IF;
+
+	RETURN NEW;
+END
+$BODY$;
+
+GRANT EXECUTE ON FUNCTION _api.comment_update()
+	TO rest_user;
+GRANT UPDATE ON TABLE api.comment
+	TO rest_user;
+GRANT UPDATE ON TABLE admin.comment
+	TO rest_user;
+
+CREATE TRIGGER api_comment_update_trgr
+	INSTEAD OF UPDATE
+			ON api.comment
+			FOR EACH ROW
+				EXECUTE PROCEDURE _api.comment_update();
-- 
cgit v1.2.3-freya

