From ffe62d3c78859981729afd0e62afef5171b40c01 Mon Sep 17 00:00:00 2001 From: Inferencebot Date: Tue, 17 Nov 2026 05:04:50 +0000 Subject: [PATCH] portal: derive source from OC email visibility instead of trusting a stale flag oc-incognito disables email matching by design, so a profile that later shows an email leaves the member silently unmatched. The sweep now flips those to oc and records oc_email; profiles still hiding an email are untouched. Test covers both. --- app/main.py | 46 +++++++++++++++++++++++++++++ tests/test_derive_member_sources.py | 23 +++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/test_derive_member_sources.py diff --git a/app/main.py b/app/main.py index 4cce9ee..1965956 100644 --- a/app/main.py +++ b/app/main.py @@ -14,6 +14,7 @@ Three responsibilities: """ import os +import inspect import json import logging import re @@ -9168,6 +9169,46 @@ async def settle_held_credits(send: bool = True) -> dict: "stale": stale, "notified": notified} +async def derive_member_sources() -> dict: + """Keep `source` honest: a profile that now shows an email is not incognito. + + `oc-incognito` exists because a member's OC profile hid its email; reconciliation + then matches on the stored slug and skips email matching. When that profile gains + a visible email the flag goes stale and keeps email matching switched off — which + is exactly how a credit pack ends up held for a member we could have matched. + This pass flips the flag back and records the email. + """ + try: + rows = fetch_member_index() + if inspect.isawaitable(rows): + rows = await rows + except Exception as exc: # noqa: BLE001 + return {"error": f"index unavailable: {str(exc)[:120]}"} + slug2email = {(r[1] or "").strip().lower(): (r[0] or "").strip().lower() + for r in rows if len(r) > 1 and r[0]} + flipped: list[dict] = [] + conn = get_db() + try: + stale = conn.execute( + "SELECT email, slug FROM members WHERE source = 'oc-incognito'").fetchall() + finally: + conn.close() + for email, slug in stale: + visible = slug2email.get((slug or "").strip().lower()) + if not visible: + continue + set_member_source(email, "oc") + conn = get_db() + try: + conn.execute("UPDATE members SET oc_email = ? WHERE lower(email) = ?", + (visible, email.lower())) + conn.commit() + finally: + conn.close() + flipped.append({"email": email, "oc_email": visible}) + return {"checked": len(stale), "flipped": flipped} + + @app.post("/admin/sweep") @limiter.limit("20/minute") async def admin_sweep(request: Request): @@ -9221,6 +9262,11 @@ async def admin_sweep(request: Request): except Exception as exc: # noqa: BLE001 result["held_credits"] = {"error": str(exc)[:200]} + try: + result["member_sources"] = await derive_member_sources() + except Exception as exc: # noqa: BLE001 + result["member_sources"] = {"error": str(exc)[:200]} + # Paid Open Collective contributors with no portal row — the net for members # the sweep cannot see (hidden email, org account). Emails info@ only when it # finds something, and only when the set changes. Was a daily script on the diff --git a/tests/test_derive_member_sources.py b/tests/test_derive_member_sources.py new file mode 100644 index 0000000..be96f66 --- /dev/null +++ b/tests/test_derive_member_sources.py @@ -0,0 +1,23 @@ +import asyncio, os, sys, tempfile +os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "sources.db") +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +import app.main as m +m.DB_PATH = os.environ["DB_PATH"] + +conn = m.get_db() +conn.execute("INSERT OR REPLACE INTO members (email, slug, active, source) VALUES (?,?,?,?)", + ("a@example.org", "slug-a", 1, "oc-incognito")) +conn.execute("INSERT OR REPLACE INTO members (email, slug, active, source) VALUES (?,?,?,?)", + ("b@example.org", "slug-b", 1, "oc-incognito")) +conn.commit(); conn.close() + +m.fetch_member_index = lambda: [("a@example.org", "slug-a"), ("other@example.org", "slug-z")] +r = asyncio.run(m.derive_member_sources()) +conn = m.get_db() +a = conn.execute("SELECT source, oc_email FROM members WHERE email='a@example.org'").fetchone() +b = conn.execute("SELECT source, oc_email FROM members WHERE email='b@example.org'").fetchone() +conn.close() +print(" flipped:", r["flipped"], "| a:", tuple(a), "| b:", tuple(b)) +assert tuple(a) == ("oc", "a@example.org"), "visible email must flip the source and record it" +assert tuple(b) == ("oc-incognito", None), "a profile still hiding its email stays incognito" +print("DERIVE SOURCES OK: flips only where the profile now shows an email")