refactor: unify inline directives and media fetch

This commit is contained in:
Peter Steinberger
2026-01-10 03:01:04 +01:00
parent 4075895c4c
commit f28a4a34ad
15 changed files with 345 additions and 178 deletions

View File

@@ -1,3 +1,5 @@
import { parseInlineDirectives } from "../utils/directive-tags.js";
/**
* Extract audio mode tag from text.
* Supports [[audio_as_voice]] to send audio as voice bubble instead of file.
@@ -8,24 +10,10 @@ export function parseAudioTag(text?: string): {
audioAsVoice: boolean;
hadTag: boolean;
} {
if (!text) return { text: "", audioAsVoice: false, hadTag: false };
let cleaned = text;
let audioAsVoice = false; // default: audio file (backward compatible)
let hadTag = false;
// [[audio_as_voice]] -> send as voice bubble (opt-in)
const voiceMatch = cleaned.match(/\[\[audio_as_voice\]\]/i);
if (voiceMatch) {
cleaned = cleaned.replace(/\[\[audio_as_voice\]\]/gi, " ");
audioAsVoice = true;
hadTag = true;
}
// Clean up whitespace
cleaned = cleaned
.replace(/[ \t]+/g, " ")
.replace(/[ \t]*\n[ \t]*/g, "\n")
.trim();
return { text: cleaned, audioAsVoice, hadTag };
const result = parseInlineDirectives(text, { stripReplyTags: false });
return {
text: result.text,
audioAsVoice: result.audioAsVoice,
hadTag: result.hasAudioTag,
};
}

18
src/media/audio.ts Normal file
View File

@@ -0,0 +1,18 @@
import { getFileExtension } from "./mime.js";
const VOICE_AUDIO_EXTENSIONS = new Set([".oga", ".ogg", ".opus"]);
export function isVoiceCompatibleAudio(opts: {
contentType?: string | null;
fileName?: string | null;
}): boolean {
const mime = opts.contentType?.toLowerCase();
if (mime && (mime.includes("ogg") || mime.includes("opus"))) {
return true;
}
const fileName = opts.fileName?.trim();
if (!fileName) return false;
const ext = getFileExtension(fileName);
if (!ext) return false;
return VOICE_AUDIO_EXTENSIONS.has(ext);
}