From c02f67e0d1dcabde598d65d346c63097e309d410 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 23 Jun 2021 18:44:32 +0300 Subject: [PATCH] Send warning if bridge bot doesn't have redaction permissions --- mautrix_telegram/commands/portal/bridge.py | 4 +++- mautrix_telegram/commands/portal/create_chat.py | 5 ++++- mautrix_telegram/commands/portal/util.py | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/mautrix_telegram/commands/portal/bridge.py b/mautrix_telegram/commands/portal/bridge.py index ffe2ff10..33f1a779 100644 --- a/mautrix_telegram/commands/portal/bridge.py +++ b/mautrix_telegram/commands/portal/bridge.py @@ -23,7 +23,7 @@ from mautrix.types import EventID, RoomID from ...types import TelegramID from ... import portal as po from .. import command_handler, CommandEvent, SECTION_CREATING_PORTALS -from .util import user_has_power_level, get_initial_state +from .util import user_has_power_level, get_initial_state, warn_missing_power @command_handler(needs_auth=False, needs_puppeting=False, @@ -191,4 +191,6 @@ async def _locked_confirm_bridge(evt: CommandEvent, portal: 'po.Portal', room_id asyncio.ensure_future(portal.update_matrix_room(user, entity, direct=False, levels=levels), loop=evt.loop) + await warn_missing_power(levels, evt) + return await evt.reply("Bridging complete. Portal synchronization should begin momentarily.") diff --git a/mautrix_telegram/commands/portal/create_chat.py b/mautrix_telegram/commands/portal/create_chat.py index c48a0f47..fb75e15f 100644 --- a/mautrix_telegram/commands/portal/create_chat.py +++ b/mautrix_telegram/commands/portal/create_chat.py @@ -18,7 +18,7 @@ from mautrix.types import EventID from ... import portal as po from ...types import TelegramID from .. import command_handler, CommandEvent, SECTION_CREATING_PORTALS -from .util import user_has_power_level, get_initial_state +from .util import user_has_power_level, get_initial_state, warn_missing_power @command_handler(help_section=SECTION_CREATING_PORTALS, @@ -58,6 +58,9 @@ async def create(evt: CommandEvent) -> EventID: await evt.reply(f"Failed to add the following users to the chat:\n\n{error_list}\n\n" "You can try `$cmdprefix+sp search -r ` to help the bridge find " "those users.") + + await warn_missing_power(levels, evt) + try: await portal.create_telegram_chat(evt.sender, invites=invites, supergroup=supergroup) except ValueError as e: diff --git a/mautrix_telegram/commands/portal/util.py b/mautrix_telegram/commands/portal/util.py index c3db1a01..c3fbe71f 100644 --- a/mautrix_telegram/commands/portal/util.py +++ b/mautrix_telegram/commands/portal/util.py @@ -18,14 +18,16 @@ from typing import Tuple, Optional from mautrix.errors import MatrixRequestError from mautrix.appservice import IntentAPI from mautrix.types import RoomID, EventType, PowerLevelStateEventContent +from .. import CommandEvent from ... import user as u OptStr = Optional[str] -async def get_initial_state(intent: IntentAPI, room_id: RoomID - ) -> Tuple[OptStr, OptStr, Optional[PowerLevelStateEventContent], bool]: +async def get_initial_state( + intent: IntentAPI, room_id: RoomID +) -> Tuple[OptStr, OptStr, Optional[PowerLevelStateEventContent], bool]: state = await intent.get_state(room_id) title: OptStr = None about: OptStr = None @@ -49,6 +51,14 @@ async def get_initial_state(intent: IntentAPI, room_id: RoomID return title, about, levels, encrypted +async def warn_missing_power(levels: PowerLevelStateEventContent, evt: CommandEvent) -> None: + if levels.get_user_level(evt.az.bot_mxid) < levels.redact: + await evt.reply("Warning: The bot does not have privileges to redact messages on Matrix. " + "Message deletions from Telegram will not be bridged unless you give " + "redaction permissions to " + f"[{evt.az.bot_mxid}](https://matrix.to/#/{evt.az.bot_mxid})") + + async def user_has_power_level(room_id: RoomID, intent: IntentAPI, sender: u.User, event: str) -> bool: if sender.is_admin: