Home Reference Source

src/chat/ChatMessage.ts

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

interface ChatMessageOptions {
	id?: string;
	direction?: ChatMessageDirection;
	type?: ChatMessageType;
	status?: ChatMessageStatus;
	progress?: number;
	timestamp?: number;
}

/**
 * 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 {
	private composer: string;
	private data: ChatMessageFile | string;
	private id?: string;
	private direction: ChatMessageDirection;
	private type: ChatMessageType;
	private status: ChatMessageStatus;
	private progress: number;
	private timestamp?: number;

	/** @ignore */
	public static of(composer: string, data: string, {
		id,
		direction = ChatMessageDirection.INCOMING,
		type = ChatMessageType.TEXT,
		status = ChatMessageStatus.SENDING,
		progress = 0,
		timestamp,
	}: ChatMessageOptions = {}): ChatMessage {
		return new ChatMessage(composer, data, direction, type, status, progress, id, timestamp);
	}

	/** @private */
	private constructor(
		composer: string,
		data: ChatMessageFile | string,
		direction: ChatMessageDirection,
		type: ChatMessageType,
		status: ChatMessageStatus,
		progress: number,
		id?: string,
		timestamp?: number,
	) {
		/** @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);
	}

	/**
	 * @ignore
	 * 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,
	}: ChatMessageOptions & {data?: ChatMessageFile | string} = {}): ChatMessage {
		return new ChatMessage(this.composer, data, this.direction, type, status, progress, id, timestamp);
	}

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

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

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

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

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

	/**
	 * @desc Returns the timestamp when the message was sent.
	 * @return {number}
	 */
	getTimestamp(): number|undefined {
		return this.timestamp;
	}

	/**
	 * @desc 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(): number {
		return this.progress;
	}

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

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