mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 20:37:03 +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
103 lines
3.8 KiB
Python
103 lines
3.8 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""The append-only message log: dedupe, naming, and the roster join."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from signal_bot.store import Store, open_store
|
|
from tests.test_store import GROUP, counted, event
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
def test_the_event_table_holds_no_message_body(store: Store) -> None:
|
|
"""The privacy guarantee, pinned so a later edit cannot quietly break it.
|
|
|
|
The statistics need to know somebody wrote 140 characters at 23:41, never
|
|
what they wrote. Any new column here is a deliberate decision, not a
|
|
convenient place to stash a body.
|
|
"""
|
|
columns = {
|
|
row["name"]
|
|
for row in store.events.connection.execute("PRAGMA table_info(message_events)").fetchall()
|
|
}
|
|
assert columns == {
|
|
"group_id",
|
|
"uuid",
|
|
"display_name",
|
|
"sent_at_ms",
|
|
"day",
|
|
"local_hour",
|
|
"media_kind",
|
|
"media_count",
|
|
"char_len",
|
|
"is_command",
|
|
"is_reply",
|
|
}
|
|
|
|
|
|
def test_events_accumulate(store: Store) -> None:
|
|
store.events.record(event(at=1))
|
|
store.events.record(event(at=2))
|
|
store.events.record(event(uuid="u2", name="Bob", at=3))
|
|
assert counted(store, GROUP) == 3
|
|
|
|
|
|
def test_a_replayed_frame_is_recorded_once(store: Store) -> None:
|
|
"""signal-cli replays undelivered frames when the bot reconnects.
|
|
|
|
Without ON CONFLICT DO NOTHING every restart would inflate the counts the
|
|
bot had just published, and nothing in the output would look wrong.
|
|
"""
|
|
store.events.record(event(at=7))
|
|
store.events.record(event(at=7))
|
|
assert counted(store, GROUP) == 1
|
|
|
|
|
|
def test_a_rename_does_not_rewrite_past_attribution(store: Store) -> None:
|
|
"""group_people follows the new name; the events keep the old one."""
|
|
store.events.record(event(name="Alice", at=1))
|
|
store.events.note_person(GROUP, "u1", "Alice", "", "2026-08-29")
|
|
store.events.record(event(name="Alicja", at=2))
|
|
store.events.note_person(GROUP, "u1", "Alicja", "", "2026-08-30")
|
|
assert store.events.people(GROUP)[0].display_name == "Alicja"
|
|
rows = store.events.connection.execute(
|
|
"SELECT display_name FROM message_events WHERE group_id = ? ORDER BY sent_at_ms",
|
|
(GROUP,),
|
|
).fetchall()
|
|
assert [r["display_name"] for r in rows] == ["Alice", "Alicja"]
|
|
|
|
|
|
def test_a_known_number_is_not_erased_by_an_empty_one(store: Store) -> None:
|
|
"""The roster join exists for exactly the people spelled by number."""
|
|
store.events.note_person(GROUP, "u1", "Alice", "+15550001111", "2026-08-29")
|
|
store.events.note_person(GROUP, "u1", "Alice", "", "2026-08-30")
|
|
assert store.events.people(GROUP)[0].number == "+15550001111"
|
|
|
|
|
|
def test_roster_matches_either_spelling(store: Store) -> None:
|
|
"""The listing gives a uuid for some members and a number for others."""
|
|
store.events.note_person(GROUP, "u1", "Alice", "+15550001111", "2026-08-29")
|
|
store.events.note_person(GROUP, "u2", "Bob", "", "2026-08-29")
|
|
assert [p.display_name for p in store.events.roster(GROUP, ["+15550001111"])] == ["Alice"]
|
|
assert [p.display_name for p in store.events.roster(GROUP, ["u2"])] == ["Bob"]
|
|
assert store.events.roster(GROUP, []) == []
|
|
assert store.events.roster(GROUP, ["nobody"]) == []
|
|
|
|
|
|
def test_first_day_is_none_without_history(store: Store) -> None:
|
|
assert store.events.first_day(GROUP) is None
|
|
store.events.record(event(day="2026-08-29"))
|
|
store.events.record(event(at=2, day="2026-08-30"))
|
|
assert store.events.first_day(GROUP) == "2026-08-29"
|
|
|
|
|
|
def test_events_survive_a_restart(tmp_path: Path) -> None:
|
|
db = str(tmp_path / "restart.sqlite3")
|
|
with open_store(db) as first:
|
|
first.events.record(event())
|
|
with open_store(db) as second:
|
|
assert counted(second, GROUP) == 1
|