CREATE FUNCTION _api.follow_delete()
RETURNS TRIGGER
LANGUAGE plpgsql VOLATILE
AS $BODY$
DECLARE
	_user_id INTEGER;
BEGIN
	_user_id = _api.get_user_id();

	IF OLD.follower_id <> _user_id THEN
		PERFORM _api.raise_deny();
	END IF;

	UPDATE xssbook.follow SET
		value = FALSE,
		modified = clock_timestamp()
		WHERE id = OLD.id;
END
$BODY$;

GRANT EXECUTE ON FUNCTION _api.follow_delete()
	TO rest_user;
GRANT DELETE ON TABLE api.follow
	TO rest_user;
GRANT UPDATE ON TABLE xssbook.follow
	TO rest_user;

CREATE TRIGGER api_follow_delete_trgr
	INSTEAD OF DELETE
			ON api.follow
			FOR EACH ROW
				EXECUTE PROCEDURE _api.follow_delete();
