Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 431ae32

Browse files
authored
Reuse media content/info types from the js-sdk (#12308)
1 parent 89eb884 commit 431ae32

19 files changed

+74
-258
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module.exports = {
7676
group: [
7777
"matrix-js-sdk/src/**",
7878
"!matrix-js-sdk/src/matrix",
79+
"!matrix-js-sdk/src/types",
7980
"matrix-js-sdk/lib",
8081
"matrix-js-sdk/lib/",
8182
"matrix-js-sdk/lib/**",

src/@types/matrix-js-sdk.d.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2024 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { BLURHASH_FIELD } from "../utils/image-media";
18+
19+
// Matrix JS SDK extensions
20+
declare module "matrix-js-sdk" {
21+
export interface FileInfo {
22+
/**
23+
* @see https://github.com/matrix-org/matrix-spec-proposals/pull/2448
24+
*/
25+
[BLURHASH_FIELD]?: string;
26+
}
27+
}

src/ContentMessages.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ import {
2828
UploadProgress,
2929
THREAD_RELATION_TYPE,
3030
} from "matrix-js-sdk/src/matrix";
31+
import {
32+
ImageInfo,
33+
AudioInfo,
34+
VideoInfo,
35+
EncryptedFile,
36+
MediaEventContent,
37+
MediaEventInfo,
38+
} from "matrix-js-sdk/src/types";
3139
import encrypt from "matrix-encrypt-attachment";
3240
import extractPngChunks from "png-chunks-extract";
3341
import { logger } from "matrix-js-sdk/src/logger";
3442
import { removeElement } from "matrix-js-sdk/src/utils";
3543

36-
import {
37-
AudioInfo,
38-
EncryptedFile,
39-
ImageInfo,
40-
IMediaEventContent,
41-
IMediaEventInfo,
42-
VideoInfo,
43-
} from "./customisations/models/IMediaEventContent";
4444
import dis from "./dispatcher/dispatcher";
4545
import { _t } from "./languageHandler";
4646
import Modal from "./Modal";
@@ -537,7 +537,7 @@ export default class ContentMessages {
537537
promBefore?: Promise<any>,
538538
): Promise<void> {
539539
const fileName = file.name || _t("common|attachment");
540-
const content: Omit<IMediaEventContent, "info"> & { info: Partial<IMediaEventInfo> } = {
540+
const content: Omit<MediaEventContent, "info"> & { info: Partial<MediaEventInfo> } = {
541541
body: fileName,
542542
info: {
543543
size: file.size,

src/components/views/messages/MAudioBody.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ limitations under the License.
1717
import React from "react";
1818
import { logger } from "matrix-js-sdk/src/logger";
1919
import { IContent } from "matrix-js-sdk/src/matrix";
20+
import { MediaEventContent } from "matrix-js-sdk/src/types";
2021

2122
import { Playback } from "../../../audio/Playback";
2223
import InlineSpinner from "../elements/InlineSpinner";
2324
import { _t } from "../../../languageHandler";
2425
import AudioPlayer from "../audio_messages/AudioPlayer";
25-
import { IMediaEventContent } from "../../../customisations/models/IMediaEventContent";
2626
import MFileBody from "./MFileBody";
2727
import { IBodyProps } from "./IBodyProps";
2828
import { PlaybackManager } from "../../../audio/PlaybackManager";
@@ -67,7 +67,7 @@ export default class MAudioBody extends React.PureComponent<IBodyProps, IState>
6767
// We should have a buffer to work with now: let's set it up
6868

6969
// Note: we don't actually need a waveform to render an audio event, but voice messages do.
70-
const content = this.props.mxEvent.getContent<IMediaEventContent & IContent>();
70+
const content = this.props.mxEvent.getContent<MediaEventContent & IContent>();
7171
const waveform = content?.["org.matrix.msc1767.audio"]?.waveform?.map((p: number) => p / 1024);
7272

7373
// We should have a buffer to work with now: let's set it up

src/components/views/messages/MFileBody.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ limitations under the License.
1616

1717
import React, { AllHTMLAttributes, createRef } from "react";
1818
import { logger } from "matrix-js-sdk/src/logger";
19+
import { MediaEventContent } from "matrix-js-sdk/src/types";
1920

2021
import { _t } from "../../../languageHandler";
2122
import Modal from "../../../Modal";
2223
import AccessibleButton from "../elements/AccessibleButton";
2324
import { mediaFromContent } from "../../../customisations/Media";
2425
import ErrorDialog from "../dialogs/ErrorDialog";
2526
import { fileSize, presentableTextForFile } from "../../../utils/FileUtils";
26-
import { IMediaEventContent } from "../../../customisations/models/IMediaEventContent";
2727
import { IBodyProps } from "./IBodyProps";
2828
import { FileDownloader } from "../../../utils/FileDownloader";
2929
import TextWithTooltip from "../elements/TextWithTooltip";
@@ -128,8 +128,8 @@ export default class MFileBody extends React.Component<IProps, IState> {
128128
const media = mediaFromContent(this.props.mxEvent.getContent());
129129
return media.srcHttp;
130130
}
131-
private get content(): IMediaEventContent {
132-
return this.props.mxEvent.getContent<IMediaEventContent>();
131+
private get content(): MediaEventContent {
132+
return this.props.mxEvent.getContent<MediaEventContent>();
133133
}
134134

135135
private get fileName(): string {

src/components/views/messages/MImageBody.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import classNames from "classnames";
2121
import { CSSTransition, SwitchTransition } from "react-transition-group";
2222
import { logger } from "matrix-js-sdk/src/logger";
2323
import { ClientEvent, ClientEventHandlerMap } from "matrix-js-sdk/src/matrix";
24+
import { ImageContent } from "matrix-js-sdk/src/types";
2425
import { Tooltip } from "@vector-im/compound-web";
2526

2627
import MFileBody from "./MFileBody";
@@ -30,7 +31,6 @@ import SettingsStore from "../../../settings/SettingsStore";
3031
import Spinner from "../elements/Spinner";
3132
import { Media, mediaFromContent } from "../../../customisations/Media";
3233
import { BLURHASH_FIELD, createThumbnail } from "../../../utils/image-media";
33-
import { ImageContent } from "../../../customisations/models/IMediaEventContent";
3434
import ImageView from "../elements/ImageView";
3535
import { IBodyProps } from "./IBodyProps";
3636
import { ImageSize, suggestedSize as suggestedImageSize } from "../../../settings/enums/ImageSize";

src/components/views/messages/MImageReplyBody.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ limitations under the License.
1515
*/
1616

1717
import React from "react";
18+
import { ImageContent } from "matrix-js-sdk/src/types";
1819

1920
import MImageBody from "./MImageBody";
20-
import { ImageContent } from "../../../customisations/models/IMediaEventContent";
2121

2222
const FORCED_IMAGE_HEIGHT = 44;
2323

src/components/views/messages/MStickerBody.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ limitations under the License.
1616

1717
import React, { ComponentProps, ReactNode } from "react";
1818
import { Tooltip } from "@vector-im/compound-web";
19+
import { MediaEventContent } from "matrix-js-sdk/src/types";
1920

2021
import MImageBody from "./MImageBody";
2122
import { BLURHASH_FIELD } from "../../../utils/image-media";
22-
import { IMediaEventContent } from "../../../customisations/models/IMediaEventContent";
2323

2424
export default class MStickerBody extends MImageBody {
2525
// Mostly empty to prevent default behaviour of MImageBody
@@ -80,7 +80,7 @@ export default class MStickerBody extends MImageBody {
8080
return null;
8181
}
8282

83-
protected getBanner(content: IMediaEventContent): ReactNode {
83+
protected getBanner(content: MediaEventContent): ReactNode {
8484
return null; // we don't need a banner, we have a tooltip
8585
}
8686
}

src/components/views/messages/MVideoBody.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ limitations under the License.
1616

1717
import React, { ReactNode } from "react";
1818
import { decode } from "blurhash";
19+
import { MediaEventContent } from "matrix-js-sdk/src/types";
1920
import { logger } from "matrix-js-sdk/src/logger";
2021

2122
import { _t } from "../../../languageHandler";
2223
import SettingsStore from "../../../settings/SettingsStore";
2324
import InlineSpinner from "../elements/InlineSpinner";
2425
import { mediaFromContent } from "../../../customisations/Media";
2526
import { BLURHASH_FIELD } from "../../../utils/image-media";
26-
import { IMediaEventContent } from "../../../customisations/models/IMediaEventContent";
2727
import { IBodyProps } from "./IBodyProps";
2828
import MFileBody from "./MFileBody";
2929
import { ImageSize, suggestedSize as suggestedVideoSize } from "../../../settings/enums/ImageSize";
@@ -62,7 +62,7 @@ export default class MVideoBody extends React.PureComponent<IBodyProps, IState>
6262
}
6363

6464
private getContentUrl(): string | undefined {
65-
const content = this.props.mxEvent.getContent<IMediaEventContent>();
65+
const content = this.props.mxEvent.getContent<MediaEventContent>();
6666
// During export, the content url will point to the MSC, which will later point to a local url
6767
if (this.props.forExport) return content.file?.url ?? content.url;
6868
const media = mediaFromContent(content);
@@ -82,7 +82,7 @@ export default class MVideoBody extends React.PureComponent<IBodyProps, IState>
8282
// there's no need of thumbnail when the content is local
8383
if (this.props.forExport) return null;
8484

85-
const content = this.props.mxEvent.getContent<IMediaEventContent>();
85+
const content = this.props.mxEvent.getContent<MediaEventContent>();
8686
const media = mediaFromContent(content);
8787

8888
if (media.isEncrypted && this.state.decryptedThumbnailUrl) {
@@ -121,7 +121,7 @@ export default class MVideoBody extends React.PureComponent<IBodyProps, IState>
121121
posterLoading: true,
122122
});
123123

124-
const content = this.props.mxEvent.getContent<IMediaEventContent>();
124+
const content = this.props.mxEvent.getContent<MediaEventContent>();
125125
const media = mediaFromContent(content);
126126
if (media.hasThumbnail) {
127127
const image = new Image();
@@ -157,7 +157,7 @@ export default class MVideoBody extends React.PureComponent<IBodyProps, IState>
157157
this.props.onHeightChanged?.();
158158
} else {
159159
logger.log("NOT preloading video");
160-
const content = this.props.mxEvent.getContent<IMediaEventContent>();
160+
const content = this.props.mxEvent.getContent<MediaEventContent>();
161161

162162
let mimetype = content?.info?.mimetype;
163163

src/customisations/Media.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616

1717
import { MatrixClient, ResizeMethod } from "matrix-js-sdk/src/matrix";
18+
import { MediaEventContent } from "matrix-js-sdk/src/types";
1819
import { Optional } from "matrix-events-sdk";
1920

2021
import { MatrixClientPeg } from "../MatrixClientPeg";
21-
import { IMediaEventContent, IPreparedMedia, prepEventContentAsMedia } from "./models/IMediaEventContent";
22+
import { IPreparedMedia, prepEventContentAsMedia } from "./models/IMediaEventContent";
2223
import { UserFriendlyError } from "../languageHandler";
2324

2425
// Populate this class with the details of your customisations when copying it.
@@ -154,11 +155,11 @@ export class Media {
154155

155156
/**
156157
* Creates a media object from event content.
157-
* @param {IMediaEventContent} content The event content.
158+
* @param {MediaEventContent} content The event content.
158159
* @param {MatrixClient} client? Optional client to use.
159160
* @returns {Media} The media object.
160161
*/
161-
export function mediaFromContent(content: Partial<IMediaEventContent>, client?: MatrixClient): Media {
162+
export function mediaFromContent(content: Partial<MediaEventContent>, client?: MatrixClient): Media {
162163
return new Media(prepEventContentAsMedia(content), client);
163164
}
164165

0 commit comments

Comments
 (0)