73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Iterable
|
|
|
|
APP_MAIN = "main"
|
|
APP_MESSENGER = "messenger"
|
|
VALID_APPS = {APP_MAIN, APP_MESSENGER}
|
|
|
|
NOTIFICATION_CATEGORIES = (
|
|
("messages", "Сообщения"),
|
|
("tasks", "Задачи и доски"),
|
|
("requests", "Заявления"),
|
|
("procurement", "Закупки"),
|
|
("documents", "Документы и регламенты"),
|
|
("guests", "Гости"),
|
|
("calendar", "Календарь"),
|
|
("feed", "Новости"),
|
|
("other", "Остальные уведомления"),
|
|
)
|
|
CATEGORY_KEYS = tuple(key for key, _label in NOTIFICATION_CATEGORIES)
|
|
|
|
MESSAGE_VERBS = {
|
|
"chat_added_to_chat",
|
|
"chat_new_message",
|
|
"chat_reply",
|
|
"chat_mention",
|
|
"announcement_new_message",
|
|
}
|
|
|
|
PREFIX_CATEGORIES = (
|
|
(("task_", "board_"), "tasks"),
|
|
(("request_",), "requests"),
|
|
(("procurement_",), "procurement"),
|
|
(("document_", "regulation_"), "documents"),
|
|
(("guest_",), "guests"),
|
|
(("event_", "calendar_"), "calendar"),
|
|
(("feed_",), "feed"),
|
|
)
|
|
|
|
|
|
def notification_category(verb: str) -> str:
|
|
if verb in MESSAGE_VERBS or verb.startswith("chat_"):
|
|
return "messages"
|
|
for prefixes, category in PREFIX_CATEGORIES:
|
|
if verb.startswith(prefixes):
|
|
return category
|
|
return "other"
|
|
|
|
|
|
def default_routes() -> dict[str, list[str]]:
|
|
return {key: [APP_MAIN] for key in CATEGORY_KEYS}
|
|
|
|
|
|
def normalize_destinations(destinations: object) -> list[str]:
|
|
if not isinstance(destinations, Iterable) or isinstance(
|
|
destinations,
|
|
(str, bytes),
|
|
):
|
|
return [APP_MAIN]
|
|
normalized = []
|
|
for destination in destinations:
|
|
if destination in VALID_APPS and destination not in normalized:
|
|
normalized.append(destination)
|
|
return normalized or [APP_MAIN]
|
|
|
|
|
|
def normalize_routes(routes: object) -> dict[str, list[str]]:
|
|
source = routes if isinstance(routes, dict) else {}
|
|
return {
|
|
key: normalize_destinations(source.get(key, [APP_MAIN]))
|
|
for key in CATEGORY_KEYS
|
|
}
|