portal: derive source from OC email visibility instead of trusting a stale flag
ci/woodpecker/push/woodpecker Pipeline was canceled

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.
This commit is contained in:
inference-admin committed 2026-11-17 05:04:50 +00:00
1 parent 35735d67a5
commit ffe62d3c78
2 files changed
+69

No files matched your search

+46
View File
@@ -14,6 +14,7 @@ Three responsibilities:
""" """
import os import os
import inspect
import json import json
import logging import logging
import re import re
@@ -9168,6 +9169,46 @@ async def settle_held_credits(send: bool = True) -> dict:
"stale": stale, "notified": notified} "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") @app.post("/admin/sweep")
@limiter.limit("20/minute") @limiter.limit("20/minute")
async def admin_sweep(request: Request): async def admin_sweep(request: Request):
@@ -9221,6 +9262,11 @@ async def admin_sweep(request: Request):
except Exception as exc: # noqa: BLE001 except Exception as exc: # noqa: BLE001
result["held_credits"] = {"error": str(exc)[:200]} 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 # 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 # 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 # finds something, and only when the set changes. Was a daily script on the
+23
View File
@@ -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")