Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore constructor props commits #4716

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions server/models/chan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@ export type ChanConfig = {
};

class Chan {
// TODO: don't force existence, figure out how to make TS infer it.
id!: number;
messages!: Msg[];
name!: string;
key!: string;
topic!: string;
firstUnread!: number;
unread!: number;
highlight!: number;
users!: Map<string, User>;
muted!: boolean;
type!: ChanType;
state!: ChanState;
id: number;
messages: Msg[];
name: string;
key: string;
topic: string;
firstUnread: number;
unread: number;
highlight: number;
users: Map<string, User>;
muted: boolean;
type: ChanType;
state: ChanState;

userAway?: boolean;
special?: SpecialChanType;
Expand All @@ -63,20 +62,22 @@ class Chan {
static optionalProperties = ["userAway", "special", "data", "closed", "num_users"];

constructor(attr?: Partial<Chan>) {
_.defaults(this, attr, {
id: 0,
messages: [],
name: "",
key: "",
topic: "",
type: ChanType.CHANNEL,
state: ChanState.PARTED,
firstUnread: 0,
unread: 0,
highlight: 0,
users: new Map(),
muted: false,
});
this.id = 0;
this.messages = [];
this.name = "";
this.key = "";
this.topic = "";
this.type = ChanType.CHANNEL;
this.state = ChanState.PARTED;
this.firstUnread = 0;
this.unread = 0;
this.highlight = 0;
this.users = new Map();
this.muted = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a class now, why don't we assign all of these as defaults on the class properties instead of setting them in the constructors?


if (attr) {
Object.assign(this, attr);
Copy link
Member

@xPaw xPaw Mar 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better fix would be make all of the parameters that chan creation uses explicit parameters of the constructor, such as constructor(name, type, key, muted) (and whatever else parameters can be set on creation.

Alternatively, it could be static helpers such as createNewQuery which returns an instance of new Chan that is set to type of query.

these comment also applies to the other models

}
}

destroy() {
Expand Down
145 changes: 74 additions & 71 deletions server/models/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,93 +96,96 @@ export type NetworkConfig = {
};

class Network {
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!: Chan[];
uuid!: string;
proxyHost!: string;
proxyPort!: number;
proxyUsername!: string;
proxyPassword!: string;
proxyEnabled!: boolean;
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: Chan[];
uuid: string;
proxyHost: string;
proxyPort: number;
proxyUsername: string;
proxyPassword: string;
proxyEnabled: boolean;
highlightRegex?: RegExp;

irc?: IrcFramework.Client & {
options?: NetworkIrcOptions;
};

chanCache!: Chan[];
ignoreList!: IgnoreList;
keepNick!: string | null;
chanCache: Chan[];
ignoreList: IgnoreList;
keepNick: string | null;

status!: NetworkStatus;

serverOptions!: {
serverOptions: {
CHANTYPES: string[];
PREFIX: Prefix;
NETWORK: string;
};

// TODO: this is only available on export
hasSTSPolicy!: boolean;
hasSTSPolicy: boolean;
status: NetworkStatus;

constructor(attr?: Partial<Network>) {
_.defaults(this, attr, {
name: "",
nick: "",
host: "",
port: 6667,
tls: false,
userDisconnected: false,
rejectUnauthorized: false,
password: "",
awayMessage: "",
commands: [],
username: "",
realname: "",
leaveMessage: "",
sasl: "",
saslAccount: "",
saslPassword: "",
channels: [],
irc: null,
serverOptions: {
CHANTYPES: ["#", "&"],
PREFIX: new Prefix([
{symbol: "!", mode: "Y"},
{symbol: "@", mode: "o"},
{symbol: "%", mode: "h"},
{symbol: "+", mode: "v"},
]),
NETWORK: "",
},

proxyHost: "",
proxyPort: 1080,
proxyUsername: "",
proxyPassword: "",
proxyEnabled: false,

chanCache: [],
ignoreList: [],
keepNick: null,
});
this.name = "";
this.nick = "";
this.host = "";
this.port = 6667;
this.tls = false;
this.userDisconnected = false;
this.rejectUnauthorized = false;
this.password = "";
this.awayMessage = "";
this.commands = [];
this.username = "";
this.realname = "";
this.leaveMessage = "";
this.sasl = "";
this.saslAccount = "";
this.saslPassword = "";
this.channels = [];
this.serverOptions = {
CHANTYPES: ["#", "&"],
PREFIX: new Prefix([
{symbol: "!", mode: "Y"},
{symbol: "@", mode: "o"},
{symbol: "%", mode: "h"},
{symbol: "+", mode: "v"},
]),
NETWORK: "",
};
this.proxyHost = "";
this.proxyPort = 1080;
this.proxyUsername = "";
this.proxyPassword = "";
this.proxyEnabled = false;

this.chanCache = [];
this.ignoreList = [];
this.keepNick = null;
this.hasSTSPolicy = false;
this.uuid = "invalid"; // sentinel value that makes us generate a new one

this.status = {connected: false, secure: false};

if (attr) {
Object.assign(this, attr);
}

if (!this.uuid) {
if (this.uuid === "invalid" || !this.uuid) {
this.uuid = uuidv4();
}

Expand Down
35 changes: 16 additions & 19 deletions server/models/user.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import _ from "lodash";
import Prefix from "./prefix";

class User {
modes!: string[];
modes: string[];
// Users in the channel have only one mode assigned
mode!: string;
away!: string;
nick!: string;
lastMessage!: number;
away: string;
nick: string;
lastMessage: number;

constructor(attr: Partial<User>, prefix?: Prefix) {
_.defaults(this, attr, {
modes: [],
away: "",
nick: "",
lastMessage: 0,
});

Object.defineProperty(this, "mode", {
get() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this.modes[0] || "";
},
});
this.modes = [];
this.away = "";
this.nick = "";
this.lastMessage = 0;

if (attr) {
Object.assign(this, attr);
}

this.setModes(this.modes, prefix || new Prefix([]));
}

get mode() {
return this.modes[0] || "";
}

setModes(modes: string[], prefix: Prefix) {
// irc-framework sets character mode, but The Lounge works with symbols
this.modes = modes.map((mode) => prefix.modeToSymbol[mode]);
Expand Down