Phase 5: groups

Group wallets, group passes, spending rules and admin-only settings.

- Groups recorded from my_chat_member; admin rights verified with
  getChatMember and cached for five minutes
- Group context in entitlement resolution: group pass, group credits under
  the spending rule (anyone, admins only, capped per person per day) and
  the group's daily cap; refunds go back to the wallet that paid
- Group gate before any lookup: bot on or off, who may use it, allowed
  and blocked sites, plus the owner's maintenance mode
- /settings with inline toggles checked on every press, /grouplang,
  /autodetect, /allow, /block, /spending, /groupwallet, /grouppass,
  /groupstats
- Deep links with the target pre-selected: paying for a group happens in
  the private chat; a refused download in a group offers "Buy for me" and
  "Add group credits"
- Optional link detection, tidy mode, default quality and language per
  group; per-member usage
- /quality, /language, /app, /privacy, /terms, /support
- Remaining owner tools: /stats, /queue all, /user, /group, /ban, /unban,
  /grant (passes and gift codes), /credit, /refund, /blocklist,
  /broadcast with confirmation, /maint, /reload i18n

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 09:46:32 +00:00
co-authored by Claude Fable 5.2
parent 7d4893bc3f
commit 51371da0e0
23 changed files with 3231 additions and 81 deletions
+22
View File
@@ -43,6 +43,7 @@ phase; sections for later phases say so.
| `internal/billing/lnbits` | LNbits HTTP client. |
| `internal/cache` | file_id and stored-copy cache, TTLs, eviction, takedown. |
| `internal/ffmpeg` | Transcoding lower profiles from a stored copy. |
| `internal/groups` | Group admin verification with a cache, host list matching. |
| `internal/links` | URL extraction, validation, redaction. |
| `internal/storage` | `Storage` interface and the local disk backend. |
| `internal/db` | Schema migrations and queries for SQLite and Postgres. |
@@ -329,6 +330,27 @@ when over budget (least recently and least often used first, keeping their
`file_id`), and removes stored files that no live job and no cache entry
references.
## Groups
A group has a row in `groups`, optional `group_settings`, and a wallet of
its own (`wallets.owner_type = 'group'`). `group_usage_daily` counts what
each member used per day; it backs the spending caps and the
usage-by-member view.
For a request from a group, `service.entitle` loads the group's settings
and wallet, asks `groups.Admins` whether the requester is an admin, works
out whether this requester may spend group credits right now (rule,
per-person cap, group daily cap), and hands all of it to the resolver as
plain data. The resolver stays pure.
`service.gate` then applies what is not about money: maintenance mode,
whether the bot is on in the group, who may use it, and the site lists.
It runs before a link is even looked up, and again when a download starts.
Admin-only actions are checked in one place each: `requireAdmin` for
commands and `cbSetting` for buttons, both through `groups.Admins`, which
asks Telegram and caches the answer.
## Internationalization
Handlers never contain user-facing text. They call `i18n.Bundle` with a
+64
View File
@@ -452,3 +452,67 @@ Read from the LNbits source at `v0.12.12` (the target) and `v1.7.1` (current).
- Each worker updates its own yt-dlp on a schedule (`yt-dlp -U`).
`/ytdlp update` is delivered to workers with their next claim, so it
reaches every connected worker within seconds.
## Phase 5
### Groups
- **Groups are recorded** from `my_chat_member` updates, and also the first
time a command is used in one, in case that update was missed.
- **Admin rights come from Telegram only** (`getChatMember`), cached for
five minutes per group and user. A change of admins can therefore take up
to five minutes to reach the bot. The cache for a group is dropped when
the bot's own status in it changes.
- **Anonymous admins are not supported for commands.** A message sent "as
the group" carries no user to verify. Buttons work, because a button
press always carries the real user.
- **The settings panel is visible to every member**, so each button press
is checked again. A member who presses gets a notice and nothing changes.
- **A switched-off bot is silent in that group**, except for `/settings`
from an admin, which is how it is switched on again.
- **"Pass holders"** means: the requester has a pass of their own, or the
group has one, or they are an admin.
- **Spending rule "capped"** counts group credits a member spent today
(UTC). The first time it is chosen in the quick settings the cap defaults
to four downloads' worth.
- **Site lists** match a host and its subdomains. With an empty allow list
every site is allowed. At most 50 entries per list.
- **Group size and duration caps** can only lower what the group's pass or
credits allow, and only apply when the group pays. They are set from the
Mini App (Phase 6), not from a command.
- **Group language** overrides each member's language for everything the
bot says in that group.
- **Default quality** (group or personal) skips the card and starts that
format at once.
- **"Delete command messages"** needs the bot to be an admin with the right
to delete. Without it the delete fails quietly.
- **Auto-detect** takes the first link of a message. It needs the bot to
see ordinary messages (admin, or group privacy off), which the bot says
when the setting is turned on.
### Paying for a group
- **Anyone can pay into a group wallet.** What is bought belongs to the
group. Invoices only ever appear in the payer's private chat; the group
shows deep links (`?start=gw_<chat id>`) with the group pre-selected.
- A purchase for a group does not auto-start the download that was refused.
The requester presses the format button again.
- Refunds for downloads paid with group credits go back to the group.
### Owner tools
- Owner commands exist only in a private chat with a configured owner. For
everyone else, and for owners in groups, they are unknown commands.
- `/grant <id> <day|week|month>` and `/credit`, `/refund` take a user ID,
or a negative chat ID for a group. `/refund <id> pass <duration>` extends
a running pass.
- `/grant code ...` creates gift codes.
- `/broadcast` shows a preview and sends only after the owner presses the
confirm button. It goes to every user who is not banned, about 20
messages per second.
- `/maint on` refuses new downloads for everyone but owners. Running jobs
finish.
- `/reload i18n` needs `LOCALES_DIR`; catalogs built into the binary cannot
change at runtime.
- `/privacy`, `/terms` and `/support` are short texts in the catalog.
Replace them with your own before running a public bot.
+12
View File
@@ -158,6 +158,18 @@ Only in a private chat, only for the user IDs in `OWNER_IDS`.
| `/ytdlp update` | Update yt-dlp on every worker now |
| `/cache` | Cache size, hit rate, volume saved |
| `/purge <link>` | Take a video down: delete its files and block it for good |
| `/blocklist add site <host>` | Refuse a whole site. Also `add key <extractor:id>`, `remove`, and no argument to list. |
| `/stats` | Users, jobs, queue, cache, payments, and whether the ledger and balances agree |
| `/queue all` | Every queued and running job with its worker and tags |
| `/user <id>`, `/group <chat id>` | Look one up |
| `/ban <id>`, `/unban <id>` | A banned user gets no answers |
| `/grant <id> day\|week\|month` | Give a pass to a user, or to a group (negative ID) |
| `/grant code credits <sats> [uses]` | Create a gift code. Also `code day\|week\|month`. |
| `/credit <id> <sats>` | Add credits |
| `/refund <id> <sats>`, `/refund <id> pass <duration>` | Give sats back, or extend a pass |
| `/broadcast <text>` | Preview, then send to everyone on confirmation |
| `/maint on\|off` | Refuse new downloads for everyone but owners |
| `/reload i18n` | Reload translations from `LOCALES_DIR` |
## Backups