|
| 1 | +/* |
| 2 | +Copyright 2019 New Vector Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | +import PropTypes from 'prop-types'; |
| 19 | +import {_t} from "../../../../../languageHandler"; |
| 20 | +import MatrixClientPeg from "../../../../../MatrixClientPeg"; |
| 21 | +import Pill from "../../../elements/Pill"; |
| 22 | +import {makeUserPermalink} from "../../../../../utils/permalinks/Permalinks"; |
| 23 | + |
| 24 | +const BRIDGE_EVENT_TYPES = [ |
| 25 | + "uk.half-shot.bridge", |
| 26 | + // m.bridge |
| 27 | +]; |
| 28 | + |
| 29 | +export default class BridgeSettingsTab extends React.Component { |
| 30 | + static propTypes = { |
| 31 | + roomId: PropTypes.string.isRequired, |
| 32 | + }; |
| 33 | + |
| 34 | + constructor() { |
| 35 | + super(); |
| 36 | + |
| 37 | + this.state = { |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + componentWillMount() { |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + _renderBridgeCard(event, room) { |
| 46 | + const content = event.getContent(); |
| 47 | + if (!content || !content.channel || !content.protocol) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + const protocolName = content.protocol.displayname || content.protocol.id; |
| 51 | + const channelName = content.channel.displayname || content.channel.id; |
| 52 | + const networkName = content.network ? " on " + (content.network.displayname || content.network.id) : ""; |
| 53 | + let status = null; |
| 54 | + if (content.status === "active") { |
| 55 | + status = (<p> Status: <b>Active</b> </p>); |
| 56 | + } else if (content.status === "disabled") { |
| 57 | + status = (<p> Status: <b>Disabled</b> </p>); |
| 58 | + } |
| 59 | + |
| 60 | + let creator = null; |
| 61 | + if (content.creator) { |
| 62 | + creator = (<p> |
| 63 | + This bridge was provisioned by <Pill |
| 64 | + type={Pill.TYPE_USER_MENTION} |
| 65 | + room={room} |
| 66 | + url={makeUserPermalink(content.creator)} |
| 67 | + shouldShowPillAvatar={true} |
| 68 | + /> |
| 69 | + </p>); |
| 70 | + } |
| 71 | + |
| 72 | + const bot = (<p> |
| 73 | + The bridge is managed by the <Pill |
| 74 | + type={Pill.TYPE_USER_MENTION} |
| 75 | + room={room} |
| 76 | + url={makeUserPermalink(event.getSender())} |
| 77 | + shouldShowPillAvatar={true} |
| 78 | + /> bot user.</p> |
| 79 | + ); |
| 80 | + |
| 81 | + const chanAndNetworkInfo = ( |
| 82 | + <p> Bridged into {channelName}{networkName}, on {protocolName}</p> |
| 83 | + ); |
| 84 | + |
| 85 | + return (<li key={event.stateKey}> |
| 86 | + <div> |
| 87 | + <h3>{channelName}{networkName} ({protocolName})</h3> |
| 88 | + <details> |
| 89 | + {status} |
| 90 | + {creator} |
| 91 | + {bot} |
| 92 | + {chanAndNetworkInfo} |
| 93 | + </details> |
| 94 | + </div> |
| 95 | + </li>); |
| 96 | + } |
| 97 | + |
| 98 | + static getBridgeStateEvents(roomId) { |
| 99 | + const client = MatrixClientPeg.get(); |
| 100 | + const roomState = (client.getRoom(roomId)).currentState; |
| 101 | + |
| 102 | + const bridgeEvents = Array.concat(...BRIDGE_EVENT_TYPES.map((typeName) => |
| 103 | + Object.values(roomState.events[typeName] || {}), |
| 104 | + )); |
| 105 | + |
| 106 | + return bridgeEvents; |
| 107 | + } |
| 108 | + |
| 109 | + render() { |
| 110 | + // This settings tab will only be invoked if the following function returns more |
| 111 | + // than 0 events, so no validation is needed at this stage. |
| 112 | + const bridgeEvents = BridgeSettingsTab.getBridgeStateEvents(this.props.roomId); |
| 113 | + const client = MatrixClientPeg.get(); |
| 114 | + const room = client.getRoom(this.props.roomId); |
| 115 | + |
| 116 | + return ( |
| 117 | + <div className="mx_SettingsTab"> |
| 118 | + <div className="mx_SettingsTab_heading">{_t("Bridge Info")}</div> |
| 119 | + <div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'> |
| 120 | + <p> Below is a list of bridges connected to this room. </p> |
| 121 | + <ul className="mx_RoomSettingsDialog_BridgeList"> |
| 122 | + { bridgeEvents.map((event) => this._renderBridgeCard(event, room)) } |
| 123 | + </ul> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ); |
| 127 | + } |
| 128 | +} |
0 commit comments