From 2fd18c7309a86d8fcf6b5aa3166900e2f5a17287 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Thu, 22 Jul 2021 03:23:11 -0400 Subject: [PATCH] Add limit parameter to sync command --- matrix_puppeteer_line/commands/conn.py | 12 +++++-- matrix_puppeteer_line/example-config.yaml | 7 ++-- matrix_puppeteer_line/user.py | 44 +++++++++++++---------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/matrix_puppeteer_line/commands/conn.py b/matrix_puppeteer_line/commands/conn.py index 3630880..852a0cb 100644 --- a/matrix_puppeteer_line/commands/conn.py +++ b/matrix_puppeteer_line/commands/conn.py @@ -41,12 +41,18 @@ async def ping(evt: CommandEvent) -> None: @command_handler(needs_auth=True, management_only=False, help_section=SECTION_CONNECTION, - help_text="Synchronize contacts and portals") + help_text="Synchronize contacts and chats", help_args="[_limit_]") async def sync(evt: CommandEvent) -> None: - await evt.sender.sync() + limit = 0 + for arg in evt.args: + try: + limit = int(arg) + except ValueError: + pass + await evt.sender.sync(limit) @command_handler(needs_auth=True, management_only=False, help_section=SECTION_CONNECTION, - help_text="Synchronize contacts") + help_text="Synchronize contacts only") async def sync_contacts(evt: CommandEvent) -> None: await evt.sender.sync_contacts() diff --git a/matrix_puppeteer_line/example-config.yaml b/matrix_puppeteer_line/example-config.yaml index 0a9e01e..c235ef8 100644 --- a/matrix_puppeteer_line/example-config.yaml +++ b/matrix_puppeteer_line/example-config.yaml @@ -81,8 +81,9 @@ bridge: # Maximum length of displayname displayname_max_length: 100 - # Number of conversations to sync (and create portals for) on login. - # Set 0 to disable automatic syncing. + # Number of conversations to sync (and create portals for) on login + # and with the "sync" command with no numeric argument passed to it. + # Set 0 to disable automatic syncing on login. initial_conversation_sync: 10 # Whether or not the LINE users of logged in Matrix users should be # invited to rooms when the user sends a message from another client. @@ -97,7 +98,7 @@ bridge: # Whether or not created rooms should have federation enabled. # If false, created portal rooms will never be federated. federate_rooms: true - # Settings for backfilling messages from the Messages app. + # Settings for backfilling messages. backfill: # If using double puppeting, should notifications be disabled # while the initial backfill is in progress? diff --git a/matrix_puppeteer_line/user.py b/matrix_puppeteer_line/user.py index 8e0f1f8..bccc482 100644 --- a/matrix_puppeteer_line/user.py +++ b/matrix_puppeteer_line/user.py @@ -131,7 +131,7 @@ class User(DBUser, BaseUser): self._track_metric(METRIC_CONNECTED, await self.client.is_connected()) await asyncio.sleep(5) - async def sync(self) -> None: + async def sync(self, limit: int = 0) -> None: await self.sync_contacts() # TODO Use some kind of async lock / event to queue syncing actions self.is_syncing = True @@ -139,24 +139,32 @@ class User(DBUser, BaseUser): self._connection_check_task.cancel() self._connection_check_task = self.loop.create_task(self._check_connection_loop()) await self.client.pause() - await self.sync_own_profile() - await self.client.set_last_message_ids( - await DBMessage.get_max_mids(), - await DBMessage.get_max_outgoing_mids(), - await DBReceipt.get_max_mids_per_num_read()) - limit = self.config["bridge.initial_conversation_sync"] - self.log.info("Syncing chats") - await self.send_bridge_notice("Synchronizing chats...") + try: + await self.sync_own_profile() + await self.client.set_last_message_ids( + await DBMessage.get_max_mids(), + await DBMessage.get_max_outgoing_mids(), + await DBReceipt.get_max_mids_per_num_read()) - # TODO Since only chat ID is used, retrieve only that - chat_infos = await self.client.get_chats() - for chat_info in chat_infos[:limit]: - portal = await po.Portal.get_by_chat_id(chat_info.id, create=True) - chat_info_full = await self.client.get_chat(chat_info.id) - await portal.create_matrix_room(self, chat_info_full) - await self.send_bridge_notice("Chat synchronization complete") - await self.client.resume() - self.is_syncing = False + if limit <= 0: + limit = self.config["bridge.initial_conversation_sync"] + if limit == 0: + self.log.info("Skipping chat sync") + return + + self.log.info("Syncing chats") + await self.send_bridge_notice("Synchronizing chats...") + + # TODO Since only chat ID is used, retrieve only that + chat_infos = await self.client.get_chats() + for chat_info in chat_infos[:limit]: + portal = await po.Portal.get_by_chat_id(chat_info.id, create=True) + chat_info_full = await self.client.get_chat(chat_info.id) + await portal.create_matrix_room(self, chat_info_full) + await self.send_bridge_notice("Chat synchronization complete") + finally: + await self.client.resume() + self.is_syncing = False async def sync_contacts(self) -> None: await self.send_bridge_notice("Synchronizing contacts...")