Coverage for notifications/preferences.py: 91.67%
12 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 20:17 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 20:17 +0000
1"""User notification preference defaults (SB-57).
3The `user_notification_preferences` table stores per-user opt-in flags for each
4push notification event type. Missing rows fall back to these defaults — so
5existing users keep the current behavior with zero migration backfill.
7Defaults intentionally mirror the pre-SB-57 behavior:
8 - kickoff/goal/halftime/fulltime → ON (the events that fire today).
9 - yellow_card/red_card → OFF (previously hard-coded skip in
10 dispatcher.py).
12Keeping defaults in code (not in the DB CHECK / DEFAULT clause) means we can
13adjust them later without a migration.
14"""
16from __future__ import annotations
18EVENT_TYPES: tuple[str, ...] = (
19 "kickoff",
20 "goal",
21 "halftime",
22 "fulltime",
23 "yellow_card",
24 "red_card",
25)
27# Events only a squad's player channel receives (SB-1610). Never a fan channel,
28# a club channel, or anyone's personal push: they are content for the players.
29# Kickoff reminders join this list once the delivery worker (SB-1604) can send
30# at a set time; see docs/08-integrations/live-match-notifications.md.
31PLAYER_EVENT_TYPES: tuple[str, ...] = ("lineup",)
33# Every event a chat channel can be limited to, in display order.
34CHANNEL_EVENT_TYPES: tuple[str, ...] = EVENT_TYPES + PLAYER_EVENT_TYPES
37def is_player_only(event_type: str) -> bool:
38 return event_type in PLAYER_EVENT_TYPES
41# What setup screens call each event. Keys match CHANNEL_EVENT_TYPES exactly.
42EVENT_LABELS: dict[str, str] = {
43 "kickoff": "Kickoff",
44 "goal": "Goals",
45 "halftime": "Halftime",
46 "fulltime": "Full time",
47 "yellow_card": "Yellow cards",
48 "red_card": "Red cards",
49 "lineup": "Lineup posted",
50}
52DEFAULT_PREFERENCES: dict[str, bool] = {
53 "kickoff": True,
54 "goal": True,
55 "halftime": True,
56 "fulltime": True,
57 "yellow_card": False,
58 "red_card": False,
59}
62def merge_with_defaults(stored: dict[str, bool] | None) -> dict[str, bool]:
63 """Return a complete prefs dict, with `stored` overriding defaults."""
64 return {**DEFAULT_PREFERENCES, **(stored or {})}
67def validate_event_type(event_type: str) -> bool:
68 return event_type in EVENT_TYPES