From 6f66057f1362618bedffd146f921c610d84ff4b7 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 14 Apr 2024 13:54:24 +0200 Subject: [PATCH] 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` --- server/models/network.ts | 2 +- server/plugins/inputs/ignore.ts | 32 ++++++++++++-------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/server/models/network.ts b/server/models/network.ts index 65d9463091..3860fa4519 100644 --- a/server/models/network.ts +++ b/server/models/network.ts @@ -45,7 +45,7 @@ type NetworkStatus = { }; export type IgnoreListItem = Hostmask & { - when?: number; + when: number; }; type IgnoreList = IgnoreListItem[]; diff --git a/server/plugins/inputs/ignore.ts b/server/plugins/inputs/ignore.ts index d7e88c3f13..0e348b50a5 100644 --- a/server/plugins/inputs/ignore.ts +++ b/server/plugins/inputs/ignore.ts @@ -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`, }) ); }