2021-10-26 00:56:27 -04:00
|
|
|
'use strict';
|
|
|
|
|
2022-02-23 02:18:17 -05:00
|
|
|
const {padutils: {warnDeprecated}} = require('./pad_utils');
|
|
|
|
|
2021-10-26 00:56:27 -04:00
|
|
|
/**
|
|
|
|
* Represents a chat message stored in the database and transmitted among users. Plugins can extend
|
|
|
|
* the object with additional properties.
|
|
|
|
*
|
|
|
|
* Supports serialization to JSON.
|
|
|
|
*/
|
|
|
|
class ChatMessage {
|
|
|
|
static fromObject(obj) {
|
2022-02-26 18:51:08 -05:00
|
|
|
// The userId property was renamed to authorId, and userName was renamed to displayName. Accept
|
|
|
|
// the old names in case the db record was written by an older version of Etherpad.
|
|
|
|
obj = Object.assign({}, obj); // Don't mutate the caller's object.
|
|
|
|
if ('userId' in obj && !('authorId' in obj)) obj.authorId = obj.userId;
|
|
|
|
delete obj.userId;
|
|
|
|
if ('userName' in obj && !('displayName' in obj)) obj.displayName = obj.userName;
|
|
|
|
delete obj.userName;
|
2021-10-26 00:56:27 -04:00
|
|
|
return Object.assign(new ChatMessage(), obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {?string} [text] - Initial value of the `text` property.
|
2021-10-26 01:05:24 -04:00
|
|
|
* @param {?string} [authorId] - Initial value of the `authorId` property.
|
2021-10-26 00:56:27 -04:00
|
|
|
* @param {?number} [time] - Initial value of the `time` property.
|
|
|
|
*/
|
2021-10-26 01:05:24 -04:00
|
|
|
constructor(text = null, authorId = null, time = null) {
|
2021-10-26 00:56:27 -04:00
|
|
|
/**
|
|
|
|
* The raw text of the user's chat message (before any rendering or processing).
|
|
|
|
*
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
this.text = text;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's author ID.
|
|
|
|
*
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
2021-10-26 01:05:24 -04:00
|
|
|
this.authorId = authorId;
|
2021-10-26 00:56:27 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The message's timestamp, as milliseconds since epoch.
|
|
|
|
*
|
|
|
|
* @type {?number}
|
|
|
|
*/
|
|
|
|
this.time = time;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user's display name.
|
|
|
|
*
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
2021-10-26 01:05:24 -04:00
|
|
|
this.displayName = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias of `authorId`, for compatibility with old plugins.
|
|
|
|
*
|
|
|
|
* @deprecated Use `authorId` instead.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2022-02-23 02:18:17 -05:00
|
|
|
get userId() {
|
|
|
|
warnDeprecated('ChatMessage.userId property is deprecated; use .authorId instead');
|
|
|
|
return this.authorId;
|
|
|
|
}
|
|
|
|
set userId(val) {
|
|
|
|
warnDeprecated('ChatMessage.userId property is deprecated; use .authorId instead');
|
|
|
|
this.authorId = val;
|
|
|
|
}
|
2021-10-26 01:05:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias of `displayName`, for compatibility with old plugins.
|
|
|
|
*
|
|
|
|
* @deprecated Use `displayName` instead.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2022-02-23 02:18:17 -05:00
|
|
|
get userName() {
|
|
|
|
warnDeprecated('ChatMessage.userName property is deprecated; use .displayName instead');
|
|
|
|
return this.displayName;
|
|
|
|
}
|
|
|
|
set userName(val) {
|
|
|
|
warnDeprecated('ChatMessage.userName property is deprecated; use .displayName instead');
|
|
|
|
this.displayName = val;
|
|
|
|
}
|
2021-10-26 01:05:24 -04:00
|
|
|
|
|
|
|
// TODO: Delete this method once users are unlikely to roll back to a version of Etherpad that
|
|
|
|
// doesn't support authorId and displayName.
|
|
|
|
toJSON() {
|
2022-02-26 20:35:17 -05:00
|
|
|
const {authorId, displayName, ...obj} = this;
|
|
|
|
obj.userId = authorId;
|
|
|
|
obj.userName = displayName;
|
|
|
|
return obj;
|
2021-10-26 00:56:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ChatMessage;
|