Skip to content

Commit

Permalink
add portal to cache also when creating a portal from the matrix side
Browse files Browse the repository at this point in the history
  • Loading branch information
maltee1 committed Mar 21, 2023
1 parent 4367812 commit 21a0d80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
10 changes: 1 addition & 9 deletions mautrix_telegram/commands/portal/create_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,11 @@ async def create(evt: CommandEvent) -> EventID:
about=about,
encrypted=encrypted,
)
invites, errors = await portal.get_telegram_users_in_matrix_room(evt.sender, pre_create=True)
if len(errors) > 0:
error_list = "\n".join(f"* [{mxid}](https://matrix.to/#/{mxid})" for mxid in errors)
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)
await portal.create_telegram_chat(evt.sender, supergroup=supergroup)
except ValueError as e:
await portal.delete()
return await evt.reply(e.args[0])
14 changes: 1 addition & 13 deletions mautrix_telegram/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,8 @@ async def handle_puppet_group_invite(
levels.users[self.az.bot_mxid] = 100 if invited_by_level >= 100 else invited_by_level
await double_puppet.intent.set_power_levels(room_id, levels)

invites, errors = await portal.get_telegram_users_in_matrix_room(
invited_by, pre_create=True
)
if len(errors) > 0:
error_list = "\n".join(f"* [{mxid}](https://matrix.to/#/{mxid})" for mxid in errors)
await portal.az.intent.send_notice(
room_id,
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.",
)

try:
await portal.create_telegram_chat(invited_by, invites=invites, supergroup=True)
await portal.create_telegram_chat(invited_by, supergroup=True)
except ValueError as e:
await portal.delete()
await portal.az.intent.send_notice(room_id, e.args[0])
Expand Down
23 changes: 18 additions & 5 deletions mautrix_telegram/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,17 @@ async def save(self) -> None:

async def get_telegram_users_in_matrix_room(
self, source: u.User, pre_create: bool = False
) -> tuple[list[InputUser], list[UserID]]:
) -> tuple[list[InputUser], list[UserID], list[u.User]]:
user_tgids = {}
users = []
intent = self.az.intent if pre_create else self.main_intent
user_mxids = await intent.get_room_members(self.mxid, (Membership.JOIN, Membership.INVITE))
for mxid in user_mxids:
if mxid == self.az.bot_mxid:
continue
mx_user = await u.User.get_by_mxid(mxid, create=False)
if mx_user and mx_user.tgid:
users.append(mx_user)
user_tgids[mx_user.tgid] = mxid
puppet_id = p.Puppet.get_id_from_mxid(mxid)
if puppet_id:
Expand All @@ -505,7 +507,7 @@ async def get_telegram_users_in_matrix_room(
f"creating a group: {e}"
)
errors.append(mxid)
return input_users, errors
return input_users, errors, users

async def upgrade_telegram_chat(self, source: u.User) -> None:
if self.peer_type != "chat":
Expand Down Expand Up @@ -556,11 +558,20 @@ async def set_telegram_username(self, source: u.User, username: str) -> None:
if await self._update_username(username):
await self.save()

async def create_telegram_chat(
self, source: u.User, invites: list[InputUser], supergroup: bool = False
) -> None:
async def create_telegram_chat(self, source: u.User, supergroup: bool = False) -> None:
if not self.mxid:
raise ValueError("Can't create Telegram chat for portal without Matrix room.")
invites, errors, users = await self.get_telegram_users_in_matrix_room(
source, pre_create=True
)
if len(errors) > 0:
error_list = "\n".join(f"* [{mxid}](https://matrix.to/#/{mxid})" for mxid in errors)
await self.az.intent.send_notice(
self.mxid,
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.",
)
elif self.tgid:
raise ValueError("Can't create Telegram chat for portal with existing Telegram chat.")

Expand Down Expand Up @@ -610,6 +621,8 @@ async def create_telegram_chat(
await self.main_intent.set_power_levels(self.mxid, levels)
await self.handle_matrix_power_levels(source, levels.users, {}, None)
await self.update_bridge_info()
for user in users:
await user.register_portal(self)
await self.main_intent.send_notice(self.mxid, f"Telegram chat created. ID: {self.tgid}")

async def handle_matrix_invite(
Expand Down

0 comments on commit 21a0d80

Please sign in to comment.