Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

3.6 KB
Raw Blame
CREATE OR REPLACE FUNCTION public.reserve_automation_public_reply (
  p_id             text,
  p_attempt_id     text,
  p_account_limit  integer,
  p_message_digest text    DEFAULT NULL::text
)
  RETURNS TABLE (
    outcome  text,
    retry_at timestamp with time zone
  )
  LANGUAGE plpgsql
  SET search_path TO ''
  AS $function$
#variable_conflict use_column
declare
    job public.automation_public_replies%rowtype;
    controls public.automation_accounts%rowtype;
    free_at timestamptz;
begin
    -- one public reservation at a time across every Worker; private replies take another lock, so neither waits on the other
    perform pg_advisory_xact_lock(hashtext('reserve_automation_public_reply'));

    select * into job from public.automation_public_replies p where p.id = p_id and p.status = 'processing' for update;
    if not found then
        return query select 'gone'::text, null::timestamptz;
        return;
    end if;

    select * into controls from public.automation_accounts a where a.account_id = job.account_id;
    if controls.paused then
        return query select 'paused'::text, null::timestamptz;
        return;
    end if;
    if controls.public_cooldown_until > now() then
        return query select 'cooldown'::text, controls.public_cooldown_until;
        return;
    end if;

    -- one public reply per person and post a day, however often they comment; it also ends a loop with another
    -- account's automation answering every reply
    if job.from_id is not null and exists (
        select 1 from public.automation_public_replies p
        where p.account_id = job.account_id and p.from_id = job.from_id and p.media_id is not distinct from job.media_id
            and p.id <> job.id and p.status in ('sending', 'accepted', 'needs_review') and p.created_at > now() - interval '1 day'
    ) then
        return query select 'recipient_limit'::text, null::timestamptz;
        return;
    end if;

    -- when the account is full, a reply frees up an hour after the oldest one in the window
    select case when count(*) >= p_account_limit then min(t.started_at) + interval '1 hour' end into free_at
    from public.automation_attempts t
    where t.account_id = job.account_id and t.kind = 'public_reply' and t.started_at > now() - interval '1 hour';
    if free_at is not null then
        insert into public.automation_accounts (account_id, user_id, public_limited_until)
        values (job.account_id, job.user_id, free_at)
        on conflict (account_id) do update set public_limited_until = excluded.public_limited_until;
        return query select 'account_limit'::text, free_at;
        return;
    end if;

    update public.automation_public_replies p
    set status = 'sending', claimed_at = now(), attempts = p.attempts + 1
    where p.id = p_id;
    insert into public.automation_attempts (id, user_id, account_id, automation_comment_id, rule_id, recipient_id, kind, public_reply_id, message_digest)
    values (p_attempt_id, job.user_id, job.account_id, job.automation_comment_id, job.rule_id, job.from_id, 'public_reply', job.id, p_message_digest);
    return query select 'ok'::text, null::timestamptz;
end;
$function$;

GRANT EXECUTE ON FUNCTION "public"."reserve_automation_public_reply"(text, text, integer, text) TO "service_role";

REVOKE ALL ON FUNCTION "public"."reserve_automation_public_reply"(text, text, integer, text) FROM "postgres";

GRANT EXECUTE ON FUNCTION "public"."reserve_automation_public_reply"(text, text, integer, text) TO "postgres";

REVOKE ALL ON FUNCTION "public"."reserve_automation_public_reply"(text, text, integer, text) FROM PUBLIC, "anon", "authenticated";