From ebfbc13bbf8ba3c52fe3c031f141d7588ec33e93 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 9 Feb 2023 02:21:15 +0100 Subject: [PATCH 1/5] Add config option to enable/disable bridging of direct messages --- CHANGELOG.md | 2 ++ mautrix_telegram/example-config.yaml | 6 +++++- mautrix_telegram/portal.py | 5 +++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9a9b113..538e0510 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # unreleased ### Added +* Added `bridge.filter.users` config option to specify how the bridge should + handle filtering of direct messages in puppeting mode. * Added `allow_contact_info` config option to specify whether personal names and avatars for other users should be bridged. * The option is only safe to enable on single-user instances, using it diff --git a/mautrix_telegram/example-config.yaml b/mautrix_telegram/example-config.yaml index 0b80346f..ca30f077 100644 --- a/mautrix_telegram/example-config.yaml +++ b/mautrix_telegram/example-config.yaml @@ -458,7 +458,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". @@ -467,6 +466,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: null # 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 b46db06f..a7b8da33 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -416,8 +416,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": @@ -440,6 +440,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: bool | None = 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"] From cfca1da8cfe21df6c01c88c3b590215942e2b984 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 9 Feb 2023 02:26:06 +0100 Subject: [PATCH 2/5] Make `true` the default setting to maintain backwards compatibility --- mautrix_telegram/example-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mautrix_telegram/example-config.yaml b/mautrix_telegram/example-config.yaml index ca30f077..f2145d39 100644 --- a/mautrix_telegram/example-config.yaml +++ b/mautrix_telegram/example-config.yaml @@ -470,7 +470,7 @@ bridge: # 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: null + users: true # The prefix for commands. Only required in non-management rooms. command_prefix: "!tg" From 2c7dfc28cc814d18063bfb53828a6dc7975735e7 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 16 Feb 2023 17:23:00 +0100 Subject: [PATCH 3/5] Add `bridge.filter.users` to the config upgrader --- mautrix_telegram/config.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index 492bbe07..8459e111 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -197,6 +197,10 @@ def do_update(self, helper: ConfigUpdateHelper) -> None: copy("bridge.filter.mode") copy("bridge.filter.list") + if "bridge.filter.users" not in self: + base["bridge.filter.users"] = True + else: + copy("bridge.filter.users") copy("bridge.command_prefix") From ccbf2be4fd2e09d4a55f950b6772e2b0e3c9ed37 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 26 Feb 2023 19:25:58 +0100 Subject: [PATCH 4/5] Apply suggestion Co-authored-by: Tulir Asokan --- mautrix_telegram/config.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index 8459e111..b9f333ac 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -197,10 +197,7 @@ def do_update(self, helper: ConfigUpdateHelper) -> None: copy("bridge.filter.mode") copy("bridge.filter.list") - if "bridge.filter.users" not in self: - base["bridge.filter.users"] = True - else: - copy("bridge.filter.users") + copy("bridge.filter.users") copy("bridge.command_prefix") From b7e070476fc06cd96e8a36228ba1904abb9a4720 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 7 May 2023 18:01:52 +0300 Subject: [PATCH 5/5] Move type hint and remove changelog entry --- CHANGELOG.md | 2 -- mautrix_telegram/portal.py | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8d5cddc..b5e8729d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,6 @@ # v0.13.0 (2023-02-26) ### Added -* Added `bridge.filter.users` config option to specify how the bridge should - handle filtering of direct messages in puppeting mode. * Added `allow_contact_info` config option to specify whether personal names and avatars for other users should be bridged. * The option is only safe to enable on single-user instances, using it diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index acf566e5..e1944d32 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -258,6 +258,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 @@ -456,7 +457,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: bool | None = cls.config["bridge.filter.filter_users"] + 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"]