Repository: louislam/uptime-kuma
File: server/uptime-kuma-server.js (JavaScript)

Code before:
    }

    /**
     * Initialise app after the database has been set up
     * @returns {Promise<void>}
     */
    async initAfterDatabaseReady() {
        // Static
        this.app.use("/screenshots", express.static(Database.screenshotDir));

        process.env.TZ = await this.getTimezone();
        dayjs.tz.setDefault(process.env.TZ);
        log.debug("DEBUG", "Timezone: " + process.env.TZ);
        log.debug("DEBUG", "Current Time: " + dayjs.tz().format());

        await this.loadMaintenanceList();
    }

    /**
     * Send list of monitors to client
     * @param {Socket} socket Socket to send list on
     * @param {string} op list, add, update, delete
     * @param {number} monitorID update or deleted monitor id
     * @returns {Promise<object>} List of monitors
     */
    async sendMonitorList(socket, op = OPERATIONS.LIST, monitorID = null) {
        let result = {};
        let list = {};

        if (op !== OPERATIONS.DELETE) {
            list = await this.getMonitorJSONList(socket.userID, monitorID);
        }

        result["op"] = op;
        result["monitorID"] = monitorID;
        result["list"] = list;
        this.io.to(socket.userID).emit("monitorList", result);
        return result;
    }

    /**
     * Get a list of monitors for the given user.
     * @param {string} userID - The ID of the user to get monitors for.
     * @param {number} monitorID - The ID of monitor for.
     * @returns {Promise<object>} A promise that resolves to an object with monitor IDs as keys and monitor objects as values.
     *
     * Generated by Trelent
     */
    async getMonitorJSONList(userID, monitorID = null) {
        let result = {};


Review comment (suggestion):
This seems pretty strange
the whole part why you added `op` here and above seems to be to create a fast-path for `OPERATIONS.DELETE`, right?

Instead of doing this, please just remove the calls for `OPERATIONS.DELETE` if we really don't use the `list` there..
Cleans up the code a lot..

Code after:
    }

    /**
     * Initialise app after the database has been set up
     * @returns {Promise<void>}
     */
    async initAfterDatabaseReady() {
        // Static
        this.app.use("/screenshots", express.static(Database.screenshotDir));

        process.env.TZ = await this.getTimezone();
        dayjs.tz.setDefault(process.env.TZ);
        log.debug("DEBUG", "Timezone: " + process.env.TZ);
        log.debug("DEBUG", "Current Time: " + dayjs.tz().format());

        await this.loadMaintenanceList();
    }

    /**
     * Send list of monitors to client
     * @param {Socket} socket Socket to send list on
     * @returns {Promise<object>} List of monitors
     */
    async sendMonitorList(socket) {
        let list = await this.getMonitorJSONList(socket.userID);
        this.io.to(socket.userID).emit("monitorList", list);
        return list;
    }

    /**
     * Update Monitor into list
     * @param {Socket} socket Socket to send list on
     * @param {number} monitorID update or deleted monitor id
     * @returns {Promise<void>}
     */
    async sendUpdateMonitorIntoList(socket, monitorID) {
        let list = await this.getMonitorJSONList(socket.userID, monitorID);
        this.io.to(socket.userID).emit("updateMonitorIntoList", list);
    }

    /**
     * Delete Monitor from list
     * @param {Socket} socket Socket to send list on
     * @param {number} monitorID update or deleted monitor id
     * @returns {Promise<void>}
     */
    async sendDeleteMonitorFromList(socket, monitorID) {
        this.io.to(socket.userID).emit("deleteMonitorFromList", monitorID);
    }

    /**