Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
pull/21833/head
Michael Telatynski 2019-10-17 19:18:14 +01:00
parent 93429d7c2e
commit 7e4d429fa3
1 changed files with 103 additions and 99 deletions

View File

@ -532,114 +532,118 @@ const BanToggleButton = withLegacyMatrixClient(({matrixClient: cli, member, star
</AccessibleButton>; </AccessibleButton>;
}); });
const MuteToggleButton = withLegacyMatrixClient(({matrixClient: cli, member, room, powerLevels, startUpdating, stopUpdating}) => { const MuteToggleButton = withLegacyMatrixClient(
const isMuted = _isMuted(member, powerLevels); ({matrixClient: cli, member, room, powerLevels, startUpdating, stopUpdating}) => {
const onMuteToggle = async () => { const isMuted = _isMuted(member, powerLevels);
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); const onMuteToggle = async () => {
const roomId = member.roomId; const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
const target = member.userId; const roomId = member.roomId;
const target = member.userId;
// if muting self, warn as it may be irreversible // if muting self, warn as it may be irreversible
if (target === cli.getUserId()) { if (target === cli.getUserId()) {
try { try {
if (!(await _warnSelfDemote())) return; if (!(await _warnSelfDemote())) return;
} catch (e) { } catch (e) {
console.error("Failed to warn about self demotion: ", e); console.error("Failed to warn about self demotion: ", e);
return; return;
}
} }
}
const powerLevelEvent = room.currentState.getStateEvents("m.room.power_levels", ""); const powerLevelEvent = room.currentState.getStateEvents("m.room.power_levels", "");
if (!powerLevelEvent) return; if (!powerLevelEvent) return;
const powerLevels = powerLevelEvent.getContent(); const powerLevels = powerLevelEvent.getContent();
const levelToSend = ( const levelToSend = (
(powerLevels.events ? powerLevels.events["m.room.message"] : null) || (powerLevels.events ? powerLevels.events["m.room.message"] : null) ||
powerLevels.events_default powerLevels.events_default
); );
let level; let level;
if (isMuted) { // unmute if (isMuted) { // unmute
level = levelToSend; level = levelToSend;
} else { // mute } else { // mute
level = levelToSend - 1; level = levelToSend - 1;
} }
level = parseInt(level); level = parseInt(level);
if (!isNaN(level)) { if (!isNaN(level)) {
startUpdating(); startUpdating();
cli.setPowerLevel(roomId, target, level, powerLevelEvent).then(() => { cli.setPowerLevel(roomId, target, level, powerLevelEvent).then(() => {
// NO-OP; rely on the m.room.member event coming down else we could // NO-OP; rely on the m.room.member event coming down else we could
// get out of sync if we force setState here! // get out of sync if we force setState here!
console.log("Mute toggle success"); console.log("Mute toggle success");
}, function(err) { }, function(err) {
console.error("Mute error: " + err); console.error("Mute error: " + err);
Modal.createTrackedDialog('Failed to mute user', '', ErrorDialog, { Modal.createTrackedDialog('Failed to mute user', '', ErrorDialog, {
title: _t("Error"), title: _t("Error"),
description: _t("Failed to mute user"), description: _t("Failed to mute user"),
});
}).finally(() => {
stopUpdating();
}); });
}).finally(() => { }
stopUpdating(); };
});
const muteLabel = isMuted ? _t("Unmute") : _t("Mute");
return <AccessibleButton className="mx_UserInfo_field" onClick={onMuteToggle}>
{ muteLabel }
</AccessibleButton>;
},
);
const RoomAdminToolsContainer = withLegacyMatrixClient(
({matrixClient: cli, room, children, member, startUpdating, stopUpdating}) => {
let kickButton;
let banButton;
let muteButton;
let redactButton;
const powerLevels = useRoomPowerLevels(room);
const editPowerLevel = (
(powerLevels.events ? powerLevels.events["m.room.power_levels"] : null) ||
powerLevels.state_default
);
const me = room.getMember(cli.getUserId());
const isMe = me.userId === member.userId;
const canAffectUser = member.powerLevel < me.powerLevel || isMe;
if (canAffectUser && me.powerLevel >= powerLevels.kick) {
kickButton = <RoomKickButton member={member} startUpdating={startUpdating} stopUpdating={stopUpdating} />;
}
if (me.powerLevel >= powerLevels.redact) {
redactButton = (
<RedactMessagesButton member={member} startUpdating={startUpdating} stopUpdating={stopUpdating} />
);
}
if (canAffectUser && me.powerLevel >= powerLevels.ban) {
banButton = <BanToggleButton member={member} startUpdating={startUpdating} stopUpdating={stopUpdating} />;
}
if (canAffectUser && me.powerLevel >= editPowerLevel) {
muteButton = (
<MuteToggleButton
member={member}
room={room}
powerLevels={powerLevels}
startUpdating={startUpdating}
stopUpdating={stopUpdating}
/>
);
} }
};
const muteLabel = isMuted ? _t("Unmute") : _t("Mute"); if (kickButton || banButton || muteButton || redactButton || children) {
return <AccessibleButton className="mx_UserInfo_field" onClick={onMuteToggle}> return <GenericAdminToolsContainer>
{ muteLabel } { muteButton }
</AccessibleButton>; { kickButton }
}); { banButton }
{ redactButton }
{ children }
</GenericAdminToolsContainer>;
}
const RoomAdminToolsContainer = withLegacyMatrixClient(({matrixClient: cli, room, children, member, startUpdating, stopUpdating}) => { return <div />;
let kickButton; },
let banButton; );
let muteButton;
let redactButton;
const powerLevels = useRoomPowerLevels(room);
const editPowerLevel = (
(powerLevels.events ? powerLevels.events["m.room.power_levels"] : null) ||
powerLevels.state_default
);
const me = room.getMember(cli.getUserId());
const isMe = me.userId === member.userId;
const canAffectUser = member.powerLevel < me.powerLevel || isMe;
if (canAffectUser && me.powerLevel >= powerLevels.kick) {
kickButton = <RoomKickButton member={member} startUpdating={startUpdating} stopUpdating={stopUpdating} />;
}
if (me.powerLevel >= powerLevels.redact) {
redactButton = (
<RedactMessagesButton member={member} startUpdating={startUpdating} stopUpdating={stopUpdating} />
);
}
if (canAffectUser && me.powerLevel >= powerLevels.ban) {
banButton = <BanToggleButton member={member} startUpdating={startUpdating} stopUpdating={stopUpdating} />;
}
if (canAffectUser && me.powerLevel >= editPowerLevel) {
muteButton = (
<MuteToggleButton
member={member}
room={room}
powerLevels={powerLevels}
startUpdating={startUpdating}
stopUpdating={stopUpdating}
/>
);
}
if (kickButton || banButton || muteButton || redactButton || children) {
return <GenericAdminToolsContainer>
{ muteButton }
{ kickButton }
{ banButton }
{ redactButton }
{ children }
</GenericAdminToolsContainer>;
}
return <div />;
});
const GroupAdminToolsSection = withLegacyMatrixClient( const GroupAdminToolsSection = withLegacyMatrixClient(
({matrixClient: cli, children, groupId, groupMember, startUpdating, stopUpdating}) => { ({matrixClient: cli, children, groupId, groupMember, startUpdating, stopUpdating}) => {