2015-09-18 16:34:36 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-18 16:34:36 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2015-11-26 15:02:31 +03:00
|
|
|
var React = require('react');
|
2015-11-26 16:49:39 +03:00
|
|
|
var MatrixClientPeg = require('../../../MatrixClientPeg');
|
2015-11-30 17:39:29 +03:00
|
|
|
var Avatar = require('../../../Avatar');
|
2016-01-15 19:13:25 +03:00
|
|
|
var sdk = require("../../../index");
|
2015-09-18 16:34:36 +03:00
|
|
|
|
2015-11-26 15:02:31 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'RoomAvatar',
|
|
|
|
|
2016-01-15 19:13:25 +03:00
|
|
|
propTypes: {
|
|
|
|
room: React.PropTypes.object.isRequired,
|
|
|
|
width: React.PropTypes.number,
|
|
|
|
height: React.PropTypes.number,
|
|
|
|
resizeMethod: React.PropTypes.string
|
|
|
|
},
|
|
|
|
|
2015-09-18 16:34:36 +03:00
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
2015-10-11 04:08:39 +03:00
|
|
|
width: 36,
|
|
|
|
height: 36,
|
2015-09-18 16:34:36 +03:00
|
|
|
resizeMethod: 'crop'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-22 15:08:35 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2016-01-15 19:13:25 +03:00
|
|
|
urls: [
|
|
|
|
this.getRoomAvatarUrl(), // highest priority
|
|
|
|
this.getOneToOneAvatar(),
|
|
|
|
this.getFallbackAvatar() // lowest priority
|
|
|
|
].filter(function(url) {
|
|
|
|
return url != null;
|
|
|
|
})
|
2015-10-22 15:08:35 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-01-15 19:13:25 +03:00
|
|
|
getRoomAvatarUrl: function() {
|
|
|
|
return this.props.room.getAvatarUrl(
|
2015-10-20 12:30:58 +03:00
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
2015-09-18 16:34:36 +03:00
|
|
|
this.props.width, this.props.height, this.props.resizeMethod,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-10-22 15:08:35 +03:00
|
|
|
getOneToOneAvatar: function() {
|
|
|
|
var userIds = Object.keys(this.props.room.currentState.members);
|
|
|
|
|
|
|
|
if (userIds.length == 2) {
|
|
|
|
var theOtherGuy = null;
|
|
|
|
if (this.props.room.currentState.members[userIds[0]].userId == MatrixClientPeg.get().credentials.userId) {
|
|
|
|
theOtherGuy = this.props.room.currentState.members[userIds[1]];
|
|
|
|
} else {
|
|
|
|
theOtherGuy = this.props.room.currentState.members[userIds[0]];
|
|
|
|
}
|
|
|
|
return theOtherGuy.getAvatarUrl(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
|
|
|
this.props.width, this.props.height, this.props.resizeMethod,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
} else if (userIds.length == 1) {
|
|
|
|
return this.props.room.currentState.members[userIds[0]].getAvatarUrl(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
|
|
|
this.props.width, this.props.height, this.props.resizeMethod,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2015-09-18 16:34:36 +03:00
|
|
|
},
|
|
|
|
|
2015-11-26 15:02:31 +03:00
|
|
|
getFallbackAvatar: function() {
|
2015-11-30 17:39:29 +03:00
|
|
|
return Avatar.defaultAvatarUrlForString(this.props.room.roomId);
|
2015-11-26 15:02:31 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2016-01-15 19:13:25 +03:00
|
|
|
var BaseAvatar = sdk.getComponent("avatars.BaseAvatar");
|
|
|
|
return (
|
|
|
|
<BaseAvatar {...this.props} name={this.props.room.name}
|
|
|
|
idName={this.props.room.roomId} urls={this.state.urls} />
|
|
|
|
);
|
2015-09-18 16:34:36 +03:00
|
|
|
}
|
2015-11-26 15:02:31 +03:00
|
|
|
});
|