element-web/src/components/views/rooms/MemberInfo.js

658 lines
23 KiB
JavaScript
Raw Normal View History

2015-09-18 17:39:16 +00:00
/*
2016-01-07 04:06:39 +00:00
Copyright 2015, 2016 OpenMarket Ltd
2015-09-18 17:39:16 +00: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.
*/
/*
* State vars:
* 'can': {
* kick: boolean,
* ban: boolean,
* mute: boolean,
* modifyLevel: boolean
* },
* 'muted': boolean,
* 'isTargetMod': boolean
*/
2015-11-26 17:49:39 +00:00
var React = require('react');
var MatrixClientPeg = require("../../../MatrixClientPeg");
var dis = require("../../../dispatcher");
var Modal = require("../../../Modal");
var sdk = require('../../../index');
var UserSettingsStore = require('../../../UserSettingsStore');
var createRoom = require('../../../createRoom');
2016-07-05 04:54:18 +00:00
import {emojifyText} from '../../../HtmlUtils';
2015-09-18 17:39:16 +00:00
2015-11-26 17:49:39 +00:00
module.exports = React.createClass({
displayName: 'MemberInfo',
2015-09-18 17:39:16 +00:00
propTypes: {
member: React.PropTypes.object.isRequired,
onFinished: React.PropTypes.func,
},
getDefaultProps: function() {
return {
onFinished: function() {}
};
},
getInitialState: function() {
return {
can: {
kick: false,
ban: false,
mute: false,
modifyLevel: false
},
muted: false,
isTargetMod: false,
updating: 0,
devicesLoading: true,
devices: null,
existingOneToOneRoomId: null,
2015-09-18 17:39:16 +00:00
}
},
componentWillMount: function() {
this._cancelDeviceList = null;
this.setState({
existingOneToOneRoomId: this.getExistingOneToOneRoomId()
});
},
componentDidMount: function() {
this._updateStateForNewMember(this.props.member);
MatrixClientPeg.get().on("deviceVerificationChanged", this.onDeviceVerificationChanged);
},
componentWillReceiveProps: function(newProps) {
if (this.props.member.userId != newProps.member.userId) {
this._updateStateForNewMember(newProps.member);
}
},
componentWillUnmount: function() {
var client = MatrixClientPeg.get();
if (client) {
client.removeListener("deviceVerificationChanged", this.onDeviceVerificationChanged);
}
if (this._cancelDeviceList) {
this._cancelDeviceList();
}
},
getExistingOneToOneRoomId: function() {
var self = this;
var rooms = MatrixClientPeg.get().getRooms();
var userIds = [
this.props.member.userId,
MatrixClientPeg.get().credentials.userId
];
var existingRoomId;
// roomId can be null here because of a hack in MatrixChat.onUserClick where we
// abuse this to view users rather than room members.
var currentMembers;
if (this.props.member.roomId) {
var currentRoom = MatrixClientPeg.get().getRoom(this.props.member.roomId);
currentMembers = currentRoom.getJoinedMembers();
}
// reuse the first private 1:1 we find
existingRoomId = null;
for (var i = 0; i < rooms.length; i++) {
// don't try to reuse public 1:1 rooms
var join_rules = rooms[i].currentState.getStateEvents("m.room.join_rules", '');
if (join_rules && join_rules.getContent().join_rule === 'public') continue;
var members = rooms[i].getJoinedMembers();
if (members.length === 2 &&
userIds.indexOf(members[0].userId) !== -1 &&
userIds.indexOf(members[1].userId) !== -1)
{
existingRoomId = rooms[i].roomId;
break;
}
}
return existingRoomId;
},
onDeviceVerificationChanged: function(userId, device) {
if (userId == this.props.member.userId) {
// no need to re-download the whole thing; just update our copy of
// the list.
var devices = MatrixClientPeg.get().listDeviceKeys(userId);
this.setState({devices: devices});
}
},
_updateStateForNewMember: function(member) {
var newState = this._calculateOpsPermissions(member);
newState.devicesLoading = true;
newState.devices = null;
this.setState(newState);
if (this._cancelDeviceList) {
this._cancelDeviceList();
this._cancelDeviceList = null;
}
this._downloadDeviceList(member);
},
_downloadDeviceList: function(member) {
var cancelled = false;
this._cancelDeviceList = function() { cancelled = true; }
var client = MatrixClientPeg.get();
var self = this;
client.downloadKeys([member.userId], true).finally(function() {
self._cancelDeviceList = null;
}).done(function() {
if (cancelled) {
// we got cancelled - presumably a different user now
return;
}
var devices = client.listDeviceKeys(member.userId);
self.setState({devicesLoading: false, devices: devices});
}, function(err) {
console.log("Error downloading devices", err);
self.setState({devicesLoading: false});
});
},
2015-09-18 17:39:16 +00:00
onKick: function() {
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
2015-09-18 17:39:16 +00:00
var roomId = this.props.member.roomId;
var target = this.props.member.userId;
this.setState({ updating: this.state.updating + 1 });
MatrixClientPeg.get().kick(roomId, target).then(function() {
// NO-OP; rely on the m.room.member event coming down else we could
// get out of sync if we force setState here!
console.log("Kick success");
}, function(err) {
Modal.createDialog(ErrorDialog, {
title: "Kick error",
description: err.message
});
}
).finally(()=>{
this.setState({ updating: this.state.updating - 1 });
});
2015-09-22 15:37:39 +00:00
this.props.onFinished();
2015-09-18 17:39:16 +00:00
},
onBan: function() {
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
2015-09-18 17:39:16 +00:00
var roomId = this.props.member.roomId;
var target = this.props.member.userId;
this.setState({ updating: this.state.updating + 1 });
MatrixClientPeg.get().ban(roomId, target).then(
function() {
// NO-OP; rely on the m.room.member event coming down else we could
// get out of sync if we force setState here!
console.log("Ban success");
}, function(err) {
Modal.createDialog(ErrorDialog, {
title: "Ban error",
description: err.message
});
}
).finally(()=>{
this.setState({ updating: this.state.updating - 1 });
});
2015-09-22 15:37:39 +00:00
this.props.onFinished();
2015-09-18 17:39:16 +00:00
},
onMuteToggle: function() {
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
2015-09-18 17:39:16 +00:00
var roomId = this.props.member.roomId;
var target = this.props.member.userId;
var room = MatrixClientPeg.get().getRoom(roomId);
if (!room) {
2015-09-22 15:37:39 +00:00
this.props.onFinished();
2015-09-18 17:39:16 +00:00
return;
}
var powerLevelEvent = room.currentState.getStateEvents(
"m.room.power_levels", ""
);
if (!powerLevelEvent) {
2015-09-22 15:37:39 +00:00
this.props.onFinished();
2015-09-18 17:39:16 +00:00
return;
}
var isMuted = this.state.muted;
var powerLevels = powerLevelEvent.getContent();
var levelToSend = (
(powerLevels.events ? powerLevels.events["m.room.message"] : null) ||
powerLevels.events_default
);
var level;
if (isMuted) { // unmute
level = levelToSend;
}
else { // mute
level = levelToSend - 1;
}
2016-01-22 15:29:57 +00:00
level = parseInt(level);
2015-09-18 17:39:16 +00:00
2016-01-22 15:29:57 +00:00
if (level !== NaN) {
this.setState({ updating: this.state.updating + 1 });
MatrixClientPeg.get().setPowerLevel(roomId, target, level, powerLevelEvent).then(
2016-01-22 15:29:57 +00:00
function() {
// NO-OP; rely on the m.room.member event coming down else we could
// get out of sync if we force setState here!
console.log("Mute toggle success");
}, function(err) {
Modal.createDialog(ErrorDialog, {
title: "Mute error",
description: err.message
});
}
).finally(()=>{
this.setState({ updating: this.state.updating - 1 });
});
2016-01-22 15:29:57 +00:00
}
this.props.onFinished();
2015-09-18 17:39:16 +00:00
},
onModToggle: function() {
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
2015-09-18 17:39:16 +00:00
var roomId = this.props.member.roomId;
var target = this.props.member.userId;
var room = MatrixClientPeg.get().getRoom(roomId);
if (!room) {
2015-09-22 15:37:39 +00:00
this.props.onFinished();
2015-09-18 17:39:16 +00:00
return;
}
var powerLevelEvent = room.currentState.getStateEvents(
"m.room.power_levels", ""
);
if (!powerLevelEvent) {
2015-09-22 15:37:39 +00:00
this.props.onFinished();
2015-09-18 17:39:16 +00:00
return;
}
var me = room.getMember(MatrixClientPeg.get().credentials.userId);
if (!me) {
2015-09-22 15:37:39 +00:00
this.props.onFinished();
2015-09-18 17:39:16 +00:00
return;
}
var defaultLevel = powerLevelEvent.getContent().users_default;
var modLevel = me.powerLevel - 1;
2016-01-18 01:18:02 +00:00
if (modLevel > 50 && defaultLevel < 50) modLevel = 50; // try to stick with the vector level defaults
2015-09-18 17:39:16 +00:00
// toggle the level
var newLevel = this.state.isTargetMod ? defaultLevel : modLevel;
this.setState({ updating: this.state.updating + 1 });
MatrixClientPeg.get().setPowerLevel(roomId, target, parseInt(newLevel), powerLevelEvent).then(
function() {
// NO-OP; rely on the m.room.member event coming down else we could
// get out of sync if we force setState here!
console.log("Mod toggle success");
}, function(err) {
2016-03-22 13:19:29 +00:00
if (err.errcode == 'M_GUEST_ACCESS_FORBIDDEN') {
var NeedToRegisterDialog = sdk.getComponent("dialogs.NeedToRegisterDialog");
Modal.createDialog(NeedToRegisterDialog, {
title: "Please Register",
description: "This action cannot be performed by a guest user. Please register to be able to do this."
});
} else {
Modal.createDialog(ErrorDialog, {
title: "Moderator toggle error",
2016-03-22 13:19:29 +00:00
description: err.message
});
}
}
).finally(()=>{
this.setState({ updating: this.state.updating - 1 });
});
this.props.onFinished();
2015-09-18 17:39:16 +00:00
},
2016-03-21 00:49:18 +00:00
_applyPowerChange: function(roomId, target, powerLevel, powerLevelEvent) {
this.setState({ updating: this.state.updating + 1 });
MatrixClientPeg.get().setPowerLevel(roomId, target, parseInt(powerLevel), powerLevelEvent).then(
2016-03-21 00:49:18 +00:00
function() {
// NO-OP; rely on the m.room.member event coming down else we could
// get out of sync if we force setState here!
console.log("Power change success");
}, function(err) {
Modal.createDialog(ErrorDialog, {
title: "Failure to change power level",
description: err.message
});
}
).finally(()=>{
this.setState({ updating: this.state.updating - 1 });
});
2016-03-21 00:49:18 +00:00
this.props.onFinished();
},
2016-01-18 01:18:02 +00:00
onPowerChange: function(powerLevel) {
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
var roomId = this.props.member.roomId;
var target = this.props.member.userId;
var room = MatrixClientPeg.get().getRoom(roomId);
2016-03-21 00:49:18 +00:00
var self = this;
2016-01-18 01:18:02 +00:00
if (!room) {
this.props.onFinished();
return;
}
var powerLevelEvent = room.currentState.getStateEvents(
"m.room.power_levels", ""
);
if (!powerLevelEvent) {
this.props.onFinished();
return;
}
2016-03-21 00:49:18 +00:00
if (powerLevelEvent.getContent().users) {
var myPower = powerLevelEvent.getContent().users[MatrixClientPeg.get().credentials.userId];
if (parseInt(myPower) === parseInt(powerLevel)) {
var QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
Modal.createDialog(QuestionDialog, {
title: "Warning",
description:
<div>
You will not be able to undo this change as you are promoting the user to have the same power level as yourself.<br/>
Are you sure?
</div>,
button: "Continue",
onFinished: function(confirmed) {
if (confirmed) {
self._applyPowerChange(roomId, target, powerLevel, powerLevelEvent);
}
else {
self.props.onFinished();
}
},
});
}
2016-03-21 00:49:18 +00:00
else {
this._applyPowerChange(roomId, target, powerLevel, powerLevelEvent);
}
}
else {
this._applyPowerChange(roomId, target, powerLevel, powerLevelEvent);
}
},
2016-01-18 01:18:02 +00:00
2015-09-18 17:39:16 +00:00
onChatClick: function() {
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
// TODO: keep existingOneToOneRoomId updated if we see any room member changes anywhere
var useExistingOneToOneRoom = this.state.existingOneToOneRoomId && (this.state.existingOneToOneRoomId !== this.props.member.roomId);
2015-09-18 17:39:16 +00:00
// check if there are any existing rooms with just us and them (1:1)
// If so, just view that room. If not, create a private room with them.
if (this.state.existingOneToOneRoomId) {
2015-09-18 17:39:16 +00:00
dis.dispatch({
action: 'view_room',
room_id: this.state.existingOneToOneRoomId,
2015-09-18 17:39:16 +00:00
});
this.props.onFinished();
2015-09-18 17:39:16 +00:00
}
else {
self.setState({ updating: self.state.updating + 1 });
createRoom({
createOpts: {
invite: [this.props.member.userId],
},
}).finally(function() {
self.props.onFinished();
self.setState({ updating: self.state.updating - 1 });
}).done();
2015-09-18 17:39:16 +00:00
}
2015-09-22 15:37:39 +00:00
},
onLeaveClick: function() {
dis.dispatch({
action: 'leave_room',
room_id: this.props.member.roomId,
2015-09-22 15:37:39 +00:00
});
2016-06-01 22:42:34 +00:00
this.props.onFinished();
2015-09-18 17:39:16 +00:00
},
_calculateOpsPermissions: function(member) {
2015-09-18 17:39:16 +00:00
var defaultPerms = {
can: {},
muted: false,
modifyLevel: false
};
var room = MatrixClientPeg.get().getRoom(member.roomId);
2015-09-18 17:39:16 +00:00
if (!room) {
return defaultPerms;
}
var powerLevels = room.currentState.getStateEvents(
"m.room.power_levels", ""
);
if (!powerLevels) {
return defaultPerms;
}
var me = room.getMember(MatrixClientPeg.get().credentials.userId);
if (!me) {
return defaultPerms;
}
var them = member;
2015-09-18 17:39:16 +00:00
return {
can: this._calculateCanPermissions(
me, them, powerLevels.getContent()
),
muted: this._isMuted(them, powerLevels.getContent()),
isTargetMod: them.powerLevel > powerLevels.getContent().users_default
};
},
_calculateCanPermissions: function(me, them, powerLevels) {
var can = {
kick: false,
ban: false,
mute: false,
modifyLevel: false
};
var canAffectUser = them.powerLevel < me.powerLevel;
if (!canAffectUser) {
//console.log("Cannot affect user: %s >= %s", them.powerLevel, me.powerLevel);
return can;
}
var editPowerLevel = (
(powerLevels.events ? powerLevels.events["m.room.power_levels"] : null) ||
powerLevels.state_default
);
2016-01-18 01:40:19 +00:00
var levelToSend = (
(powerLevels.events ? powerLevels.events["m.room.message"] : null) ||
powerLevels.events_default
);
2015-09-18 17:39:16 +00:00
can.kick = me.powerLevel >= powerLevels.kick;
can.ban = me.powerLevel >= powerLevels.ban;
can.mute = me.powerLevel >= editPowerLevel;
2016-01-18 01:40:19 +00:00
can.toggleMod = me.powerLevel > them.powerLevel && them.powerLevel >= levelToSend;
2015-09-18 17:39:16 +00:00
can.modifyLevel = me.powerLevel > them.powerLevel;
return can;
},
_isMuted: function(member, powerLevelContent) {
if (!powerLevelContent || !member) {
return false;
}
var levelToSend = (
(powerLevelContent.events ? powerLevelContent.events["m.room.message"] : null) ||
powerLevelContent.events_default
);
return member.powerLevel < levelToSend;
2015-11-26 17:49:39 +00:00
},
onCancel: function(e) {
dis.dispatch({
action: "view_user",
member: null
});
},
onMemberAvatarClick: function () {
var avatarUrl = this.props.member.user.avatarUrl;
if(!avatarUrl) return;
2016-04-03 23:18:18 +00:00
var httpUrl = MatrixClientPeg.get().mxcUrlToHttp(avatarUrl);
var ImageView = sdk.getComponent("elements.ImageView");
var params = {
src: httpUrl,
name: this.props.member.name
};
Modal.createDialog(ImageView, params, "mx_Dialog_lightbox");
},
_renderDevices: function() {
if (!UserSettingsStore.isFeatureEnabled("e2e_encryption")) {
return null;
}
var devices = this.state.devices;
var MemberDeviceInfo = sdk.getComponent('rooms.MemberDeviceInfo');
var Spinner = sdk.getComponent("elements.Spinner");
var devComponents;
if (this.state.devicesLoading) {
// still loading
devComponents = <Spinner />;
} else if (devices === null) {
devComponents = "Unable to load device list";
} else if (devices.length === 0) {
devComponents = "No registered devices";
} else {
devComponents = [];
for (var i = 0; i < devices.length; i++) {
devComponents.push(<MemberDeviceInfo key={i}
userId={this.props.member.userId}
device={devices[i]}/>);
}
}
return (
<div>
<h3>Devices</h3>
<div className="mx_MemberInfo_devices">
{devComponents}
</div>
</div>
);
},
2015-11-26 17:49:39 +00:00
render: function() {
2016-01-18 01:18:02 +00:00
var startChat, kickButton, banButton, muteButton, giveModButton, spinner;
if (this.props.member.userId !== MatrixClientPeg.get().credentials.userId) {
// FIXME: we're referring to a vector component from react-sdk
var BottomLeftMenuTile = sdk.getComponent('rooms.BottomLeftMenuTile');
var label;
if (this.state.existingOneToOneRoomId) {
if (this.state.existingOneToOneRoomId == this.props.member.roomId) {
label = "Start new direct chat";
}
else {
label = "Go to direct chat";
}
}
else {
label = "Start direct chat";
}
startChat = <BottomLeftMenuTile collapsed={ false } img="img/create-big.svg"
label={ label } onClick={ this.onChatClick }/>
2015-11-26 17:49:39 +00:00
}
if (this.state.updating) {
2015-11-26 17:49:39 +00:00
var Loader = sdk.getComponent("elements.Spinner");
spinner = <Loader imgClassName="mx_ContextualMenu_spinner"/>;
}
if (this.state.can.kick) {
kickButton = <div className="mx_MemberInfo_field" onClick={this.onKick}>
{ this.props.member.membership === "invite" ? "Disinvite" : "Kick" }
2015-11-26 17:49:39 +00:00
</div>;
}
if (this.state.can.ban) {
banButton = <div className="mx_MemberInfo_field" onClick={this.onBan}>
Ban
</div>;
}
if (this.state.can.mute) {
var muteLabel = this.state.muted ? "Unmute" : "Mute";
muteButton = <div className="mx_MemberInfo_field" onClick={this.onMuteToggle}>
{muteLabel}
</div>;
}
2016-01-18 01:40:19 +00:00
if (this.state.can.toggleMod) {
2016-01-18 01:18:02 +00:00
var giveOpLabel = this.state.isTargetMod ? "Revoke Moderator" : "Make Moderator";
2015-11-26 17:49:39 +00:00
giveModButton = <div className="mx_MemberInfo_field" onClick={this.onModToggle}>
{giveOpLabel}
</div>
}
2016-01-18 01:26:15 +00:00
// TODO: we should have an invite button if this MemberInfo is showing a user who isn't actually in the current room yet
// e.g. clicking on a linkified userid in a room
2016-01-18 01:18:02 +00:00
var adminTools;
if (kickButton || banButton || muteButton || giveModButton) {
adminTools =
2016-01-18 01:18:02 +00:00
<div>
<h3>Admin tools</h3>
<div className="mx_MemberInfo_buttons">
{muteButton}
{kickButton}
{banButton}
{giveModButton}
</div>
</div>
}
2016-07-05 04:54:18 +00:00
let memberNameHTML = emojifyText(this.props.member.name);
2015-11-26 17:49:39 +00:00
var MemberAvatar = sdk.getComponent('avatars.MemberAvatar');
2016-01-18 01:18:02 +00:00
var PowerSelector = sdk.getComponent('elements.PowerSelector');
2015-11-26 17:49:39 +00:00
return (
<div className="mx_MemberInfo">
2015-12-06 22:07:49 +00:00
<img className="mx_MemberInfo_cancel" src="img/cancel.svg" width="18" height="18" onClick={this.onCancel}/>
2015-11-26 17:49:39 +00:00
<div className="mx_MemberInfo_avatar">
<MemberAvatar onClick={this.onMemberAvatarClick} member={this.props.member} width={48} height={48} />
2015-11-26 17:49:39 +00:00
</div>
2016-01-18 01:18:02 +00:00
2016-07-05 04:54:18 +00:00
<h2 dangerouslySetInnerHTML={memberNameHTML}></h2>
2016-01-18 01:18:02 +00:00
<div className="mx_MemberInfo_profile">
<div className="mx_MemberInfo_profileField">
{ this.props.member.userId }
</div>
<div className="mx_MemberInfo_profileField">
Level: <b><PowerSelector controlled={true} value={ parseInt(this.props.member.powerLevel) } disabled={ !this.state.can.modifyLevel } onChange={ this.onPowerChange }/></b>
2016-01-18 01:18:02 +00:00
</div>
2015-11-26 17:49:39 +00:00
</div>
2016-01-18 01:18:02 +00:00
{ startChat }
{ this._renderDevices() }
2016-01-18 01:18:02 +00:00
{ adminTools }
{ spinner }
2015-11-26 17:49:39 +00:00
</div>
);
2015-09-18 17:39:16 +00:00
}
2015-11-26 17:49:39 +00:00
});