Skip to content

Commit 1cf513c

Browse files
committed
fix(msteams): correlate attachment references
1 parent b3f4709 commit 1cf513c

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎extensions/msteams/src/attachments.helpers.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,30 @@ describe("msteams attachment helpers", () => {
214214
).toEqual({ placeholder: "", expectedMediaCount: 0 });
215215
});
216216

217+
it("does not count HTML references separately from files or cards", () => {
218+
expect(
219+
resolveMSTeamsInboundAttachmentPresentation([
220+
createHtmlAttachment('<attachment id="file-1"></attachment>'),
221+
{
222+
id: "file-1",
223+
contentType: CONTENT_TYPE_APPLICATION_PDF,
224+
contentUrl: TEST_URL_PDF,
225+
},
226+
]),
227+
).toEqual({ placeholder: "<media:document>", expectedMediaCount: 1 });
228+
229+
expect(
230+
resolveMSTeamsInboundAttachmentPresentation([
231+
createHtmlAttachment('<attachment id="card-1"></attachment>'),
232+
{
233+
id: "card-1",
234+
contentType: "application/vnd.microsoft.card.adaptive",
235+
content: { type: "AdaptiveCard" },
236+
},
237+
]),
238+
).toEqual({ placeholder: "", expectedMediaCount: 0 });
239+
});
240+
217241
it("counts repeated inline URLs once while keeping data images per occurrence", () => {
218242
const repeatedUrl = "https://example.com/repeated.png";
219243
expect(

‎extensions/msteams/src/attachments/html.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,22 @@ function countDistinctInlineCandidates(
170170
return dataCount + seenUrls.size;
171171
}
172172

173+
function countUnrepresentedHtmlAttachmentIds(attachments: MSTeamsAttachmentLike[]): number {
174+
const representedIds = new Set<string>();
175+
for (const attachment of attachments) {
176+
const contentType = normalizeContentType(attachment.contentType) ?? "";
177+
if (contentType.startsWith("text/html")) {
178+
continue;
179+
}
180+
const id = attachment.id?.trim();
181+
if (id) {
182+
representedIds.add(id);
183+
}
184+
}
185+
return extractMSTeamsHtmlAttachmentIds(attachments).filter((id) => !representedIds.has(id))
186+
.length;
187+
}
188+
173189
export function resolveMSTeamsInboundAttachmentPresentation(
174190
attachments: MSTeamsAttachmentLike[] | undefined,
175191
limits?: { maxInlineBytes?: number; maxInlineTotalBytes?: number },
@@ -181,7 +197,10 @@ export function resolveMSTeamsInboundAttachmentPresentation(
181197
const fileAttachments = list.filter(isAdvertisedFileAttachment);
182198
const inlinePlaceholderCount = countDistinctInlineCandidates(list, limits);
183199
const inlineExpectedCount = countDistinctInlineImages(list);
184-
const htmlAttachmentCount = extractMSTeamsHtmlAttachmentIds(list).length;
200+
// Teams HTML uses <attachment> tags as references. A matching attachment
201+
// entry is the same resource (and may be a card), so count only unmatched
202+
// IDs that need the Graph/Bot Framework hosted-content fallback.
203+
const htmlAttachmentCount = countUnrepresentedHtmlAttachmentIds(list);
185204
const expectedMediaCount = fileAttachments.length + inlineExpectedCount + htmlAttachmentCount;
186205
if (expectedMediaCount === 0) {
187206
return { placeholder: "", expectedMediaCount: 0 };

‎extensions/msteams/src/attachments/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Msteams type declarations define plugin contracts.
22
export type MSTeamsAttachmentLike = {
3+
id?: string | null;
34
contentType?: string | null;
45
contentUrl?: string | null;
56
name?: string | null;

0 commit comments

Comments
 (0)