diff --git a/surfsense_backend/alembic/versions/59_add_teams_connector_enums.py b/surfsense_backend/alembic/versions/59_add_teams_connector_enums.py index f13fbe9e5..d4f6629a7 100644 --- a/surfsense_backend/alembic/versions/59_add_teams_connector_enums.py +++ b/surfsense_backend/alembic/versions/59_add_teams_connector_enums.py @@ -86,7 +86,7 @@ def downgrade() -> None: "ELASTICSEARCH_CONNECTOR", "WEBCRAWLER_CONNECTOR", ) - + # All document values except TEAMS_CONNECTOR old_document_values = ( "EXTENSION", diff --git a/surfsense_backend/app/connectors/teams_connector.py b/surfsense_backend/app/connectors/teams_connector.py index 5603357e5..c639ab177 100644 --- a/surfsense_backend/app/connectors/teams_connector.py +++ b/surfsense_backend/app/connectors/teams_connector.py @@ -253,7 +253,9 @@ class TeamsConnector: access_token = await self._get_valid_token() async with httpx.AsyncClient() as client: - url = f"{self.GRAPH_API_BASE}/teams/{team_id}/channels/{channel_id}/messages" + url = ( + f"{self.GRAPH_API_BASE}/teams/{team_id}/channels/{channel_id}/messages" + ) # Note: The Graph API for channel messages doesn't support $filter parameter # We fetch all messages and filter them client-side @@ -270,7 +272,7 @@ class TeamsConnector: data = response.json() messages = data.get("value", []) - + # Filter messages by date if needed (client-side filtering) if start_date or end_date: # Make sure comparison dates are timezone-aware (UTC) @@ -278,26 +280,28 @@ class TeamsConnector: start_date = start_date.replace(tzinfo=UTC) if end_date and end_date.tzinfo is None: end_date = end_date.replace(tzinfo=UTC) - + filtered_messages = [] for message in messages: created_at_str = message.get("createdDateTime") if not created_at_str: continue - + # Parse the ISO 8601 datetime string (already timezone-aware) - created_at = datetime.fromisoformat(created_at_str.replace('Z', '+00:00')) - + created_at = datetime.fromisoformat( + created_at_str.replace("Z", "+00:00") + ) + # Check if message is within date range if start_date and created_at < start_date: continue if end_date and created_at > end_date: continue - + filtered_messages.append(message) - + return filtered_messages - + return messages async def get_message_replies( diff --git a/surfsense_backend/app/routes/teams_add_connector_route.py b/surfsense_backend/app/routes/teams_add_connector_route.py index ce014be0d..9ce84e171 100644 --- a/surfsense_backend/app/routes/teams_add_connector_route.py +++ b/surfsense_backend/app/routes/teams_add_connector_route.py @@ -343,8 +343,12 @@ async def teams_callback( except IntegrityError as e: await session.rollback() - logger.error("Database integrity error creating Teams connector: %s", str(e)) - redirect_url = f"{config.NEXT_FRONTEND_URL}/dashboard?error=connector_creation_failed" + logger.error( + "Database integrity error creating Teams connector: %s", str(e) + ) + redirect_url = ( + f"{config.NEXT_FRONTEND_URL}/dashboard?error=connector_creation_failed" + ) return RedirectResponse(url=redirect_url) except HTTPException: diff --git a/surfsense_backend/app/tasks/connector_indexers/teams_indexer.py b/surfsense_backend/app/tasks/connector_indexers/teams_indexer.py index 3b28d4293..2709adaf1 100644 --- a/surfsense_backend/app/tasks/connector_indexers/teams_indexer.py +++ b/surfsense_backend/app/tasks/connector_indexers/teams_indexer.py @@ -173,10 +173,14 @@ async def index_teams_messages( end_datetime = None if start_date_str: # Parse as naive datetime and make it timezone-aware (UTC) - start_datetime = datetime.strptime(start_date_str, "%Y-%m-%d").replace(tzinfo=UTC) + start_datetime = datetime.strptime(start_date_str, "%Y-%m-%d").replace( + tzinfo=UTC + ) if end_date_str: # Parse as naive datetime, set to end of day, and make it timezone-aware (UTC) - end_datetime = datetime.strptime(end_date_str, "%Y-%m-%d").replace(hour=23, minute=59, second=59, tzinfo=UTC) + end_datetime = datetime.strptime(end_date_str, "%Y-%m-%d").replace( + hour=23, minute=59, second=59, tzinfo=UTC + ) # Process each team for team in teams: @@ -314,8 +318,10 @@ async def index_teams_messages( chunks = await create_document_chunks( combined_document_string ) - doc_embedding = config.embedding_model_instance.embed( - combined_document_string + doc_embedding = ( + config.embedding_model_instance.embed( + combined_document_string + ) ) # Update existing document @@ -337,11 +343,14 @@ async def index_teams_messages( # Delete old chunks and add new ones existing_document.chunks = chunks - existing_document.updated_at = get_current_timestamp() + existing_document.updated_at = ( + get_current_timestamp() + ) documents_indexed += 1 logger.info( - "Successfully updated Teams message %s", message_id + "Successfully updated Teams message %s", + message_id, ) continue