diff --git a/src/components/views/messages/BridgeError.js b/src/components/views/messages/BridgeError.js index 162b4bb0995..dc9246fde12 100644 --- a/src/components/views/messages/BridgeError.js +++ b/src/components/views/messages/BridgeError.js @@ -16,16 +16,14 @@ limitations under the License. import React from 'react'; import PropTypes from 'prop-types'; +import { EventTimeline } from 'matrix-js-sdk'; import { _t } from '../../../languageHandler'; -import sdk from '../../../index'; -import MatrixClientPeg from '../../../MatrixClientPeg'; -import { unicodeToShortcode } from '../../../HtmlUtils'; export default class BridgeError extends React.PureComponent { static propTypes = { - matrixClient: PropTypes.object.isRequired, mxEvent: PropTypes.object.isRequired, + room: PropTypes.object.isRequired, }; constructor(props) { @@ -33,29 +31,108 @@ export default class BridgeError extends React.PureComponent { // this.state = {} } - _get_relations() { + /** + * Returns all bridge error relations for this event. + * + * @returns {MatrixEvent[]} + */ + _getRelations() { const { mxEvent, room } = this.props; // const room = matrixClient.getRoom(mxEvent.getRoomId()); const timelineSet = room.getUnfilteredTimelineSet(); - return timelineSet.getRelationsForEvent( + const relations = timelineSet.getRelationsForEvent( mxEvent.getId(), "m.reference", "de.nasnotfound.bridge_error", - ); + ); + + return relations ? relations.getRelations() : []; + } + + /** + * Returns a list of all users matched by the regex at the time of the event. + * + * @param {string} regexStr + * @returns {RoomMember[]} + */ + _findMembersFromRegex(regexStr) { + const { room } = this.props; + const regex = new RegExp(regexStr); + + // TODO[V02460@gmail.com]: Get room state at the proper point in time + const roomState = room.getLiveTimeline().getState(EventTimeline.FORWARDS); + const members = roomState.getMembers(); + + return members.filter(m => regex.test(m.userId)); + } + + /** + * Returns the network name and the affected users for the given relation. + * + * @param {MatrixEvent} relation + * @returns {{networkName: string, affectedUsers: RoomMember[]}} + */ + _getRelationInfo(relation) { + if (!relation.event || !relation.event.content) { + return { networkName: "", affected_users: [] }; + } + const content = relation.event.content; + + const networkName = (typeof content.network_name === 'string' ? + content.network_name : + "" + ); + const affectedUsersRegex = (Array.isArray(content.affected_users) ? + content.affected_users : + [] + ); + const affectedUsers = affectedUsersRegex.flatMap( + this._findMembersFromRegex.bind(this), + ); + + return { networkName, affectedUsers }; + } + + /** + * Returns the rendered string for the given relation. + * + * @param {{networkName: string, affectedUsers: RoomMember[]}} relationInfo + * @return {string} + */ + _renderInfo(relationInfo) { + const usernames = relationInfo.affected_users.map(u => u.name); + + if (relationInfo.networkName) { + return _t( + "%(networkName)s: %(affectedUsers)s", + { + networkName: relationInfo.networkName, + affectedUsers: usernames.join(", "), + }, + ); + } else { + return _t( + "Affected users: %(affectedUsers)s", + { affectedUsers: relationInfo.affectedUsers }, + ); + } } render() { - const relations = this._get_relations() - const isBridgeError = !!(relations && relations.getRelations()); + const relations = this._getRelations(); + const isBridgeError = !!relations.length; if (!isBridgeError) { - return null + return null; } + const relationInfos = relations.map(this._getRelationInfo.bind(this)); + const renderedInfos = relationInfos.map(this._renderInfo.bind(this)); + return (
- ⚠ {_t("Event not delivered")} + ⚠ {_t("Event not delivered") + " (" + renderedInfos.join("; ") + ")" }
); } diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 8d581b5f0a4..d74a910a7c7 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -917,6 +917,8 @@ "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.": "When someone puts a URL in their message, a URL preview can be shown to give more information about that link such as the title, description, and an image from the website.", "Members": "Members", "Files": "Files", + "%(networkName)s: %(affectedUsers)s": "%(networkName)s: %(affectedUsers)s", + "Affected users: %(affectedUsers)s": "Affected users: %(affectedUsers)s", "Event not delivered": "Event not delivered", "Sunday": "Sunday", "Monday": "Monday",