diff --git a/app/main.py b/app/main.py index e01408d..4a6abe0 100644 --- a/app/main.py +++ b/app/main.py @@ -351,6 +351,10 @@ GUEST_BUDGET = float(os.environ.get("GUEST_BUDGET", "5.0")) GUEST_DAYS = float(os.environ.get("GUEST_DAYS", "60")) # One guest per inviting member per rolling 30 days (abuse protection). GUEST_PER_INVITER_DAYS = float(os.environ.get("GUEST_PER_INVITER_DAYS", "30")) +# The ADMIN-PANEL guest-invite path records THIS account as the inviter in the +# guests ledger, and admin invites are NOT bound by the member's +# one-per-30-days cap (admins are not members). +ADMIN_INVITER_EMAIL = os.environ.get("ADMIN_INVITER_EMAIL", "info@inference.coop").strip().lower() # Trial-period approval gate for MEMBER-initiated guest invitations. Off (the # default): a member's request waits in PENDING until the operator approves or # rejects it in the panel. On: a member's request provisions immediately, @@ -1129,7 +1133,9 @@ def is_guest_account(email: str) -> bool: return get_member_account_type(email) == "guest" -def guest_provision_guard(conn, inviter_email: str, recipient_email: str) -> str | None: +def guest_provision_guard( + conn, inviter_email: str, recipient_email: str, admin: bool = False +) -> str | None: """Decide whether a guest may be sent (pure — runs on any connection). Abuse-protection rules (operator decision, 2026-11-08): @@ -1139,6 +1145,12 @@ def guest_provision_guard(conn, inviter_email: str, recipient_email: str) -> str recipient refuses, as does an existing portal row under that email (they already have/had a guest account or are a member). + admin=True is the ADMIN-PANEL path: the inviter is the co-op's admin + account, not a member, so the member-side inviter rules (active member, + one-per-30-days) do NOT apply — admins are not bound by the member's + monthly cap. The recipient-side rules still run on every path: one guest + per recipient ever, and no existing portal row. + Returns None when the guest may proceed, else a human-readable refusal. """ inviter = (inviter_email or "").strip().lower() @@ -1149,19 +1161,20 @@ def guest_provision_guard(conn, inviter_email: str, recipient_email: str) -> str return "A valid recipient email is required" if inviter == recipient: return "You cannot guest the account you are guest access from" - row = conn.execute( - "SELECT active, account_type FROM members WHERE email = ?", (inviter,) - ).fetchone() - if not row or not row[0] or (row[1] or "member") == "guest": - return f"Inviter {inviter} is not an active member account" - recent = conn.execute( - "SELECT COUNT(*) FROM guests WHERE inviter_email = ? AND " - "created_at >= datetime('now', ?)", - (inviter, f"-{int(GUEST_PER_INVITER_DAYS)} days"), - ).fetchone()[0] - if recent: - return (f"{inviter} already sent a guest in the last " - f"{int(GUEST_PER_INVITER_DAYS)} days — one guest per member per month") + if not admin: + row = conn.execute( + "SELECT active, account_type FROM members WHERE email = ?", (inviter,) + ).fetchone() + if not row or not row[0] or (row[1] or "member") == "guest": + return f"Inviter {inviter} is not an active member account" + recent = conn.execute( + "SELECT COUNT(*) FROM guests WHERE inviter_email = ? AND " + "created_at >= datetime('now', ?)", + (inviter, f"-{int(GUEST_PER_INVITER_DAYS)} days"), + ).fetchone()[0] + if recent: + return (f"{inviter} already sent a guest in the last " + f"{int(GUEST_PER_INVITER_DAYS)} days — one guest per member per month") ever = conn.execute( "SELECT COUNT(*) FROM guests WHERE recipient_email = ?", (recipient,) ).fetchone()[0] @@ -2534,6 +2547,14 @@ def guest_invitation_email(name: str, setup_url: str, inviter_name: str = "") -> assets. No Loomio/Matrix — guests receive no member invitations. """ subject = "You've been invited to the Inference Cooperative" + # Neutral greeting: no name is guessed from the email — the guest chooses + # their own display name during Cloudron account setup. + if name: + greet = f"Hi {name}," + h1 = f"You're invited, {name}" + else: + greet = "Hi there," + h1 = "You're invited" if inviter_name: invited_line = f"{inviter_name} has invited you to the Inference Cooperative" html_invited = f"{inviter_name} has invited you to the Inference Cooperative" @@ -2541,7 +2562,7 @@ def guest_invitation_email(name: str, setup_url: str, inviter_name: str = "") -> invited_line = "A member has invited you to the Inference Cooperative" html_invited = "A member has invited you to the Inference Cooperative" text = ( - f"Hi {name},\n\n" + f"{greet}\n\n" f"{invited_line}.\n\n" "It's a member-governed AI cooperative that runs open models on\n" "renewable energy.\n\n" @@ -2573,7 +2594,7 @@ def guest_invitation_email(name: str, setup_url: str, inviter_name: str = "") -> -

