: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(conversations): rehost images from slack connect channels (#115706)
Alex V624c79978d05
diff --git a/products/conversations/backend/api/tests/test_slack_image_sync.py b/products/conversations/backend/api/tests/test_slack_image_sync.pyindex 1b3571473e4c..da628a6f21f3 100644--- a/products/conversations/backend/api/tests/test_slack_image_sync.py+++ b/products/conversations/backend/api/tests/test_slack_image_sync.py@@ -94,28 +94,35 @@ class TestSlackImageIngest(SimpleTestCase):assert payload == expected_bytes+ @parameterized.expand(+ [+ ("full_file_in_event", False),+ ("slack_connect_stub", True),+ ]+ )@patch("products.conversations.backend.slack.save_file_to_uploaded_media")@patch("products.conversations.backend.slack._download_slack_image_bytes")- def test_extract_slack_files_copies_to_uploaded_media(self, mock_download: MagicMock, mock_save: MagicMock) -> None:+ def test_extract_slack_files_copies_to_uploaded_media(+ self, _label: str, is_stub: bool, mock_download: MagicMock, mock_save: MagicMock+ ) -> None:mock_download.return_value = VALID_PNG_BYTESmock_save.return_value = "https://app.posthog.com/uploaded_media/abc"- fake_team = fake_slack_team()+ full_file = {+ "id": "F123",+ "mimetype": "image/png",+ "name": "test.png",+ "url_private_download": "https://files.slack.com/files-pri/T/F/test.png",+ }fake_client = fake_slack_client()+ fake_client.files_info.return_value = {"ok": True, "file": full_file}+ event_file = {"id": "F123", "mode": "file_access", "file_access": "check_file_info"} if is_stub else full_file- files = [- {- "id": "F123",- "mimetype": "image/png",- "name": "test.png",- "url_private_download": "https://files.slack.com/files-pri/T/F/test.png",- }- ]- images = extract_slack_files(files, fake_team, fake_client)+ images = extract_slack_files([event_file], fake_slack_team(), fake_client)assert len(images) == 1assert images[0]["url"] == "https://app.posthog.com/uploaded_media/abc"- mock_download.assert_called_once()+ assert mock_download.call_args.args[0] == "https://files.slack.com/files-pri/T/F/test.png"mock_save.assert_called_once()@parameterized.expand(@@ -163,6 +170,51 @@ class TestSlackImageIngest(SimpleTestCase):else:assert split.files == []+ @parameterized.expand(+ [+ ("download_fails", False),+ ("lookup_fails", True),+ ]+ )+ @patch("products.conversations.backend.slack.save_file_to_uploaded_media")+ @patch("products.conversations.backend.slack._download_slack_image_bytes")+ def test_extract_slack_files_stub_failure(+ self, _label: str, lookup_fails: bool, mock_download: MagicMock, mock_save: MagicMock+ ) -> None:+ permalink = "https://acme.slack.com/files/U1/F123/test.jpg"+ fake_client = fake_slack_client()+ if lookup_fails:+ fake_client.files_info.side_effect = Exception("slack down")+ else:+ mock_download.return_value = None+ fake_client.files_info.return_value = {+ "ok": True,+ "file": {+ "id": "F123",+ "mimetype": "image/jpeg",+ "name": "test.jpg",+ "url_private_download": "https://files.slack.com/files-pri/T/F/test.jpg",+ "permalink": permalink,+ },+ }++ attachments = extract_slack_files(+ [{"id": "F123", "mode": "file_access", "file_access": "check_file_info"}],+ fake_slack_team(),+ fake_client,+ )+ split = split_slack_attachments(attachments)++ mock_save.assert_not_called()+ assert split.images == []+ if lookup_fails:+ mock_download.assert_not_called()+ assert split.files == []+ else:+ assert split.files == [+ {"url": permalink, "name": "test.jpg", "mimetype": "image/jpeg", "unavailable": True}+ ]+@parameterized.expand([("install_predating_scope_records", None),diff --git a/products/conversations/backend/slack.py b/products/conversations/backend/slack.pyindex 522ff2201c76..02e233beaccd 100644--- a/products/conversations/backend/slack.py+++ b/products/conversations/backend/slack.py@@ -410,6 +410,24 @@ def _slack_hosted_fallback(f: dict) -> dict | None:}+def _resolve_slack_file_stub(f: dict, client: WebClient | None) -> dict:+ """The full file object for a file that arrived without a download URL.++ In a Slack Connect channel the Events API sends only the file id, with+ ``file_access: "check_file_info"``. The download URLs and the permalink are only+ available from ``files.info``. Without them the file can be neither re-hosted nor linked.+ """+ file_id = f.get("id")+ if f.get("url_private_download") or f.get("url_private") or not file_id or client is None:+ return f+ try:+ full_file = client.files_info(file=file_id).get("file")+ except Exception:+ logger.warning("🖼️ slack_file_info_failed", file_id=file_id, exc_info=True)+ return f+ return full_file if isinstance(full_file, dict) else f++def extract_slack_files(files: list[dict] | None, team: Team, client: WebClient | None = None) -> list[dict]:"""Extract attachments from Slack and re-host them in UploadedMedia.@@ -431,7 +449,10 @@ def extract_slack_files(files: list[dict] | None, team: Team, client: WebClientattachments: list[dict] = []unavailable_count = 0for f in files[:MAX_ATTACHMENTS_PER_MESSAGE]:- attachment = _rehost_slack_file(f, team, bot_token) if can_read_files else None+ attachment: dict | None = None+ if can_read_files:+ f = _resolve_slack_file_stub(f, client)+ attachment = _rehost_slack_file(f, team, bot_token)if attachment is None:attachment = _slack_hosted_fallback(f)unavailable_count += 1