This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BridgeError: Add display of network name and affected users
Signed-off-by: Kai A. Hiller <[email protected]>
- Loading branch information
Kai A. Hiller
authored and
Kai A. Hiller
committed
Jul 25, 2019
1 parent
1d33ca1
commit ac9116e
Showing
2 changed files
with
90 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,46 +16,123 @@ 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) { | ||
super(props); | ||
// 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[[email protected]]: 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: "", affectedUsers: [] }; | ||
} | ||
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.affectedUsers.map(u => u.name).join(", "); | ||
|
||
if (relationInfo.networkName) { | ||
return _t( | ||
"%(networkName)s: %(affectedUsers)s", | ||
{ | ||
networkName: relationInfo.networkName, | ||
affectedUsers: usernames, | ||
}, | ||
); | ||
} else { | ||
return _t( | ||
"Affected users: %(affectedUsers)s", | ||
{ affectedUsers: usernames }, | ||
); | ||
} | ||
} | ||
|
||
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 ( | ||
<div className="mx_BridgeError"> | ||
⚠ {_t("Event not delivered")} | ||
⚠ {_t("Event not delivered") + " (" + renderedInfos.join("; ") + ")" } | ||
</div> | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters