Get rid of mediaSrc and avatarSrc props

This commit is contained in:
Jaiwanth 2021-06-09 15:23:47 +05:30
parent dbb3614374
commit 573ababb8c
9 changed files with 35 additions and 43 deletions

View file

@ -34,10 +34,10 @@ interface IProps extends Omit<React.ComponentProps<typeof BaseAvatar>, "name" |
resizeMethod?: ResizeMethod; resizeMethod?: ResizeMethod;
// The onClick to give the avatar // The onClick to give the avatar
onClick?: React.MouseEventHandler; onClick?: React.MouseEventHandler;
// Whether the onClick of the avatar should be overriden to dispatch `Action.ViewUser` // Whether the onClick of the avatar should be overridden to dispatch `Action.ViewUser`
viewUserOnClick?: boolean; viewUserOnClick?: boolean;
title?: string; title?: string;
avatarSrc?: string; forExport?: boolean;
} }
interface IState { interface IState {
@ -68,7 +68,7 @@ export default class MemberAvatar extends React.Component<IProps, IState> {
private static getState(props: IProps): IState { private static getState(props: IProps): IState {
if (props.member?.name) { if (props.member?.name) {
let imageUrl = null; let imageUrl = null;
if (props.avatarSrc) imageUrl = props.avatarSrc; if (props.forExport && props.member.getMxcAvatarUrl()) imageUrl = "AvatarForExport";
else if (props.member.getMxcAvatarUrl()) { else if (props.member.getMxcAvatarUrl()) {
imageUrl = mediaFromMxc(props.member.getMxcAvatarUrl()).getThumbnailOfSourceHttp( imageUrl = mediaFromMxc(props.member.getMxcAvatarUrl()).getThumbnailOfSourceHttp(
props.width, props.width,
@ -95,7 +95,7 @@ export default class MemberAvatar extends React.Component<IProps, IState> {
let {member, fallbackUserId, onClick, viewUserOnClick, ...otherProps} = this.props; let {member, fallbackUserId, onClick, viewUserOnClick, ...otherProps} = this.props;
const userId = member ? member.userId : fallbackUserId; const userId = member ? member.userId : fallbackUserId;
otherProps = omit(otherProps, "avatarSrc"); otherProps = omit(otherProps, "forExport");
if (viewUserOnClick) { if (viewUserOnClick) {
onClick = () => { onClick = () => {

View file

@ -42,7 +42,7 @@ export default class MAudioBody extends React.Component {
} }
_getContentUrl() { _getContentUrl() {
if (this.props.mediaSrc) return this.props.mediaSrc; if (this.props.forExport) return "forExport";
const media = mediaFromContent(this.props.mxEvent.getContent()); const media = mediaFromContent(this.props.mxEvent.getContent());
if (media.isEncrypted) { if (media.isEncrypted) {
return this.state.decryptedUrl; return this.state.decryptedUrl;
@ -52,7 +52,7 @@ export default class MAudioBody extends React.Component {
} }
getFileBody() { getFileBody() {
if (this.props.mediaSrc) return null; if (this.props.forExport) return null;
return <MFileBody {...this.props} decryptedBlob={this.state.decryptedBlob} showGenericPlaceholder={false} />; return <MFileBody {...this.props} decryptedBlob={this.state.decryptedBlob} showGenericPlaceholder={false} />;
} }
@ -95,7 +95,7 @@ export default class MAudioBody extends React.Component {
); );
} }
if (!this.props.mediaSrc && content.file !== undefined && this.state.decryptedUrl === null) { if (!this.props.forExport && content.file !== undefined && this.state.decryptedUrl === null) {
// Need to decrypt the attachment // Need to decrypt the attachment
// The attachment is decrypted in componentDidMount. // The attachment is decrypted in componentDidMount.
// For now add an img tag with a 16x16 spinner. // For now add an img tag with a 16x16 spinner.

View file

@ -102,8 +102,7 @@ export default class MFileBody extends React.Component {
tileShape: PropTypes.string, tileShape: PropTypes.string,
/* whether or not to show the default placeholder for the file. Defaults to true. */ /* whether or not to show the default placeholder for the file. Defaults to true. */
showGenericPlaceholder: PropTypes.bool, showGenericPlaceholder: PropTypes.bool,
/* to set source to local file path during export */
mediaSrc: PropTypes.string,
forExport: PropTypes.bool, forExport: PropTypes.bool,
}; };
@ -152,6 +151,7 @@ export default class MFileBody extends React.Component {
} }
_getContentUrl() { _getContentUrl() {
if (this.props.forExport) return null;
const media = mediaFromContent(this.props.mxEvent.getContent()); const media = mediaFromContent(this.props.mxEvent.getContent());
return media.srcHttp; return media.srcHttp;
} }
@ -185,9 +185,9 @@ export default class MFileBody extends React.Component {
); );
} }
if (this.props.mediaSrc) { if (this.props.forExport) {
return <span className="mx_MFileBody"> return <span className="mx_MFileBody">
<a href={this.props.mediaSrc}> <a href="forExport">
{ placeholder } { placeholder }
</a> </a>
</span>; </span>;

View file

@ -172,7 +172,7 @@ export default class MImageBody extends React.Component {
} }
_getContentUrl() { _getContentUrl() {
if (this.props.mediaSrc) return this.props.mediaSrc; if (this.props.forExport) return "forExport";
const media = mediaFromContent(this.props.mxEvent.getContent()); const media = mediaFromContent(this.props.mxEvent.getContent());
if (media.isEncrypted) { if (media.isEncrypted) {
return this.state.decryptedUrl; return this.state.decryptedUrl;
@ -370,9 +370,9 @@ export default class MImageBody extends React.Component {
let gifLabel = null; let gifLabel = null;
// e2e image hasn't been decrypted yet // e2e image hasn't been decrypted yet
if (!this.props.mediaSrc && content.file !== undefined && this.state.decryptedUrl === null) { if (!this.props.forExport && content.file !== undefined && this.state.decryptedUrl === null) {
placeholder = <InlineSpinner w={32} h={32} />; placeholder = <InlineSpinner w={32} h={32} />;
} else if (!this.props.mediaSrc && !this.state.imgLoaded) { } else if (!this.props.forExport && !this.state.imgLoaded) {
// Deliberately, getSpinner is left unimplemented here, MStickerBody overides // Deliberately, getSpinner is left unimplemented here, MStickerBody overides
placeholder = this.getPlaceholder(); placeholder = this.getPlaceholder();
} }
@ -418,7 +418,7 @@ export default class MImageBody extends React.Component {
</div> </div>
} }
<div style={{display: this.props.mediaSrc ? "block" : !showPlaceholder ? undefined : 'none'}}> <div style={{display: showPlaceholder ? 'none' : undefined}}>
{ img } { img }
{ gifLabel } { gifLabel }
</div> </div>
@ -450,7 +450,7 @@ export default class MImageBody extends React.Component {
// Overidden by MStickerBody // Overidden by MStickerBody
getFileBody() { getFileBody() {
if (this.props.mediaSrc) return null; if (this.props.forExport) return null;
return <MFileBody {...this.props} decryptedBlob={this.state.decryptedBlob} showGenericPlaceholder={false} />; return <MFileBody {...this.props} decryptedBlob={this.state.decryptedBlob} showGenericPlaceholder={false} />;
} }
@ -468,7 +468,7 @@ export default class MImageBody extends React.Component {
const contentUrl = this._getContentUrl(); const contentUrl = this._getContentUrl();
let thumbUrl; let thumbUrl;
if ((this._isGif() && SettingsStore.getValue("autoplayGifsAndVideos")) || this.props.mediaSrc) { if (this.props.forExport || (this._isGif() && SettingsStore.getValue("autoplayGifsAndVideos"))) {
thumbUrl = contentUrl; thumbUrl = contentUrl;
} else { } else {
thumbUrl = this._getThumbUrl(); thumbUrl = this._getThumbUrl();

View file

@ -30,7 +30,7 @@ interface IProps {
/* called when the video has loaded */ /* called when the video has loaded */
onHeightChanged: () => void; onHeightChanged: () => void;
/* used to refer to the local file while exporting */ /* used to refer to the local file while exporting */
mediaSrc?: string; forExport?: boolean;
} }
interface IState { interface IState {
@ -78,7 +78,7 @@ export default class MVideoBody extends React.PureComponent<IProps, IState> {
} }
private getContentUrl(): string|null { private getContentUrl(): string|null {
if (this.props.mediaSrc) return this.props.mediaSrc; if (this.props.forExport) return "forExport";
const media = mediaFromContent(this.props.mxEvent.getContent()); const media = mediaFromContent(this.props.mxEvent.getContent());
if (media.isEncrypted) { if (media.isEncrypted) {
return this.state.decryptedUrl; return this.state.decryptedUrl;
@ -94,7 +94,7 @@ export default class MVideoBody extends React.PureComponent<IProps, IState> {
private getThumbUrl(): string|null { private getThumbUrl(): string|null {
// there's no need of thumbnail when the content is local // there's no need of thumbnail when the content is local
if (this.props.mediaSrc) return null; if (this.props.forExport) return null;
const content = this.props.mxEvent.getContent(); const content = this.props.mxEvent.getContent();
const media = mediaFromContent(content); const media = mediaFromContent(content);
@ -191,7 +191,7 @@ export default class MVideoBody extends React.PureComponent<IProps, IState> {
} }
private getFileBody = () => { private getFileBody = () => {
if (this.props.mediaSrc) return null; if (this.props.forExport) return null;
return <MFileBody {...this.props} decryptedBlob={this.state.decryptedBlob} showGenericPlaceholder={false} />; return <MFileBody {...this.props} decryptedBlob={this.state.decryptedBlob} showGenericPlaceholder={false} />;
} }
@ -209,7 +209,7 @@ export default class MVideoBody extends React.PureComponent<IProps, IState> {
} }
// Important: If we aren't autoplaying and we haven't decrypted it yet, show a video with a poster. // Important: If we aren't autoplaying and we haven't decrypted it yet, show a video with a poster.
if (!this.props.mediaSrc && content.file !== undefined && this.state.decryptedUrl === null && autoplay) { if (!this.props.forExport && content.file !== undefined && this.state.decryptedUrl === null && autoplay) {
// Need to decrypt the attachment // Need to decrypt the attachment
// The attachment is decrypted in componentDidMount. // The attachment is decrypted in componentDidMount.
// For now add an img tag with a spinner. // For now add an img tag with a spinner.

View file

@ -23,7 +23,7 @@ import MVoiceMessageBody from "./MVoiceMessageBody";
interface IProps { interface IProps {
mxEvent: MatrixEvent; mxEvent: MatrixEvent;
mediaSrc?: string; forExport?: boolean;
} }
@replaceableComponent("views.messages.MVoiceOrAudioBody") @replaceableComponent("views.messages.MVoiceOrAudioBody")
@ -31,7 +31,7 @@ export default class MVoiceOrAudioBody extends React.PureComponent<IProps> {
public render() { public render() {
const isVoiceMessage = !!this.props.mxEvent.getContent()['org.matrix.msc2516.voice']; const isVoiceMessage = !!this.props.mxEvent.getContent()['org.matrix.msc2516.voice'];
const voiceMessagesEnabled = SettingsStore.getValue("feature_voice_messages"); const voiceMessagesEnabled = SettingsStore.getValue("feature_voice_messages");
if (isVoiceMessage && voiceMessagesEnabled && !this.props.mediaSrc) { if (!this.props.forExport && isVoiceMessage && voiceMessagesEnabled) {
return <MVoiceMessageBody {...this.props} />; return <MVoiceMessageBody {...this.props} />;
} else { } else {
return <MAudioBody {...this.props} />; return <MAudioBody {...this.props} />;

View file

@ -44,9 +44,6 @@ export default class MessageEvent extends React.Component {
/* the shape of the tile, used */ /* the shape of the tile, used */
tileShape: PropTypes.string, tileShape: PropTypes.string,
/* to set source to local file path during export */
mediaSrc: PropTypes.string,
/* to set source to local file path during export */ /* to set source to local file path during export */
forExport: PropTypes.bool, forExport: PropTypes.bool,
@ -126,7 +123,6 @@ export default class MessageEvent extends React.Component {
highlightLink={this.props.highlightLink} highlightLink={this.props.highlightLink}
showUrlPreview={this.props.showUrlPreview} showUrlPreview={this.props.showUrlPreview}
tileShape={this.props.tileShape} tileShape={this.props.tileShape}
mediaSrc={this.props.mediaSrc}
forExport={this.props.forExport} forExport={this.props.forExport}
maxImageHeight={this.props.maxImageHeight} maxImageHeight={this.props.maxImageHeight}
replacingEventId={this.props.replacingEventId} replacingEventId={this.props.replacingEventId}

View file

@ -251,12 +251,6 @@ interface IProps {
forExport?: boolean; forExport?: boolean;
// Used while exporting to refer to the local source rather than the online one
mediaSrc?: string;
// Used while exporting to refer to the local avatar rather than the online one
avatarSrc?: string;
// show twelve hour timestamps // show twelve hour timestamps
isTwelveHour?: boolean; isTwelveHour?: boolean;
@ -963,7 +957,7 @@ export default class EventTile extends React.Component<IProps, IState> {
avatar = ( avatar = (
<div className="mx_EventTile_avatar"> <div className="mx_EventTile_avatar">
<MemberAvatar <MemberAvatar
avatarSrc={this.props.avatarSrc} forExport={this.props.forExport}
member={member} member={member}
width={avatarSize} width={avatarSize}
height={avatarSize} height={avatarSize}
@ -1190,7 +1184,6 @@ export default class EventTile extends React.Component<IProps, IState> {
forExport={this.props.forExport} forExport={this.props.forExport}
replacingEventId={this.props.replacingEventId} replacingEventId={this.props.replacingEventId}
editState={this.props.editState} editState={this.props.editState}
mediaSrc={this.props.mediaSrc}
highlights={this.props.highlights} highlights={this.props.highlights}
highlightLink={this.props.highlightLink} highlightLink={this.props.highlightLink}
showUrlPreview={this.props.showUrlPreview} showUrlPreview={this.props.showUrlPreview}

View file

@ -257,11 +257,10 @@ export default class HTMLExporter extends Exporter {
} }
protected async getEventTile(mxEv: MatrixEvent, continuation: boolean, mediaSrc?: string) { protected async getEventTile(mxEv: MatrixEvent, continuation: boolean, filePath?: string) {
const hasAvatar = this.hasAvatar(mxEv); const hasAvatar = this.hasAvatar(mxEv);
if (hasAvatar) await this.saveAvatarIfNeeded(mxEv); if (hasAvatar) await this.saveAvatarIfNeeded(mxEv);
const eventTile = <li className="mx_Export_EventWrapper" id={mxEv.getId()}>
return <li className="mx_Export_EventWrapper" id={mxEv.getId()}>
<MatrixClientContext.Provider value = {this.matrixClient}> <MatrixClientContext.Provider value = {this.matrixClient}>
<EventTile <EventTile
mxEvent={mxEv} mxEvent={mxEv}
@ -275,8 +274,6 @@ export default class HTMLExporter extends Exporter {
checkUnmounting={() => false} checkUnmounting={() => false}
isTwelveHour={false} isTwelveHour={false}
last={false} last={false}
mediaSrc={mediaSrc}
avatarSrc={hasAvatar ? `users/${mxEv.sender.userId}` : null}
lastInSection={false} lastInSection={false}
permalinkCreator={this.permalinkCreator} permalinkCreator={this.permalinkCreator}
lastSuccessful={false} lastSuccessful={false}
@ -289,10 +286,16 @@ export default class HTMLExporter extends Exporter {
/> />
</MatrixClientContext.Provider> </MatrixClientContext.Provider>
</li> </li>
let eventTileMarkup = renderToStaticMarkup(eventTile);
if (filePath) eventTileMarkup = eventTileMarkup.replace(/(src=|href=)"forExport"/, `$1"${filePath}"`);
if (hasAvatar) {
eventTileMarkup = eventTileMarkup.replace(/src="AvatarForExport"/, `src="users/${mxEv.sender.userId}"`);
}
return eventTileMarkup;
} }
protected async createMessageBody(mxEv: MatrixEvent, joined = false) { protected async createMessageBody(mxEv: MatrixEvent, joined = false) {
let eventTile: JSX.Element; let eventTile: string;
const attachmentTypes = ["m.sticker", "m.image", "m.file", "m.video", "m.audio"] const attachmentTypes = ["m.sticker", "m.image", "m.file", "m.video", "m.audio"]
if (mxEv.getType() === attachmentTypes[0] || attachmentTypes.includes(mxEv.getContent().msgtype)) { if (mxEv.getType() === attachmentTypes[0] || attachmentTypes.includes(mxEv.getContent().msgtype)) {
@ -302,7 +305,7 @@ export default class HTMLExporter extends Exporter {
this.zip.file(filePath, blob); this.zip.file(filePath, blob);
} else eventTile = await this.getEventTile(mxEv, joined); } else eventTile = await this.getEventTile(mxEv, joined);
return renderToStaticMarkup(eventTile); return eventTile;
} }
protected async createHTML(events: MatrixEvent[]) { protected async createHTML(events: MatrixEvent[]) {