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 PublicClient from "./plugins/packages/publicClient";
import SqliteMessageStorage from "./plugins/messageStorage/sqlite"; import SqliteMessageStorage from "./plugins/messageStorage/sqlite";
import TextFileMessageStorage from "./plugins/messageStorage/text"; 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 ClientManager from "./clientManager";
import {MessageStorage, SearchQuery, SearchResponse} from "./plugins/messageStorage/types"; import {MessageStorage, SearchQuery, SearchResponse} from "./plugins/messageStorage/types";
@ -96,7 +96,7 @@ class Client {
[socketId: string]: {token: string; openChannel: number}; [socketId: string]: {token: string; openChannel: number};
}; };
config!: UserConfig & { config!: UserConfig & {
networks?: Network[]; networks?: NetworkConfig[];
}; };
id!: number; id!: number;
idMsg!: number; idMsg!: number;
@ -112,7 +112,11 @@ class Client {
fileHash!: string; fileHash!: string;
constructor(manager: ClientManager, name?: string, config = {} as UserConfig) { constructor(
manager: ClientManager,
name?: string,
config = {} as UserConfig & {networks: NetworkConfig[]}
) {
_.merge(this, { _.merge(this, {
awayMessage: "", awayMessage: "",
lastActiveChannel: -1, lastActiveChannel: -1,

View File

@ -7,6 +7,7 @@ import path from "path";
import Auth from "./plugins/auth"; import Auth from "./plugins/auth";
import Client, {UserConfig} from "./client"; import Client, {UserConfig} from "./client";
import Config from "./config"; import Config from "./config";
import {NetworkConfig} from "./models/network";
import WebPush from "./plugins/webpush"; import WebPush from "./plugins/webpush";
import log from "./log"; import log from "./log";
import {Server} from "socket.io"; import {Server} from "socket.io";
@ -283,7 +284,7 @@ class ClientManager {
try { try {
const data = fs.readFileSync(userPath, "utf-8"); const data = fs.readFileSync(userPath, "utf-8");
return JSON.parse(data) as UserConfig; return JSON.parse(data) as UserConfig & {networks: NetworkConfig[]};
} catch (e: any) { } catch (e: any) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
log.error(`Failed to read user ${colors.bold(name)}: ${e}`); log.error(`Failed to read user ${colors.bold(name)}: ${e}`);

View File

@ -33,6 +33,13 @@ export type FilteredChannel = Chan & {
totalMessages: number; totalMessages: number;
}; };
export type ChanConfig = {
name: string;
key?: string;
muted?: boolean;
type?: string;
};
class Chan { class Chan {
id: number; id: number;
messages: Msg[]; messages: Msg[];

View File

@ -1,7 +1,7 @@
import _ from "lodash"; import _ from "lodash";
import {v4 as uuidv4} from "uuid"; import {v4 as uuidv4} from "uuid";
import IrcFramework, {Client as IRCClient} from "irc-framework"; 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 Msg, {MessageType} from "./msg";
import Prefix from "./prefix"; import Prefix from "./prefix";
import Helper, {Hostmask} from "../helper"; 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 { class Network {
nick: string; nick: string;
name: string; name: string;