From 3d73fdefbcaa7271368792b290f719192a0d122f Mon Sep 17 00:00:00 2001 From: Lembot Date: Fri, 9 Jan 2026 23:03:04 -0500 Subject: [PATCH] feat: auto-follow objects created by user When a user creates a proposition, document, tag, or source, automatically add it to their default follow list ("My Follows"). Implements database triggers on the propositions, documents, tags, and sources tables that insert into follow_list_entries after creation. Closes #133 Co-Authored-By: Claude Opus 4.5 --- .../V088__auto_follow_created_entities.sql | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 ratings-sql/V088__auto_follow_created_entities.sql diff --git a/ratings-sql/V088__auto_follow_created_entities.sql b/ratings-sql/V088__auto_follow_created_entities.sql new file mode 100644 index 0000000..84ae098 --- /dev/null +++ b/ratings-sql/V088__auto_follow_created_entities.sql @@ -0,0 +1,80 @@ +-- V088: Auto-follow objects created by user +-- When a user creates a proposition, document, tag, or source, +-- automatically add it to their default follow list ("My Follows"). + +-- ============================================================================= +-- Function: auto_follow_created_entity +-- Generic trigger function to add newly created entities to creator's default follow list +-- ============================================================================= + +CREATE OR REPLACE FUNCTION public.auto_follow_created_entity() +RETURNS TRIGGER AS $$ +DECLARE + v_default_list_id INTEGER; + v_entity_type VARCHAR(50); +BEGIN + -- Get the creator's default follow list + v_default_list_id := public.get_user_default_follow_list_id(NEW.creator_id); + + -- If no default list exists, skip (shouldn't happen for normal users) + IF v_default_list_id IS NULL THEN + RETURN NEW; + END IF; + + -- Determine entity type based on the table name + v_entity_type := TG_ARGV[0]; + + -- Insert into follow_list_entries (ignore if already exists) + INSERT INTO public.follow_list_entries (creator_id, follow_list_id, entity_type, entity_id) + VALUES (NEW.creator_id, v_default_list_id, v_entity_type, NEW.id) + ON CONFLICT (follow_list_id, entity_type, entity_id) DO NOTHING; + + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +COMMENT ON FUNCTION public.auto_follow_created_entity() IS 'Trigger function to automatically add newly created entities to the creator''s default follow list'; + +-- ============================================================================= +-- Trigger: Auto-follow newly created propositions +-- ============================================================================= + +CREATE TRIGGER trg_auto_follow_proposition + AFTER INSERT ON public.propositions + FOR EACH ROW + EXECUTE FUNCTION public.auto_follow_created_entity('proposition'); + +COMMENT ON TRIGGER trg_auto_follow_proposition ON public.propositions IS 'Automatically adds new propositions to the creator''s default follow list'; + +-- ============================================================================= +-- Trigger: Auto-follow newly created documents +-- ============================================================================= + +CREATE TRIGGER trg_auto_follow_document + AFTER INSERT ON public.documents + FOR EACH ROW + EXECUTE FUNCTION public.auto_follow_created_entity('document'); + +COMMENT ON TRIGGER trg_auto_follow_document ON public.documents IS 'Automatically adds new documents to the creator''s default follow list'; + +-- ============================================================================= +-- Trigger: Auto-follow newly created tags +-- ============================================================================= + +CREATE TRIGGER trg_auto_follow_tag + AFTER INSERT ON public.tags + FOR EACH ROW + EXECUTE FUNCTION public.auto_follow_created_entity('tag'); + +COMMENT ON TRIGGER trg_auto_follow_tag ON public.tags IS 'Automatically adds new tags to the creator''s default follow list'; + +-- ============================================================================= +-- Trigger: Auto-follow newly created sources +-- ============================================================================= + +CREATE TRIGGER trg_auto_follow_source + AFTER INSERT ON public.sources + FOR EACH ROW + EXECUTE FUNCTION public.auto_follow_created_entity('source'); + +COMMENT ON TRIGGER trg_auto_follow_source ON public.sources IS 'Automatically adds new sources to the creator''s default follow list'; -- GitLab