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.

fix(tasks): render the comment slack dm heading once (#82719)

Shy Alter0a6c3e73d3ec
diff --git a/products/tasks/backend/logic/services/comment_slack_dm.py b/products/tasks/backend/logic/services/comment_slack_dm.py
index 939e2b3a972d..99c44311b102 100644
--- a/products/tasks/backend/logic/services/comment_slack_dm.py
+++ b/products/tasks/backend/logic/services/comment_slack_dm.py
@@ -147,7 +147,7 @@ def send_comment_slack_dms(*, team_id: int, comment_id: UUID, task_id: UUID, rec
if not slack_user_id:
_skip(comment_id, "recipient_not_found_in_slack", user_id=user_id)
continue
- fallback, blocks = _message(
+ heading, blocks = _message(
kind=kind,
comment=comment,
task=task,
@@ -162,8 +162,8 @@ def send_comment_slack_dms(*, team_id: int, comment_id: UUID, task_id: UUID, rec
)
slack.client.chat_postMessage(
channel=slack_user_id,
- text=fallback,
- attachments=[{"color": _ACCENT, "blocks": blocks}],
+ text=heading,
+ attachments=[{"color": _ACCENT, "blocks": blocks}] if blocks else None,
unfurl_links=False,
)
except Exception as exc:
@@ -358,8 +358,6 @@ def _message(
# A pipe in the title would end the link label early, so it can't survive into the label.
label = escape_slack_mrkdwn(title).replace("|", "-")
heading = template.format(author=f"*{escape_slack_mrkdwn(author)}*", link=f"<{url}|{label}>")
- # Plain-text twin for the notification preview and older clients.
- fallback = template.format(author=escape_slack_mrkdwn(author), link=escape_slack_mrkdwn(title))
body, _ = rich_content_to_slack_payload(
comment.rich_content,
@@ -369,10 +367,11 @@ def _message(
slack_user_id_by_email=slack_user_id_by_email,
)
body = _truncate_body(body.strip())
- text = f"{heading}\n\n{body}" if body else heading
- blocks: list[dict] = [{"type": "section", "text": {"type": "mrkdwn", "text": text}}]
+ blocks: list[dict] = []
+ if body:
+ blocks.append({"type": "section", "text": {"type": "mrkdwn", "text": body}})
# Three canvases in one task otherwise produce three identical headings.
location = _LOCATIONS.get(comment.scope)
if location:
blocks.append({"type": "context", "elements": [{"type": "mrkdwn", "text": location}]})
- return fallback, blocks
+ return heading, blocks
diff --git a/products/tasks/backend/tests/test_comment_slack_dm.py b/products/tasks/backend/tests/test_comment_slack_dm.py
index 63438cf90008..80e7a2109ab6 100644
--- a/products/tasks/backend/tests/test_comment_slack_dm.py
+++ b/products/tasks/backend/tests/test_comment_slack_dm.py
@@ -52,7 +52,10 @@ class TestCommentSlackDm(CommentActivityTestCase):
with self.captureOnCommitCallbacks(execute=True):
super()._record_activity(comment, user_ids)
- def _dm_text(self) -> str:
+ def _dm_heading(self) -> str:
+ return self.slack_client.chat_postMessage.call_args.kwargs["text"]
+
+ def _dm_body(self) -> str:
attachment = self.slack_client.chat_postMessage.call_args.kwargs["attachments"][0]
return attachment["blocks"][0]["text"]["text"]
@@ -65,9 +68,10 @@ class TestCommentSlackDm(CommentActivityTestCase):
self._record_activity(comment, [self.author.id])
assert self._dm_channels() == ["U-author"]
- text = self._dm_text()
- assert "mentioned you" in text
- assert "this needs a guard" in text
+ assert "mentioned you" in self._dm_heading()
+ body = self._dm_body()
+ assert "this needs a guard" in body
+ assert "mentioned you" not in body
def test_an_overlong_comment_is_not_cut_inside_a_link(self):
filler = "x" * 790
@@ -75,9 +79,9 @@ class TestCommentSlackDm(CommentActivityTestCase):
self._record_activity(comment, [self.author.id])
- text = self._dm_text()
- assert text.endswith("…")
- assert text.rfind("<") < text.rfind(">")
+ body = self._dm_body()
+ assert body.endswith("…")
+ assert body.rfind("<") <= body.rfind(">")
def test_inline_mention_lookups_are_bounded(self) -> None:
third = User.objects.create_user(email="[email protected]", first_name="Carol", password="password")
@@ -99,8 +103,7 @@ class TestCommentSlackDm(CommentActivityTestCase):
self._record_activity(comment, [self.author.id])
assert lookup.call_count == 2
- text = self._dm_text()
- assert "<@U-one> <@U-two> @Member 2" in text
+ assert "<@U-one> <@U-two> @Member 2" in self._dm_body()
def test_inline_mentions_only_query_slack_for_current_organization_members(self) -> None:
comment = self._comment(content="@[Member]([email protected]) and @[Outsider]([email protected])")
@@ -119,9 +122,9 @@ class TestCommentSlackDm(CommentActivityTestCase):
lookup.assert_called_once()
assert lookup.call_args.args[2] == "[email protected]"
- assert "<@U-member> and @Outsider" in self._dm_text()
+ assert "<@U-member> and @Outsider" in self._dm_body()
- def test_fallback_text_escapes_user_controlled_slack_markup(self):
+ def test_heading_escapes_user_controlled_slack_markup(self):
self.peer.first_name = "<@U-ATTACKER>"
self.peer.last_name = ""
self.peer.save(update_fields=["first_name", "last_name"])
@@ -130,8 +133,10 @@ class TestCommentSlackDm(CommentActivityTestCase):
self._record_activity(self._comment(), [self.author.id])
- fallback = self.slack_client.chat_postMessage.call_args.kwargs["text"]
- assert fallback == "&lt;@U-ATTACKER&gt; mentioned you on &lt;https://example.com|click me&gt;"
+ heading = self._dm_heading()
+ assert "<@U-ATTACKER>" not in heading
+ assert "*&lt;@U-ATTACKER&gt;* mentioned you on " in heading
+ assert heading.endswith("|&lt;https://example.com-click me&gt;>")
def test_no_dm_when_the_user_opted_out(self):
self._opt_in(self.author, False)
@@ -208,7 +213,8 @@ class TestCommentSlackDm(CommentActivityTestCase):
assert self._dm_channels() == ["U-author"]
assert (
- f"/code/task/{self.task.id}?comment={comment.id}&scope=desktop_canvas&item={canvas.id}" in self._dm_text()
+ f"/code/task/{self.task.id}?comment={comment.id}&scope=desktop_canvas&item={canvas.id}"
+ in self._dm_heading()
)
def test_dm_links_to_the_desktop_task_bridge_anchored_on_the_comment(self):
@@ -216,7 +222,7 @@ class TestCommentSlackDm(CommentActivityTestCase):
self._record_activity(comment, [self.author.id])
- assert f"/code/task/{self.task.id}?comment={comment.id}" in self._dm_text()
+ assert f"/code/task/{self.task.id}?comment={comment.id}" in self._dm_heading()
def test_canvas_comment_does_not_dm_a_recipient_without_canvas_access(self):
personal_channel = Channel.objects.unscoped().create(