Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(conversations): return frozen dataclasses instead of tuples (#76123)

Arno Hillen22c5ecf1a96c
diff --git a/products/conversations/backend/api/tests/test_slack_image_sync.py b/products/conversations/backend/api/tests/test_slack_image_sync.py
index 3aeb34b880fe..4259e4a35b70 100644
--- a/products/conversations/backend/api/tests/test_slack_image_sync.py
+++ b/products/conversations/backend/api/tests/test_slack_image_sync.py
@@ -127,18 +127,18 @@ class TestSlackImageIngest(SimpleTestCase):
if permalink:
files[0]["permalink"] = permalink
attachments = extract_slack_files(files, fake_team, fake_client)
- images, file_attachments = split_slack_attachments(attachments)
+ split = split_slack_attachments(attachments)
mock_save.assert_not_called()
# Nothing is re-hosted, so nothing can be inlined
- assert images == []
+ assert split.images == []
if expects_link:
# Rendered as a link instead of vanishing from the ticket
- assert file_attachments == [
+ assert split.files == [
{"url": permalink, "name": "test.jpg", "mimetype": "image/jpeg", "unavailable": True}
]
else:
- assert file_attachments == []
+ assert split.files == []
@parameterized.expand(
[
@@ -214,12 +214,12 @@ class TestSlackImageIngest(SimpleTestCase):
}
]
attachments = extract_slack_files(files, fake_team, fake_client)
- images, file_attachments = split_slack_attachments(attachments)
+ split = split_slack_attachments(attachments)
- assert images == []
- assert len(file_attachments) == 1
- assert file_attachments[0]["mimetype"] == "application/pdf"
- assert file_attachments[0]["name"] == "invoice.pdf"
+ assert split.images == []
+ assert len(split.files) == 1
+ assert split.files[0]["mimetype"] == "application/pdf"
+ assert split.files[0]["name"] == "invoice.pdf"
# Non-image bytes are stored without image validation
assert mock_save.call_args.kwargs["validate_images"] is False
diff --git a/products/conversations/backend/slack.py b/products/conversations/backend/slack.py
index eccdb4bdf557..5902105122fb 100644
--- a/products/conversations/backend/slack.py
+++ b/products/conversations/backend/slack.py
@@ -11,6 +11,7 @@ All three converge to create_or_update_slack_ticket().
import re
import json
+from dataclasses import dataclass
from datetime import UTC, datetime
from typing import Any, Literal, NamedTuple
from urllib.parse import urljoin, urlparse
@@ -260,15 +261,21 @@ def _is_inline_image(attachment: dict) -> bool:
return (attachment.get("mimetype") or "").startswith("image/") and not attachment.get("unavailable")
-def split_slack_attachments(attachments: list[dict]) -> tuple[list[dict], list[dict]]:
- """Partition extracted attachments into (images, non-image files) by mimetype.
+@dataclass(frozen=True, kw_only=True, slots=True)
+class SplitAttachments:
+ images: list[dict]
+ files: list[dict]
+
+
+def split_slack_attachments(attachments: list[dict]) -> SplitAttachments:
+ """Partition extracted attachments into images and non-image files by mimetype.
Attachments we couldn't re-host go to the file bucket whatever their mimetype:
they point at Slack, so they can only be rendered as a link, not inlined.
"""
images = [a for a in attachments if _is_inline_image(a)]
files = [a for a in attachments if not _is_inline_image(a)]
- return images, files
+ return SplitAttachments(images=images, files=files)
def _rehost_slack_file(f: dict, team: Team, bot_token: str | None) -> dict | None:
@@ -412,7 +419,7 @@ def create_or_update_slack_ticket(
)
# Extract attachments from Slack files, making them publicly accessible
- images, file_attachments = split_slack_attachments(extract_slack_files(files, team, client))
+ attachments = split_slack_attachments(extract_slack_files(files, team, client))
# Resolve Slack user info for this message author
user_info = resolve_slack_user(client, slack_user_id, workspace=slack_team_id or "")
@@ -453,7 +460,7 @@ def create_or_update_slack_ticket(
Ticket.objects.filter(id=ticket.id, team=team).update(slack_team_id=slack_team_id)
# Allow messages with only attachments (no text)
- if not cleaned_text and not images and not file_attachments:
+ if not cleaned_text and not attachments.images and not attachments.files:
logger.warning(
"🧵 slack_support_ticket_ingest_empty_after_processing",
team_id=team_id,
@@ -463,7 +470,9 @@ def create_or_update_slack_ticket(
)
return ticket
- content, rich_content = build_content_with_images(cleaned_text, rich_content, images, file_attachments)
+ content, rich_content = build_content_with_images(
+ cleaned_text, rich_content, attachments.images, attachments.files
+ )
Comment.objects.create(
team=team,
@@ -480,8 +489,8 @@ def create_or_update_slack_ticket(
"slack_author_name": user_info["name"],
"slack_author_email": user_info.get("email"),
"slack_author_avatar": user_info.get("avatar"),
- "slack_images": images if images else None,
- "slack_files": file_attachments if file_attachments else None,
+ "slack_images": attachments.images if attachments.images else None,
+ "slack_files": attachments.files if attachments.files else None,
},
)
@@ -494,7 +503,7 @@ def create_or_update_slack_ticket(
# New ticket from top-level message
# Allow messages with only attachments (no text)
- if not cleaned_text and not images and not file_attachments:
+ if not cleaned_text and not attachments.images and not attachments.files:
logger.warning(
"🧵 slack_support_ticket_ingest_empty_after_processing",
team_id=team_id,
@@ -504,7 +513,7 @@ def create_or_update_slack_ticket(
)
return None
- content, rich_content = build_content_with_images(cleaned_text, rich_content, images, file_attachments)
+ content, rich_content = build_content_with_images(cleaned_text, rich_content, attachments.images, attachments.files)
# Serialize concurrent ticket creation for the same Slack thread via Redis lock.
# Without this, two reaction_added events from different users race through the
@@ -562,8 +571,8 @@ def create_or_update_slack_ticket(
"slack_author_name": user_info["name"],
"slack_author_email": user_info.get("email"),
"slack_author_avatar": user_info.get("avatar"),
- "slack_images": images if images else None,
- "slack_files": file_attachments if file_attachments else None,
+ "slack_images": attachments.images if attachments.images else None,
+ "slack_files": attachments.files if attachments.files else None,
},
)
@@ -1325,7 +1334,7 @@ def _backfill_thread_replies(
if not reply_text.strip() and not reply_files:
continue
- images, file_attachments = split_slack_attachments(extract_slack_files(reply_files, team, client))
+ attachments = split_slack_attachments(extract_slack_files(reply_files, team, client))
if reply_user not in user_cache:
user_cache[reply_user] = resolve_slack_user(client, reply_user, workspace=slack_team_id or "")
@@ -1348,7 +1357,7 @@ def _backfill_thread_replies(
cleaned_text, rich_content = slack_to_content_and_rich_content(
reply_text, reply_blocks, user_names=reply_user_names
)
- if not cleaned_text and not images and not file_attachments:
+ if not cleaned_text and not attachments.images and not attachments.files:
continue
if is_team_member:
@@ -1356,7 +1365,9 @@ def _backfill_thread_replies(
else:
customer_message_count += 1
- content, rich_content = build_content_with_images(cleaned_text, rich_content, images, file_attachments)
+ content, rich_content = build_content_with_images(
+ cleaned_text, rich_content, attachments.images, attachments.files
+ )
comments_to_create.append(
Comment(
@@ -1374,8 +1385,8 @@ def _backfill_thread_replies(
"slack_author_name": user_info["name"],
"slack_author_email": user_info.get("email"),
"slack_author_avatar": user_info.get("avatar"),
- "slack_images": images if images else None,
- "slack_files": file_attachments if file_attachments else None,
+ "slack_images": attachments.images if attachments.images else None,
+ "slack_files": attachments.files if attachments.files else None,
},
)
)