'use strict';
const EventEmitter = require('events').EventEmitter;
const inherits = require('util').inherits;
/**
* @class
* @classdesc A Message represents a Message in a Channel.
* @property {String} author - The name of the user that authored this Message.
* @property {String} body - The body of the Message.
* @property {Channel} channel - The Channel the Message belongs to.
* @property {Date} dateUpdated - When the Message was updated.
* @property {Number} index - Index of Message in the Channel's messages stream.
* @property {String} lastUpdatedBy - The name of the last user updated this Message.
* @property {String} sid - The server-assigned unique identifier for
* the Message.
* @property {Date} timestamp - When the Message was sent.
* @fires Message#updated
*/
function Message(channel, entityId, data) {
if (!(this instanceof Message)) {
return new Message(channel, entityId, data);
}
var body = data.text;
var dateUpdated = data.dateUpdated ? new Date(data.dateUpdated) : null;
var lastUpdatedBy = data.lastUpdatedBy ? data.lastUpdatedBy : null;
Object.defineProperties(this, {
_body: {
get: () => body,
set: (_body) => body = _body
},
_dateUpdated: {
get: () => dateUpdated,
set: (_dateUpdated) => dateUpdated = _dateUpdated
},
_lastUpdatedBy: {
get: () => lastUpdatedBy,
set: (_lastUpdatedBy) => lastUpdatedBy = _lastUpdatedBy
},
author: {
enumerable: true,
value: data.author
},
body: {
enumerable: true,
get: () => body
},
channel: {
enumerable: true,
value: channel
},
dateUpdated: {
enumerable: true,
get: () => dateUpdated
},
index: {
enumerable: true,
value: parseInt(entityId)
},
lastUpdatedBy: {
enumerable: true,
get: () => lastUpdatedBy
},
sid: {
enumerable: true,
value: data.sid
},
timestamp: {
enumerable: true,
value: new Date(data.timestamp)
}
});
EventEmitter.call(this);
}
inherits(Message, EventEmitter);
Message.prototype._update = function(data) {
let updated = false;
if ((data.text || ((typeof data.text) === 'string')) && data.text !== this._body) {
this._body = data.text;
updated = true;
}
if (data.lastUpdatedBy && data.lastUpdatedBy !== this._lastUpdatedBy) {
this._lastUpdatedBy = data.lastUpdatedBy;
updated = true;
}
if (data.dateUpdated && new Date(data.dateUpdated).getTime() !== this._dateUpdated.getTime()) {
this._dateUpdated = new Date(data.dateUpdated);
updated = true;
}
if (updated) {
this.emit('updated', this);
}
};
/**
* Remove the Message.
* @returns {Promise<Message>}
*/
Message.prototype.remove = function removeMessage() {
return this.channel._session.addCommand('deleteMessage', {
channelSid: this.channel.sid,
messageIdx: this.index.toString()
}).then(()=>this);
};
/**
* Edit message body.
* @param {String} body - new body of Message.
* @returns {Promise<Message>}
*/
Message.prototype.updateBody = function updateMessageBody(body) {
if (typeof body !== 'string') {
throw new Error('Body <String> is a required parameter for updateBody');
}
return this.channel._session.addCommand('editMessage', {
channelSid: this.channel.sid,
messageIdx: this.index.toString(),
text: body
}).then(()=>this);
};
/**
* Fired when the Message's fields have been updated.
* @param {Message} message
* @event Message#updated
*/
module.exports = Message;