2017-08-30 09:55:25 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 New Vector 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';
|
2017-10-19 09:28:59 +00:00
|
|
|
import FlairStore from '../../../stores/FlairStore';
|
2017-09-21 12:25:36 +00:00
|
|
|
import dis from '../../../dispatcher';
|
2017-08-30 09:55:25 +00:00
|
|
|
|
|
|
|
|
2017-09-21 12:25:36 +00:00
|
|
|
class FlairAvatar extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.onClick = this.onClick.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
// Don't trigger onClick of parent element
|
|
|
|
ev.stopPropagation();
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_group',
|
2017-09-21 13:22:43 +00:00
|
|
|
group_id: this.props.groupProfile.groupId,
|
2017-09-21 12:25:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const httpUrl = this.context.matrixClient.mxcUrlToHttp(
|
2017-10-23 13:56:24 +00:00
|
|
|
this.props.groupProfile.avatarUrl, 16, 16, 'scale', false);
|
2017-11-09 12:38:43 +00:00
|
|
|
const tooltip = this.props.groupProfile.name ?
|
|
|
|
`${this.props.groupProfile.name} (${this.props.groupProfile.groupId})`:
|
|
|
|
this.props.groupProfile.groupId;
|
2017-09-27 13:35:59 +00:00
|
|
|
return <img
|
|
|
|
src={httpUrl}
|
2017-10-23 13:56:24 +00:00
|
|
|
width="16"
|
|
|
|
height="16"
|
2017-09-27 13:35:59 +00:00
|
|
|
onClick={this.onClick}
|
2017-11-09 12:38:43 +00:00
|
|
|
title={tooltip} />;
|
2017-09-21 12:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FlairAvatar.propTypes = {
|
2017-09-21 13:22:43 +00:00
|
|
|
groupProfile: PropTypes.shape({
|
|
|
|
groupId: PropTypes.string.isRequired,
|
2017-11-10 11:43:05 +00:00
|
|
|
name: PropTypes.string,
|
2017-09-21 13:22:43 +00:00
|
|
|
avatarUrl: PropTypes.string.isRequired,
|
|
|
|
}),
|
2017-09-21 12:25:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FlairAvatar.contextTypes = {
|
|
|
|
matrixClient: React.PropTypes.instanceOf(MatrixClient).isRequired,
|
|
|
|
};
|
|
|
|
|
2017-08-30 09:55:25 +00:00
|
|
|
export default class Flair extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
2017-09-21 12:25:36 +00:00
|
|
|
profiles: [],
|
2017-08-30 09:55:25 +00:00
|
|
|
};
|
2017-10-04 13:06:49 +00:00
|
|
|
this.onRoomStateEvents = this.onRoomStateEvents.bind(this);
|
2017-08-30 09:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._unmounted = true;
|
2017-10-04 13:06:49 +00:00
|
|
|
this.context.matrixClient.removeListener('RoomState.events', this.onRoomStateEvents);
|
2017-08-30 09:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 15:44:14 +00:00
|
|
|
componentWillMount() {
|
2017-08-30 09:55:25 +00:00
|
|
|
this._unmounted = false;
|
2017-11-10 15:42:11 +00:00
|
|
|
this._generateAvatars();
|
2017-10-04 13:06:49 +00:00
|
|
|
this.context.matrixClient.on('RoomState.events', this.onRoomStateEvents);
|
|
|
|
}
|
|
|
|
|
|
|
|
onRoomStateEvents(event) {
|
2017-10-19 09:28:59 +00:00
|
|
|
if (event.getType() === 'm.room.related_groups' && FlairStore.groupSupport()) {
|
2017-10-04 13:06:49 +00:00
|
|
|
this._generateAvatars();
|
|
|
|
}
|
2017-08-30 09:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 12:25:36 +00:00
|
|
|
async _getGroupProfiles(groups) {
|
2017-08-30 09:55:25 +00:00
|
|
|
const profiles = [];
|
|
|
|
for (const groupId of groups) {
|
|
|
|
let groupProfile = null;
|
|
|
|
try {
|
2017-10-19 09:28:59 +00:00
|
|
|
groupProfile = await FlairStore.getGroupProfileCached(this.context.matrixClient, groupId);
|
2017-08-30 09:55:25 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error('Could not get profile for group', groupId, err);
|
|
|
|
}
|
|
|
|
profiles.push(groupProfile);
|
|
|
|
}
|
2017-09-21 12:25:36 +00:00
|
|
|
return profiles.filter((p) => p !== null);
|
2017-08-30 09:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _generateAvatars() {
|
2017-10-19 09:28:59 +00:00
|
|
|
let groups = await FlairStore.getPublicisedGroupsCached(this.context.matrixClient, this.props.userId);
|
2017-10-04 13:06:49 +00:00
|
|
|
if (this.props.roomId && this.props.showRelated) {
|
|
|
|
const relatedGroupsEvent = this.context.matrixClient
|
|
|
|
.getRoom(this.props.roomId)
|
|
|
|
.currentState
|
|
|
|
.getStateEvents('m.room.related_groups', '');
|
|
|
|
const relatedGroups = relatedGroupsEvent ?
|
|
|
|
relatedGroupsEvent.getContent().groups || [] : [];
|
|
|
|
if (relatedGroups && relatedGroups.length > 0) {
|
|
|
|
groups = groups.filter((groupId) => {
|
|
|
|
return relatedGroups.includes(groupId);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
groups = [];
|
|
|
|
}
|
|
|
|
}
|
2017-08-31 15:46:39 +00:00
|
|
|
if (!groups || groups.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-21 12:25:36 +00:00
|
|
|
const profiles = await this._getGroupProfiles(groups);
|
2017-08-30 09:55:25 +00:00
|
|
|
if (!this.unmounted) {
|
2017-10-23 16:06:47 +00:00
|
|
|
this.setState({profiles: profiles.filter((profile) => {return profile.avatarUrl;})});
|
2017-08-30 09:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-09-21 12:25:36 +00:00
|
|
|
if (this.state.profiles.length === 0) {
|
2017-08-30 09:55:25 +00:00
|
|
|
return <div />;
|
|
|
|
}
|
2017-09-21 12:25:36 +00:00
|
|
|
const avatars = this.state.profiles.map((profile, index) => {
|
2017-09-28 10:21:06 +00:00
|
|
|
return <FlairAvatar key={index} groupProfile={profile} />;
|
2017-08-30 09:55:25 +00:00
|
|
|
});
|
|
|
|
return (
|
2017-10-11 15:02:01 +00:00
|
|
|
<span className="mx_Flair">
|
2017-09-28 10:21:06 +00:00
|
|
|
{ avatars }
|
2017-08-30 09:55:25 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Flair.propTypes = {
|
|
|
|
userId: PropTypes.string,
|
2017-10-04 13:06:49 +00:00
|
|
|
|
|
|
|
// Whether to show only the flair associated with related groups of the given room,
|
|
|
|
// or all flair associated with a user.
|
|
|
|
showRelated: PropTypes.bool,
|
|
|
|
// The room that this flair will be displayed in. Optional. Only applies when showRelated = true.
|
|
|
|
roomId: PropTypes.string,
|
2017-08-30 09:55:25 +00:00
|
|
|
};
|
|
|
|
|
2017-08-31 16:52:53 +00:00
|
|
|
// TODO: We've decided that all components should follow this pattern, which means removing withMatrixClient and using
|
|
|
|
// this.context.matrixClient everywhere instead of this.props.matrixClient.
|
|
|
|
// See https://github.com/vector-im/riot-web/issues/4951.
|
2017-08-30 09:55:25 +00:00
|
|
|
Flair.contextTypes = {
|
|
|
|
matrixClient: React.PropTypes.instanceOf(MatrixClient).isRequired,
|
|
|
|
};
|