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');
|
2016-03-01 21:23:57 +03:00
|
|
|
var ContentRepo = require("matrix-js-sdk").ContentRepo;
|
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-03-02 14:59:17 +03:00
|
|
|
// Room may be left unset here, but if it is,
|
|
|
|
// oobData.avatarUrl should be set (else there
|
|
|
|
// would be nowhere to get the avatar from)
|
2016-01-15 19:13:25 +03:00
|
|
|
propTypes: {
|
2016-03-01 21:23:57 +03:00
|
|
|
room: React.PropTypes.object,
|
|
|
|
oobData: React.PropTypes.object,
|
2016-01-15 19:13:25 +03:00
|
|
|
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,
|
2016-03-01 21:23:57 +03:00
|
|
|
resizeMethod: 'crop',
|
|
|
|
oobData: {},
|
2015-09-18 16:34:36 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-22 15:08:35 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2016-01-15 20:05:05 +03:00
|
|
|
urls: this.getImageUrls(this.props)
|
2015-10-22 15:08:35 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-01-15 20:05:05 +03:00
|
|
|
componentWillReceiveProps: function(newProps) {
|
|
|
|
this.setState({
|
|
|
|
urls: this.getImageUrls(newProps)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getImageUrls: function(props) {
|
|
|
|
return [
|
2016-03-01 21:23:57 +03:00
|
|
|
ContentRepo.getHttpUriForMxc(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
|
|
|
props.oobData.avatarUrl,
|
|
|
|
props.width, props.height, props.resizeMethod
|
|
|
|
), // highest priority
|
|
|
|
this.getRoomAvatarUrl(props),
|
2016-01-15 20:05:05 +03:00
|
|
|
this.getOneToOneAvatar(props),
|
|
|
|
this.getFallbackAvatar(props) // lowest priority
|
|
|
|
].filter(function(url) {
|
2016-03-03 19:16:40 +03:00
|
|
|
return (url != null && url != "");
|
2016-01-15 20:05:05 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getRoomAvatarUrl: function(props) {
|
2016-03-01 21:23:57 +03:00
|
|
|
if (!this.props.room) return null;
|
|
|
|
|
2016-01-15 20:05:05 +03:00
|
|
|
return props.room.getAvatarUrl(
|
2015-10-20 12:30:58 +03:00
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
2016-01-15 20:05:05 +03:00
|
|
|
props.width, props.height, props.resizeMethod,
|
2015-09-18 16:34:36 +03:00
|
|
|
false
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-01-15 20:05:05 +03:00
|
|
|
getOneToOneAvatar: function(props) {
|
2016-03-01 21:23:57 +03:00
|
|
|
if (!this.props.room) return null;
|
|
|
|
|
2016-01-22 14:11:56 +03:00
|
|
|
var mlist = props.room.currentState.members;
|
|
|
|
var userIds = [];
|
2016-12-19 21:41:34 +03:00
|
|
|
var leftUserIds = [];
|
2016-01-22 14:11:56 +03:00
|
|
|
// for .. in optimisation to return early if there are >2 keys
|
|
|
|
for (var uid in mlist) {
|
2016-12-19 21:41:34 +03:00
|
|
|
if (mlist.hasOwnProperty(uid)) {
|
|
|
|
if (["join", "invite"].includes(mlist[uid].membership)) {
|
|
|
|
userIds.push(uid);
|
|
|
|
} else {
|
|
|
|
leftUserIds.push(uid);
|
|
|
|
}
|
2016-01-22 14:11:56 +03:00
|
|
|
}
|
|
|
|
if (userIds.length > 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 15:08:35 +03:00
|
|
|
|
|
|
|
if (userIds.length == 2) {
|
|
|
|
var theOtherGuy = null;
|
2016-01-22 14:11:56 +03:00
|
|
|
if (mlist[userIds[0]].userId == MatrixClientPeg.get().credentials.userId) {
|
|
|
|
theOtherGuy = mlist[userIds[1]];
|
2015-10-22 15:08:35 +03:00
|
|
|
} else {
|
2016-01-22 14:11:56 +03:00
|
|
|
theOtherGuy = mlist[userIds[0]];
|
2015-10-22 15:08:35 +03:00
|
|
|
}
|
|
|
|
return theOtherGuy.getAvatarUrl(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
2016-01-15 20:05:05 +03:00
|
|
|
props.width, props.height, props.resizeMethod,
|
2015-10-22 15:08:35 +03:00
|
|
|
false
|
|
|
|
);
|
|
|
|
} else if (userIds.length == 1) {
|
2016-12-19 21:41:34 +03:00
|
|
|
// The other 1-1 user left, leaving just the current user, so show the left user's avatar
|
|
|
|
if (leftUserIds.length === 1) {
|
|
|
|
return mlist[leftUserIds[0]].getAvatarUrl(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
|
|
|
props.width, props.height, props.resizeMethod,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
2016-01-22 14:11:56 +03:00
|
|
|
return mlist[userIds[0]].getAvatarUrl(
|
2015-10-22 15:08:35 +03:00
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
2016-01-15 20:05:05 +03:00
|
|
|
props.width, props.height, props.resizeMethod,
|
2015-10-22 15:08:35 +03:00
|
|
|
false
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2015-09-18 16:34:36 +03:00
|
|
|
},
|
|
|
|
|
2016-01-15 20:05:05 +03:00
|
|
|
getFallbackAvatar: function(props) {
|
2016-03-01 21:23:57 +03:00
|
|
|
if (!this.props.room) return null;
|
|
|
|
|
2016-01-15 20:05:05 +03:00
|
|
|
return Avatar.defaultAvatarUrlForString(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");
|
2016-03-01 21:23:57 +03:00
|
|
|
|
2016-07-27 13:38:04 +03:00
|
|
|
var {room, oobData, ...otherProps} = this.props;
|
|
|
|
|
|
|
|
var roomName = room ? room.name : oobData.name;
|
2016-03-01 21:23:57 +03:00
|
|
|
|
2016-01-15 19:13:25 +03:00
|
|
|
return (
|
2016-07-27 13:38:04 +03:00
|
|
|
<BaseAvatar {...otherProps} name={roomName}
|
|
|
|
idName={room ? room.roomId : null}
|
2016-03-01 21:23:57 +03:00
|
|
|
urls={this.state.urls} />
|
2016-01-15 19:13:25 +03:00
|
|
|
);
|
2015-09-18 16:34:36 +03:00
|
|
|
}
|
2015-11-26 15:02:31 +03:00
|
|
|
});
|