Home Reference Source

src/chat/ChatMessage.js

import {ChatMessageDirection} from './ChatMessageDirection';
import {ChatMessageStatus} from './ChatMessageStatus';
import {ChatMessageType} from './ChatMessageType';

/**
 * Instances of this class represents a message of the chat.
 * For more information about how instances of this class can be obtained see
 * {@link Chat#getMessages} or {@link Chat#getPendingMessages}.
 */
export class ChatMessage {
	/** @protected */
	static of(composer, data, {
		id = null,
		direction = ChatMessageDirection.INCOMING,
		type = ChatMessageType.TEXT,
		status = ChatMessageStatus.SENDING,
		progress = 0,
		timestamp = null,
	} = {}) {
		return new ChatMessage(composer, data, id, direction, type, status, progress, timestamp);
	}

	/** @private */
	constructor(composer, data, id, direction, type, status, progress, timestamp) {
		/** @private */
		this.composer = composer;
		/** @private */
		this.data = data;
		/** @private */
		this.id = id;
		/** @private */
		this.direction = direction;
		/** @private */
		this.type = type;
		/** @private */
		this.status = status;
		/** @private */
		this.progress = progress;
		/** @private */
		this.timestamp = timestamp;
		Object.freeze(this);
	}

	/**
	 * @protected
	 * Returns a new ChatMessage with the provided values updated.
	 * @return {ChatMessage}
	 */
	with({
		data = this.data,
		id = this.id,
		type = this.type,
		status = this.status,
		progress = this.progress,
		timestamp = this.timestamp,
	} = {}) {
		return new ChatMessage(this.composer, data, id, this.direction, type, status, progress, timestamp);
	}

	/**
	 * Returns the ID of the message.
	 * @return {string}
	 */
	getId() {
		return this.id;
	}

	/**
	 * Returns the direction of the direction of the message.
	 * @return {ChatMessageDirection}
	 */
	getDirection() {
		return this.direction;
	}

	/**
	 * Returns the type of the message.
	 * @return {ChatMessageType}
	 */
	getType() {
		return this.type;
	}

	/**
	* Returns the address of the composer of the message.
	* @return {string}
	*/
	getComposer() {
		return this.composer;
	}

	/**
	 * Returns the data of the message.
	 * @return {string|ChatMessageFile}
	 */
	getData() {
		return this.data;
	}

	/**
	 * Returns the timestamp when the message was send.
	 * @return {number}
	 */
	getTimestamp() {
		return this.timestamp;
	}

	/**
	 * Returns a number between 0 and 100 indicating the percentage of the message
	 * that was sent. Note that text messages can only have two values: 0 and 100.
	 * @return {number}
	 */
	getProgress() {
		return this.progress;
	}

	/**
	 * Returns the current status of the message.
	 * @return {ChatMessageStatus}
	 */
	getStatus() {
		return this.status;
	}

	/**
	 * Returns true when the current status of the message is the specified.
	 * @param {ChatMessageStatus} status
	 * @return {boolean}
	 */
	hasStatus(status) {
		return this.status === status;
	}
}