import {Chat} from "./chat"; import {StreamSendable} from "../../helpers/sendable.base"; import {assign, difference, keys, pick} from "lodash"; export interface MultichatMessageSender { user: { profile_picture: string }, service: { profile_picture: string }, client: object | null } export interface MultichatMessage { stream: string; msg_type: string; content: string; origin: string; order: number; msg_interface: string; sent_by: MultichatMessageSender; received_by: Array; profile_picture: string; msg_meta: { [key: string]: any } | Array | any; } export interface ChatMessageData { id: number, chat: number | Chat, content: string, origin: string, msg_meta: string, msg_interface: string, msg_type: string, agent_picture?: string, multichat_command?: string, profile_picture: string; sent_by: number | { [key: string]: any }, order: number, received_by: Array } export class ChatMessage implements StreamSendable, ChatMessageData { [key: string]: any id: number; stream: string = 'messages'; msg_type: string = 'utterance'; content: string; origin: string; order: number; msg_interface: string; sent_by: { user: { profile_picture: string }, client: object | null }; received_by: Array; msg_meta: any; showProfileImage: boolean; profile_picture: string; protected statusListener: Function; isLast: boolean; chat: Chat; rawData: MultichatMessage | ChatMessageData; protected loadData(chat?: Chat, data?: MultichatMessage | ChatMessageData) { if (chat) { this.chat = chat; } if (data) { this.rawData = data; assign(this, pick(data, difference(keys(data), ['chat']))); this.isLast = ChatMessage.isLastMessage(this); } } constructor(chat?: Chat, data?: MultichatMessage | ChatMessageData) { this.loadData(chat, data); } getContents() { return { content: this.content ? this.content : '', msg_meta: this.msg_meta, msg_interface: this.msg_interface, msg_type: this.msg_type }; } public static isLastMessage(msg: ChatMessage, origin?: string, customKey?: string): boolean { return msg.chat.state.nFromLast(msg, origin, customKey) === 0; } }