Skip to content

Commit bd7fb1d

Browse files
authored
fix(slack): preserve threads on ephemeral menu replies (#128341)
1 parent b3b33d2 commit bd7fb1d

2 files changed

Lines changed: 51 additions & 12 deletions

File tree

‎extensions/slack/src/monitor/slash.test.ts‎

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@ async function runArgMenuAction(
502502
userName?: string;
503503
channelId?: string;
504504
channelName?: string;
505+
message?: { ts: string; thread_ts?: string };
506+
container?: { message_ts: string; thread_ts?: string };
505507
respond?: ReturnType<typeof vi.fn>;
506508
includeRespond?: boolean;
507509
},
@@ -515,6 +517,8 @@ async function runArgMenuAction(
515517
user: { id: params.userId ?? "U1", name: params.userName ?? "Ada" },
516518
channel: { id: params.channelId ?? "C1", name: params.channelName ?? "directmessage" },
517519
trigger_id: "t1",
520+
...(params.message ? { message: params.message } : {}),
521+
...(params.container ? { container: params.container } : {}),
518522
},
519523
};
520524
if (includeRespond) {
@@ -1223,17 +1227,28 @@ describe("Slack native command argument menus", () => {
12231227
expect(firstDispatchArg().ctx?.OriginatingTo).toBe("team:TGRID1:user:U1");
12241228
});
12251229

1226-
it("does not apply the response_url call cap to Web API action replies", async () => {
1230+
it.each([
1231+
{ name: "top-level", message: undefined, threadTs: undefined },
1232+
{
1233+
name: "threaded",
1234+
message: { ts: "171.222", thread_ts: "170.111" },
1235+
threadTs: "170.111",
1236+
},
1237+
])("does not cap $name Web API action replies", async ({ message, threadTs }) => {
12271238
mockSixDispatchedReplies();
12281239

12291240
await runArgMenuAction(argMenuHandler, {
12301241
action: {
12311242
value: encodeValue({ command: "usage", arg: "mode", value: "tokens", userId: "U1" }),
12321243
},
1244+
message,
12331245
includeRespond: false,
12341246
});
12351247

12361248
expect(harness.postEphemeral).toHaveBeenCalledTimes(6);
1249+
for (const [payload] of harness.postEphemeral.mock.calls) {
1250+
expect(payload.thread_ts).toBe(threadTs);
1251+
}
12371252
});
12381253

12391254
it("keeps table fallback tokens literal in Web API action replies", async () => {
@@ -1280,6 +1295,7 @@ describe("Slack native command argument menus", () => {
12801295
action: {
12811296
value: encodeValue({ command: "usage", arg: "mode", value: "tokens", userId: "U1" }),
12821297
},
1298+
message: { ts: "171.222", thread_ts: "170.111" },
12831299
});
12841300

12851301
expect(respond).toHaveBeenCalledTimes(5);
@@ -1488,17 +1504,37 @@ describe("Slack native command argument menus", () => {
14881504
expect(trackEvent).toHaveBeenCalledTimes(1);
14891505
});
14901506

1491-
it("falls back to postEphemeral with token when respond is unavailable", async () => {
1492-
await runArgMenuAction(argMenuHandler, {
1493-
action: { value: "garbage" },
1494-
includeRespond: false,
1495-
});
1507+
it.each([
1508+
{ name: "a top-level action", message: undefined, container: undefined, threadTs: undefined },
1509+
{
1510+
name: "the action container's parent thread",
1511+
message: { ts: "171.222", thread_ts: "169.999" },
1512+
container: { message_ts: "171.222", thread_ts: "170.111" },
1513+
threadTs: "170.111",
1514+
},
1515+
{
1516+
name: "the action message's parent thread",
1517+
message: { ts: "171.222", thread_ts: "170.111" },
1518+
container: { message_ts: "171.222" },
1519+
threadTs: "170.111",
1520+
},
1521+
])(
1522+
"keeps $name when respond falls back to postEphemeral",
1523+
async ({ message, container, threadTs }) => {
1524+
await runArgMenuAction(argMenuHandler, {
1525+
action: { value: "garbage" },
1526+
message,
1527+
container,
1528+
includeRespond: false,
1529+
});
14961530

1497-
const payload = firstCallPayload(harness.postEphemeral, "postEphemeral");
1498-
expect(payload.token).toBe("bot-token");
1499-
expect(payload.channel).toBe("C1");
1500-
expect(payload.user).toBe("U1");
1501-
});
1531+
const payload = firstCallPayload(harness.postEphemeral, "postEphemeral");
1532+
expect(payload.token).toBe("bot-token");
1533+
expect(payload.channel).toBe("C1");
1534+
expect(payload.user).toBe("U1");
1535+
expect(payload.thread_ts).toBe(threadTs);
1536+
},
1537+
);
15021538

15031539
it("treats malformed percent-encoding as an invalid button", async () => {
15041540
await runArgMenuAction(argMenuHandler, {

‎extensions/slack/src/monitor/slash.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Slack plugin module implements slash behavior.
22
import type {
33
AllMiddlewareArgs,
4+
BlockAction,
45
SlackActionMiddlewareArgs,
56
SlackCommandMiddlewareArgs,
67
SlackOptionsMiddlewareArgs,
@@ -91,7 +92,7 @@ const SLACK_COMMAND_ARG_ACTION_BLOCKS_MAX = SLACK_MAX_BLOCKS - SLACK_COMMAND_ARG
9192

9293
type SlackCommandHandlerArgs = SlackCommandMiddlewareArgs &
9394
Pick<AllMiddlewareArgs, "context" | "client">;
94-
type SlackArgActionHandlerArgs = SlackActionMiddlewareArgs &
95+
type SlackArgActionHandlerArgs = SlackActionMiddlewareArgs<BlockAction> &
9596
Pick<AllMiddlewareArgs, "context" | "client">;
9697
type SlackArgOptionsHandlerArgs = SlackOptionsMiddlewareArgs<"block_suggestion"> &
9798
Pick<AllMiddlewareArgs, "context" | "client">;
@@ -1177,11 +1178,13 @@ export async function registerSlackMonitorSlashCommands(params: {
11771178
blocks?: (Block | KnownBlock)[];
11781179
mrkdwn?: boolean;
11791180
});
1181+
const threadTs = body.container?.thread_ts ?? body.message?.thread_ts;
11801182
await args.client.chat.postEphemeral({
11811183
token: ctx.botToken,
11821184
channel: body.channel.id,
11831185
user: body.user.id,
11841186
text: payload.text ?? "",
1187+
...(threadTs ? { thread_ts: threadTs } : {}),
11851188
...(payload.blocks ? { blocks: payload.blocks } : {}),
11861189
...(typeof payload.mrkdwn === "boolean" ? { mrkdwn: payload.mrkdwn } : {}),
11871190
});

0 commit comments

Comments
 (0)