summary refs log tree commit diff
path: root/user.ts
blob: 26d7ebb2715d97d4b6299231634c400df8173923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// because why not make *more files

import { getRandomInt } from "./lib.js";
import cuid from "cuid";
import type { WebSocket } from 'ws'
import type { IncomingMessage } from "node:http";
import type Server from "./server.ts";
import Timespamp from "./timestamp.ts";

export default class User {
    id: string = cuid();
    username: string;
    nickname: string;
    guest: boolean;
    socket: WebSocket;
    joinReq: IncomingMessage;
    ip: string;
    t: Timespamp;
    channel: string;
    client?: string = undefined;
    admin: boolean = false;
    /**
     * the user class
     */
    constructor (request: IncomingMessage, socket: WebSocket, server: Server) {
        const anonID = getRandomInt(0, 99999);
        const annonNum = "0".repeat(5 - anonID.toString().length) + anonID.toString()
        this.username = server.config.accounts.annonFormat ? server.config.accounts.annonFormat.replace('[num]', annonNum) : 'Anonymous' + annonNum;
        this.nickname = server.config.accounts.annonFormat ? server.config.accounts.annonFormat.replace('[num]', annonNum) : 'Anonymous' + annonNum;
        this.guest = true;
        this.socket = socket;
        this.joinReq = request;
        this.ip = (request.headers["x-forwarded-for"] ?? [null])[0] ?? request.socket.remoteAddress ?? '';
        this.t = new Timespamp(new Date());
        this.channel = "home";
    }
    name () {
        return this.nickname != "" ? this.nickname : this.username;
    }
}