Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add portal to cache also when creating a portal from the matrix side #902

Merged
merged 3 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 22 additions & 6 deletions mautrix_telegram/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
UserID,
VideoInfo,
)
from mautrix.util import background_task, magic, variation_selector
from mautrix.util import background_task, magic, markdown, variation_selector
from mautrix.util.format_duration import format_duration
from mautrix.util.message_send_checkpoint import MessageSendCheckpointStatus
from mautrix.util.simple_lock import SimpleLock
Expand Down 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,23 @@ 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)
command_prefix = self.config["bridge.command_prefix"]
message = (
f"Failed to add the following users to the chat:\n\n{error_list}\n\n"
f"You can try `{command_prefix} search -r <username>` to help the bridge find "
"those users."
)
await self.az.intent.send_notice(
self.mxid, text=message, html=markdown.render(message)
)
elif self.tgid:
raise ValueError("Can't create Telegram chat for portal with existing Telegram chat.")

Expand Down Expand Up @@ -610,6 +624,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