Skip to content

Commit 65bcdf2

Browse files
authored
fix(discord): report actual delivered message kinds (#128575)
1 parent 45d6a08 commit 65bcdf2

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

‎extensions/discord/src/send.outbound.ts‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ export async function sendMessageDiscord(
208208

209209
// Forum/Media channels reject POST /messages; auto-create a thread post instead.
210210
const channel = await resolveDiscordChannel(rest, channelId);
211+
const deliveredResults: DiscordSendResult[] = [];
212+
let deliveryThreadId: string | undefined;
213+
const reportResult: DiscordSendProgress = async (progressResult, kind, replyToId) => {
214+
const deliveredResult = toDiscordSendResult(progressResult, deliveryThreadId ?? channelId, {
215+
kind,
216+
threadId: deliveryThreadId,
217+
reply: createReusableDiscordReplyReference(replyToId),
218+
});
219+
deliveredResults.push(deliveredResult);
220+
await opts.onDeliveryResult?.(deliveredResult);
221+
};
211222

212223
if (isForumLikeChannel(channel)) {
213224
if (((channel.flags ?? 0) & DISCORD_FORUM_REQUIRE_TAG_FLAG) !== 0) {
@@ -275,6 +286,7 @@ export async function sendMessageDiscord(
275286
}
276287

277288
const threadId = threadRes.id;
289+
deliveryThreadId = threadId;
278290
const messageId = threadRes.message?.id ?? threadId;
279291
const resultChannelId = threadRes.message?.channel_id ?? threadId;
280292
const remainingChunks = chunks.slice(1);
@@ -286,13 +298,8 @@ export async function sendMessageDiscord(
286298
channelId,
287299
{ kind: "text", threadId },
288300
);
289-
const deliveredResults: DiscordSendResult[] = [starterResult];
301+
deliveredResults.push(starterResult);
290302
await opts.onDeliveryResult?.(starterResult);
291-
const reportThreadResult: DiscordSendProgress = async (result, kind) => {
292-
const deliveredResult = toDiscordSendResult(result, threadId, { kind, threadId });
293-
deliveredResults.push(deliveredResult);
294-
await opts.onDeliveryResult?.(deliveredResult);
295-
};
296303

297304
try {
298305
if (opts.mediaUrl) {
@@ -314,7 +321,7 @@ export async function sendMessageDiscord(
314321
suppressEmbeds,
315322
allowedMentions: opts.allowedMentions,
316323
maxChars: textLimit,
317-
onResult: reportThreadResult,
324+
onResult: reportResult,
318325
onPlatformSendDispatch: opts.onPlatformSendDispatch,
319326
});
320327
await sendDiscordThreadTextChunks({
@@ -328,7 +335,7 @@ export async function sendMessageDiscord(
328335
silent: opts.silent,
329336
suppressEmbeds,
330337
allowedMentions: opts.allowedMentions,
331-
onResult: reportThreadResult,
338+
onResult: reportResult,
332339
onPlatformSendDispatch: opts.onPlatformSendDispatch,
333340
});
334341
} else {
@@ -343,7 +350,7 @@ export async function sendMessageDiscord(
343350
silent: opts.silent,
344351
suppressEmbeds,
345352
allowedMentions: opts.allowedMentions,
346-
onResult: reportThreadResult,
353+
onResult: reportResult,
347354
onPlatformSendDispatch: opts.onPlatformSendDispatch,
348355
});
349356
}
@@ -369,14 +376,6 @@ export async function sendMessageDiscord(
369376
}
370377

371378
let result: DiscordChannelMessageResult;
372-
const reportResult: DiscordSendProgress = async (progressResult, kind, replyToId) => {
373-
await opts.onDeliveryResult?.(
374-
toDiscordSendResult(progressResult, channelId, {
375-
kind,
376-
reply: createReusableDiscordReplyReference(replyToId),
377-
}),
378-
);
379-
};
380379
try {
381380
if (opts.mediaUrl) {
382381
result = await sendDiscordMedia({
@@ -436,10 +435,11 @@ export async function sendMessageDiscord(
436435
accountId: accountInfo.accountId,
437436
direction: "outbound",
438437
});
439-
return toDiscordSendResult(result, channelId, {
440-
kind: opts.mediaUrl ? "media" : opts.components || opts.embeds ? "card" : "text",
441-
reply: opts.reply,
442-
});
438+
return {
439+
messageId: result.id || "unknown",
440+
channelId: result.channel_id ?? channelId,
441+
receipt: createDiscordSendReceiptFromResults({ results: deliveredResults }),
442+
};
443443
}
444444

445445
export async function sendStickerDiscord(

‎extensions/discord/src/send.sends-basic-channel-messages.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ describe("sendMessageDiscord", () => {
392392
];
393393
const onDeliveryResult = vi.fn();
394394

395-
await sendMessageDiscord("channel:789", "a".repeat(2_500), {
395+
const result = await sendMessageDiscord("channel:789", "a".repeat(2_500), {
396396
rest,
397397
token: "t",
398398
cfg: DISCORD_TEST_CFG,
@@ -415,6 +415,7 @@ describe("sendMessageDiscord", () => {
415415
"card",
416416
"text",
417417
]);
418+
expect(result.receipt.parts.map(({ kind }) => kind)).toEqual(["card", "text"]);
418419
});
419420

420421
it("delivers embed-only and native Components V2 messages over real HTTP", async () => {
@@ -1008,6 +1009,7 @@ describe("sendMessageDiscord", () => {
10081009
});
10091010

10101011
expect(res.messageId).toBe("fallback-msg");
1012+
expectSingleReceiptPart(res.receipt, { platformMessageId: "fallback-msg", kind: "text" });
10111013
expect(postMock).toHaveBeenCalledTimes(2);
10121014
expectBodyFileName(requireRestBody(postMock, 0), "photo.jpg");
10131015
const fallbackBody = requireRestBody(postMock, 1);
@@ -1199,17 +1201,20 @@ describe("sendMessageDiscord", () => {
11991201
name: "preserves reply reference across all text chunks by default",
12001202
params: { text: "a".repeat(2001) },
12011203
expectsSecondReply: true,
1204+
expectedKinds: ["text", "text"],
12021205
},
12031206
{
12041207
name: "limits reply reference to the first text chunk when requested",
12051208
params: { text: "a".repeat(2001), replyScope: "first" as const },
12061209
expectsSecondReply: false,
12071210
checksReceipt: true,
1211+
expectedKinds: ["text", "text"],
12081212
},
12091213
{
12101214
name: "preserves reply reference for follow-up text chunks after media caption split by default",
12111215
params: { text: "a".repeat(2500), mediaUrl: "file:///tmp/photo.jpg" },
12121216
expectsSecondReply: true,
1217+
expectedKinds: ["media", "text"],
12131218
},
12141219
{
12151220
name: "limits media caption reply reference to the first physical message when requested",
@@ -1219,9 +1224,11 @@ describe("sendMessageDiscord", () => {
12191224
replyScope: "first" as const,
12201225
},
12211226
expectsSecondReply: false,
1227+
expectedKinds: ["media", "text"],
12221228
},
1223-
])("$name", async ({ params, expectsSecondReply, checksReceipt }) => {
1229+
])("$name", async ({ params, expectsSecondReply, checksReceipt, expectedKinds }) => {
12241230
const { firstBody, secondBody, result } = await sendChunkedReplyAndCollectBodies(params);
1231+
expect(result.receipt.parts.map(({ kind }) => kind)).toEqual(expectedKinds);
12251232
expectReplyReference(firstBody, "orig-123");
12261233
if (expectsSecondReply) {
12271234
expectReplyReference(secondBody, "orig-123");

0 commit comments

Comments
 (0)