Skip to content

Commit 6ebf503

Browse files
committed
refactor(media): centralize voice compatibility policy
1 parent 03fee3c commit 6ebf503

6 files changed

Lines changed: 62 additions & 40 deletions

File tree

extensions/matrix/src/matrix/send/formatting.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ export function resolveMatrixVoiceDecision(opts: {
7777
if (!opts.wantsVoice) {
7878
return { useVoice: false };
7979
}
80-
if (
81-
getCore().media.isVoiceCompatibleAudio({
82-
contentType: opts.contentType,
83-
fileName: opts.fileName,
84-
})
85-
) {
80+
if (isMatrixVoiceCompatibleAudio(opts)) {
8681
return { useVoice: true };
8782
}
8883
return { useVoice: false };
8984
}
85+
86+
function isMatrixVoiceCompatibleAudio(opts: { contentType?: string; fileName?: string }): boolean {
87+
// Matrix currently shares the core voice compatibility policy.
88+
// Keep this wrapper as the seam if Matrix policy diverges later.
89+
return getCore().media.isVoiceCompatibleAudio({
90+
contentType: opts.contentType,
91+
fileName: opts.fileName,
92+
});
93+
}

src/media/audio.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import { describe, expect, it } from "vitest";
2-
import { isVoiceCompatibleAudio } from "./audio.js";
2+
import {
3+
isVoiceCompatibleAudio,
4+
TELEGRAM_VOICE_AUDIO_EXTENSIONS,
5+
TELEGRAM_VOICE_MIME_TYPES,
6+
} from "./audio.js";
37

48
describe("isVoiceCompatibleAudio", () => {
59
it.each([
6-
{ contentType: "audio/ogg", fileName: null },
7-
{ contentType: "audio/opus", fileName: null },
10+
...Array.from(TELEGRAM_VOICE_MIME_TYPES, (contentType) => ({ contentType, fileName: null })),
811
{ contentType: "audio/ogg; codecs=opus", fileName: null },
9-
{ contentType: "audio/mpeg", fileName: null },
10-
{ contentType: "audio/mp3", fileName: null },
11-
{ contentType: "audio/mp4", fileName: null },
1212
{ contentType: "audio/mp4; codecs=mp4a.40.2", fileName: null },
13-
{ contentType: "audio/x-m4a", fileName: null },
14-
{ contentType: "audio/m4a", fileName: null },
1513
])("returns true for MIME type $contentType", (opts) => {
1614
expect(isVoiceCompatibleAudio(opts)).toBe(true);
1715
});
1816

19-
it.each([".ogg", ".oga", ".opus", ".mp3", ".m4a"])("returns true for extension %s", (ext) => {
17+
it.each(Array.from(TELEGRAM_VOICE_AUDIO_EXTENSIONS))("returns true for extension %s", (ext) => {
2018
expect(isVoiceCompatibleAudio({ fileName: `voice${ext}` })).toBe(true);
2119
});
2220

src/media/audio.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { getFileExtension } from "./mime.js";
1+
import { getFileExtension, normalizeMimeType } from "./mime.js";
22

3-
const VOICE_AUDIO_EXTENSIONS = new Set([".oga", ".ogg", ".opus", ".mp3", ".m4a"]);
3+
export const TELEGRAM_VOICE_AUDIO_EXTENSIONS = new Set([".oga", ".ogg", ".opus", ".mp3", ".m4a"]);
44

55
/**
66
* MIME types compatible with voice messages.
77
* Telegram sendVoice supports OGG/Opus, MP3, and M4A.
88
* https://core.telegram.org/bots/api#sendvoice
99
*/
10-
const VOICE_MIME_TYPES = new Set([
10+
export const TELEGRAM_VOICE_MIME_TYPES = new Set([
1111
"audio/ogg",
1212
"audio/opus",
1313
"audio/mpeg",
@@ -17,16 +17,13 @@ const VOICE_MIME_TYPES = new Set([
1717
"audio/m4a",
1818
]);
1919

20-
export function isVoiceCompatibleAudio(opts: {
20+
export function isTelegramVoiceCompatibleAudio(opts: {
2121
contentType?: string | null;
2222
fileName?: string | null;
2323
}): boolean {
24-
const mime = opts.contentType?.toLowerCase().trim();
25-
if (mime) {
26-
const baseMime = mime.split(";")[0].trim();
27-
if (VOICE_MIME_TYPES.has(baseMime)) {
28-
return true;
29-
}
24+
const mime = normalizeMimeType(opts.contentType);
25+
if (mime && TELEGRAM_VOICE_MIME_TYPES.has(mime)) {
26+
return true;
3027
}
3128
const fileName = opts.fileName?.trim();
3229
if (!fileName) {
@@ -36,5 +33,16 @@ export function isVoiceCompatibleAudio(opts: {
3633
if (!ext) {
3734
return false;
3835
}
39-
return VOICE_AUDIO_EXTENSIONS.has(ext);
36+
return TELEGRAM_VOICE_AUDIO_EXTENSIONS.has(ext);
37+
}
38+
39+
/**
40+
* Backward-compatible alias used across plugin/runtime call sites.
41+
* Keeps existing behavior while making Telegram-specific policy explicit.
42+
*/
43+
export function isVoiceCompatibleAudio(opts: {
44+
contentType?: string | null;
45+
fileName?: string | null;
46+
}): boolean {
47+
return isTelegramVoiceCompatibleAudio(opts);
4048
}

src/media/mime.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import JSZip from "jszip";
22
import { describe, expect, it } from "vitest";
3-
import { detectMime, extensionForMime, imageMimeFromFormat, isAudioFileName } from "./mime.js";
3+
import {
4+
detectMime,
5+
extensionForMime,
6+
imageMimeFromFormat,
7+
isAudioFileName,
8+
normalizeMimeType,
9+
} from "./mime.js";
410

511
async function makeOoxmlZip(opts: { mainMime: string; partPath: string }): Promise<Buffer> {
612
const zip = new JSZip();
@@ -110,3 +116,15 @@ describe("isAudioFileName", () => {
110116
}
111117
});
112118
});
119+
120+
describe("normalizeMimeType", () => {
121+
it("normalizes case and strips parameters", () => {
122+
expect(normalizeMimeType("Audio/MP4; codecs=mp4a.40.2")).toBe("audio/mp4");
123+
});
124+
125+
it("returns undefined for empty input", () => {
126+
expect(normalizeMimeType(" ")).toBeUndefined();
127+
expect(normalizeMimeType(null)).toBeUndefined();
128+
expect(normalizeMimeType(undefined)).toBeUndefined();
129+
});
130+
});

src/media/mime.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const AUDIO_FILE_EXTENSIONS = new Set([
5252
".wav",
5353
]);
5454

55-
function normalizeHeaderMime(mime?: string | null): string | undefined {
55+
export function normalizeMimeType(mime?: string | null): string | undefined {
5656
if (!mime) {
5757
return undefined;
5858
}
@@ -120,7 +120,7 @@ async function detectMimeImpl(opts: {
120120
const ext = getFileExtension(opts.filePath);
121121
const extMime = ext ? MIME_BY_EXT[ext] : undefined;
122122

123-
const headerMime = normalizeHeaderMime(opts.headerMime);
123+
const headerMime = normalizeMimeType(opts.headerMime);
124124
const sniffed = await sniffMime(opts.buffer);
125125

126126
// Prefer sniffed types, but don't let generic container types override a more
@@ -145,10 +145,11 @@ async function detectMimeImpl(opts: {
145145
}
146146

147147
export function extensionForMime(mime?: string | null): string | undefined {
148-
if (!mime) {
148+
const normalized = normalizeMimeType(mime);
149+
if (!normalized) {
149150
return undefined;
150151
}
151-
return EXT_BY_MIME[mime.toLowerCase()];
152+
return EXT_BY_MIME[normalized];
152153
}
153154

154155
export function isGifMedia(opts: {

src/telegram/voice.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import { isVoiceCompatibleAudio } from "../media/audio.js";
2-
3-
export function isTelegramVoiceCompatible(opts: {
4-
contentType?: string | null;
5-
fileName?: string | null;
6-
}): boolean {
7-
return isVoiceCompatibleAudio(opts);
8-
}
1+
import { isTelegramVoiceCompatibleAudio } from "../media/audio.js";
92

103
export function resolveTelegramVoiceDecision(opts: {
114
wantsVoice: boolean;
@@ -15,7 +8,7 @@ export function resolveTelegramVoiceDecision(opts: {
158
if (!opts.wantsVoice) {
169
return { useVoice: false };
1710
}
18-
if (isTelegramVoiceCompatible(opts)) {
11+
if (isTelegramVoiceCompatibleAudio(opts)) {
1912
return { useVoice: true };
2013
}
2114
const contentType = opts.contentType ?? "unknown";

0 commit comments

Comments
 (0)