signal-bot/signal_bot/manage/deps.py
Krzysztof kuhy Rudnicki 72e90acb47
Some checks are pending
CI / check (push) Waiting to run
CI / web (push) Waiting to run
Let the operator add aliases from the Commands tab
A custom alias is data the command table is built from, so it cannot be a
live switch like the ones already on the tab. Instead the management page
writes aliases_custom.json in the bot's state directory and then asks the
bot to reload, in one step: whenever the answer is anything but `reloaded`
-- a word another command has, a name no command has, a bot still
connecting -- the file is restored byte for byte and the bot's own reason is
shown next to the field. What is on disk is therefore always what the bot
serves, and a typo cannot sit in the file poisoning every later reload.

The registry appends the operator's words to the shipped list at
registration, which keeps `!pomoc` on the shipped spelling and routes the
new words through the same clash check as aliases.json. An alias for a name
no command has fails the whole build (ConfigError -> `kept`), because a
silently ignored entry is how a file drifts. The tagger treats custom
aliases as reserved words too, so a person cannot be given one.

registry.py crossed the 250-line cap and now re-exports the record and the
error types from command.py; the bot's state directory lookup, previously
duplicated in pack.py and the suggestions export, is state_dir.py.

Proven end to end: !e2ekot is unknown, the file is written and reload.sh
runs, !e2ekot posts a cat, a file naming a command that does not exist is
refused with 409 and the previous table still answers.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-04 16:20:05 +02:00

78 lines
2.7 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Everything the management app is pointed at, as one value.
Every path is a field rather than a module constant. That is what lets the
tests point the whole app at ``tmp_path`` and never go near the real ``.env``
(mode 0600, holding the live account) or the real album (mode 0700, holding
photographs of people who did not agree to be in a test fixture).
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pathlib import Path
import httpx
from signal_bot.config import Config
from signal_bot.pack_album import Album
@dataclass(frozen=True, slots=True)
class ManageDeps:
"""What one running management page is bound to."""
config: Config
"""The bot's own configuration, already validated by load_config."""
env_path: Path
"""The .env the page edits. Not derived: the tests point it elsewhere."""
dist_dir: Path
"""Where `pnpm build` puts the page. Absent until it has been run."""
album: Album
"""The one pack this session tags, chosen by --dir."""
database_path: Path
"""The bot's database, read read-only.
Required, with no default, and that is the point. Config.database_path
is the relative "signal_bot.sqlite3", pytest runs from the repo root,
and the operator's real database sits there -- so a default here would
quietly point the whole test suite at live private data.
"""
export_path: Path
"""Where the export is written. Never inside the repository.
A file the group can write into, inside a public git tree, is both a
leak and a wedge: the personal-data gate would refuse every future
commit once somebody's name landed in it, and it cannot recognise the
misspelled names this particular file is full of.
"""
aliases_path: Path
"""The operator's own trigger words, ``custom_aliases.FILE_NAME``.
Written here and read by the bot at every reload, so it has to be the
same path both processes resolve -- which is why tool/web.py takes it
from ``custom_aliases.default_path`` rather than naming a file of its
own. Required for the same reason database_path is: a default would
point the test suite at the operator's real file.
"""
client: httpx.AsyncClient
"""Shared, so the proxied /healthz and the group listing pool sockets."""
port: int
"""The port this app is served on, so the Host guard knows what to allow."""
@property
def bot_url(self) -> str:
"""The bot's own loopback base URL, which this app proxies to."""
return f"http://{self.config.bind_host}:{self.config.bind_port}"