ignore: clean up the types and conditionals

Now that ignorelist doesn't muddy the waters, we can clean up
all the funny conditional types and enforce `when`
This commit is contained in:
Reto Brunner 2024-04-14 13:54:24 +02:00
parent 071a5afda6
commit b8400a3a46
2 changed files with 13 additions and 21 deletions

View file

@ -45,7 +45,7 @@ type NetworkStatus = {
};
export type IgnoreListItem = Hostmask & {
when?: number;
when: number;
};
type IgnoreList = IgnoreListItem[];

View file

@ -8,11 +8,8 @@ const commands = ["ignore", "unignore"];
const input: PluginInputHandler = function (network, chan, cmd, args) {
const client = this;
let target: string;
// let hostmask: cmd === "ignoreList" ? string : undefined;
let hostmask: IgnoreListItem | undefined;
if (cmd !== "ignorelist" && (args.length === 0 || args[0].trim().length === 0)) {
if (args.length === 0 || args[0].trim().length === 0) {
chan.pushMessage(
client,
new Msg({
@ -24,16 +21,13 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
return;
}
if (cmd !== "ignorelist") {
// Trim to remove any spaces from the hostmask
target = args[0].trim();
hostmask = Helper.parseHostmask(target) as IgnoreListItem;
}
const target = args[0].trim();
const hostmask = Helper.parseHostmask(target);
switch (cmd) {
case "ignore": {
// IRC nicks are case insensitive
if (hostmask!.nick.toLowerCase() === network.nick.toLowerCase()) {
if (hostmask.nick.toLowerCase() === network.nick.toLowerCase()) {
chan.pushMessage(
client,
new Msg({
@ -46,7 +40,7 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
if (
network.ignoreList.some(function (entry) {
return Helper.compareHostmask(entry, hostmask!);
return Helper.compareHostmask(entry, hostmask);
})
) {
chan.pushMessage(
@ -59,17 +53,17 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
return;
}
hostmask!.when = Date.now();
network.ignoreList.push(hostmask!);
network.ignoreList.push({
...hostmask,
when: Date.now(),
});
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`,
text: `\u0002${hostmask.nick}!${hostmask.ident}@${hostmask.hostname}\u000f added to ignorelist`,
})
);
return;
@ -77,7 +71,7 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
case "unignore": {
const idx = network.ignoreList.findIndex(function (entry) {
return Helper.compareHostmask(entry, hostmask!);
return Helper.compareHostmask(entry, hostmask);
});
if (idx === -1) {
@ -98,9 +92,7 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
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`,
text: `Successfully removed \u0002${hostmask.nick}!${hostmask.ident}@${hostmask.hostname}\u000f from ignorelist`,
})
);
}