diff --git a/src/WidgetUtils.js b/src/WidgetUtils.js new file mode 100644 index 0000000000..34c998978d --- /dev/null +++ b/src/WidgetUtils.js @@ -0,0 +1,58 @@ +/* +Copyright 2017 Vector Creations Ltd + +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 MatrixClientPeg from './MatrixClientPeg'; + +export default class WidgetUtils { + + /* Returns true if user is able to send state events to modify widgets in this room + * @param roomId -- The ID of the room to check + * @return Boolean -- true if the user can modify widgets in this room + * @throws Error -- specifies the error reason + */ + static canUserModifyWidgets(roomId) { + if (!roomId) { + console.warn('No room ID specified'); + return false; + } + + const client = MatrixClientPeg.get(); + if (!client) { + console.warn('User must be be logged in'); + return false; + } + + const room = client.getRoom(roomId); + if (!room) { + console.warn(`Room ID ${roomId} is not recognised`); + return false; + } + + const me = client.credentials.userId; + if (!me) { + console.warn('Failed to get user ID'); + return false; + } + + const member = room.getMember(me); + if (!member || member.membership !== "join") { + console.warn(`User ${me} is not in room ${roomId}`); + return false; + } + + return room.currentState.maySendStateEvent('im.vector.modular.widgets', me); + } +} diff --git a/src/components/views/elements/AppPermission.js b/src/components/views/elements/AppPermission.js new file mode 100644 index 0000000000..dbdf74dbbc --- /dev/null +++ b/src/components/views/elements/AppPermission.js @@ -0,0 +1,74 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import url from 'url'; +import { _t } from '../../../languageHandler'; + +export default class AppPermission extends React.Component { + constructor(props) { + super(props); + + const curlBase = this.getCurlBase(); + this.state = { curlBase: curlBase}; + } + + // Return string representation of content URL without query parameters + getCurlBase() { + const wurl = url.parse(this.props.url); + let curl; + let curlString; + + const searchParams = new URLSearchParams(wurl.search); + + if(this.isScalarWurl(wurl) && searchParams && searchParams.get('url')) { + curl = url.parse(searchParams.get('url')); + if(curl) { + curl.search = curl.query = ""; + curlString = curl.format(); + } + } + if (!curl && wurl) { + wurl.search = wurl.query = ""; + curlString = wurl.format(); + } + return curlString; + } + + isScalarWurl(wurl) { + if(wurl && wurl.hostname && ( + wurl.hostname === 'scalar.vector.im' || + wurl.hostname === 'scalar-staging.riot.im' || + wurl.hostname === 'demo.riot.im' || + wurl.hostname === 'localhost' + )) { + return true; + } + return false; + } + + render() { + return ( +