Skip to content

Commit

Permalink
ignore: clean up the types and conditionals
Browse files Browse the repository at this point in the history
Now that ignorelist doesn't muddy the waters, we can clean up
all the funny conditional types and enforce `when`
  • Loading branch information
brunnre8 committed Apr 14, 2024
1 parent 8ddd8de commit 6f66057
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion server/models/network.ts
Expand Up @@ -45,7 +45,7 @@ type NetworkStatus = {
};

export type IgnoreListItem = Hostmask & {
when?: number;
when: number;
};

type IgnoreList = IgnoreListItem[];
Expand Down
32 changes: 12 additions & 20 deletions server/plugins/inputs/ignore.ts
Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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(
Expand All @@ -59,25 +53,25 @@ 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;
}

case "unignore": {
const idx = network.ignoreList.findIndex(function (entry) {
return Helper.compareHostmask(entry, hostmask!);
return Helper.compareHostmask(entry, hostmask);
});

if (idx === -1) {
Expand All @@ -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`,
})
);
}
Expand Down

0 comments on commit 6f66057

Please sign in to comment.