Coverage for notifications/registry.py: 95.83%

22 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 18:21 +0000

1"""The set of chat platforms MT can post to. 

2 

3This is the only list of platforms in the codebase. API validation, the setup 

4UI (through GET /api/notifications/platforms) and delivery all read it. To add 

5a platform, write an adapter in `notifications/adapters/` and register it below; 

6`tests/unit/test_notification_adapters.py` holds every adapter to the contract 

7automatically. 

8""" 

9 

10from __future__ import annotations 

11 

12from notifications.adapters.base import ChannelAdapter, UnsupportedPlatformError 

13from notifications.adapters.discord import DiscordAdapter 

14from notifications.adapters.slack import SlackAdapter 

15from notifications.adapters.telegram import TelegramAdapter 

16 

17_adapters: dict[str, ChannelAdapter] = {} 

18 

19 

20def register(adapter: ChannelAdapter) -> None: 

21 """Add a platform. A duplicate id is a programming error, so it raises.""" 

22 if adapter.id in _adapters: 

23 raise ValueError(f"Platform already registered: {adapter.id}") 

24 _adapters[adapter.id] = adapter 

25 

26 

27def get_adapter(platform: str) -> ChannelAdapter: 

28 """Return the adapter for `platform`, or raise UnsupportedPlatformError.""" 

29 try: 

30 return _adapters[platform] 

31 except KeyError: 

32 raise UnsupportedPlatformError(f"Unsupported platform: {platform}") from None 

33 

34 

35def all_adapters() -> list[ChannelAdapter]: 

36 """Every platform, in registration order (the order the UI shows).""" 

37 return list(_adapters.values()) 

38 

39 

40def platform_ids() -> list[str]: 

41 return list(_adapters) 

42 

43 

44register(TelegramAdapter()) 

45register(DiscordAdapter()) 

46register(SlackAdapter())