From 759ccf301c0f0bcc10fbaa2e8b5ed25b059e6458 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 7 May 2023 17:03:48 +0200 Subject: [PATCH] Allow filtering direct chats with filter config (#892) --- mautrix_telegram/config.py | 1 + mautrix_telegram/example-config.yaml | 6 +++++- mautrix_telegram/portal.py | 6 ++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index f17ddc42..e102d301 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -205,6 +205,7 @@ def do_update(self, helper: ConfigUpdateHelper) -> None: copy("bridge.filter.mode") copy("bridge.filter.list") + copy("bridge.filter.users") copy("bridge.command_prefix") diff --git a/mautrix_telegram/example-config.yaml b/mautrix_telegram/example-config.yaml index d72a7654..03ab58ba 100644 --- a/mautrix_telegram/example-config.yaml +++ b/mautrix_telegram/example-config.yaml @@ -483,7 +483,6 @@ bridge: # Filter rooms that can/can't be bridged. Can also be managed using the `filter` and # `filter-mode` management commands. # - # Filters do not affect direct chats. # An empty blacklist will essentially disable the filter. filter: # Filter mode to use. Either "blacklist" or "whitelist". @@ -492,6 +491,11 @@ bridge: mode: blacklist # The list of group/channel IDs to filter. list: [] + # How to handle direct chats: + # If users is "null", direct chats will follow the previous settings. + # If users is "true", direct chats will always be bridged. + # If users is "false", direct chats will never be bridged. + users: true # The prefix for commands. Only required in non-management rooms. command_prefix: "!tg" diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 767c41e2..088832ff 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -281,6 +281,7 @@ class Portal(DBPortal, BasePortal): # Config cache filter_mode: str filter_list: list[int] + filter_users: bool | None max_initial_member_sync: int sync_channel_members: bool @@ -455,8 +456,8 @@ def main_intent(self) -> IntentAPI: def allow_bridging(self) -> bool: if self._bridging_blocked_at_runtime: return False - elif self.peer_type == "user": - return True + elif self.peer_type == "user" and self.filter_users is not None: + return self.filter_users elif self.filter_mode == "whitelist": return self.tgid in self.filter_list elif self.filter_mode == "blacklist": @@ -487,6 +488,7 @@ def init_cls(cls, bridge: "TelegramBridge") -> None: cls.private_chat_portal_meta = cls.config["bridge.private_chat_portal_meta"] cls.filter_mode = cls.config["bridge.filter.mode"] cls.filter_list = cls.config["bridge.filter.list"] + cls.filter_users = cls.config["bridge.filter.filter_users"] cls.hs_domain = cls.config["homeserver.domain"] cls.backfill_msc2716 = cls.config["bridge.backfill.msc2716"] cls.backfill_enable = cls.config["bridge.backfill.enable"]