Coverage for notifications/membership.py: 21.21%
63 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:03 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:03 +0000
1"""Who may see and join a notification channel (SB-1607). Pure: no IO.
3A channel is visible to a viewer as one of:
5- `joinable`: their club or squad entitles them; joining needs no approval.
6- `requestable`: they may ask, and a manager decides (a fan and their club's
7 squad fan channel).
8- `hidden`: they may not know it exists.
10Rules:
12- Players see only player channels, and only for a squad they are currently on
13 (or their club's, for a club player channel), at U14 and above.
14- Fans never see player channels. A club fan channel is joinable by the club's
15 fans; a squad fan channel is requested.
16- Managers may join the channels of what they manage; team managers also join
17 their club's fan channel, since it is their My Club.
18- Global channels are never listed: they are the operator's feed.
20A grant (`channel_members` row) is not enough on its own. The invite link is
21shown only while the viewer is still not `hidden`, so a player who leaves a
22squad loses the link without anyone revoking anything.
23"""
25from __future__ import annotations
27from dataclasses import dataclass
28from typing import TYPE_CHECKING, Literal
30from notifications.audience import players_allowed_at
32if TYPE_CHECKING:
33 from mt_tools.my_team import ChannelViewer
35Access = Literal["hidden", "requestable", "joinable"]
36MemberStatus = Literal["requested", "active", "denied", "revoked"]
39@dataclass(frozen=True)
40class ChannelFacts:
41 """What the rules need about a channel. `club_id` is its own club, or its team's."""
43 scope_type: str
44 audience: str
45 club_id: int | None
46 team_id: int | None = None
47 age_group_id: int | None = None
48 age_group_name: str | None = None
51def channel_access(viewer: ChannelViewer, channel: ChannelFacts) -> Access:
52 if channel.scope_type not in ("club", "team"):
53 return "hidden"
54 if viewer.kind == "admin":
55 return "joinable"
56 if channel.audience == "players":
57 return _player_channel(viewer, channel)
58 return _fan_channel(viewer, channel)
61def manages(viewer: ChannelViewer, channel: ChannelFacts) -> bool:
62 if viewer.kind == "club_manager":
63 return viewer.club_id is not None and viewer.club_id == channel.club_id
64 if viewer.kind == "team_manager":
65 return channel.scope_type == "team" and viewer.team_id is not None and viewer.team_id == channel.team_id
66 return False
69def _player_channel(viewer: ChannelViewer, channel: ChannelFacts) -> Access:
70 if channel.scope_type == "team" and not players_allowed_at(channel.age_group_name):
71 return "hidden"
72 if manages(viewer, channel):
73 return "joinable"
74 if viewer.kind != "player":
75 return "hidden"
76 for squad in viewer.squads:
77 if not players_allowed_at(squad.age_group_name):
78 continue
79 if channel.scope_type == "team" and (squad.team_id, squad.age_group_id) == (
80 channel.team_id,
81 channel.age_group_id,
82 ):
83 return "joinable"
84 if channel.scope_type == "club" and squad.club_id is not None and squad.club_id == channel.club_id:
85 return "joinable"
86 return "hidden"
89def _fan_channel(viewer: ChannelViewer, channel: ChannelFacts) -> Access:
90 if viewer.kind == "player":
91 return "hidden"
92 if manages(viewer, channel):
93 return "joinable"
94 same_club = viewer.club_id is not None and viewer.club_id == channel.club_id
95 if channel.scope_type == "club":
96 return "joinable" if same_club and viewer.kind in ("fan", "team_manager") else "hidden"
97 return "requestable" if same_club and viewer.kind == "fan" else "hidden"
100State = Literal["can_join", "can_request", "requested", "member", "denied", "revoked"]
103def viewer_state(access: Access, status: MemberStatus | None) -> State | None:
104 """What the viewer sees for a channel; None when it is hidden from them."""
105 if access == "hidden":
106 return None
107 if status == "active":
108 return "member"
109 if status in ("requested", "denied", "revoked"):
110 return status
111 return "can_join" if access == "joinable" else "can_request"
114def can_be_granted(target_access: Access) -> bool:
115 """A manager can only grant access to someone the rules already allow to ask or join."""
116 return target_access != "hidden"