From a7cafbf3678325fc13c4607ae3ca17de4bdea257 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Wed, 4 May 2022 03:01:07 -0400 Subject: [PATCH] Implement resync_max_disconnected_time --- .../example-config.yaml | 1 - matrix_appservice_kakaotalk/user.py | 20 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/matrix_appservice_kakaotalk/example-config.yaml b/matrix_appservice_kakaotalk/example-config.yaml index 7fda682..ee77cd6 100644 --- a/matrix_appservice_kakaotalk/example-config.yaml +++ b/matrix_appservice_kakaotalk/example-config.yaml @@ -189,7 +189,6 @@ bridge: # The number of seconds that a disconnection can last without triggering an automatic re-sync # and missed message backfilling when reconnecting. # Set to 0 to always re-sync, or -1 to never re-sync automatically. - # TODO Actually use this setting resync_max_disconnected_time: 5 # Should users remain logged in after being disconnected from chatroom updates? # This is a convenience feature, but might make the bridge look more suspicious to KakaoTalk. diff --git a/matrix_appservice_kakaotalk/user.py b/matrix_appservice_kakaotalk/user.py index 3058eda..5d2529c 100644 --- a/matrix_appservice_kakaotalk/user.py +++ b/matrix_appservice_kakaotalk/user.py @@ -492,8 +492,8 @@ class User(DBUser, BaseUser): # TODO Look for a way to sync all channels without (re-)logging in try: login_result = await self.client.connect() - await self.on_connect() - if login_result: + should_sync = await self.on_connect() + if login_result and should_sync: await self._sync_channels(login_result, sync_count) return True except AuthenticationRequired as e: @@ -696,23 +696,23 @@ class User(DBUser, BaseUser): # region KakaoTalk event handling - async def on_connect(self) -> None: - self.is_connected = True - self._track_metric(METRIC_CONNECTED, True) - """ TODO Don't auto-resync channels if disconnection was too short + async def on_connect(self) -> bool: now = time.monotonic() disconnected_at = self._connection_time max_delay = self.config["bridge.resync_max_disconnected_time"] first_connect = self.is_connected is None - if not first_connect and disconnected_at + max_delay < now: - duration = int(now - disconnected_at) + self.is_connected = True + self._track_metric(METRIC_CONNECTED, True) + duration = int(now - disconnected_at) + skip_sync = not first_connect and duration < max_delay + if skip_sync: self.log.debug(f"Disconnection lasted {duration} seconds, not re-syncing channels...") - """ - if self.temp_disconnect_notices: + elif self.temp_disconnect_notices: await self.send_bridge_notice("Connected to KakaoTalk chats") await self.push_bridge_state(BridgeStateEvent.CONNECTED) self.was_connected = True await self.save() + return not skip_sync async def on_disconnect(self, res: KickoutRes | None) -> None: self.is_connected = False