import Contact from './Contact' import Attachment from './Attachment' import { Input } from './Input' import { InputSet } from './InputSet' class Message { private to: Contact[] = []; private content: string; private accessToken: string = ''; private sentDate: number = 0; private messageId: string = ''; private from: Contact; private teamToken: string = ''; private accountAddress: string = ''; private references: string[] = []; private subject: string = ''; private type: string = 'message'; private encoding: string = 'plain'; private method: string = ''; private replyTo: string = ''; private inline: boolean = false; private cc: Contact[] = []; private bcc: Contact[] = []; private attachments: Attachment[] = []; private sentBy: string = ''; private inputSet: InputSet; constructor (content: string) { this.content = content; } get SentDate(): number { return this.sentDate } get Content(): string { return this.content } get MessageId(): string { return this.messageId } get References(): string[] { return this.references } get From(): Contact { return this.from } get InputSet(): InputSet { return this.inputSet } get Subject(): string { return this.subject } get Inline(): boolean { return this.inline } get To() { return this.to } get AccessToken() { return this.accessToken } get TeamToken() { return this.teamToken } get AccountAddress() { return this.accountAddress } get Type() { return this.type } get Encoding() { return this.encoding } get Method() { return this.method } get ReplyTo() { return this.replyTo } get CC() { return this.cc } get BCC() { return this.bcc } get SentBy() { return this.sentBy } setSubject (subject: string) { this.subject = subject; return this; } setReceivers (receiver: Contact[]) { this.to = receiver; return this; } setAccessToken (accessToken: string): Message { this.accessToken = accessToken; return this; } setInline (inline: boolean): Message { this.inline = inline; return this; } setSentDate (sentDate: number): Message { this.sentDate = sentDate; return this; } setMessageId (messageId: string): Message { this.messageId = messageId; return this; } setTeamToken (teamToken: string): Message { this.teamToken = teamToken; return this; } setType (type: string): Message { this.type = type; return this; } setAccountAddress (accountAddress: string): Message { this.accountAddress = accountAddress; return this; } setEncoding (encoding: string): Message { this.encoding = encoding; return this; } setMethod (method: string): Message { this.method = method; return this; } setReplyTo (replyTo: string): Message { this.replyTo = replyTo; return this; } setSentBy (sentBy: string): Message { this.sentBy = sentBy; return this; } setFrom (from: Contact): Message { this.from = from; return this; } setCC (cc: Contact[]): Message { this.cc = cc; return this; } setBCC (bcc: Contact[]): Message { this.bcc = bcc; return this; } setReferences (references: string[]): Message { this.references = references; return this; } setAttachments (attachments: Attachment[]): Message { this.attachments = attachments; return this; } setInputSet (inputSet: InputSet) { this.inputSet = inputSet; return this; } getAttachments (): Attachment[] { return this.attachments; } buildCore (token: string, botAddress: string, botName:string): Message { let date = Date.now(); this .setAccessToken(token) .setEncoding('plain') .setSentDate(date) .setMessageId(this.getMessageId(date, botAddress)) .setFrom(new Contact(botAddress).setName(botAddress)) .setAccountAddress(botAddress) .setSentBy(botAddress) return this; } getMessageId(date: number, botAddress: string): string { let id = '<' + date + '.' const chars = '0123456789' for (let i = 0; i < 7; i++) { id += chars.charAt(Math.floor(Math.random() * 10)) } id += this.getHostName(botAddress); return id + '>' } private getHostName (botAddress: string): string { const index = botAddress.indexOf('@') return botAddress.substring(index, botAddress.length) } toJSON (): any { return { access_token: this.accessToken, account_address: this.accountAddress, subject: this.subject, sent_date: this.sentDate, message_id: this.messageId, team_token: this.teamToken, content: this.content, type: this.type, encoding: this.encoding, method: this.method, reply_to: this.replyTo, to: this.to.map((contact: Contact) => contact.toJSON()), from: this.from.toJSON(), cc: this.cc.map((contact: Contact) => contact.toJSON()), bcc: this.bcc.map((contact: Contact) => contact.toJSON()), references: this.references, inline: this.inline, attachments: this.attachments.map((attachment: Attachment) => attachment.toJSON()), sent_by: this.sentBy, input_set: this.inputSet ? this.inputSet.toJSON() : null } } } export default Message;