from collections.abc import Iterable from utils.policy.models import ( CaptureToggles, ChatKind, ChatMeta, FolderSpec, PolicySet, ) def _category_match(folder: FolderSpec, chat: ChatMeta) -> bool: if chat.kind is ChatKind.GROUP: return folder.groups if chat.kind is ChatKind.CHANNEL: return folder.broadcasts if chat.is_bot: return folder.bots if chat.is_contact is True: return folder.contacts if chat.is_contact is False: return folder.non_contacts return False def folder_contains(folder: FolderSpec, chat: ChatMeta) -> bool: if chat.chat_id in folder.exclude_ids: return False if chat.chat_id in folder.include_ids or chat.chat_id in folder.pinned_ids: return True if folder.is_chatlist: return False return _category_match(folder, chat) def resolve( chat: ChatMeta, folders: Iterable[FolderSpec], policies: PolicySet ) -> CaptureToggles: chat_override = policies.chat.get(chat.chat_id) if chat_override is not None: return chat_override for folder in sorted(folders, key=lambda f: f.order_index): toggles = policies.folder.get(folder.folder_id) if toggles is not None and folder_contains(folder, chat): return toggles return policies.defaults.get(chat.kind, CaptureToggles())