Skip to content

Add Supabase persistence for members, violations & join requests (closes #1) - #19

Open
Kaaveh wants to merge 3 commits into
TechImmigrants:mainfrom
Kaaveh:main
Open

Add Supabase persistence for members, violations & join requests (closes #1)#19
Kaaveh wants to merge 3 commits into
TechImmigrants:mainfrom
Kaaveh:main

Conversation

@Kaaveh

@Kaaveh Kaaveh commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #1.

Moderation state currently lives in an in-memory _warnings dict, so every
warning/mute/ban count is lost when the bot restarts. This PR introduces a
Supabase-backed data layer that persists moderation state and adds an audit
trail for member tracking and join-request decisions.

Changes

  • New src/services/db.py — a Supabase data-access layer with helpers for
    warning counts, violation logging, member upserts, and join-request audit
    logging. The synchronous Supabase calls are dispatched via
    asyncio.to_thread so DB I/O never blocks the bot's event loop.
  • src/handlers/moderation.py — replaces the in-memory _warnings dict.
    The warning count is derived from the violations table, and each
    warn/mute/ban is recorded. The violation is written before enforcement so
    the count advances even if a Telegram API call fails.
  • src/handlers/join_requests.py — logs every approve/decline decision to
    join_requests and upserts approved users into members.
  • src/handlers/admin_commands.py + src/main.py — adds an admin-only
    /violations <user_id> command to query a user's violation history.
  • infra/schema.sql — idempotent DDL for the members, violations, and
    join_requests tables (with indexes on user_id).
  • README.md — documents the db service, schema, and Supabase setup.
  • Teststests/test_db.py covers the fallback path; removed two stale
    welcome_message assertions in tests/test_spam_checker.py that were
    already failing on the branch (leftovers from the merged welcome refactor,
    now covered by tests/test_welcome.py).

Graceful degradation

If SUPABASE_URL / SUPABASE_KEY are unset (local dev, CI), the bot still
runs: warning counts fall back to an in-memory counter for the lifetime of the
process. Persistence across restarts applies only when Supabase is configured.
No new dependencies — supabase was already in requirements.txt and the env
vars already existed in config.py / .env.example.

Setup

Apply the schema once:

psql "$SUPABASE_DB_URL" -f infra/schema.sql   # or paste into the Supabase SQL editor

claude and others added 3 commits June 9, 2026 03:55
Replaces the in-memory _warnings dict in moderation with a Supabase-backed
data layer so moderation state survives bot restarts.

- New services/db.py: cached Supabase client plus helpers for warning
  counts, violation logging, member upserts, and join-request audit logs.
  Falls back to an in-memory counter when Supabase is unconfigured so the
  bot and tests still run without a database.
- moderation.py: derive warning count from the DB and log each warn/mute/ban
  as a violation.
- join_requests.py: record every approve/decline decision and upsert
  approved members for the audit trail.
- admin_commands.py + main.py: add /violations <user_id> so admins can
  query a user's violation history.
- infra/schema.sql: idempotent DDL for members, violations, join_requests.
- Drop stale welcome_message assertions left over from the welcome refactor.

Closes #1
- db.py: dispatch the synchronous Supabase calls through asyncio.to_thread
  so network I/O no longer blocks the bot's event loop; public helpers are
  now async and awaited by callers.
- get_warning_count: add limit(1) so count="exact" no longer pulls back
  every matching row just to read the count.
- moderation.py: decide the action, then record the violation before
  enforcing it, so the warning count advances even if a Telegram call fails
  and the read/record window stays small.
- Update join_requests, admin_commands, and tests to await the async helpers.
Add Supabase persistence for members, violations & join requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Supabase database for member tracking & moderation logs

2 participants