2018-04-11 22:58:04 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-02-24 03:43:44 +00:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2019-12-20 00:45:24 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2018-04-11 22:58:04 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-03-04 21:17:29 +00:00
|
|
|
import React from "react";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
|
2021-03-04 21:17:29 +00:00
|
|
|
import SyntaxHighlight from "../views/elements/SyntaxHighlight";
|
|
|
|
import { _t } from "../../languageHandler";
|
|
|
|
import MatrixClientContext from "../../contexts/MatrixClientContext";
|
2021-03-06 15:09:46 +00:00
|
|
|
import { canEditContent } from "../../utils/EventUtils";
|
2021-03-09 12:46:37 +00:00
|
|
|
import { MatrixClientPeg } from '../../MatrixClientPeg';
|
2021-09-12 07:33:33 +00:00
|
|
|
import { IDialogProps } from "../views/dialogs/IDialogProps";
|
|
|
|
import BaseDialog from "../views/dialogs/BaseDialog";
|
2022-03-23 20:17:57 +00:00
|
|
|
import { DevtoolsContext } from "../views/dialogs/devtools/BaseTool";
|
|
|
|
import { StateEventEditor } from "../views/dialogs/devtools/RoomState";
|
|
|
|
import { stringify, TimelineEventEditor } from "../views/dialogs/devtools/Event";
|
2022-04-11 18:37:52 +00:00
|
|
|
import CopyableText from "../views/elements/CopyableText";
|
2018-04-11 22:58:04 +00:00
|
|
|
|
2021-09-12 07:33:33 +00:00
|
|
|
interface IProps extends IDialogProps {
|
|
|
|
mxEvent: MatrixEvent; // the MatrixEvent associated with the context menu
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
isEditing: boolean;
|
|
|
|
}
|
2018-04-11 22:58:04 +00:00
|
|
|
|
2021-09-12 07:33:33 +00:00
|
|
|
export default class ViewSource extends React.Component<IProps, IState> {
|
|
|
|
constructor(props: IProps) {
|
2021-03-04 21:17:29 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2021-03-06 09:30:31 +00:00
|
|
|
isEditing: false,
|
2021-03-04 21:17:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-23 20:17:57 +00:00
|
|
|
private onBack = (): void => {
|
2021-03-06 14:47:29 +00:00
|
|
|
// TODO: refresh the "Event ID:" modal header
|
2021-03-06 09:30:31 +00:00
|
|
|
this.setState({ isEditing: false });
|
2022-03-23 20:17:57 +00:00
|
|
|
};
|
2021-03-04 21:17:29 +00:00
|
|
|
|
2021-09-12 07:33:33 +00:00
|
|
|
private onEdit(): void {
|
2021-03-06 09:30:31 +00:00
|
|
|
this.setState({ isEditing: true });
|
2021-03-04 21:17:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 09:30:31 +00:00
|
|
|
// returns the dialog body for viewing the event source
|
2021-09-12 07:33:33 +00:00
|
|
|
private viewSourceContent(): JSX.Element {
|
2021-03-06 09:30:31 +00:00
|
|
|
const mxEvent = this.props.mxEvent.replacingEvent() || this.props.mxEvent; // show the replacing event, not the original, if it is an edit
|
2021-03-08 20:15:34 +00:00
|
|
|
const isEncrypted = mxEvent.isEncrypted();
|
2021-09-12 07:33:33 +00:00
|
|
|
// @ts-ignore
|
2021-06-19 14:37:48 +00:00
|
|
|
const decryptedEventSource = mxEvent.clearEvent; // FIXME: clearEvent is private
|
2021-03-06 09:30:31 +00:00
|
|
|
const originalEventSource = mxEvent.event;
|
2022-04-11 18:37:52 +00:00
|
|
|
const copyOriginalFunc = (): string => {
|
|
|
|
return stringify(originalEventSource);
|
|
|
|
};
|
2021-03-06 09:30:31 +00:00
|
|
|
if (isEncrypted) {
|
2022-04-11 18:37:52 +00:00
|
|
|
const copyDecryptedFunc = (): string => {
|
|
|
|
return stringify(decryptedEventSource);
|
|
|
|
};
|
2021-03-06 09:30:31 +00:00
|
|
|
return (
|
2021-03-04 21:17:29 +00:00
|
|
|
<>
|
|
|
|
<details open className="mx_ViewSource_details">
|
|
|
|
<summary>
|
2022-04-11 18:37:52 +00:00
|
|
|
<span className="mx_ViewSource_heading">
|
|
|
|
{ _t("Decrypted event source") }
|
|
|
|
</span>
|
2021-03-04 21:17:29 +00:00
|
|
|
</summary>
|
2022-06-25 12:50:58 +00:00
|
|
|
<CopyableText getTextToCopy={copyDecryptedFunc}>
|
|
|
|
<SyntaxHighlight language="json">
|
|
|
|
{ stringify(decryptedEventSource) }
|
|
|
|
</SyntaxHighlight>
|
|
|
|
</CopyableText>
|
2021-03-04 21:17:29 +00:00
|
|
|
</details>
|
|
|
|
<details className="mx_ViewSource_details">
|
|
|
|
<summary>
|
2022-04-11 18:37:52 +00:00
|
|
|
<span className="mx_ViewSource_heading">
|
|
|
|
{ _t("Original event source") }
|
|
|
|
</span>
|
2021-03-04 21:17:29 +00:00
|
|
|
</summary>
|
2022-06-25 12:50:58 +00:00
|
|
|
<CopyableText getTextToCopy={copyOriginalFunc}>
|
|
|
|
<SyntaxHighlight language="json">
|
|
|
|
{ stringify(originalEventSource) }
|
|
|
|
</SyntaxHighlight>
|
|
|
|
</CopyableText>
|
2021-03-04 21:17:29 +00:00
|
|
|
</details>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
} else {
|
2021-03-06 09:30:31 +00:00
|
|
|
return (
|
2021-03-04 21:17:29 +00:00
|
|
|
<>
|
2022-04-11 18:37:52 +00:00
|
|
|
<div className="mx_ViewSource_heading">
|
|
|
|
{ _t("Original event source") }
|
|
|
|
</div>
|
2022-06-25 12:50:58 +00:00
|
|
|
<CopyableText getTextToCopy={copyOriginalFunc}>
|
|
|
|
<SyntaxHighlight language="json">
|
|
|
|
{ stringify(originalEventSource) }
|
|
|
|
</SyntaxHighlight>
|
|
|
|
</CopyableText>
|
2021-03-04 21:17:29 +00:00
|
|
|
</>
|
|
|
|
);
|
2021-03-03 21:48:39 +00:00
|
|
|
}
|
2021-03-06 09:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the SendCustomEvent component prefilled with the correct details
|
2021-09-12 07:33:33 +00:00
|
|
|
private editSourceContent(): JSX.Element {
|
2021-03-06 09:30:31 +00:00
|
|
|
const mxEvent = this.props.mxEvent.replacingEvent() || this.props.mxEvent; // show the replacing event, not the original, if it is an edit
|
|
|
|
|
|
|
|
const isStateEvent = mxEvent.isState();
|
|
|
|
const roomId = mxEvent.getRoomId();
|
2021-03-06 14:47:29 +00:00
|
|
|
|
2021-03-06 09:30:31 +00:00
|
|
|
if (isStateEvent) {
|
|
|
|
return (
|
|
|
|
<MatrixClientContext.Consumer>
|
2021-07-19 21:43:11 +00:00
|
|
|
{ (cli) => (
|
2022-03-23 20:17:57 +00:00
|
|
|
<DevtoolsContext.Provider value={{ room: cli.getRoom(roomId) }}>
|
|
|
|
<StateEventEditor onBack={this.onBack} mxEvent={mxEvent} />
|
|
|
|
</DevtoolsContext.Provider>
|
2021-07-19 21:43:11 +00:00
|
|
|
) }
|
2021-03-06 09:30:31 +00:00
|
|
|
</MatrixClientContext.Consumer>
|
|
|
|
);
|
|
|
|
}
|
2022-03-23 20:17:57 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<MatrixClientContext.Consumer>
|
|
|
|
{ (cli) => (
|
|
|
|
<DevtoolsContext.Provider value={{ room: cli.getRoom(roomId) }}>
|
|
|
|
<TimelineEventEditor onBack={this.onBack} mxEvent={mxEvent} />
|
|
|
|
</DevtoolsContext.Provider>
|
|
|
|
) }
|
|
|
|
</MatrixClientContext.Consumer>
|
|
|
|
);
|
2021-03-06 09:30:31 +00:00
|
|
|
}
|
2021-03-03 20:38:30 +00:00
|
|
|
|
2021-09-12 07:33:33 +00:00
|
|
|
private canSendStateEvent(mxEvent: MatrixEvent): boolean {
|
2021-03-09 12:46:37 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const room = cli.getRoom(mxEvent.getRoomId());
|
|
|
|
return room.currentState.mayClientSendStateEvent(mxEvent.getType(), cli);
|
|
|
|
}
|
|
|
|
|
2021-09-12 07:33:33 +00:00
|
|
|
public render(): JSX.Element {
|
2021-03-06 09:30:31 +00:00
|
|
|
const mxEvent = this.props.mxEvent.replacingEvent() || this.props.mxEvent; // show the replacing event, not the original, if it is an edit
|
2021-03-04 21:17:29 +00:00
|
|
|
|
2021-03-06 09:30:31 +00:00
|
|
|
const isEditing = this.state.isEditing;
|
|
|
|
const roomId = mxEvent.getRoomId();
|
|
|
|
const eventId = mxEvent.getId();
|
2021-03-09 12:46:37 +00:00
|
|
|
const canEdit = mxEvent.isState() ? this.canSendStateEvent(mxEvent) : canEditContent(this.props.mxEvent);
|
2018-04-11 22:58:04 +00:00
|
|
|
return (
|
2021-03-04 22:07:59 +00:00
|
|
|
<BaseDialog className="mx_ViewSource" onFinished={this.props.onFinished} title={_t("View Source")}>
|
2022-06-25 12:50:58 +00:00
|
|
|
<div className="mx_ViewSource_header">
|
|
|
|
<CopyableText getTextToCopy={() => roomId} border={false}>
|
|
|
|
{ _t("Room ID: %(roomId)s", { roomId }) }
|
|
|
|
</CopyableText>
|
|
|
|
<CopyableText getTextToCopy={() => eventId} border={false}>
|
|
|
|
{ _t("Event ID: %(eventId)s", { eventId }) }
|
|
|
|
</CopyableText>
|
2019-02-24 03:43:44 +00:00
|
|
|
</div>
|
2022-06-25 12:50:58 +00:00
|
|
|
{ isEditing ? this.editSourceContent() : this.viewSourceContent() }
|
2021-07-19 21:43:11 +00:00
|
|
|
{ !isEditing && canEdit && (
|
2021-03-04 21:17:29 +00:00
|
|
|
<div className="mx_Dialog_buttons">
|
2021-07-19 21:43:11 +00:00
|
|
|
<button onClick={() => this.onEdit()}>{ _t("Edit") }</button>
|
2021-03-04 21:17:29 +00:00
|
|
|
</div>
|
2021-07-19 21:43:11 +00:00
|
|
|
) }
|
2019-02-24 03:43:44 +00:00
|
|
|
</BaseDialog>
|
2018-04-11 22:58:04 +00:00
|
|
|
);
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
|
|
|
}
|