/* Copyright 2015, 2016 OpenMarket 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. */ 'use strict'; import React from 'react'; import PropTypes from 'prop-types'; import {MatrixClient} from 'matrix-js-sdk'; import sdk from '../../../index'; import Flair from '../elements/Flair.js'; import FlairStore from '../../../stores/FlairStore'; import { _t } from '../../../languageHandler'; export default React.createClass({ displayName: 'SenderProfile', propTypes: { mxEvent: PropTypes.object.isRequired, // event whose sender we're showing text: PropTypes.string, // Text to show. Defaults to sender name onClick: PropTypes.func, }, contextTypes: { matrixClient: PropTypes.instanceOf(MatrixClient), }, getInitialState() { return { groups: null, relatedGroups: [], }; }, componentWillMount() { this.unmounted = false; this._updateRelatedGroups(); FlairStore.getPublicisedGroupsCached( this.context.matrixClient, this.props.mxEvent.getSender(), ).then((groups) => { if (this.unmounted) return; this.setState({groups}); }); this.context.matrixClient.on('RoomState.events', this.onRoomStateEvents); }, componentWillUnmount() { this.unmounted = true; this.context.matrixClient.removeListener('RoomState.events', this.onRoomStateEvents); }, onRoomStateEvents(event) { if (event.getType() === 'm.room.related_groups' && event.getRoomId() === this.props.mxEvent.getRoomId() ) { this._updateRelatedGroups(); } }, _updateRelatedGroups() { if (this.unmounted) return; const relatedGroupsEvent = this.context.matrixClient .getRoom(this.props.mxEvent.getRoomId()) .currentState .getStateEvents('m.room.related_groups', ''); this.setState({ relatedGroups: relatedGroupsEvent ? relatedGroupsEvent.getContent().groups || [] : [], }); }, render() { const EmojiText = sdk.getComponent('elements.EmojiText'); const {mxEvent} = this.props; let name = mxEvent.sender ? mxEvent.sender.name : mxEvent.getSender(); const {msgtype} = mxEvent.getContent(); if (msgtype === 'm.emote') { return ; // emote message must include the name so don't duplicate it } let groups = this.state.groups || []; if (this.state.relatedGroups && this.state.relatedGroups.length > 0) { groups = groups.filter((groupId) => { return this.state.relatedGroups.includes(groupId); }); } else { groups = []; } name = groups.length > 0 ? name.replace(' (IRC)', '') : name; const nameElem = { name || '' }; // Name + flair const nameFlair = { nameElem } { this.props.enableFlair ? : null } ; const content = this.props.text ? { _t(this.props.text, { senderName: () => nameElem }) } : nameFlair; return (
{ content }
); }, });