Add option to not bridge chats with too many members

This commit is contained in:
Tulir Asokan
2022-06-22 12:05:48 +03:00
parent bac3abcb4c
commit e8eef1c31e
6 changed files with 29 additions and 2 deletions
+2
View File
@@ -193,6 +193,8 @@ class Bot(AbstractUser):
)
else:
return await reply("Portal is not public. Use `/invite <mxid>` to get an invite.")
else:
return await reply("Couldn't create portal room")
async def handle_command_invite(
self, portal: po.Portal, reply: ReplyFunc, mxid_input: UserID
+4 -1
View File
@@ -233,7 +233,10 @@ async def join(evt: CommandEvent) -> EventID | None:
updates.stringify(),
)
raise e
return await evt.reply(f"Created room for {portal.title}")
if portal.mxid:
return await evt.reply(f"Created room for {portal.title}")
else:
return await evt.reply(f"Couldn't create room for {portal.title}")
return None
+1
View File
@@ -111,6 +111,7 @@ class Config(BaseBridgeConfig):
copy("bridge.allow_avatar_remove")
copy("bridge.max_initial_member_sync")
copy("bridge.max_member_count")
copy("bridge.sync_channel_members")
copy("bridge.skip_deleted_members")
copy("bridge.startup_sync")
+4
View File
@@ -148,6 +148,10 @@ bridge:
# will not send any more members.
# -1 means no limit (which means it's limited to 10000 by the server)
max_initial_member_sync: 100
# Maximum number of participants in chats to bridge. Only applies when the portal is being created.
# If there are more members when trying to create a room, the room creation will be cancelled.
# -1 means no limit (which means all chats can be bridged)
max_member_count: -1
# Whether or not to sync the member list in channels.
# If no channel admins have logged into the bridge, the bridge won't be able to sync the member
# list regardless of this setting.
+17 -1
View File
@@ -326,6 +326,7 @@ class Portal(DBPortal, BasePortal):
self._sponsored_msg_lock = asyncio.Lock()
self._sponsored_seen = {}
self._new_messages_after_sponsored = True
self._bridging_blocked_at_runtime = False
self._msg_conv = putil.TelegramMessageConverter(self)
@@ -385,7 +386,9 @@ class Portal(DBPortal, BasePortal):
@property
def allow_bridging(self) -> bool:
if self.peer_type == "user":
if self._bridging_blocked_at_runtime:
return False
elif self.peer_type == "user":
return True
elif self.filter_mode == "whitelist":
return self.tgid in self.filter_list
@@ -747,6 +750,16 @@ class Portal(DBPortal, BasePortal):
entity = await self.get_entity(user)
self.log.trace("Fetched data: %s", entity)
participants_count = 2
if isinstance(entity, Chat):
participants_count = entity.participants_count
elif isinstance(entity, Channel) and not entity.broadcast:
participants_count = entity.participants_count
if 0 < self.config["bridge.max_member_count"] < participants_count:
self.log.warning(f"Not bridging chat, too many participants (%d)", participants_count)
self._bridging_blocked_at_runtime = True
return None
self.log.debug("Creating room")
try:
@@ -2636,6 +2649,9 @@ class Portal(DBPortal, BasePortal):
if not self.mxid:
self.log.trace("Got telegram message %d, but no room exists, creating...", evt.id)
await self.create_matrix_room(source, invites=[source.mxid], update_if_exists=False)
if not self.mxid:
self.log.warning("Room doesn't exist even after creating, dropping %d", evt.id)
return
if (
self.peer_type == "user"
+1
View File
@@ -26,6 +26,7 @@ from telethon.tl.functions.contacts import GetContactsRequest, SearchRequest
from telethon.tl.functions.updates import GetStateRequest
from telethon.tl.functions.users import GetUsersRequest
from telethon.tl.types import (
Channel,
Chat,
ChatForbidden,
InputUserSelf,