mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-11 12:07:53 +01:00
The tally was one integer per person, deleted after each post. Nothing dated survived it, so weeks, months, records, streaks and weekday averages were all unanswerable. This replaces it with an append-only per-message log holding metadata only -- never a message body -- and rebuilds the nightly report on top. What changes for the group: a photo posted without a caption now counts (it was worth zero), stickers count, the bot's own messages no longer do, people who said nothing appear with a 0, and the report covers the previous full calendar day in Europe/Warsaw instead of "since the last post" -- which used to merge two days whenever a group went quiet. Notable decisions, each with the trap it avoids: - Events and people are PERMANENT_TABLES: neither forget_group nor a future SCHEMA_VERSION bump may drop them. Both paths used to cost a day's counter and would now cost the entire history, and forget_group fires on a listing that merely omits members. - is_own comes from which envelope branch carried the payload, not from comparing identities. The account is spelled as a uuid on some frames and a +E.164 number on others, so any comparison misses one of them. - The roster is stashed in refresh(), not _adopt(), which returns early once the watched set stops changing -- i.e. always, in the steady state. - The roster matches on uuid OR number because /v1/groups spells kuhy as +48123456789 while his messages carry an ACI uuid. Joining on uuid alone would have listed him twice every night, once with 0. - Members whose display name we have never seen are counted, not named: their only spellings are a uuid or a phone number. - (group, uuid, sent_at_ms) is the primary key, so a websocket redelivery after a reconnect cannot inflate the counts. - Days come from a stored Warsaw date string, never a timestamp range: 2026-03-29 is 23 hours long and 2026-10-25 is 25. - Polish number agreement is a real rule with a 12-14 exception, so "44 znaki" and "45 znaków" are both spelled correctly. config.py and startup.py were two lines under the 250-line cap and both had to grow, so config_env and startup_retry come out of them first, as pure moves. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""Which messages count toward the daily tally, and which chats we listen to."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from signal_bot.discovery import GroupDirectory
|
|
from signal_bot.models import IncomingMessage
|
|
|
|
|
|
def watched_group(message: IncomingMessage, directory: GroupDirectory) -> str | None:
|
|
"""Return the canonical id of the watched group a message came from.
|
|
|
|
The bot answers in every group it has been added to, and in nothing
|
|
else. Direct messages are rejected here by an explicit lookup rather
|
|
than a falsy check: a DM carries no group id at all, and one must never
|
|
reach a handler no matter how the surrounding code is later rearranged.
|
|
|
|
It returns the id rather than a bool so that the gate and the reply
|
|
router are the same decision. Answering "may this run?" and "where does
|
|
the answer go?" separately is how a bot ends up replying to the wrong
|
|
group.
|
|
|
|
Args:
|
|
message: The parsed inbound message.
|
|
directory: The live set of groups the bot is a member of.
|
|
|
|
Returns:
|
|
The canonical group id, or None for a direct message or any group
|
|
the bot is not scoped to.
|
|
"""
|
|
if message.group_id is None:
|
|
return None
|
|
return directory.resolve(message.group_id)
|
|
|
|
|
|
def should_count(message: IncomingMessage) -> bool:
|
|
"""True when a message should be recorded against its sender.
|
|
|
|
A reaction is not a message -- it is an annotation on someone else's --
|
|
so it stays out. Everything a person actually sent counts, including a
|
|
sticker and including a photo posted with no caption. Until 0.1.0 the
|
|
last clause was ``bool(message.text)``, which silently valued a
|
|
photo-only message at zero.
|
|
|
|
The bot's own messages never count. ``is_own`` comes from the envelope
|
|
branch rather than from comparing identities, which is what makes it
|
|
hold on the frames that carry only one spelling of the account.
|
|
"""
|
|
if message.is_reaction or message.is_own:
|
|
return False
|
|
return bool(message.text) or bool(message.attachment_types) or message.has_sticker
|
|
|
|
|
|
def media_kind(message: IncomingMessage) -> str:
|
|
"""Name the kind of media a message carries, or "" for plain text.
|
|
|
|
Several different kinds in one message collapse to "mixed": the group
|
|
reads a count of photos, not a taxonomy, and a message carrying a photo
|
|
and a video is neither.
|
|
"""
|
|
if message.has_sticker and not message.attachment_types:
|
|
return "sticker"
|
|
kinds = {_kind_of(content_type) for content_type in message.attachment_types}
|
|
if not kinds:
|
|
return ""
|
|
if len(kinds) > 1:
|
|
return "mixed"
|
|
return kinds.pop()
|
|
|
|
|
|
def _kind_of(content_type: str) -> str:
|
|
"""Map one MIME type onto the coarse kind the group cares about."""
|
|
major, _, _ = content_type.partition("/")
|
|
if major in {"image", "video", "audio"}:
|
|
return major
|
|
return "file"
|