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');
|
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
|
|
|
|
2015-11-30 14:14:30 +00:00
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
onFinished: function() {}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-09-18 17:39:16 +00:00
|
|
|
componentDidMount: function() {
|
|
|
|
// work out the current state
|
|
|
|
if (this.props.member) {
|
2015-12-04 16:15:55 +00:00
|
|
|
var memberState = this._calculateOpsPermissions(this.props.member);
|
2015-09-18 17:39:16 +00:00
|
|
|
this.setState(memberState);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-04 16:15:55 +00:00
|
|
|
componentWillReceiveProps: function(newProps) {
|
|
|
|
var memberState = this._calculateOpsPermissions(newProps.member);
|
|
|
|
this.setState(memberState);
|
|
|
|
},
|
|
|
|
|
2015-09-18 17:39:16 +00:00
|
|
|
onKick: function() {
|
2015-11-30 14:11:04 +00:00
|
|
|
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;
|
|
|
|
MatrixClientPeg.get().kick(roomId, target).done(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
|
|
|
|
});
|
|
|
|
});
|
2015-09-22 15:37:39 +00:00
|
|
|
this.props.onFinished();
|
2015-09-18 17:39:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onBan: function() {
|
2015-11-30 14:11:04 +00:00
|
|
|
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;
|
|
|
|
MatrixClientPeg.get().ban(roomId, target).done(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
|
|
|
|
});
|
|
|
|
});
|
2015-09-22 15:37:39 +00:00
|
|
|
this.props.onFinished();
|
2015-09-18 17:39:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onMuteToggle: function() {
|
2015-11-30 14:11:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
MatrixClientPeg.get().setPowerLevel(roomId, target, level, powerLevelEvent).done(
|
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
2015-09-22 15:37:39 +00:00
|
|
|
this.props.onFinished();
|
2015-09-18 17:39:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onModToggle: function() {
|
2015-11-30 14:11:04 +00:00
|
|
|
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;
|
|
|
|
MatrixClientPeg.get().setPowerLevel(roomId, target, newLevel, powerLevelEvent).done(
|
|
|
|
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) {
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
title: "Mod error",
|
|
|
|
description: err.message
|
|
|
|
});
|
|
|
|
});
|
2015-09-22 15:37:39 +00:00
|
|
|
this.props.onFinished();
|
2015-09-18 17:39:16 +00:00
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
if (!room) {
|
|
|
|
this.props.onFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var powerLevelEvent = room.currentState.getStateEvents(
|
|
|
|
"m.room.power_levels", ""
|
|
|
|
);
|
|
|
|
if (!powerLevelEvent) {
|
|
|
|
this.props.onFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MatrixClientPeg.get().setPowerLevel(roomId, target, powerLevel, powerLevelEvent).done(
|
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.props.onFinished();
|
|
|
|
},
|
|
|
|
|
2015-09-18 17:39:16 +00:00
|
|
|
onChatClick: function() {
|
|
|
|
// 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.
|
2015-10-27 17:01:03 +00:00
|
|
|
var self = this;
|
2015-09-18 17:39:16 +00:00
|
|
|
var rooms = MatrixClientPeg.get().getRooms();
|
|
|
|
var userIds = [
|
|
|
|
this.props.member.userId,
|
|
|
|
MatrixClientPeg.get().credentials.userId
|
|
|
|
];
|
|
|
|
var existingRoomId = null;
|
|
|
|
for (var i = 0; i < rooms.length; i++) {
|
|
|
|
var members = rooms[i].getJoinedMembers();
|
|
|
|
if (members.length === 2) {
|
|
|
|
var hasTargetUsers = true;
|
|
|
|
for (var j = 0; j < members.length; j++) {
|
|
|
|
if (userIds.indexOf(members[j].userId) === -1) {
|
|
|
|
hasTargetUsers = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasTargetUsers) {
|
|
|
|
existingRoomId = rooms[i].roomId;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existingRoomId) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: existingRoomId
|
|
|
|
});
|
2015-10-27 17:01:03 +00:00
|
|
|
this.props.onFinished();
|
2015-09-18 17:39:16 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-10-27 17:01:03 +00:00
|
|
|
self.setState({ creatingRoom: true });
|
2015-09-18 17:39:16 +00:00
|
|
|
MatrixClientPeg.get().createRoom({
|
|
|
|
invite: [this.props.member.userId],
|
|
|
|
preset: "private_chat"
|
|
|
|
}).done(function(res) {
|
2015-10-27 17:01:03 +00:00
|
|
|
self.setState({ creatingRoom: false });
|
2015-09-18 17:39:16 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: res.room_id
|
|
|
|
});
|
2015-10-27 17:01:03 +00:00
|
|
|
self.props.onFinished();
|
2015-09-18 17:39:16 +00:00
|
|
|
}, function(err) {
|
2015-10-27 17:01:03 +00:00
|
|
|
self.setState({ creatingRoom: false });
|
2015-09-18 17:39:16 +00:00
|
|
|
console.error(
|
|
|
|
"Failed to create room: %s", JSON.stringify(err)
|
|
|
|
);
|
2015-10-27 17:01:03 +00:00
|
|
|
self.props.onFinished();
|
2015-09-18 17:39:16 +00:00
|
|
|
});
|
|
|
|
}
|
2015-09-22 15:37:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onLeaveClick: function() {
|
2015-12-13 13:49:28 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'leave_room',
|
|
|
|
room_id: this.props.member.roomId,
|
2015-09-22 15:37:39 +00:00
|
|
|
});
|
|
|
|
this.props.onFinished();
|
2015-09-18 17:39:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
can: {
|
|
|
|
kick: false,
|
|
|
|
ban: false,
|
|
|
|
mute: false,
|
|
|
|
modifyLevel: false
|
|
|
|
},
|
|
|
|
muted: false,
|
2015-10-27 17:01:03 +00:00
|
|
|
isTargetMod: false,
|
|
|
|
creatingRoom: false
|
2015-09-18 17:39:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-04 16:15:55 +00:00
|
|
|
_calculateOpsPermissions: function(member) {
|
2015-09-18 17:39:16 +00:00
|
|
|
var defaultPerms = {
|
|
|
|
can: {},
|
|
|
|
muted: false,
|
|
|
|
modifyLevel: false
|
|
|
|
};
|
2015-12-04 16:15:55 +00:00
|
|
|
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);
|
2015-12-04 16:15:55 +00:00
|
|
|
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
|
|
|
|
);
|
|
|
|
can.kick = me.powerLevel >= powerLevels.kick;
|
|
|
|
can.ban = me.powerLevel >= powerLevels.ban;
|
|
|
|
can.mute = me.powerLevel >= editPowerLevel;
|
|
|
|
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
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
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');
|
|
|
|
startChat = <BottomLeftMenuTile collapsed={ false } img="img/create-big.svg" label="Start chat" onClick={ this.onChatClick }/>
|
2015-11-26 17:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.creatingRoom) {
|
|
|
|
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}>
|
|
|
|
Kick
|
|
|
|
</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>;
|
|
|
|
}
|
|
|
|
if (this.state.can.modifyLevel) {
|
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 =
|
|
|
|
<div>
|
|
|
|
<h3>Admin tools</h3>
|
|
|
|
|
|
|
|
<div className="mx_MemberInfo_buttons">
|
|
|
|
{muteButton}
|
|
|
|
{kickButton}
|
|
|
|
{banButton}
|
|
|
|
{giveModButton}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
|
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 member={this.props.member} width={48} height={48} />
|
|
|
|
</div>
|
2016-01-18 01:18:02 +00:00
|
|
|
|
2015-11-26 17:49:39 +00:00
|
|
|
<h2>{ this.props.member.name }</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 value={ parseInt(this.props.member.powerLevel) } disabled={ !this.state.can.modifyLevel } onChange={ this.onPowerChange }/></b>
|
|
|
|
</div>
|
2015-11-26 17:49:39 +00:00
|
|
|
</div>
|
2016-01-18 01:18:02 +00:00
|
|
|
|
|
|
|
{ startChat }
|
|
|
|
|
|
|
|
{ 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
|
|
|
});
|
2015-09-18 17:39:16 +00:00
|
|
|
|