/* Copyright 2017 Vector Creations Ltd Copyright 2018, 2019 New Vector Ltd Copyright 2019, 2020 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 from 'react'; import PropTypes from 'prop-types'; import url from 'url'; import * as sdk from '../../../index'; import { _t } from '../../../languageHandler'; import SdkConfig from '../../../SdkConfig'; import WidgetUtils from "../../../utils/WidgetUtils"; import {MatrixClientPeg} from "../../../MatrixClientPeg"; export default class AppPermission extends React.Component { static propTypes = { url: PropTypes.string.isRequired, creatorUserId: PropTypes.string.isRequired, roomId: PropTypes.string.isRequired, onPermissionGranted: PropTypes.func.isRequired, isRoomEncrypted: PropTypes.bool, }; static defaultProps = { onPermissionGranted: () => {}, }; constructor(props) { super(props); // The first step is to pick apart the widget so we can render information about it const urlInfo = this.parseWidgetUrl(); // The second step is to find the user's profile so we can show it on the prompt const room = MatrixClientPeg.get().getRoom(this.props.roomId); let roomMember; if (room) roomMember = room.getMember(this.props.creatorUserId); // Set all this into the initial state this.state = { ...urlInfo, roomMember, }; } parseWidgetUrl() { const widgetUrl = url.parse(this.props.url); const params = new URLSearchParams(widgetUrl.search); // HACK: We're relying on the query params when we should be relying on the widget's `data`. // This is a workaround for Scalar. if (WidgetUtils.isScalarUrl(widgetUrl) && params && params.get('url')) { const unwrappedUrl = url.parse(params.get('url')); return { widgetDomain: unwrappedUrl.host || unwrappedUrl.hostname, isWrapped: true, }; } else { return { widgetDomain: widgetUrl.host || widgetUrl.hostname, isWrapped: false, }; } } render() { const brand = SdkConfig.get().brand; const AccessibleButton = sdk.getComponent("views.elements.AccessibleButton"); const MemberAvatar = sdk.getComponent("views.avatars.MemberAvatar"); const BaseAvatar = sdk.getComponent("views.avatars.BaseAvatar"); const TextWithTooltip = sdk.getComponent("views.elements.TextWithTooltip"); const displayName = this.state.roomMember ? this.state.roomMember.name : this.props.creatorUserId; const userId = displayName === this.props.creatorUserId ? null : this.props.creatorUserId; const avatar = this.state.roomMember ? : ; const warningTooltipText = (
{_t("Any of the following data may be shared:")}
  • {_t("Your display name")}
  • {_t("Your avatar URL")}
  • {_t("Your user ID")}
  • {_t("Your theme")}
  • {_t("%(brand)s URL", { brand })}
  • {_t("Room ID")}
  • {_t("Widget ID")}
); const warningTooltip = ( ); // Due to i18n limitations, we can't dedupe the code for variables in these two messages. const warning = this.state.isWrapped ? _t("Using this widget may share data with %(widgetDomain)s & your Integration Manager.", {widgetDomain: this.state.widgetDomain}, {helpIcon: () => warningTooltip}) : _t("Using this widget may share data with %(widgetDomain)s.", {widgetDomain: this.state.widgetDomain}, {helpIcon: () => warningTooltip}); const encryptionWarning = this.props.isRoomEncrypted ? _t("Widgets do not use message encryption.") : null; return (
{_t("Widget added by")}
{avatar}

{displayName}

{userId}
{warning}
{_t("This widget may use cookies.")} {encryptionWarning}
{_t("Continue")}
); } }