Skip to content

Commit

Permalink
fix(handlers): fix createTextMessage altText and update snapshot
Browse files Browse the repository at this point in the history
- don't let altText leak to FlexComponent
  • Loading branch information
MrOrz committed Oct 16, 2023
1 parent c84199b commit 2e04335
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/webhook/handlers/__tests__/askingArticleSource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ it('returns instructions if user did not forward the whole message', async () =>
expect(replies).toMatchInlineSnapshot(`
Array [
Object {
"altText": "",
"altText": "Instructions",
"contents": Object {
"body": Object {
"contents": Array [
Expand Down
30 changes: 22 additions & 8 deletions src/webhook/handlers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
TextMessage,
FlexMessage,
FlexText,
FlexComponent,
} from '@line/bot-sdk';
import { t, msgid, ngettext } from 'ttag';
import GraphemeSplitter from 'grapheme-splitter';
Expand Down Expand Up @@ -715,21 +716,34 @@ type FlexTextWithoutType =
* @returns A single flex bubble message
*/
export function createTextMessage(textProps: FlexTextWithoutType): FlexMessage {
const altText = 'altText' in textProps ? textProps.altText : textProps.text;

const content: FlexComponent = {
type: 'text',
wrap: true,
// Exclude altText from FlexComponent content
...(() => {
if (!('altText' in textProps)) {
return textProps;
}
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
altText,
...other
} = textProps;
return other;
})(),
};

return {
type: 'flex',
altText: textProps.text ?? textProps.altText,
altText,
contents: {
type: 'bubble',
body: {
type: 'box',
layout: 'vertical',
contents: [
{
type: 'text',
wrap: true,
...textProps,
},
],
contents: [content],
},
},
};
Expand Down

0 comments on commit 2e04335

Please sign in to comment.