60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
/**
|
|
* @description Base class for all messages
|
|
* @since 1.0
|
|
* @author MiTask
|
|
* @module Message
|
|
*/
|
|
class Message {
|
|
type: string;
|
|
peerName: string;
|
|
peerAvatar: string;
|
|
topic: string | null;
|
|
timestamp: number;
|
|
|
|
/**
|
|
* @since 1.0
|
|
* @author MiTask
|
|
* @constructor
|
|
* @param messageType Type of the message (text, file, audio, icon)
|
|
* @param peerName Peer username
|
|
* @param peerAvatar Peer avatar URL
|
|
* @param topic Chat room topic string
|
|
* @param timestamp UNIX Timestamp
|
|
*/
|
|
constructor(messageType: string, peerName: string, peerAvatar: string, topic: string | null, timestamp: number) {
|
|
this.type = messageType;
|
|
this.peerName = peerName;
|
|
this.peerAvatar = peerAvatar;
|
|
this.topic = topic;
|
|
this.timestamp = timestamp;
|
|
}
|
|
|
|
/**
|
|
* @since 1.0
|
|
* @author MiTask
|
|
* @returns JSON Object with all of the information about the message
|
|
*/
|
|
protected toJson(): {name: String, topic: String, avatar: String, type: String, timestamp: Number} {
|
|
return {
|
|
type: this.type,
|
|
name: this.peerName,
|
|
avatar: this.peerAvatar,
|
|
topic: this.topic ? this.topic : "",
|
|
timestamp: this.timestamp
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @since 1.0
|
|
* @author MiTask
|
|
* @returns {string} JSON String with all of the information about the message
|
|
*/
|
|
toJsonString(): string {
|
|
return JSON.stringify({
|
|
...this.toJson()
|
|
});
|
|
}
|
|
}
|
|
|
|
export default Message;
|