/* Copyright 2015, 2016 OpenMarket 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. */ /* * State vars: * 'can': { * kick: boolean, * ban: boolean, * mute: boolean, * modifyLevel: boolean * }, * 'muted': boolean, * 'isTargetMod': boolean */ 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'); import {emojifyText} from '../../../HtmlUtils'; module.exports = React.createClass({ displayName: 'MemberInfo', 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, } }, 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}); }); }, onKick: function() { var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); 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 }); }); this.props.onFinished(); }, onBan: function() { var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); 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 }); }); this.props.onFinished(); }, onMuteToggle: function() { 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; } 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; } level = parseInt(level); if (level !== NaN) { this.setState({ updating: this.state.updating + 1 }); MatrixClientPeg.get().setPowerLevel(roomId, target, level, 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("Mute toggle success"); }, function(err) { Modal.createDialog(ErrorDialog, { title: "Mute error", description: err.message }); } ).finally(()=>{ this.setState({ updating: this.state.updating - 1 }); }); } this.props.onFinished(); }, onModToggle: function() { 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; } var me = room.getMember(MatrixClientPeg.get().credentials.userId); if (!me) { this.props.onFinished(); return; } var defaultLevel = powerLevelEvent.getContent().users_default; var modLevel = me.powerLevel - 1; if (modLevel > 50 && defaultLevel < 50) modLevel = 50; // try to stick with the vector level defaults // 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) { 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", description: err.message }); } } ).finally(()=>{ this.setState({ updating: this.state.updating - 1 }); }); this.props.onFinished(); }, _applyPowerChange: function(roomId, target, powerLevel, powerLevelEvent) { this.setState({ updating: this.state.updating + 1 }); MatrixClientPeg.get().setPowerLevel(roomId, target, parseInt(powerLevel), 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("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 }); }); this.props.onFinished(); }, 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); var self = this; if (!room) { this.props.onFinished(); return; } var powerLevelEvent = room.currentState.getStateEvents( "m.room.power_levels", "" ); if (!powerLevelEvent) { this.props.onFinished(); return; } 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: