Skip to content

Commit

Permalink
Bridge bans to Telegram. Fixes #303
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Sep 22, 2019
1 parent 835175a commit d5470de
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
24 changes: 24 additions & 0 deletions mautrix_telegram/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,30 @@ async def handle_raw_leave(self, room_id: RoomID, user_id: UserID, sender_id: Us
else:
await portal.leave_matrix(user, event_id)

async def handle_ban(self, room_id: RoomID, user_id: UserID, banned_by: UserID, reason: str,
event_id: EventID) -> None:
self.log.debug(f"{user_id} was banned from {room_id} by {banned_by} for {reason}")

sender = u.User.get_by_mxid(banned_by, create=False)
if not sender:
return
await sender.ensure_started()

portal = po.Portal.get_by_mxid(room_id)
if not portal:
return

puppet = pu.Puppet.get_by_mxid(user_id)
if puppet:
await portal.ban_matrix(puppet, sender)
return

user = u.User.get_by_mxid(user_id, create=False)
if not user:
return
await user.ensure_started()
await portal.ban_matrix(user, sender)

@staticmethod
async def allow_message(user: 'u.User') -> bool:
return user.relaybot_whitelisted
Expand Down
34 changes: 20 additions & 14 deletions mautrix_telegram/portal/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,33 @@ async def mark_read(self, user: 'u.User', event_id: EventID) -> None:
await user.client.send_read_acknowledge(self.peer, max_id=message.tgid,
clear_mentions=True)

async def kick_matrix(self, user: Union['u.User', 'p.Puppet'], source: 'u.User') -> None:
async def _preproc_kick_ban(self, user: Union['u.User', 'p.Puppet'], source: 'u.User'
) -> Optional['AbstractUser']:
if user.tgid == source.tgid:
return
return None
if self.peer_type == "user" and user.tgid == self.tgid:
self.delete()
try:
del self.by_tgid[self.tgid_full]
del self.by_mxid[self.mxid]
except KeyError:
pass
return
return None
if isinstance(user, u.User) and await user.needs_relaybot(self):
if not self.bot:
return
# TODO kick and ban message
return
return None
# TODO kick message
return None
if await source.needs_relaybot(self):
if not self.has_bot:
return
source = self.bot
await source.client.kick_participant(self.peer, user.peer)
return None
return self.bot
return source

async def kick_matrix(self, user: Union['u.User', 'p.Puppet'], source: 'u.User') -> None:
source = await self._preproc_kick_ban(user, source)
if source is not None:
await source.client.kick_participant(self.peer, user.peer)

async def ban_matrix(self, user: Union['u.User', 'p.Puppet'], source: 'u.User'):
source = await self._preproc_kick_ban(user, source)
if source is not None:
await source.client.edit_permissions(self.peer, user.peer, view_messages=False)

async def leave_matrix(self, user: 'u.User', event_id: EventID) -> None:
if await user.needs_relaybot(self):
Expand Down
2 changes: 1 addition & 1 deletion mautrix_telegram/portal/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def _get_base_power_levels(self, levels: PowerLevelStateEventContent = None,
self.log.debug(f"default_banned_rights is None in {entity}")
dbr = ChatBannedRights(invite_users=True, change_info=True, pin_messages=True,
send_stickers=False, send_messages=False, until_date=None)
levels.ban = 99
levels.ban = 50
levels.kick = 50
levels.redact = 50
levels.invite = 50 if dbr.invite_users else 0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

install_requires=[
"aiohttp>=3.0.1,<4",
"mautrix>=0.4.0.dev70,<0.5",
"mautrix>=0.4.0.dev71,<0.5",
"SQLAlchemy>=1.2.3,<2",
"alembic>=1.0.0,<2",
"commonmark>=0.8.1,<0.10",
Expand Down

0 comments on commit d5470de

Please sign in to comment.