Add support for rendering media captions (#43)

* Add support for rendering media captions

* Run prettier

* Deduplicate body props

* Add basic test

* Fix import order in test

* Fix test?
This commit is contained in:
Tulir Asokan 2024-10-04 15:58:22 +02:00 committed by GitHub
parent 26b0e83ac4
commit 5fbc5af884
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 113 additions and 22 deletions

View file

@ -190,25 +190,42 @@ export default class MessageEvent extends React.Component<IProps> implements IMe
} }
} }
return BodyType ? ( const hasCaption =
<BodyType [MsgType.Image, MsgType.File, MsgType.Audio, MsgType.Video].includes(msgtype as MsgType) &&
ref={this.body} content.filename &&
mxEvent={this.props.mxEvent} content.filename !== content.body;
highlights={this.props.highlights} const bodyProps: IBodyProps = {
highlightLink={this.props.highlightLink} ref: this.body,
showUrlPreview={this.props.showUrlPreview} mxEvent: this.props.mxEvent,
forExport={this.props.forExport} highlights: this.props.highlights,
maxImageHeight={this.props.maxImageHeight} highlightLink: this.props.highlightLink,
replacingEventId={this.props.replacingEventId} showUrlPreview: this.props.showUrlPreview,
editState={this.props.editState} forExport: this.props.forExport,
onHeightChanged={this.props.onHeightChanged} maxImageHeight: this.props.maxImageHeight,
onMessageAllowed={this.onTileUpdate} replacingEventId: this.props.replacingEventId,
permalinkCreator={this.props.permalinkCreator} editState: this.props.editState,
mediaEventHelper={this.mediaHelper} onHeightChanged: this.props.onHeightChanged,
getRelationsForEvent={this.props.getRelationsForEvent} onMessageAllowed: this.onTileUpdate,
isSeeingThroughMessageHiddenForModeration={this.props.isSeeingThroughMessageHiddenForModeration} permalinkCreator: this.props.permalinkCreator,
inhibitInteraction={this.props.inhibitInteraction} mediaEventHelper: this.mediaHelper,
/> getRelationsForEvent: this.props.getRelationsForEvent,
) : null; isSeeingThroughMessageHiddenForModeration: this.props.isSeeingThroughMessageHiddenForModeration,
inhibitInteraction: this.props.inhibitInteraction,
};
if (hasCaption) {
return <CaptionBody {...bodyProps} WrappedBodyType={BodyType} />;
}
return BodyType ? <BodyType {...bodyProps} /> : null;
} }
} }
const CaptionBody: React.FunctionComponent<IBodyProps & { WrappedBodyType: React.ComponentType<IBodyProps> }> = ({
WrappedBodyType,
...props
}) => (
<div className="mx_EventTile_content">
<WrappedBodyType {...props} />
<TextualBody {...{ ...props, ref: undefined }} />
</div>
);

View file

@ -556,6 +556,9 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
const content = mxEvent.getContent(); const content = mxEvent.getContent();
const isNotice = content.msgtype === MsgType.Notice; const isNotice = content.msgtype === MsgType.Notice;
const isEmote = content.msgtype === MsgType.Emote; const isEmote = content.msgtype === MsgType.Emote;
const isCaption = [MsgType.Image, MsgType.File, MsgType.Audio, MsgType.Video].includes(
content.msgtype as MsgType,
);
const willHaveWrapper = const willHaveWrapper =
this.props.replacingEventId || this.props.isSeeingThroughMessageHiddenForModeration || isEmote; this.props.replacingEventId || this.props.isSeeingThroughMessageHiddenForModeration || isEmote;
@ -635,6 +638,14 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
</div> </div>
); );
} }
if (isCaption) {
return (
<div className="mx_MTextBody mx_EventTile_caption" onClick={this.onBodyLinkClick}>
{body}
{widgets}
</div>
);
}
return ( return (
<div className="mx_MTextBody mx_EventTile_content" onClick={this.onBodyLinkClick}> <div className="mx_MTextBody mx_EventTile_content" onClick={this.onBodyLinkClick}>
{body} {body}

View file

@ -38,7 +38,9 @@ export function presentableTextForFile(
shortened = false, shortened = false,
): string { ): string {
let text = fallbackText; let text = fallbackText;
if (content.body?.length) { if (content.filename?.length) {
text = content.filename;
} else if (content.body?.length) {
// The content body should be the name of the file including a // The content body should be the name of the file including a
// file extension. // file extension.
text = content.body; text = content.body;

View file

@ -8,7 +8,10 @@ Please see LICENSE files in the repository root for full details.
import React from "react"; import React from "react";
import { render, RenderResult } from "@testing-library/react"; import { render, RenderResult } from "@testing-library/react";
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; import { MatrixClient, MatrixEvent, EventType, Room, MsgType } from "matrix-js-sdk/src/matrix";
import fetchMock from "fetch-mock-jest";
import fs from "fs";
import path from "path";
import SettingsStore from "../../../../src/settings/SettingsStore"; import SettingsStore from "../../../../src/settings/SettingsStore";
import { VoiceBroadcastInfoEventType, VoiceBroadcastInfoState } from "../../../../src/voice-broadcast"; import { VoiceBroadcastInfoEventType, VoiceBroadcastInfoState } from "../../../../src/voice-broadcast";
@ -25,6 +28,26 @@ jest.mock("../../../../src/voice-broadcast/components/VoiceBroadcastBody", () =>
VoiceBroadcastBody: () => <div data-testid="voice-broadcast-body" />, VoiceBroadcastBody: () => <div data-testid="voice-broadcast-body" />,
})); }));
jest.mock("../../../../src/components/views/messages/MImageBody", () => ({
__esModule: true,
default: () => <div data-testid="image-body" />,
}));
jest.mock("../../../../src/components/views/messages/MImageReplyBody", () => ({
__esModule: true,
default: () => <div data-testid="image-reply-body" />,
}));
jest.mock("../../../../src/components/views/messages/MStickerBody", () => ({
__esModule: true,
default: () => <div data-testid="sticker-body" />,
}));
jest.mock("../../../../src/components/views/messages/TextualBody.tsx", () => ({
__esModule: true,
default: () => <div data-testid="textual-body" />,
}));
describe("MessageEvent", () => { describe("MessageEvent", () => {
let room: Room; let room: Room;
let client: MatrixClient; let client: MatrixClient;
@ -68,4 +91,42 @@ describe("MessageEvent", () => {
result.getByTestId("voice-broadcast-body"); result.getByTestId("voice-broadcast-body");
}); });
}); });
describe("when an image with a caption is sent", () => {
let result: RenderResult;
beforeEach(() => {
event = mkEvent({
event: true,
type: EventType.RoomMessage,
user: client.getUserId()!,
room: room.roomId,
content: {
body: "caption for a test image",
format: "org.matrix.custom.html",
formatted_body: "<strong>caption for a test image</strong>",
msgtype: MsgType.Image,
filename: "image.webp",
info: {
w: 40,
h: 50,
},
url: "mxc://server/image",
},
});
result = renderMessageEvent();
});
it("should render a TextualBody and an ImageBody", () => {
fetchMock.getOnce(
"https://server/_matrix/media/v3/download/server/image",
{
body: fs.readFileSync(path.resolve(__dirname, "..", "..", "..", "images", "animated-logo.webp")),
},
{ sendAsJson: false },
);
result.getByTestId("image-body");
result.getByTestId("textual-body");
});
});
}); });