From 76098d7e766ad074eb6278ee487410f1f02817c3 Mon Sep 17 00:00:00 2001 From: Val Lorentz Date: Tue, 14 Mar 2023 21:20:25 +0100 Subject: [PATCH] Fix incorrect typing of dehydrated networks and channels Client and ClientManager deal with both 'dehydrated' channels/networks (ie. directly from JSON configuration) and the 'rehydrated' ones (classes, with socket objects, message arrays, etc.). However, because their attributes are similar, both types were used interchangeably, which becomes an issue when splitting Client's configuration loading into smaller methods. --- server/client.ts | 10 +++++++--- server/clientManager.ts | 3 ++- server/models/chan.ts | 7 +++++++ server/models/network.ts | 30 +++++++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/server/client.ts b/server/client.ts index 66a408c0..049c0744 100644 --- a/server/client.ts +++ b/server/client.ts @@ -15,7 +15,7 @@ import inputs from "./plugins/inputs"; import PublicClient from "./plugins/packages/publicClient"; import SqliteMessageStorage from "./plugins/messageStorage/sqlite"; import TextFileMessageStorage from "./plugins/messageStorage/text"; -import Network, {IgnoreListItem, NetworkWithIrcFramework} from "./models/network"; +import Network, {IgnoreListItem, NetworkConfig, NetworkWithIrcFramework} from "./models/network"; import ClientManager from "./clientManager"; import {MessageStorage, SearchQuery, SearchResponse} from "./plugins/messageStorage/types"; @@ -96,7 +96,7 @@ class Client { [socketId: string]: {token: string; openChannel: number}; }; config!: UserConfig & { - networks?: Network[]; + networks?: NetworkConfig[]; }; id!: number; idMsg!: number; @@ -112,7 +112,11 @@ class Client { fileHash!: string; - constructor(manager: ClientManager, name?: string, config = {} as UserConfig) { + constructor( + manager: ClientManager, + name?: string, + config = {} as UserConfig & {networks: NetworkConfig[]} + ) { _.merge(this, { awayMessage: "", lastActiveChannel: -1, diff --git a/server/clientManager.ts b/server/clientManager.ts index 705fa432..d32081e3 100644 --- a/server/clientManager.ts +++ b/server/clientManager.ts @@ -7,6 +7,7 @@ import path from "path"; import Auth from "./plugins/auth"; import Client, {UserConfig} from "./client"; import Config from "./config"; +import {NetworkConfig} from "./models/network"; import WebPush from "./plugins/webpush"; import log from "./log"; import {Server} from "socket.io"; @@ -283,7 +284,7 @@ class ClientManager { try { const data = fs.readFileSync(userPath, "utf-8"); - return JSON.parse(data) as UserConfig; + return JSON.parse(data) as UserConfig & {networks: NetworkConfig[]}; } catch (e: any) { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions log.error(`Failed to read user ${colors.bold(name)}: ${e}`); diff --git a/server/models/chan.ts b/server/models/chan.ts index 65a4067a..82be2152 100644 --- a/server/models/chan.ts +++ b/server/models/chan.ts @@ -33,6 +33,13 @@ export type FilteredChannel = Chan & { totalMessages: number; }; +export type ChanConfig = { + name: string; + key?: string; + muted?: boolean; + type?: string; +}; + class Chan { id: number; messages: Msg[]; diff --git a/server/models/network.ts b/server/models/network.ts index a47757f5..269a415e 100644 --- a/server/models/network.ts +++ b/server/models/network.ts @@ -1,7 +1,7 @@ import _ from "lodash"; import {v4 as uuidv4} from "uuid"; import IrcFramework, {Client as IRCClient} from "irc-framework"; -import Chan, {Channel, ChanType} from "./chan"; +import Chan, {ChanConfig, Channel, ChanType} from "./chan"; import Msg, {MessageType} from "./msg"; import Prefix from "./prefix"; import Helper, {Hostmask} from "../helper"; @@ -67,6 +67,34 @@ export type NetworkWithIrcFramework = Network & { }; }; +export type NetworkConfig = { + nick: string; + name: string; + host: string; + port: number; + tls: boolean; + userDisconnected: boolean; + rejectUnauthorized: boolean; + password: string; + awayMessage: string; + commands: any[]; + username: string; + realname: string; + leaveMessage: string; + sasl: string; + saslAccount: string; + saslPassword: string; + channels: ChanConfig[]; + uuid: string; + proxyHost: string; + proxyPort: number; + proxyUsername: string; + proxyPassword: string; + proxyEnabled: boolean; + highlightRegex?: string; + ignoreList: any[]; +}; + class Network { nick: string; name: string;