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.
This commit is contained in:
Val Lorentz 2023-03-14 21:20:25 +01:00
parent efd24fd12c
commit 76098d7e76
4 changed files with 45 additions and 5 deletions

View file

@ -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,

View file

@ -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}`);

View file

@ -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[];

View file

@ -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;