From 3ac9c36d95a35fe42d8eec21f2ba8ec7b156d3d1 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 19 Mar 2023 21:58:14 +0100 Subject: [PATCH] Revert "user: don't force existence of constructor properties" This reverts commit c3e3322a79d918198bb6700169c84281c00bf8cf. --- server/models/user.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/server/models/user.ts b/server/models/user.ts index 0a58c3cd..ea2d39b0 100644 --- a/server/models/user.ts +++ b/server/models/user.ts @@ -1,29 +1,32 @@ +import _ from "lodash"; import Prefix from "./prefix"; class User { - modes: string[]; + modes!: string[]; // Users in the channel have only one mode assigned - away: string; - nick: string; - lastMessage: number; + mode!: string; + away!: string; + nick!: string; + lastMessage!: number; constructor(attr: Partial, prefix?: Prefix) { - this.modes = []; - this.away = ""; - this.nick = ""; - this.lastMessage = 0; + _.defaults(this, attr, { + modes: [], + away: "", + nick: "", + lastMessage: 0, + }); - if (attr) { - Object.assign(this, attr); - } + Object.defineProperty(this, "mode", { + get() { + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return this.modes[0] || ""; + }, + }); 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]);