Add portal to cache when creating chat from Matrix side (#902)
This commit is contained in:
@@ -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])
|
||||
|
||||
@@ -135,20 +135,8 @@ class MatrixHandler(BaseMatrixHandler):
|
||||
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])
|
||||
|
||||
@@ -209,7 +209,7 @@ from mautrix.types import (
|
||||
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
|
||||
@@ -514,8 +514,9 @@ class Portal(DBPortal, BasePortal):
|
||||
|
||||
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:
|
||||
@@ -523,6 +524,7 @@ class Portal(DBPortal, BasePortal):
|
||||
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:
|
||||
@@ -538,7 +540,7 @@ class Portal(DBPortal, BasePortal):
|
||||
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":
|
||||
@@ -589,11 +591,23 @@ class Portal(DBPortal, BasePortal):
|
||||
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.")
|
||||
|
||||
@@ -643,6 +657,8 @@ class Portal(DBPortal, BasePortal):
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user