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(conversations): warn when a resend is deduplicated (#80555)

Christiaan Hendriksen5bf4dc56affb
diff --git a/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.test.ts b/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.test.ts
index c9a646c9cabf..5dda7e6b8c1f 100644
--- a/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.test.ts
+++ b/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.test.ts
@@ -32,9 +32,9 @@ jest.mock('~/lib/api', () => {
__esModule: true,
default: {
...actual.default,
+ createResponse: jest.fn(),
comments: {
...actual.default?.comments,
- create: jest.fn().mockResolvedValue(undefined),
list: jest.fn().mockResolvedValue({ results: [] }),
},
persons: {
@@ -99,6 +99,10 @@ function makeSupportComment(overrides: Partial<CommentType> = {}): CommentType {
} as unknown as CommentType
}
+function commentResponse(comment: CommentType, status: number = 201): Response {
+ return { status, json: () => Promise.resolve(comment) } as unknown as Response
+}
+
function makeTicket(): Ticket {
return {
id: 'ticket-1',
@@ -337,7 +341,7 @@ describe('supportTicketSceneLogic replyRecipientDescription', () => {
describe('supportTicketSceneLogic sendMessage with statusAfterSend', () => {
let logic: ReturnType<typeof supportTicketSceneLogic.build>
- const commentsCreateMock = api.comments.create as jest.Mock
+ const createResponseMock = api.createResponse as jest.Mock
const ticketGetMock = api.conversationsTickets.get as jest.Mock
const ticketUpdateMock = api.conversationsTickets.update as jest.Mock
@@ -347,7 +351,7 @@ describe('supportTicketSceneLogic sendMessage with statusAfterSend', () => {
beforeEach(async () => {
initKeaTests()
- commentsCreateMock.mockReset().mockResolvedValue(makeSupportComment())
+ createResponseMock.mockReset().mockResolvedValue(commentResponse(makeSupportComment()))
ticketGetMock.mockReset().mockResolvedValue(loadedTicket())
ticketUpdateMock.mockReset()
// A non-'new', dash-free id: sendMessage early-returns on 'new' and loadTicket
@@ -386,7 +390,7 @@ describe('supportTicketSceneLogic sendMessage with statusAfterSend', () => {
})
it('does not update the ticket when the send fails', async () => {
- commentsCreateMock.mockRejectedValue(new Error('request failed'))
+ createResponseMock.mockRejectedValue(new Error('request failed'))
await expectLogic(logic, () => {
logic.actions.sendMessage('hello', null, false, undefined, 'resolved')
@@ -442,7 +446,7 @@ describe('supportTicketSceneLogic sendMessage with statusAfterSend', () => {
describe('supportTicketSceneLogic send outcome handling', () => {
let logic: ReturnType<typeof supportTicketSceneLogic.build>
- const commentsCreateMock = api.comments.create as jest.Mock
+ const createResponseMock = api.createResponse as jest.Mock
const commentsListMock = api.comments.list as jest.Mock
const ticketGetMock = api.conversationsTickets.get as jest.Mock
const captureMock = posthog.capture as jest.Mock
@@ -450,12 +454,12 @@ describe('supportTicketSceneLogic send outcome handling', () => {
const loadedTicket = (): Ticket => ({ ...makeTicket(), priority: 'medium', assignee: null }) as Ticket
const rejectWith = (status?: number): void => {
- commentsCreateMock.mockRejectedValue(Object.assign(new Error('send failed'), { status }))
+ createResponseMock.mockRejectedValue(Object.assign(new Error('send failed'), { status }))
}
beforeEach(async () => {
initKeaTests()
- commentsCreateMock.mockReset().mockResolvedValue(makeSupportComment())
+ createResponseMock.mockReset().mockResolvedValue(commentResponse(makeSupportComment()))
commentsListMock.mockReset().mockResolvedValue({ results: [] })
ticketGetMock.mockReset().mockResolvedValue(loadedTicket())
captureMock.mockClear()
@@ -470,7 +474,7 @@ describe('supportTicketSceneLogic send outcome handling', () => {
// These cases point the shared mock at failures and pending promises, which would break
// every later suite's initial message load.
commentsListMock.mockReset().mockResolvedValue({ results: [] })
- commentsCreateMock.mockReset().mockResolvedValue(makeSupportComment())
+ createResponseMock.mockReset().mockResolvedValue(commentResponse(makeSupportComment()))
})
it('shows the sent message without waiting for the next poll', async () => {
@@ -485,6 +489,20 @@ describe('supportTicketSceneLogic send outcome handling', () => {
expect(logic.values.messageSending).toBe(false)
})
+ it('flags a resend the server deduplicated instead of reporting it as sent', async () => {
+ createResponseMock.mockResolvedValue(commentResponse(makeSupportComment(), 200))
+ const onSuccess = jest.fn()
+
+ await expectLogic(logic, () => {
+ logic.actions.sendMessage('hello', null, false, onSuccess)
+ }).toFinishAllListeners()
+
+ expect(logic.values.messages.map((message) => message.id)).toEqual(['msg-sent-1'])
+ expect(onSuccess).not.toHaveBeenCalled()
+ expect(captureMock).toHaveBeenCalledWith('support reply send deduplicated', { is_private: false })
+ expect(logic.values.messageSending).toBe(false)
+ })
+
// A poll that started before the send must not replace the list with its older snapshot.
it('keeps the sent message when an in-flight poll resolves afterwards', async () => {
let resolveStalePoll: ((value: { results: CommentType[] }) => void) | undefined
@@ -546,7 +564,7 @@ describe('supportTicketSceneLogic send outcome handling', () => {
expect(logic.values.messages.map((message) => message.id)).toEqual(['msg-landed-1'])
expect(onSuccess).toHaveBeenCalledTimes(1)
- expect(commentsCreateMock).toHaveBeenCalledTimes(1)
+ expect(createResponseMock).toHaveBeenCalledTimes(1)
expect(captureMock).not.toHaveBeenCalledWith('support reply send unconfirmed', expect.anything())
})
@@ -712,7 +730,7 @@ describe('supportTicketSceneLogic private note editing', () => {
const ticketGetMock = api.conversationsTickets.get as jest.Mock
const noteUpdateMock = conversationsTicketsNotesPartialUpdate as jest.Mock
- const commentsCreateMock = api.comments.create as jest.Mock
+ const createResponseMock = api.createResponse as jest.Mock
const loadedTicket = (): Ticket => ({ ...makeTicket(), priority: 'medium', assignee: null }) as Ticket
@@ -720,7 +738,7 @@ describe('supportTicketSceneLogic private note editing', () => {
localStorage.clear()
initKeaTests()
noteUpdateMock.mockReset().mockResolvedValue(undefined)
- commentsCreateMock.mockReset().mockResolvedValue(makeSupportComment())
+ createResponseMock.mockReset().mockResolvedValue(commentResponse(makeSupportComment()))
ticketGetMock.mockReset().mockResolvedValue(loadedTicket())
logic = supportTicketSceneLogic({ id: 42 })
logic.mount()
@@ -839,7 +857,7 @@ describe('supportTicketSceneLogic private note editing', () => {
message: 'updated',
rich_content: updatedRich,
})
- expect(commentsCreateMock).not.toHaveBeenCalled()
+ expect(createResponseMock).not.toHaveBeenCalled()
expect(onSuccess).not.toHaveBeenCalled()
expect(logic.values.editingMessageId).toBeNull()
expect(logic.values.draftContent).toEqual(draft)
diff --git a/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.ts b/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.ts
index cb934194881e..4d2bcb196b5d 100644
--- a/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.ts
+++ b/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.ts
@@ -52,6 +52,7 @@ import {
conversationsTicketsNotesDestroy,
conversationsTicketsNotesPartialUpdate,
} from 'products/conversations/frontend/generated/api'
+import { getCommentsCreateUrl } from 'products/platform_features/frontend/generated/api'
import { signalsReportsList } from 'products/signals/frontend/generated/api'
import type { SignalReportApi } from 'products/signals/frontend/generated/api.schemas'
import { SignalSourceProductApi } from 'products/signals/frontend/generated/api.schemas'
@@ -1375,21 +1376,21 @@ export const supportTicketSceneLogic = kea<supportTicketSceneLogicType>([
const attemptStartedAt = dayjs()
let sent: CommentType | null = null
let unconfirmedReason: UnconfirmedSendReason = null
+ let alreadySent = false
try {
- sent = await api.comments.create(
- {
- content,
- rich_content: richContent,
- scope: 'conversations_ticket',
- item_id: ticketId,
- item_context: {
- author_type: 'support',
- is_private: isPrivate,
- },
+ const response = await api.createResponse(getCommentsCreateUrl(String(getCurrentTeamId())), {
+ content,
+ rich_content: richContent,
+ scope: 'conversations_ticket',
+ item_id: ticketId,
+ item_context: {
+ author_type: 'support',
+ is_private: isPrivate,
},
- {}
- )
+ })
+ alreadySent = response.status === 200
+ sent = (await response.json()) as CommentType
} catch (error: any) {
unconfirmedReason = classifySendFailure(error)
if (unconfirmedReason === null) {
@@ -1434,6 +1435,19 @@ export const supportTicketSceneLogic = kea<supportTicketSceneLogicType>([
return
}
+ if (alreadySent) {
+ posthog.capture('support reply send deduplicated', { is_private: isPrivate })
+ cache.messageRevision += 1
+ actions.appendMessage(sent)
+ lemonToast.warning(
+ isPrivate
+ ? "You just added this note, so we didn't add it again. Edit your draft to add something different."
+ : "You just sent this reply, so we didn't send it again. Edit your draft to send something different."
+ )
+ actions.setMessageSending(false)
+ return
+ }
+
// "Added", not "sent": email delivery is async (outbox + Celery) and can still fail
// after this API call succeeds; the per-message delivery status is the send signal.
cache.messageRevision += 1