mirror of
https://github.com/thelounge/thelounge
synced 2024-11-22 03:53:08 +00:00
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:
parent
b8a9fe08ab
commit
5274fdc21a
1 changed files with 70 additions and 65 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue