Skip to content

Commit

Permalink
Send warning if bridge bot doesn't have redaction permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jun 23, 2021
1 parent 31650aa commit c02f67e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion mautrix_telegram/commands/portal/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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,
Expand Down Expand Up @@ -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.")
5 changes: 4 additions & 1 deletion mautrix_telegram/commands/portal/create_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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,
Expand Down Expand Up @@ -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 <username>` 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:
Expand Down
14 changes: 12 additions & 2 deletions mautrix_telegram/commands/portal/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
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
Expand All @@ -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:
Expand Down

0 comments on commit c02f67e

Please sign in to comment.