Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
fix(security): route comments only to accounts whose Instagram access still works
spacesdrive6ce2c4295b61
diff --git a/docs/architecture/database/SCHEMA.md b/docs/architecture/database/SCHEMA.mdindex 168cab1..97ad193 100644--- a/docs/architecture/database/SCHEMA.md+++ b/docs/architecture/database/SCHEMA.md@@ -49,6 +49,9 @@ The first four files are baselines. Each recreates one table as it stood before| 17 | `create_automation_public_replies`, `revoke_browser_access_automation_public_replies`, `add_automation_public_replies_updated_at_trigger`, then `index_automation_public_replies_<purpose>` (five files) and `index_automation_attempts_public_reply` | The public reply table and its indexes || 18 | `replace_automation_rules_for_ig_user_function`, `create_store_automation_comments_function`, the four public reply functions, `create_automation_public_review_candidates_function`, `create_record_automation_public_review_function`, then `replace_` for the expiry, queue health, stats and comment claim functions | The public reply functions below, and the existing functions taught about public replies || 19 | `index_accounts_identity` | The unique index `accounts_identity_key` below, one row per account (#116) |+| 20 | `alter_tracked_posts_youtube_only`, `drop_tracked_posts_account_id`, `alter_accounts_without_reddit`, `replace_accounts_identity_index` | The Reddit cleanup (#145) |+| 21 | `create_mcp_access_tokens`, `revoke_browser_access_mcp_access_tokens`, `add_mcp_access_tokens_updated_at_trigger`, `index_mcp_access_tokens_user_created`, the same four for `mcp_client_grants`, `alter_audit_events_subject_types` | The Claude (MCP) tables below (#146) |+| 22 | `replace_automation_rules_for_ig_user_function` | Comments route only to accounts that still hold an unexpired Instagram token (audit lead L2, #161) |From order 10 on, every migration creates at most one entity, and the migration test enforces it. The files before it were applied to production first and stay as recorded.@@ -532,7 +535,7 @@ Every function sets `search_path = ''` and names its tables with the schema. Fun| `merge_scheduled_post(p_id, p_user_id, p_patch, p_status, p_scheduled_at, p_only_status)` | sql | Merges `p_patch` into `data` and sets `status` and `scheduled_at` when given, in one UPDATE. `p_only_status` limits the update to a post still in that status || `merge_tracked_post_data(p_id, p_user_id, p_patch)` | sql | `data = coalesce(data, '{}') \|\| p_patch` on one tracked item |-| `automation_rules_for_ig_user(p_ig_user_id)` | sql, stable | The enabled rules of every connected account with that Instagram id, oldest account first, with their private and public reply settings |+| `automation_rules_for_ig_user(p_ig_user_id)` | sql, stable | The enabled rules of every connected account with that Instagram id whose stored token is present and not expired, oldest account first, with their private and public reply settings. A member whose access ended no longer receives the account's comments || `store_automation_comments(p_comments)` | plpgsql | Inserts a webhook's comments (`on conflict do nothing`) and then a public reply job for each one whose rule asks for one, joined to the stored comment while the same rule answers it, in one transaction. Returns the new comment ids and statuses and the new public reply ids || `claim_automation_comments(p_limit, p_per_account, p_lease_seconds, p_ids)` | plpgsql | Marks `sending` jobs past the lease `needs_review` (their attempts `ambiguous`), finds the accounts with waiting jobs, leaves out paused, cooling and limited ones, locks each account's oldest due jobs with `for update skip locked` as it reads them, and claims them with accounts taking turns. Returns each job with its rule's reply, its followers-only fields and the account's token; `rule_enabled` is false once the rule or its private reply is off || `reserve_automation_send(p_id, p_attempt_id, p_global_limit, p_user_limit, p_account_limit, p_recipient_limit, p_message_digest)` | plpgsql | Under `pg_advisory_xact_lock`, checks pause, cooldown, the recipient's daily replies and the hourly limits per rule, account, user and instance, counting private replies only; a full account gets `limited_until`. On success moves the job to `sending` and inserts the attempt with the digest. Returns `outcome` and, for a full scope, `retry_at` |diff --git a/supabase/migrations/20261105032021_replace_automation_rules_for_ig_user_function.sql b/supabase/migrations/20261105032021_replace_automation_rules_for_ig_user_function.sqlnew file mode 100644index 0000000..22ebee5--- /dev/null+++ b/supabase/migrations/20261105032021_replace_automation_rules_for_ig_user_function.sql@@ -0,0 +1,35 @@+-- replaces automation_rules_for_ig_user so comments go only to accounts that still hold an unexpired Instagram token+create or replace function public.automation_rules_for_ig_user(p_ig_user_id text)+returns table (+ id text,+ user_id uuid,+ account_id text,+ keywords text[],+ match_mode text,+ case_sensitive boolean,+ media_ids text[],+ created_at timestamptz,+ private_reply_enabled boolean,+ public_reply_enabled boolean,+ public_reply_texts text[]+)+language sql+stable+set search_path = ''+as $$+ select r.id, r.user_id, r.account_id, r.keywords, r.match_mode, r.case_sensitive, r.media_ids, r.created_at,+ r.private_reply_enabled, r.public_reply_enabled, r.public_reply_texts+ from public.accounts a+ join public.automation_rules r on r.account_id = a.id and r.user_id = a.user_id+ where a.platform = 'instagram'+ and a.data ->> 'igUserId' = p_ig_user_id+ and r.enabled+ -- a member whose access ended (token removed or expired) no longer receives the account's comments+ and coalesce(a.data ->> 'accessToken', '') <> ''+ and (a.data ->> 'tokenExpiresAt' is null or (a.data ->> 'tokenExpiresAt')::timestamptz > now())+ order by a.created_at, a.id, r.created_at, r.id;+$$;++-- functions in public are exposed through the Data API, so only the service role may call it+revoke execute on function public.automation_rules_for_ig_user(text) from public, anon, authenticated;+grant execute on function public.automation_rules_for_ig_user(text) to service_role;diff --git a/supabase/rollbacks/20261105032021_replace_automation_rules_for_ig_user_function.sql b/supabase/rollbacks/20261105032021_replace_automation_rules_for_ig_user_function.sqlnew file mode 100644index 0000000..e7341f1--- /dev/null+++ b/supabase/rollbacks/20261105032021_replace_automation_rules_for_ig_user_function.sql@@ -0,0 +1,32 @@+-- puts back automation_rules_for_ig_user without the check that the account's Instagram token is still valid+create or replace function public.automation_rules_for_ig_user(p_ig_user_id text)+returns table (+ id text,+ user_id uuid,+ account_id text,+ keywords text[],+ match_mode text,+ case_sensitive boolean,+ media_ids text[],+ created_at timestamptz,+ private_reply_enabled boolean,+ public_reply_enabled boolean,+ public_reply_texts text[]+)+language sql+stable+set search_path = ''+as $$+ select r.id, r.user_id, r.account_id, r.keywords, r.match_mode, r.case_sensitive, r.media_ids, r.created_at,+ r.private_reply_enabled, r.public_reply_enabled, r.public_reply_texts+ from public.accounts a+ join public.automation_rules r on r.account_id = a.id and r.user_id = a.user_id+ where a.platform = 'instagram'+ and a.data ->> 'igUserId' = p_ig_user_id+ and r.enabled+ order by a.created_at, a.id, r.created_at, r.id;+$$;++-- functions in public are exposed through the Data API, so only the service role may call it+revoke execute on function public.automation_rules_for_ig_user(text) from public, anon, authenticated;+grant execute on function public.automation_rules_for_ig_user(text) to service_role;diff --git a/supabase/schemas/public/functions/automation_rules_for_ig_user.sql b/supabase/schemas/public/functions/automation_rules_for_ig_user.sqlindex 28c51ca..e05e931 100644--- a/supabase/schemas/public/functions/automation_rules_for_ig_user.sql+++ b/supabase/schemas/public/functions/automation_rules_for_ig_user.sql@@ -25,6 +25,9 @@ CREATE OR REPLACE FUNCTION public.automation_rules_for_ig_user (where a.platform = 'instagram'and a.data ->> 'igUserId' = p_ig_user_idand r.enabled+ -- a member whose access ended (token removed or expired) no longer receives the account's comments+ and coalesce(a.data ->> 'accessToken', '') <> ''+ and (a.data ->> 'tokenExpiresAt' is null or (a.data ->> 'tokenExpiresAt')::timestamptz > now())order by a.created_at, a.id, r.created_at, r.id;$function$;