|
| 1 | +/* |
| 2 | +Copyright 2019 The Matrix.org Foundation C.I.C. |
| 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 | +import BaseAvatar from "../../../avatars/BaseAvatar"; |
| 24 | +import { ContentRepo } from "matrix-js-sdk"; |
| 25 | + |
| 26 | +const BRIDGE_EVENT_TYPES = [ |
| 27 | + "uk.half-shot.bridge", |
| 28 | + // m.bridge |
| 29 | +]; |
| 30 | + |
| 31 | +export default class BridgeSettingsTab extends React.Component { |
| 32 | + static propTypes = { |
| 33 | + roomId: PropTypes.string.isRequired, |
| 34 | + }; |
| 35 | + |
| 36 | + _renderBridgeCard(event, room) { |
| 37 | + const content = event.getContent(); |
| 38 | + if (!content || !content.channel || !content.protocol) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + const { channel, network } = content; |
| 42 | + const protocolName = content.protocol.displayname || content.protocol.id; |
| 43 | + const channelName = channel.displayname || channel.id; |
| 44 | + const networkName = network ? network.displayname || network.id : protocolName; |
| 45 | + |
| 46 | + let creator = null; |
| 47 | + if (content.creator) { |
| 48 | + creator = <p> { _t("This bridge was provisioned by <user />", {}, { |
| 49 | + user: <Pill |
| 50 | + type={Pill.TYPE_USER_MENTION} |
| 51 | + room={room} |
| 52 | + url={makeUserPermalink(content.creator)} |
| 53 | + shouldShowPillAvatar={true} |
| 54 | + />, |
| 55 | + })}</p>; |
| 56 | + } |
| 57 | + |
| 58 | + const bot = (<p> {_t("This bridge is managed by <user />.", {}, { |
| 59 | + user: <Pill |
| 60 | + type={Pill.TYPE_USER_MENTION} |
| 61 | + room={room} |
| 62 | + url={makeUserPermalink(event.getSender())} |
| 63 | + shouldShowPillAvatar={true} |
| 64 | + />, |
| 65 | + })} </p>); |
| 66 | + let channelLink = channelName; |
| 67 | + if (channel.external_url) { |
| 68 | + channelLink = <a target="_blank" href={channel.external_url} rel="noopener">{channelName}</a>; |
| 69 | + } |
| 70 | + |
| 71 | + let networkLink = networkName; |
| 72 | + if (network && network.external_url) { |
| 73 | + networkLink = <a target="_blank" href={network.external_url} rel="noopener">{networkName}</a>; |
| 74 | + } |
| 75 | + |
| 76 | + const chanAndNetworkInfo = ( |
| 77 | + _t("Bridged into <channelLink /> <networkLink />, on <protocolName />", {}, { |
| 78 | + channelLink, |
| 79 | + networkLink, |
| 80 | + protocolName, |
| 81 | + }) |
| 82 | + ); |
| 83 | + |
| 84 | + let networkIcon = null; |
| 85 | + if (networkName && network.avatar) { |
| 86 | + const avatarUrl = ContentRepo.getHttpUriForMxc( |
| 87 | + MatrixClientPeg.get().getHomeserverUrl(), |
| 88 | + network.avatar, 32, 32, "crop", |
| 89 | + ); |
| 90 | + networkIcon = <BaseAvatar |
| 91 | + width={32} |
| 92 | + height={32} |
| 93 | + resizeMethod='crop' |
| 94 | + name={ networkName } |
| 95 | + idName={ networkName } |
| 96 | + url={ avatarUrl } |
| 97 | + />; |
| 98 | + } |
| 99 | + |
| 100 | + let channelIcon = null; |
| 101 | + if (channel.avatar) { |
| 102 | + const avatarUrl = ContentRepo.getHttpUriForMxc( |
| 103 | + MatrixClientPeg.get().getHomeserverUrl(), |
| 104 | + channel.avatar, 32, 32, "crop", |
| 105 | + ); |
| 106 | + channelIcon = <BaseAvatar |
| 107 | + width={32} |
| 108 | + height={32} |
| 109 | + resizeMethod='crop' |
| 110 | + name={ networkName } |
| 111 | + idName={ networkName } |
| 112 | + url={ avatarUrl } |
| 113 | + />; |
| 114 | + } |
| 115 | + |
| 116 | + const heading = _t("Connected to <channelIcon /> <channelName /> on <networkIcon /> <networkName />", { }, { |
| 117 | + channelIcon, |
| 118 | + channelName, |
| 119 | + networkName, |
| 120 | + networkIcon, |
| 121 | + }); |
| 122 | + |
| 123 | + return (<li key={event.stateKey}> |
| 124 | + <div> |
| 125 | + <h3>{heading}</h3> |
| 126 | + <p>{_t("Connected via %(protocolName)s", { protocolName })}</p> |
| 127 | + <details> |
| 128 | + {creator} |
| 129 | + {bot} |
| 130 | + <p>{chanAndNetworkInfo}</p> |
| 131 | + </details> |
| 132 | + </div> |
| 133 | + </li>); |
| 134 | + } |
| 135 | + |
| 136 | + static getBridgeStateEvents(roomId) { |
| 137 | + const client = MatrixClientPeg.get(); |
| 138 | + const roomState = (client.getRoom(roomId)).currentState; |
| 139 | + |
| 140 | + const bridgeEvents = Array.concat(...BRIDGE_EVENT_TYPES.map((typeName) => |
| 141 | + Object.values(roomState.events[typeName] || {}), |
| 142 | + )); |
| 143 | + |
| 144 | + return bridgeEvents; |
| 145 | + } |
| 146 | + |
| 147 | + render() { |
| 148 | + // This settings tab will only be invoked if the following function returns more |
| 149 | + // than 0 events, so no validation is needed at this stage. |
| 150 | + const bridgeEvents = BridgeSettingsTab.getBridgeStateEvents(this.props.roomId); |
| 151 | + const client = MatrixClientPeg.get(); |
| 152 | + const room = client.getRoom(this.props.roomId); |
| 153 | + |
| 154 | + return ( |
| 155 | + <div className="mx_SettingsTab"> |
| 156 | + <div className="mx_SettingsTab_heading">{_t("Bridge Info")}</div> |
| 157 | + <div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'> |
| 158 | + <p>{ _t("Below is a list of bridges connected to this room.") }</p> |
| 159 | + <ul className="mx_RoomSettingsDialog_BridgeList"> |
| 160 | + { bridgeEvents.map((event) => this._renderBridgeCard(event, room)) } |
| 161 | + </ul> |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
0 commit comments