37 lines
985 B
TypeScript
Raw Normal View History

import { Reader } from 'protobufjs/minimal';
2021-05-04 11:11:14 +10:00
import { ChatMessageProto } from '../../proto/chat/v2/chat_message';
2021-04-21 15:51:52 +10:00
// TODO: Move to waku library?
export class ChatMessage {
public constructor(
public timestamp: Date,
public nick: string,
public message: string
) {}
static decode(bytes: Uint8Array): ChatMessage {
const protoMsg = ChatMessageProto.decode(Reader.create(bytes));
const timestamp = new Date(protoMsg.timestamp * 1000);
const message = protoMsg.payload
? Array.from(protoMsg.payload)
.map((char) => {
return String.fromCharCode(char);
})
.join('')
: '';
return new ChatMessage(timestamp, protoMsg.nick, message);
}
encode(): Uint8Array {
const timestamp = Math.floor(this.timestamp.valueOf() / 1000);
const payload = Buffer.from(this.message, 'utf-8');
return ChatMessageProto.encode({
timestamp,
nick: this.nick,
payload,
}).finish();
}
}