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 & { export type IgnoreListItem = Hostmask & {
when?: number; when: number;
}; };
type IgnoreList = IgnoreListItem[]; type IgnoreList = IgnoreListItem[];

View file

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