mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 18:07:29 +01:00
A photo sent to the chat no longer enters the album by itself. pack_add strips it, checks it against the album and the queue, and writes it to pending/ with a sidecar naming the people and labels the sender proposed; the reply is "czeka na zatwierdzenie". The management page lists the queue above the tagger, with the proposal pre-ticked, and Approve moves the file in, mints the labels, records approved_at and asks the bot to rebuild its table through /_reload -- the same publish path every other data change takes, so PackHolder.reload is gone. A reload the bot refuses is reported on the page, not rolled back: the photo is in the album either way. The personal-data gate now reads pending/*.json too, so a label that is still waiting for approval is forbidden in the tree already. The e2e run boots the management page beside the bot with the same environment, proves the queue is stripped and never reaches media/, approves it through the real routes, and has !losowe answer with the photo afterwards without a restart; exiftool is found through media_strip.locate() there rather than PATH, which lacks vendor_perl on Arch. !gadaj is dropped from !odcisz: it was within a typo of !dodaj, and the nearest-command suggestion must never be a coin toss. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
105 lines
3.8 KiB
Python
105 lines
3.8 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""Turning the attachments on one message into stripped, named files.
|
|
|
|
Split out of pack_add.py at the 250-line cap, along the seam that was already
|
|
there: that half decides *whether* a command runs and what it answers, this
|
|
half does the reading, the sniffing and the stripping. Nothing here writes
|
|
to the album: since the approval queue, the only place an upload goes is
|
|
``pending/``, and ``pack_pending`` owns that.
|
|
|
|
A file the album already has, or one that is already waiting, is counted
|
|
rather than stored again, and counted separately so the reply can say which
|
|
it was -- "already there" and "already queued" ask for different things of
|
|
the sender.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from dataclasses import dataclass, field
|
|
from typing import TYPE_CHECKING
|
|
|
|
from signal_bot.media_sniff import sniff
|
|
from signal_bot.pack import MEDIA_DIR
|
|
from signal_bot.pack_pending import names as queued_names
|
|
from signal_bot.pack_store import content_name
|
|
|
|
if TYPE_CHECKING:
|
|
from signal_bot.context import CommandContext
|
|
from signal_bot.media_strip import Stripper
|
|
from signal_bot.pack_holder import PackHolder
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class Ingested:
|
|
"""What one message's attachments turned into."""
|
|
|
|
fresh: dict[str, bytes] = field(default_factory=dict)
|
|
"""Files new to both the album and the queue, by their content name."""
|
|
duplicates: int = 0
|
|
"""How many were already in the album."""
|
|
queued: int = 0
|
|
"""How many were already waiting for approval."""
|
|
|
|
|
|
async def ingest(ctx: CommandContext, holder: PackHolder, stripper: Stripper) -> Ingested:
|
|
"""Fetch, sniff, strip and name every attachment on the message.
|
|
|
|
The album check is against ``media/`` on disk, not the loaded pack's tag
|
|
table: a file the tagger has not opened yet is in the album all the same,
|
|
and the point of the check is that no byte-identical copy is stored twice.
|
|
"""
|
|
directory = holder.pack.directory
|
|
waiting = queued_names(directory)
|
|
result = Ingested()
|
|
for attachment_id in ctx.message.attachment_ids:
|
|
payload = await _fetch(ctx, attachment_id)
|
|
cleaned = _clean(payload, stripper)
|
|
if cleaned is None:
|
|
continue
|
|
name, body = cleaned
|
|
if (directory / MEDIA_DIR / name).exists():
|
|
result.duplicates += 1
|
|
elif name in waiting or name in result.fresh:
|
|
# Twice in one message is "already waiting" too: it is, as of a
|
|
# moment ago.
|
|
result.queued += 1
|
|
else:
|
|
result.fresh[name] = body
|
|
return result
|
|
|
|
|
|
async def _fetch(ctx: CommandContext, attachment_id: str) -> bytes | None:
|
|
"""Read one attachment, then delete signal-cli's own copy of it.
|
|
|
|
Deleted whether the upload is accepted or refused: the point is that no
|
|
copy survives outside the album, and a refused file is exactly the one
|
|
nobody wants left lying in the container.
|
|
"""
|
|
try:
|
|
payload = await ctx.transport.fetch_attachment(attachment_id)
|
|
except Exception:
|
|
logger.exception("could not fetch an attachment")
|
|
payload = None
|
|
try:
|
|
await ctx.transport.delete_attachment(attachment_id)
|
|
except Exception:
|
|
logger.exception("could not delete the server-side attachment copy")
|
|
return payload
|
|
|
|
|
|
def _clean(payload: bytes | None, stripper: Stripper) -> tuple[str, bytes] | None:
|
|
"""Sniff and strip one upload, or None when it is not storable."""
|
|
if payload is None:
|
|
return None
|
|
kind = sniff(payload)
|
|
if kind is None:
|
|
logger.warning("refusing an upload whose format is not on the allowlist")
|
|
return None
|
|
cleaned = stripper.strip(payload, kind)
|
|
if cleaned is None:
|
|
return None
|
|
return content_name(cleaned, kind.suffix), cleaned
|