/* Copyright 2017 Michael Telatynski <7t3chguy@gmail.com> Copyright 2018-2021 The Matrix.org Foundation C.I.C. 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. */ import React, { useState, useEffect, ChangeEvent, MouseEvent } from 'react'; import SyntaxHighlight from '../elements/SyntaxHighlight'; import { _t } from '../../../languageHandler'; import Field from "../elements/Field"; import MatrixClientContext from "../../../contexts/MatrixClientContext"; import { useEventEmitter } from "../../../hooks/useEventEmitter"; import { PHASE_UNSENT, PHASE_REQUESTED, PHASE_READY, PHASE_DONE, PHASE_STARTED, PHASE_CANCELLED, VerificationRequest, } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest"; import WidgetStore, { IApp } from "../../../stores/WidgetStore"; import { UPDATE_EVENT } from "../../../stores/AsyncStore"; import { SETTINGS } from "../../../settings/Settings"; import SettingsStore, { LEVEL_ORDER } from "../../../settings/SettingsStore"; import Modal from "../../../Modal"; import ErrorDialog from "./ErrorDialog"; import { replaceableComponent } from "../../../utils/replaceableComponent"; import { Room } from "matrix-js-sdk/src/models/room"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { SettingLevel } from '../../../settings/SettingLevel'; import BaseDialog from "./BaseDialog"; import TruncatedList from "../elements/TruncatedList"; interface IGenericEditorProps { onBack: () => void; } interface IGenericEditorState { message?: string; [inputId: string]: boolean | string; } abstract class GenericEditor< P extends IGenericEditorProps = IGenericEditorProps, S extends IGenericEditorState = IGenericEditorState, > extends React.PureComponent { protected onBack = () => { if (this.state.message) { this.setState({ message: null }); } else { this.props.onBack(); } }; protected onChange = (e: ChangeEvent) => { // @ts-ignore: Unsure how to convince TS this is okay when the state // type can be extended. this.setState({ [e.target.id]: e.target.type === 'checkbox' ? e.target.checked : e.target.value }); }; protected abstract send(); protected buttons(): React.ReactNode { return
{ !this.state.message && }
; } protected textInput(id: string, label: string): React.ReactNode { return ; } } interface ISendCustomEventProps extends IGenericEditorProps { room: Room; forceStateEvent?: boolean; forceGeneralEvent?: boolean; inputs?: { eventType?: string; stateKey?: string; evContent?: string; }; } interface ISendCustomEventState extends IGenericEditorState { isStateEvent: boolean; eventType: string; stateKey: string; evContent: string; } export class SendCustomEvent extends GenericEditor { static getLabel() { return _t('Send Custom Event'); } static contextType = MatrixClientContext; constructor(props) { super(props); const { eventType, stateKey, evContent } = Object.assign({ eventType: '', stateKey: '', evContent: '{\n\n}', }, this.props.inputs); this.state = { isStateEvent: Boolean(this.props.forceStateEvent), eventType, stateKey, evContent, }; } private doSend(content: object): Promise { const cli = this.context; if (this.state.isStateEvent) { return cli.sendStateEvent(this.props.room.roomId, this.state.eventType, content, this.state.stateKey); } else { return cli.sendEvent(this.props.room.roomId, this.state.eventType, content); } } protected send = async () => { if (this.state.eventType === '') { this.setState({ message: _t('You must specify an event type!') }); return; } let message; try { const content = JSON.parse(this.state.evContent); await this.doSend(content); message = _t('Event sent!'); } catch (e) { message = _t('Failed to send custom event.') + ' (' + e.toString() + ')'; } this.setState({ message }); }; render() { if (this.state.message) { return
{ this.state.message }
{ this.buttons() }
; } const showTglFlip = !this.state.message && !this.props.forceStateEvent && !this.props.forceGeneralEvent; return
{ this.textInput('eventType', _t('Event Type')) } { this.state.isStateEvent && this.textInput('stateKey', _t('State Key')) }

{ !this.state.message && } { showTglFlip &&
}
; } } interface ISendAccountDataProps extends IGenericEditorProps { room: Room; isRoomAccountData: boolean; forceMode: boolean; inputs?: { eventType?: string; evContent?: string; }; } interface ISendAccountDataState extends IGenericEditorState { isRoomAccountData: boolean; eventType: string; evContent: string; } class SendAccountData extends GenericEditor { static getLabel() { return _t('Send Account Data'); } static contextType = MatrixClientContext; constructor(props) { super(props); const { eventType, evContent } = Object.assign({ eventType: '', evContent: '{\n\n}', }, this.props.inputs); this.state = { isRoomAccountData: Boolean(this.props.isRoomAccountData), eventType, evContent, }; } private doSend(content: object): Promise { const cli = this.context; if (this.state.isRoomAccountData) { return cli.setRoomAccountData(this.props.room.roomId, this.state.eventType, content); } return cli.setAccountData(this.state.eventType, content); } protected send = async () => { if (this.state.eventType === '') { this.setState({ message: _t('You must specify an event type!') }); return; } let message; try { const content = JSON.parse(this.state.evContent); await this.doSend(content); message = _t('Event sent!'); } catch (e) { message = _t('Failed to send custom event.') + ' (' + e.toString() + ')'; } this.setState({ message }); }; render() { if (this.state.message) { return
{ this.state.message }
{ this.buttons() }
; } return
{ this.textInput('eventType', _t('Event Type')) }
{ !this.state.message && } { !this.state.message &&
}
; } } const INITIAL_LOAD_TILES = 20; const LOAD_TILES_STEP_SIZE = 50; interface IFilteredListProps { children: React.ReactElement[]; query: string; onChange: (value: string) => void; } interface IFilteredListState { filteredChildren: React.ReactElement[]; truncateAt: number; } class FilteredList extends React.PureComponent { static filterChildren(children: React.ReactElement[], query: string): React.ReactElement[] { if (!query) return children; const lcQuery = query.toLowerCase(); return children.filter((child) => child.key.toString().toLowerCase().includes(lcQuery)); } constructor(props) { super(props); this.state = { filteredChildren: FilteredList.filterChildren(this.props.children, this.props.query), truncateAt: INITIAL_LOAD_TILES, }; } // TODO: [REACT-WARNING] Replace with appropriate lifecycle event UNSAFE_componentWillReceiveProps(nextProps) { // eslint-disable-line camelcase if (this.props.children === nextProps.children && this.props.query === nextProps.query) return; this.setState({ filteredChildren: FilteredList.filterChildren(nextProps.children, nextProps.query), truncateAt: INITIAL_LOAD_TILES, }); } private showAll = () => { this.setState({ truncateAt: this.state.truncateAt + LOAD_TILES_STEP_SIZE, }); }; private createOverflowElement = (overflowCount: number, totalCount: number) => { return ; }; private onQuery = (ev: ChangeEvent) => { if (this.props.onChange) this.props.onChange(ev.target.value); }; private getChildren = (start: number, end: number): React.ReactElement[] => { return this.state.filteredChildren.slice(start, end); }; private getChildCount = (): number => { return this.state.filteredChildren.length; }; render() { return
; } } interface IExplorerProps { room: Room; onBack: () => void; } interface IRoomStateExplorerState { eventType?: string; event?: MatrixEvent; editing: boolean; queryEventType: string; queryStateKey: string; } class RoomStateExplorer extends React.PureComponent { static getLabel() { return _t('Explore Room State'); } static contextType = MatrixClientContext; private roomStateEvents: Map>; constructor(props) { super(props); this.roomStateEvents = this.props.room.currentState.events; this.state = { eventType: null, event: null, editing: false, queryEventType: '', queryStateKey: '', }; } private browseEventType(eventType: string) { return () => { this.setState({ eventType }); }; } private onViewSourceClick(event: MatrixEvent) { return () => { this.setState({ event }); }; } private onBack = () => { if (this.state.editing) { this.setState({ editing: false }); } else if (this.state.event) { this.setState({ event: null }); } else if (this.state.eventType) { this.setState({ eventType: null }); } else { this.props.onBack(); } }; private editEv = () => { this.setState({ editing: true }); }; private onQueryEventType = (filterEventType: string) => { this.setState({ queryEventType: filterEventType }); }; private onQueryStateKey = (filterStateKey: string) => { this.setState({ queryStateKey: filterStateKey }); }; render() { if (this.state.event) { if (this.state.editing) { return ; } return
{ JSON.stringify(this.state.event.event, null, 2) }
; } let list = null; const classes = 'mx_DevTools_RoomStateExplorer_button'; if (this.state.eventType === null) { list = { Array.from(this.roomStateEvents.entries()).map(([eventType, allStateKeys]) => { let onClickFn; if (allStateKeys.size === 1 && allStateKeys.has("")) { onClickFn = this.onViewSourceClick(allStateKeys.get("")); } else { onClickFn = this.browseEventType(eventType); } return ; }) } ; } else { const stateGroup = this.roomStateEvents.get(this.state.eventType); list = { Array.from(stateGroup.entries()).map(([stateKey, ev]) => { return ; }) } ; } return
{ list }
; } } interface IAccountDataExplorerState { [inputId: string]: boolean | string | any; isRoomAccountData: boolean; event?: MatrixEvent; editing: boolean; queryEventType: string; } class AccountDataExplorer extends React.PureComponent { static getLabel() { return _t('Explore Account Data'); } static contextType = MatrixClientContext; constructor(props) { super(props); this.state = { isRoomAccountData: false, event: null, editing: false, queryEventType: '', }; } private getData(): Record { if (this.state.isRoomAccountData) { return this.props.room.accountData; } return this.context.store.accountData; } private onViewSourceClick(event: MatrixEvent) { return () => { this.setState({ event }); }; } private onBack = () => { if (this.state.editing) { this.setState({ editing: false }); } else if (this.state.event) { this.setState({ event: null }); } else { this.props.onBack(); } }; private onChange = (e: ChangeEvent) => { this.setState({ [e.target.id]: e.target.type === 'checkbox' ? e.target.checked : e.target.value }); }; private editEv = () => { this.setState({ editing: true }); }; private onQueryEventType = (queryEventType: string) => { this.setState({ queryEventType }); }; render() { if (this.state.event) { if (this.state.editing) { return ; } return
{ JSON.stringify(this.state.event.event, null, 2) }
; } const rows = []; const classes = 'mx_DevTools_RoomStateExplorer_button'; const data = this.getData(); Object.keys(data).forEach((evType) => { const ev = data[evType]; rows.push(); }); return
{ rows }
; } } interface IServersInRoomListState { query: string; } class ServersInRoomList extends React.PureComponent { static getLabel() { return _t('View Servers in Room'); } static contextType = MatrixClientContext; private servers: React.ReactElement[]; constructor(props) { super(props); const room = this.props.room; const servers = new Set(); room.currentState.getStateEvents("m.room.member").forEach(ev => servers.add(ev.getSender().split(":")[1])); this.servers = Array.from(servers).map(s => ); this.state = { query: '', }; } private onQuery = (query: string) => { this.setState({ query }); }; render() { return
{ this.servers }
; } } const PHASE_MAP = { [PHASE_UNSENT]: "unsent", [PHASE_REQUESTED]: "requested", [PHASE_READY]: "ready", [PHASE_DONE]: "done", [PHASE_STARTED]: "started", [PHASE_CANCELLED]: "cancelled", }; const VerificationRequestExplorer: React.FC<{ txnId: string; request: VerificationRequest; }> = ({ txnId, request }) => { const [, updateState] = useState(); const [timeout, setRequestTimeout] = useState(request.timeout); /* Re-render if something changes state */ useEventEmitter(request, "change", updateState); /* Keep re-rendering if there's a timeout */ useEffect(() => { if (request.timeout == 0) return; /* Note that request.timeout is a getter, so its value changes */ const id = setInterval(() => { setRequestTimeout(request.timeout); }, 500); return () => { clearInterval(id); }; }, [request]); return (
Transaction
{txnId}
Phase
{PHASE_MAP[request.phase] || request.phase}
Timeout
{Math.floor(timeout / 1000)}
Methods
{request.methods && request.methods.join(", ")}
requestingUserId
{request.requestingUserId}
observeOnly
{JSON.stringify(request.observeOnly)}
); }; class VerificationExplorer extends React.PureComponent { static getLabel() { return _t("Verification Requests"); } /* Ensure this.context is the cli */ static contextType = MatrixClientContext; private onNewRequest = () => { this.forceUpdate(); }; componentDidMount() { const cli = this.context; cli.on("crypto.verification.request", this.onNewRequest); } componentWillUnmount() { const cli = this.context; cli.off("crypto.verification.request", this.onNewRequest); } render() { const cli = this.context; const room = this.props.room; const inRoomChannel = cli.crypto.inRoomVerificationRequests; const inRoomRequests = (inRoomChannel._requestsByRoomId || new Map()).get(room.roomId) || new Map(); return (
{Array.from(inRoomRequests.entries()).reverse().map(([txnId, request]) => , )}
); } } interface IWidgetExplorerState { query: string; editWidget?: IApp; } class WidgetExplorer extends React.Component { static getLabel() { return _t("Active Widgets"); } constructor(props) { super(props); this.state = { query: '', editWidget: null, // set to an IApp when editing }; } private onWidgetStoreUpdate = () => { this.forceUpdate(); }; private onQueryChange = (query: string) => { this.setState({ query }); }; private onEditWidget = (widget: IApp) => { this.setState({ editWidget: widget }); }; private onBack = () => { const widgets = WidgetStore.instance.getApps(this.props.room.roomId); if (this.state.editWidget && widgets.includes(this.state.editWidget)) { this.setState({ editWidget: null }); } else { this.props.onBack(); } }; componentDidMount() { WidgetStore.instance.on(UPDATE_EVENT, this.onWidgetStoreUpdate); } componentWillUnmount() { WidgetStore.instance.off(UPDATE_EVENT, this.onWidgetStoreUpdate); } render() { const room = this.props.room; const editWidget = this.state.editWidget; const widgets = WidgetStore.instance.getApps(room.roomId); if (editWidget && widgets.includes(editWidget)) { const allState = Array.from( Array.from(room.currentState.events.values()).map((e: Map) => { return e.values(); }), ).reduce((p, c) => { p.push(...c); return p; }, []); const stateEv = allState.find(ev => ev.getId() === editWidget.eventId); if (!stateEv) { // "should never happen" return
{_t("There was an error finding this widget.")}
; } return ; } return (
{widgets.map(w => { return ; })}
); } } interface ISettingsExplorerState { query: string; editSetting?: string; viewSetting?: string; explicitValues?: string; explicitRoomValues?: string; } class SettingsExplorer extends React.PureComponent { static getLabel() { return _t("Settings Explorer"); } constructor(props) { super(props); this.state = { query: '', editSetting: null, // set to a setting ID when editing viewSetting: null, // set to a setting ID when exploring in detail explicitValues: null, // stringified JSON for edit view explicitRoomValues: null, // stringified JSON for edit view }; } private onQueryChange = (ev: ChangeEvent) => { this.setState({ query: ev.target.value }); }; private onExplValuesEdit = (ev: ChangeEvent) => { this.setState({ explicitValues: ev.target.value }); }; private onExplRoomValuesEdit = (ev: ChangeEvent) => { this.setState({ explicitRoomValues: ev.target.value }); }; private onBack = () => { if (this.state.editSetting) { this.setState({ editSetting: null }); } else if (this.state.viewSetting) { this.setState({ viewSetting: null }); } else { this.props.onBack(); } }; private onViewClick = (ev: MouseEvent, settingId: string) => { ev.preventDefault(); this.setState({ viewSetting: settingId }); }; private onEditClick = (ev: MouseEvent, settingId: string) => { ev.preventDefault(); this.setState({ editSetting: settingId, explicitValues: this.renderExplicitSettingValues(settingId, null), explicitRoomValues: this.renderExplicitSettingValues(settingId, this.props.room.roomId), }); }; private onSaveClick = async () => { try { const settingId = this.state.editSetting; const parsedExplicit = JSON.parse(this.state.explicitValues); const parsedExplicitRoom = JSON.parse(this.state.explicitRoomValues); for (const level of Object.keys(parsedExplicit)) { console.log(`[Devtools] Setting value of ${settingId} at ${level} from user input`); try { const val = parsedExplicit[level]; await SettingsStore.setValue(settingId, null, level as SettingLevel, val); } catch (e) { console.warn(e); } } const roomId = this.props.room.roomId; for (const level of Object.keys(parsedExplicit)) { console.log(`[Devtools] Setting value of ${settingId} at ${level} in ${roomId} from user input`); try { const val = parsedExplicitRoom[level]; await SettingsStore.setValue(settingId, roomId, level as SettingLevel, val); } catch (e) { console.warn(e); } } this.setState({ viewSetting: settingId, editSetting: null, }); } catch (e) { Modal.createTrackedDialog('Devtools - Failed to save settings', '', ErrorDialog, { title: _t("Failed to save settings"), description: e.message, }); } }; private renderSettingValue(val: any): string { // Note: we don't .toString() a string because we want JSON.stringify to inject quotes for us const toStringTypes = ['boolean', 'number']; if (toStringTypes.includes(typeof(val))) { return val.toString(); } else { return JSON.stringify(val); } } private renderExplicitSettingValues(setting: string, roomId: string): string { const vals = {}; for (const level of LEVEL_ORDER) { try { vals[level] = SettingsStore.getValueAt(level, setting, roomId, true, true); if (vals[level] === undefined) { vals[level] = null; } } catch (e) { console.warn(e); } } return JSON.stringify(vals, null, 4); } private renderCanEditLevel(roomId: string, level: SettingLevel): React.ReactNode { const canEdit = SettingsStore.canSetValue(this.state.editSetting, roomId, level); const className = canEdit ? 'mx_DevTools_SettingsExplorer_mutable' : 'mx_DevTools_SettingsExplorer_immutable'; return {canEdit.toString()}; } render() { const room = this.props.room; if (!this.state.viewSetting && !this.state.editSetting) { // view all settings const allSettings = Object.keys(SETTINGS) .filter(n => this.state.query ? n.toLowerCase().includes(this.state.query.toLowerCase()) : true); return (
{allSettings.map(i => ( ))}
{_t("Setting ID")} {_t("Value")} {_t("Value in this room")}
this.onViewClick(e, i)}> {i} this.onEditClick(e, i)} className='mx_DevTools_SettingsExplorer_edit' > ✏ {this.renderSettingValue(SettingsStore.getValue(i))} {this.renderSettingValue(SettingsStore.getValue(i, room.roomId))}
); } else if (this.state.editSetting) { return (

{_t("Setting:")} {this.state.editSetting}

{_t("Caution:")} {_t( "This UI does NOT check the types of the values. Use at your own risk.", )}
{_t("Setting definition:")}
{JSON.stringify(SETTINGS[this.state.editSetting], null, 4)}
{LEVEL_ORDER.map(lvl => ( {this.renderCanEditLevel(null, lvl)} {this.renderCanEditLevel(room.roomId, lvl)} ))}
{_t("Level")} {_t("Settable at global")} {_t("Settable at room")}
{lvl}
); } else if (this.state.viewSetting) { return (

{_t("Setting:")} {this.state.viewSetting}

{_t("Setting definition:")}
{JSON.stringify(SETTINGS[this.state.viewSetting], null, 4)}
{_t("Value:")}  {this.renderSettingValue( SettingsStore.getValue(this.state.viewSetting), )}
{_t("Value in this room:")}  {this.renderSettingValue( SettingsStore.getValue(this.state.viewSetting, room.roomId), )}
{_t("Values at explicit levels:")}
{this.renderExplicitSettingValues(
                                this.state.viewSetting, null,
                            )}
{_t("Values at explicit levels in this room:")}
{this.renderExplicitSettingValues(
                                this.state.viewSetting, room.roomId,
                            )}
); } } } type DevtoolsDialogEntry = React.JSXElementConstructor & { getLabel: () => string; }; const Entries: DevtoolsDialogEntry[] = [ SendCustomEvent, RoomStateExplorer, SendAccountData, AccountDataExplorer, ServersInRoomList, VerificationExplorer, WidgetExplorer, SettingsExplorer, ]; interface IProps { roomId: string; onFinished: (finished: boolean) => void; } interface IState { mode?: DevtoolsDialogEntry; } @replaceableComponent("views.dialogs.DevtoolsDialog") export default class DevtoolsDialog extends React.PureComponent { constructor(props) { super(props); this.state = { mode: null, }; } private setMode(mode: DevtoolsDialogEntry) { return () => { this.setState({ mode }); }; } private onBack = () => { this.setState({ mode: null }); }; private onCancel = () => { this.props.onFinished(false); }; render() { let body; if (this.state.mode) { body = {(cli) =>
{ this.state.mode.getLabel() }
Room ID: { this.props.roomId }
} ; } else { const classes = "mx_DevTools_RoomStateExplorer_button"; body =
{ _t('Toolbox') }
Room ID: { this.props.roomId }
{ Entries.map((Entry) => { const label = Entry.getLabel(); const onClick = this.setMode(Entry); return ; }) }
; } return ( { body } ); } }