thelounge/src/models/user.ts

49 lines
980 B
TypeScript
Raw Normal View History

2022-05-02 07:56:38 +02:00
import _ from "lodash";
import Prefix from "./prefix";
class User {
2022-05-03 03:32:20 +02:00
modes!: string[];
2022-05-02 07:56:38 +02:00
// Users in the channel have only one mode assigned
2022-05-03 03:32:20 +02:00
mode!: string;
away!: string;
nick!: string;
account!: string;
2022-05-03 03:32:20 +02:00
lastMessage!: number;
2022-05-02 07:56:38 +02:00
2022-05-03 08:50:59 +02:00
constructor(attr: Partial<User>, prefix?: Prefix) {
2022-05-02 07:56:38 +02:00
_.defaults(this, attr, {
modes: [],
away: "",
nick: "",
account: "",
2022-05-02 07:56:38 +02:00
lastMessage: 0,
});
Object.defineProperty(this, "mode", {
get() {
2022-05-31 23:52:53 +02:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this.modes[0] || "";
2022-05-02 07:56:38 +02:00
},
});
2022-05-03 08:50:59 +02:00
this.setModes(this.modes, prefix || new Prefix([]));
2022-05-02 07:56:38 +02:00
}
setModes(modes: string[], prefix: Prefix) {
// irc-framework sets character mode, but The Lounge works with symbols
this.modes = modes.map((mode) => prefix.modeToSymbol[mode]);
}
toJSON() {
return {
nick: this.nick,
modes: this.modes,
lastMessage: this.lastMessage,
away: this.away,
account: this.account,
2022-05-02 07:56:38 +02:00
};
}
}
export default User;