refactor to make code paths easier to follow

Meaning:
* keep the happy path to the left, test failure conditions first
* extract helpers where needed
This commit is contained in:
Reto Brunner 2022-04-27 21:06:01 +02:00
parent 666cb48b15
commit e83033dd44

View file

@ -6,11 +6,12 @@ const Helper = require("../../helper");
exports.commands = ["ignore", "unignore", "ignorelist"];
const IGNORELIST_CHAN_NAME = "Ignored users";
exports.input = function (network, chan, cmd, args) {
const client = this;
let target;
let hostmask;
let create_new_ignored_window = false;
function emitError(msg) {
chan.pushMessage(
@ -38,23 +39,23 @@ exports.input = function (network, chan, cmd, args) {
// IRC nicks are case insensitive
if (hostmask.nick.toLowerCase() === network.nick.toLowerCase()) {
emitError("You can't ignore yourself");
} else if (
!network.ignoreList.some(function (entry) {
return Helper.compareHostmask(entry, hostmask);
})
) {
hostmask.when = Date.now();
network.ignoreList.push(hostmask);
client.save();
emitError(
`\u0002${hostmask.nick}!${hostmask.ident}@${hostmask.hostname}\u000f added to ignorelist`
);
} else {
emitError("The specified user/hostmask is already ignored");
return;
}
break;
if (hostmaskInList(network.ignoreList, hostmask)) {
emitError("The specified user/hostmask is already ignored");
return;
}
hostmask.when = Date.now();
network.ignoreList.push(hostmask);
client.save();
// TODO: This should not be an error, that's the happy path for gods sake...
emitError(
`\u0002${hostmask.nick}!${hostmask.ident}@${hostmask.hostname}\u000f added to ignorelist`
);
updateIgnoreList(client, network);
return;
}
case "unignore": {
@ -62,67 +63,78 @@ exports.input = 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();
// TODO: This should not be an error, that's the happy path for gods sake...
emitError(
`Successfully removed \u0002${hostmask.nick}!${hostmask.ident}@${hostmask.hostname}\u000f from ignorelist`
);
} else {
if (idx === -1) {
emitError("The specified user/hostmask is not ignored");
return;
}
break;
network.ignoreList.splice(idx, 1);
client.save();
// TODO: This should not be an error, that's the happy path for gods sake...
emitError(
`Successfully removed \u0002${hostmask.nick}!${hostmask.ident}@${hostmask.hostname}\u000f from ignorelist`
);
updateIgnoreList(client, network);
return;
}
case "ignorelist":
if (network.ignoreList.length === 0) {
chan.pushMessage(
client,
new Msg({
type: Msg.Type.ERROR,
text: "Ignorelist is empty",
})
);
} else {
create_new_ignored_window = true;
emitError("Ignorelist is empty");
return;
}
createOrUpdateIgnoreList(client, network, false);
break;
}
};
const chanName = "Ignored users";
const channel = network.getChannel(chanName);
function updateIgnoreList(client, network, focus) {
const channel = network.getChannel(IGNORELIST_CHAN_NAME);
if (typeof channel === "undefined" && !create_new_ignored_window) {
if (typeof channel === "undefined") {
// nothing to do, there is no open ignorelist
return;
}
const ignored = network.ignoreList.map((data) => ({
const shouldFocus = focus === undefined ? false : focus; // default to no focus
client.emit("msg:special", {
chan: channel.id,
data: chanDataFromList(network.ignoreList),
focus: shouldFocus,
});
}
function createOrUpdateIgnoreList(client, network) {
const channel = network.getChannel(IGNORELIST_CHAN_NAME);
if (typeof channel !== "undefined") {
// already have an existing window, so update and focus
return updateIgnoreList(client, network, true);
}
const newChan = client.createChannel({
type: Chan.Type.SPECIAL,
special: Chan.SpecialType.IGNORELIST,
name: IGNORELIST_CHAN_NAME,
data: chanDataFromList(network.ignoreList),
});
client.emit("join", {
network: network.uuid,
chan: newChan.getFilteredClone(true),
index: network.addChannel(newChan),
});
}
function hostmaskInList(list, hostmask) {
return list.some(function (entry) {
return Helper.compareHostmask(entry, hostmask);
});
}
function chanDataFromList(list) {
return list.map((data) => ({
hostmask: `${data.nick}!${data.ident}@${data.hostname}`,
when: data.when,
}));
if (typeof channel === "undefined" && create_new_ignored_window) {
const newChan = client.createChannel({
type: Chan.Type.SPECIAL,
special: Chan.SpecialType.IGNORELIST,
name: chanName,
data: ignored,
});
client.emit("join", {
network: network.uuid,
chan: newChan.getFilteredClone(true),
index: network.addChannel(newChan),
});
} else {
client.emit("msg:special", {
chan: channel.id,
data: ignored,
focus: false,
});
}
};
}