Merge pull request #4176 from mitaka8/feature-admin-actions

Show give/revoke modes and kick on other modes than +o
This commit is contained in:
Max Leiter 2021-03-30 22:10:54 -07:00 committed by GitHub
commit 4f6659897f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 38 deletions

View File

@ -352,8 +352,8 @@ p {
.context-menu-action-whois::before { content: "\f05a"; /* http://fontawesome.io/icon/info-circle/ */ }
.context-menu-action-ignore::before { content: "\f506"; /* https://fontawesome.com/icons/user-slash?style=solid */ }
.context-menu-action-kick::before { content: "\f05e"; /* http://fontawesome.io/icon/ban/ */ }
.context-menu-action-op::before { content: "\f1fa"; /* http://fontawesome.io/icon/at/ */ }
.context-menu-action-voice::before { content: "\f067"; /* http://fontawesome.io/icon/plus/ */ }
.context-menu-action-set-mode::before { content: "\f067"; /* http://fontawesome.io/icon/plus/ */ }
.context-menu-action-revoke-mode::before { content: "\f068"; /* http://fontawesome.io/icon/minus/ */ }
.context-menu-network::before { content: "\f233"; /* https://fontawesome.com/icons/server?style=solid */ }
.context-menu-edit::before { content: "\f303"; /* https://fontawesome.com/icons/pencil-alt?style=solid */ }
.context-menu-clear-history::before { content: "\f1f8"; /* https://fontawesome.com/icons/trash?style=solid */ }

View File

@ -185,7 +185,6 @@ export function generateChannelContextMenu($root, channel, network) {
export function generateUserContextMenu($root, channel, network, user) {
const currentChannelUser = channel.users.find((u) => u.nick === network.nick) || {};
const currentChannelModes = currentChannelUser.modes || [];
const whois = () => {
const chan = $root.$store.getters.findChannelOnCurrentNetwork(user.nick);
@ -246,7 +245,85 @@ export function generateUserContextMenu($root, channel, network, user) {
},
];
if (currentChannelModes.includes("@")) {
// Bail because we don't have a special mode.
if (currentChannelUser.modes.length < 1) {
return items;
}
// Names of the modes we are able to change
const modes = {
"~": ["owner", "q"],
"&": ["admin", "a"],
"@": ["operator", "o"],
"%": ["half-op", "h"],
"+": ["voice", "v"],
};
// Labels for the mode changes. For example .rev(['admin', 'a']) => 'Revoke admin (-a)'
const modeTextTemplate = {
revoke: (m) => `Revoke ${m[0]} (-${m[1]})`,
give: (m) => `Give ${m[0]} (+${m[1]})`,
};
const networkModes = network.serverOptions.PREFIX;
/**
* Determine whether the prefix of mode p1 has access to perform actions on p2.
*
* EXAMPLE:
* compare('@', '@') => true
* compare('&', '@') => true
* compare('+', '~') => false
* @param {string} p1 The mode performing an action
* @param {string} p2 The target mode
*
* @return {boolean} whether p1 can perform an action on p2
*/
function compare(p1, p2) {
// The modes ~ and @ can perform actions on their own mode. The others on modes below.
return "~@".indexOf(p1) > -1
? networkModes.indexOf(p1) <= networkModes.indexOf(p2)
: networkModes.indexOf(p1) < networkModes.indexOf(p2);
}
networkModes.forEach((prefix) => {
if (!compare(currentChannelUser.modes[0], prefix)) {
// Our highest mode is below the current mode. Bail.
return;
}
if (!user.modes.includes(prefix)) {
// The target doesn't already have this mode, therefore we can set it.
items.push({
label: modeTextTemplate.give(modes[prefix]),
type: "item",
class: "action-set-mode",
action() {
socket.emit("input", {
target: channel.id,
text: "/mode +" + modes[prefix][1] + " " + user.nick,
});
},
});
} else {
items.push({
label: modeTextTemplate.revoke(modes[prefix]),
type: "item",
class: "action-revoke-mode",
action() {
socket.emit("input", {
target: channel.id,
text: "/mode -" + modes[prefix][1] + " " + user.nick,
});
},
});
}
});
// Determine if we are half-op or op depending on the network modes so we can kick.
if (!compare(networkModes.indexOf("%") > -1 ? "%" : "@", currentChannelUser.modes[0])) {
if (user.modes.length === 0 || compare(currentChannelUser.modes[0], user.modes[0])) {
// Check if the target user has no mode or a mode lower than ours.
items.push({
label: "Kick",
type: "item",
@ -258,57 +335,6 @@ export function generateUserContextMenu($root, channel, network, user) {
});
},
});
if (user.modes.includes("@")) {
items.push({
label: "Revoke operator (-o)",
type: "item",
class: "action-op",
action() {
socket.emit("input", {
target: channel.id,
text: "/deop " + user.nick,
});
},
});
} else {
items.push({
label: "Give operator (+o)",
type: "item",
class: "action-op",
action() {
socket.emit("input", {
target: channel.id,
text: "/op " + user.nick,
});
},
});
}
if (user.modes.includes("+")) {
items.push({
label: "Revoke voice (-v)",
type: "item",
class: "action-voice",
action() {
socket.emit("input", {
target: channel.id,
text: "/devoice " + user.nick,
});
},
});
} else {
items.push({
label: "Give voice (+v)",
type: "item",
class: "action-voice",
action() {
socket.emit("input", {
target: channel.id,
text: "/voice " + user.nick,
});
},
});
}
}