mirror of
https://github.com/thelounge/thelounge.git
synced 2024-06-01 21:32:17 +02:00
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:
parent
666cb48b15
commit
e83033dd44
|
@ -6,11 +6,12 @@ const Helper = require("../../helper");
|
||||||
|
|
||||||
exports.commands = ["ignore", "unignore", "ignorelist"];
|
exports.commands = ["ignore", "unignore", "ignorelist"];
|
||||||
|
|
||||||
|
const IGNORELIST_CHAN_NAME = "Ignored users";
|
||||||
|
|
||||||
exports.input = function (network, chan, cmd, args) {
|
exports.input = function (network, chan, cmd, args) {
|
||||||
const client = this;
|
const client = this;
|
||||||
let target;
|
let target;
|
||||||
let hostmask;
|
let hostmask;
|
||||||
let create_new_ignored_window = false;
|
|
||||||
|
|
||||||
function emitError(msg) {
|
function emitError(msg) {
|
||||||
chan.pushMessage(
|
chan.pushMessage(
|
||||||
|
@ -38,23 +39,23 @@ exports.input = function (network, chan, cmd, args) {
|
||||||
// IRC nicks are case insensitive
|
// IRC nicks are case insensitive
|
||||||
if (hostmask.nick.toLowerCase() === network.nick.toLowerCase()) {
|
if (hostmask.nick.toLowerCase() === network.nick.toLowerCase()) {
|
||||||
emitError("You can't ignore yourself");
|
emitError("You can't ignore yourself");
|
||||||
} else if (
|
return;
|
||||||
!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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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": {
|
case "unignore": {
|
||||||
|
@ -62,67 +63,78 @@ exports.input = function (network, chan, cmd, args) {
|
||||||
return Helper.compareHostmask(entry, hostmask);
|
return Helper.compareHostmask(entry, hostmask);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if the entry exists before removing it, otherwise
|
if (idx === -1) {
|
||||||
// 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 {
|
|
||||||
emitError("The specified user/hostmask is not ignored");
|
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":
|
case "ignorelist":
|
||||||
if (network.ignoreList.length === 0) {
|
if (network.ignoreList.length === 0) {
|
||||||
chan.pushMessage(
|
emitError("Ignorelist is empty");
|
||||||
client,
|
return;
|
||||||
new Msg({
|
|
||||||
type: Msg.Type.ERROR,
|
|
||||||
text: "Ignorelist is empty",
|
|
||||||
})
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
create_new_ignored_window = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createOrUpdateIgnoreList(client, network, false);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const chanName = "Ignored users";
|
function updateIgnoreList(client, network, focus) {
|
||||||
const channel = network.getChannel(chanName);
|
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;
|
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}`,
|
hostmask: `${data.nick}!${data.ident}@${data.hostname}`,
|
||||||
when: data.when,
|
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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in a new issue