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,39 +117,42 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
text: "Ignorelist is empty",
})
);
} else {
const chanName = "Ignored users";
const ignored = network.ignoreList.map((data) => ({
hostmask: `${data.nick}!${data.ident}@${data.hostname}`,
when: data.when,
}));
let newChan = network.getChannel(chanName);
if (typeof newChan === "undefined") {
newChan = client.createChannel({
type: ChanType.SPECIAL,
special: SpecialChanType.IGNORELIST,
name: chanName,
data: ignored,
});
client.emit("join", {
network: network.uuid,
chan: newChan.getFilteredClone(true),
shouldOpen: false,
index: network.addChannel(newChan),
});
} else {
// TODO: add type for this chan/event
newChan.data = ignored;
client.emit("msg:special", {
chan: newChan.id,
data: ignored,
});
}
return;
}
const chanName = "Ignored users";
const ignored = network.ignoreList.map((data) => ({
hostmask: `${data.nick}!${data.ident}@${data.hostname}`,
when: data.when,
}));
let newChan = network.getChannel(chanName);
if (typeof newChan === "undefined") {
newChan = client.createChannel({
type: ChanType.SPECIAL,
special: SpecialChanType.IGNORELIST,
name: chanName,
data: ignored,
});
client.emit("join", {
network: network.uuid,
chan: newChan.getFilteredClone(true),
shouldOpen: false,
index: network.addChannel(newChan),
});
return;
}
// TODO: add type for this chan/event
newChan.data = ignored;
client.emit("msg:special", {
chan: newChan.id,
data: ignored,
});
break;
}
}
};