ignore: keep happy path on the left

It is much easier to follow the control flow if error checks
are done on the indented path, immediately returning.
This commit is contained in:
Reto Brunner 2024-04-14 13:33:22 +02:00
parent b8a9fe08ab
commit 5274fdc21a

View file

@ -42,25 +42,14 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
text: "You can't ignore yourself",
})
);
} else if (
!network.ignoreList.some(function (entry) {
return;
}
if (
network.ignoreList.some(function (entry) {
return Helper.compareHostmask(entry, hostmask!);
})
) {
hostmask!.when = Date.now();
network.ignoreList.push(hostmask!);
client.save();
chan.pushMessage(
client,
new Msg({
type: MessageType.ERROR,
text: `\u0002${hostmask!.nick}!${hostmask!.ident}@${
hostmask!.hostname
}\u000f added to ignorelist`,
})
);
} else {
chan.pushMessage(
client,
new Msg({
@ -68,9 +57,23 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
text: "The specified user/hostmask is already ignored",
})
);
return;
}
break;
hostmask!.when = Date.now();
network.ignoreList.push(hostmask!);
client.save();
chan.pushMessage(
client,
new Msg({
type: MessageType.ERROR, // TODO: Successfully added via type.Error 🤔 ?
text: `\u0002${hostmask!.nick}!${hostmask!.ident}@${
hostmask!.hostname
}\u000f added to ignorelist`,
})
);
return;
}
case "unignore": {
@ -78,22 +81,7 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
return Helper.compareHostmask(entry, hostmask!);
});
// Check if the entry exists before removing it, otherwise
// let the user know.
if (idx !== -1) {
network.ignoreList.splice(idx, 1);
client.save();
chan.pushMessage(
client,
new Msg({
type: MessageType.ERROR,
text: `Successfully removed \u0002${hostmask!.nick}!${hostmask!.ident}@${
hostmask!.hostname
}\u000f from ignorelist`,
})
);
} else {
if (idx === -1) {
chan.pushMessage(
client,
new Msg({
@ -101,12 +89,26 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
text: "The specified user/hostmask is not ignored",
})
);
return;
}
break;
network.ignoreList.splice(idx, 1);
client.save();
chan.pushMessage(
client,
new Msg({
type: MessageType.ERROR, // TODO: Successfully removed via type.Error 🤔 ?
text: `Successfully removed \u0002${hostmask!.nick}!${hostmask!.ident}@${
hostmask!.hostname
}\u000f from ignorelist`,
})
);
return;
}
case "ignorelist":
case "ignorelist": {
if (network.ignoreList.length === 0) {
chan.pushMessage(
client,
@ -115,7 +117,9 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
text: "Ignorelist is empty",
})
);
} else {
return;
}
const chanName = "Ignored users";
const ignored = network.ignoreList.map((data) => ({
hostmask: `${data.nick}!${data.ident}@${data.hostname}`,
@ -136,7 +140,9 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
shouldOpen: false,
index: network.addChannel(newChan),
});
} else {
return;
}
// TODO: add type for this chan/event
newChan.data = ignored;
@ -144,11 +150,10 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
chan: newChan.id,
data: ignored,
});
}
}
break;
}
}
};
export default {