From 186273291331b756f5c3dc0da816bb8eeb37905c Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Sun, 4 Jan 2026 02:47:50 +0530 Subject: [PATCH] refactor: improve error handling for Slack token refresh logic - Updated SlackHistory class to enforce the use of session and connector_id for token refresh, raising a ValueError for legacy token usage. - Simplified conditional checks for client initialization in SlackHistory. - Cleaned up unnecessary comments and whitespace in the codebase. --- .../app/connectors/slack_history.py | 20 +++++++++++-------- .../app/schemas/slack_auth_credentials.py | 1 - .../tasks/connector_indexers/slack_indexer.py | 4 +--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/surfsense_backend/app/connectors/slack_history.py b/surfsense_backend/app/connectors/slack_history.py index 6a016394e..dbf43bb24 100644 --- a/surfsense_backend/app/connectors/slack_history.py +++ b/surfsense_backend/app/connectors/slack_history.py @@ -65,19 +65,23 @@ class SlackHistory: """ # If we have a direct token (backward compatibility), use it # Check if client was initialized with a token directly (not via credentials) - if self.client and self._session is None and self._connector_id is None: + if ( + self.client + and self._session is None + and self._connector_id is None + and self._credentials is None + ): # This means it was initialized with a direct token, extract it # WebClient stores token internally, we need to get it from the client # For backward compatibility, we'll use the client directly # But we can't easily extract the token, so we'll just use the client # In this case, we'll skip refresh logic - if self._credentials is None: - # This is the old pattern - just use the client as-is - # We can't extract token easily, so we'll raise an error - # asking to use the new pattern - raise ValueError( - "Cannot refresh token: Please use session and connector_id for auto-refresh support" - ) + # This is the old pattern - just use the client as-is + # We can't extract token easily, so we'll raise an error + # asking to use the new pattern + raise ValueError( + "Cannot refresh token: Please use session and connector_id for auto-refresh support" + ) # Load credentials from DB if not provided if self._credentials is None: diff --git a/surfsense_backend/app/schemas/slack_auth_credentials.py b/surfsense_backend/app/schemas/slack_auth_credentials.py index ad6a713ef..5148a0985 100644 --- a/surfsense_backend/app/schemas/slack_auth_credentials.py +++ b/surfsense_backend/app/schemas/slack_auth_credentials.py @@ -73,4 +73,3 @@ class SlackAuthCredentialsBase(BaseModel): if isinstance(v, datetime): return v if v.tzinfo else v.replace(tzinfo=UTC) return v - diff --git a/surfsense_backend/app/tasks/connector_indexers/slack_indexer.py b/surfsense_backend/app/tasks/connector_indexers/slack_indexer.py index c7a815634..dad64ad27 100644 --- a/surfsense_backend/app/tasks/connector_indexers/slack_indexer.py +++ b/surfsense_backend/app/tasks/connector_indexers/slack_indexer.py @@ -103,9 +103,7 @@ async def index_slack_messages( ) # Use the new pattern with session and connector_id for auto-refresh - slack_client = SlackHistory( - session=session, connector_id=connector_id - ) + slack_client = SlackHistory(session=session, connector_id=connector_id) # Handle 'undefined' string from frontend (treat as None) if start_date == "undefined" or start_date == "":