You're invited, {name}

+

{h1}

{html_invited} — a member-governed AI cooperative running open models on renewable energy.

Your invitation comes with $5 of credits for the co-op's chat and API. Access runs for {int(GUEST_DAYS)} days from today; the credits themselves don't expire early.

Create your account @@ -2704,7 +2725,13 @@ def guest_limits_email(name: str, reason: str) -> tuple[str, str, str]: return subject, text, html -async def provision_guest_member(email: str, name: str, inviter_email: str) -> str: +async def provision_guest_member( + email: str, + name: str, + inviter_email: str, + admin: bool = False, + inviter_display: str | None = None, +) -> str: """Provision a GUEST account end-to-end (operator guest access feature). A guest gives a friend $5 of credits with no Open Collective membership: @@ -2714,18 +2741,23 @@ async def provision_guest_member(email: str, name: str, inviter_email: str) -> s (budget_duration None — the cap is the guest's whole life); * the portal row marked account_type='guest' with the $5 held as non-expiring credit (the credits model) and guest_expires_at set; - * the guest INVITATION email (inviter named, the $5 credits, the setup - link — the invitation and the guest welcome in one) and the + * the guest INVITATION email (inviter named, the $5 credits, the + setup link — the invitation and the guest welcome in one) and the newsletter subscription; * a guests-ledger row recording inviter → recipient; * NO Loomio/Matrix invites, no docs-RAG members group. Reuses provision_member's internals (user create, team, key, store) rather than duplicating them. Guards (guest_provision_guard) enforce one guest per - inviter per 30 days and one guest per recipient ever. + inviter per 30 days and one guest per recipient ever — except on the + admin path (admin=True), where the inviter is the co-op's admin account + and the member's monthly cap does not apply. + + inviter_display overrides the invitation email's inviter line (the admin + path passes a co-op label instead of resolving the inviter's member slug). """ conn = get_db() - refusal = guest_provision_guard(conn, inviter_email, email) + refusal = guest_provision_guard(conn, inviter_email, email, admin=admin) conn.close() if refusal: raise HTTPException(429, refusal) @@ -2768,14 +2800,19 @@ async def provision_guest_member(email: str, name: str, inviter_email: str) -> s setup_url = await cloudron_get_invite_link(user_id) # Name the inviter in the invitation: their member slug (the username the # co-op knows them by — the members table has no display-name column). + # The admin path passes inviter_display instead (a co-op label — the + # invitation comes from the co-op, not from a member). inviter_name = "" - conn = get_db() - row = conn.execute( - "SELECT slug FROM members WHERE email = ?", (inviter_email.strip().lower(),) - ).fetchone() - conn.close() - if row and row[0]: - inviter_name = row[0] + if inviter_display is not None: + inviter_name = inviter_display + else: + conn = get_db() + row = conn.execute( + "SELECT slug FROM members WHERE email = ?", (inviter_email.strip().lower(),) + ).fetchone() + conn.close() + if row and row[0]: + inviter_name = row[0] # ONE email at guest time: the invitation is also the guest welcome (merged, # not two mails to the same person at once). subject, text, html = guest_invitation_email(name, setup_url, inviter_name) @@ -4174,29 +4211,42 @@ async def admin_provision(request: Request): @app.post("/admin/provision-guest") @limiter.limit("20/minute") async def admin_provision_guest(request: Request): - """Provision a GUEST account (the operator's guest access action). + """Provision a GUEST account (the admin panel's invite action). - Same auth as /admin/provision (X-Admin-Token). Body: {"email": "...", - "name": "...", "inviter": ""}. Runs the guest - pipeline: Cloudron user in the guests group, a $5 LiteLLM team with NO - monthly reset, the portal row marked as a guest with a GUEST_DAYS expiry, - the guest welcome email, the newsletter subscription, and a guests-ledger - row recording inviter → recipient. Abuse guards: one guest per inviter per - 30 days; one guest per recipient ever; the inviter must be an active - member. No Loomio/Matrix. + Same auth as /admin/provision (X-Admin-Token). Body: {"email": "..."} — + ONE field, the standard guest-invite form. No name is collected and none + is derived: the guest chooses their own display name and username during + Cloudron account setup, and the invitation email greets neutrally. The + inviter is always the co-op's ADMIN account + (ADMIN_INVITER_EMAIL, default info@inference.coop) — the panel has no + inviter input, and the ledger records the admin account. + + Runs the guest pipeline: Cloudron user in the guests group, a $5 LiteLLM + team with NO monthly reset, the portal row marked as a guest with a + GUEST_DAYS expiry, the guest welcome email, the newsletter subscription, + and a guests-ledger row recording inviter → recipient. Admin invites + provision IMMEDIATELY (no pending state — the operator IS the approver) + and are NOT bound by the member's one-per-30-days cap; the recipient-side + guards still run (one guest per recipient ever, no existing portal row). + No Loomio/Matrix. """ _verify_admin_token(request) body = await request.json() email = (body.get("email") or "").strip().lower() - name = (body.get("name") or "").strip() or email.split("@")[0] - inviter = (body.get("inviter") or body.get("inviter_email") or "").strip().lower() + # No name is collected and none is derived: the guest chooses their own + # display name and username during Cloudron account setup, and the + # invitation email greets neutrally. + name = (body.get("name") or "").strip() if not email or "@" not in email: raise HTTPException(400, "A valid recipient email is required") - if not inviter or "@" not in inviter: - raise HTTPException(400, "The inviting member's email ('inviter') is required") + # The inviter is the admin account, never a caller-supplied member email: + # this endpoint speaks for the operator, and admins have no monthly cap. + inviter = ADMIN_INVITER_EMAIL - user_id = await provision_guest_member(email, name, inviter) + user_id = await provision_guest_member( + email, name, inviter, admin=True, inviter_display="The co-op" + ) return { "status": "provisioned", "account_type": "guest", "email": email, "inviter": inviter, "user_id": user_id, "budget": GUEST_BUDGET, @@ -4346,7 +4396,7 @@ async def admin_guest_request_approve(request_id: int, request: Request): raise HTTPException(409, f"Request #{request_id} is already {status}") try: user_id = await provision_guest_member( - recipient, name or recipient.split("@")[0], inviter) + recipient, name or "", inviter) except HTTPException as e: conn = get_db() conn.execute( @@ -6245,8 +6295,7 @@ async def broker_guest_invite(request: Request): "friend gets their account and invitation email, and " "the status shows here."), } - user_id = await provision_guest_member( - recipient, name or recipient.split("@")[0], email) + user_id = await provision_guest_member(recipient, name, email) return { "status": "provisioned", "account_type": "guest", "email": recipient, "inviter": email, "user_id": user_id, "budget": GUEST_BUDGET,