Skip to content

Commit e65bc32

Browse files
committed
fix(line): address review feedback on media auth handling
1 parent e5b2524 commit e65bc32

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/line/bot-handlers.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ describe("handleLineWebhookEvents", () => {
288288
expect(buildLineMessageContextMock).toHaveBeenCalledTimes(1);
289289
expect(buildLineMessageContextMock).toHaveBeenCalledWith(
290290
expect.objectContaining({
291+
commandAuthorized: true,
291292
allMedia: [
292293
{
293294
path: "/tmp/line-media-file.pdf",

src/line/bot-handlers.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ async function shouldProcessLineEvent(
226226
allowTextCommands: true,
227227
hasControlCommand: hasControlCommand(rawText, cfg),
228228
});
229-
return { allowed: true, commandAuthorized: commandGate.commandAuthorized };
229+
return {
230+
allowed: true,
231+
commandAuthorized: isNonTextLineMessageEvent(event) ? true : commandGate.commandAuthorized,
232+
};
230233
}
231234

232235
if (dmPolicy === "disabled") {
@@ -263,7 +266,10 @@ async function shouldProcessLineEvent(
263266
allowTextCommands: true,
264267
hasControlCommand: hasControlCommand(rawText, cfg),
265268
});
266-
return { allowed: true, commandAuthorized: commandGate.commandAuthorized };
269+
return {
270+
allowed: true,
271+
commandAuthorized: isNonTextLineMessageEvent(event) ? true : commandGate.commandAuthorized,
272+
};
267273
}
268274

269275
/** Extract raw text from a LINE message or postback event for command detection. */
@@ -281,6 +287,12 @@ function resolveEventRawText(event: MessageEvent | PostbackEvent): string {
281287
return "";
282288
}
283289

290+
function isNonTextLineMessageEvent(
291+
event: MessageEvent | PostbackEvent,
292+
): event is MessageEvent & { message: Exclude<MessageEvent["message"], { type: "text" }> } {
293+
return event.type === "message" && event.message.type !== "text";
294+
}
295+
284296
async function handleMessageEvent(event: MessageEvent, context: LineHandlerContext): Promise<void> {
285297
const { cfg, account, runtime, mediaMaxBytes, processMessage } = context;
286298
const message = event.message;

src/line/download.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ interface DownloadResult {
99
size: number;
1010
}
1111

12+
const AUDIO_BRANDS = new Set(["m4a ", "m4b ", "m4p ", "m4r ", "f4a ", "f4b "]);
13+
1214
export async function downloadLineMedia(
1315
messageId: string,
1416
channelAccessToken: string,
@@ -91,8 +93,7 @@ function detectContentType(buffer: Buffer): string {
9193
// ISO BMFF containers share `ftyp`; use major brand to separate common
9294
// M4A audio payloads from video mp4 containers.
9395
const majorBrand = buffer.toString("ascii", 8, 12).toLowerCase();
94-
const audioBrands = new Set(["m4a ", "m4b ", "m4p ", "m4r ", "f4a ", "f4b "]);
95-
if (audioBrands.has(majorBrand)) {
96+
if (AUDIO_BRANDS.has(majorBrand)) {
9697
return "audio/mp4";
9798
}
9899
return "video/mp4";

0 commit comments

Comments
 (0)