chatroom.js

import Manager from "./manager";

/**
 * @class Chatroom
 * @property {string} id Chatroom ID
 * @property {string} title Chatroom title
 * @property {boolean} isPrivate chatroom private flag
 * @property {number} numberOfVisitors Number of joined visitors
 * @property {Manager[]} managers Joined managers
 */
class Chatroom {

    constructor() {
        this.id = null;
        this.title = null;
        this.isPrivate = null;
        this.numberOfVisitors = 0;
        this.managers = [];
    }

    /**
     * Gets Chatroom ID
     */
    getId() {
        return this.id;
    }

    /**
     * Sets Chatroom ID
     * @param {string} id Chatroom ID to be set
     */
    setId(id) {
        this.id = id;
    }

    /**
     * Gets chatroom title
     */
    getTitle() {
        return this.title;
    }

    /**
     * Sets Chatroom title
     * @param {string} title Chatroom title to be set
     */
    setTitle(title) {
        this.title = title;
    }

    /**
     * Gets is chatroom private or not
     */
    getIsPrivate() {
        return this.isPrivate;
    }

    /**
     * Sets Chatroom privacy flag
     * @param {boolean} isPrivate Chatroom privacy flag value
     */
    setIsPrivate(isPrivate) {
        this.isPrivate = isPrivate;
    }

    /**
     * Gets number of Chatroom visitors
     */
    getNumberOfVisitors() {
        return this.numberOfVisitors;
    }

    /**
     * Sets number of connected visitors
     * @param {number} num Number of currently connected visitors to Chatroom
     */
    setNumberOfVisitors(num) {
        this.numberOfVisitors = num;
    }

    /**
     * Gets number of totally connected users (visitors + managers)
     */
    getTotalNumberOfUsers() {
        return this.numberOfVisitors + this.managers.length;
    }

    /**
     * Gets connected managers
     */
    getManagers() {
        return this.managers;
    }

    /**
     * Sets connected managers
     * @param {Manager[]} managers Array of connected managers
     */
    setManagers(managers) {
        this.managers = managers;
    }

}

/**
 * Static builder for Chatroom class.
 * Builds instance using passed API response object
 */
Chatroom.buildFromInfo = function(data) {
    let chatroom = new Chatroom();
    chatroom.setId(data.id);
    chatroom.setTitle(data.title);
    chatroom.setIsPrivate(data.is_private);
    chatroom.setNumberOfVisitors(data.number_of_visitors);
    let managers = [];
    for( let apiManager of data.managers) {
        managers.push(Manager.buildFromInfo(apiManager));
    }
    chatroom.setManagers(managers);
    return chatroom;
};

export default Chatroom;