mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-10 17:53:09 +00:00
It is a type used for the [TOY-CHAT](https://rfc.vac.dev/spec/22/) protocol; js-waku users should not build on top if this toy protocol and instead design message data structures appropriate to their use case.
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
import { Reader } from 'protobufjs/minimal';
|
|
|
|
import * as proto from './proto/chat_message';
|
|
|
|
/**
|
|
* ChatMessage is used by the various show case waku apps that demonstrates
|
|
* waku used as the network layer for chat group applications.
|
|
*
|
|
* This is included to help building PoC and MVPs. Apps that aim to be
|
|
* production ready should use a more appropriate data structure.
|
|
*/
|
|
export class ChatMessage {
|
|
public constructor(public proto: proto.ChatMessage) {}
|
|
|
|
/**
|
|
* Create Chat Message with a utf-8 string as payload.
|
|
*/
|
|
static fromUtf8String(
|
|
timestamp: Date,
|
|
nick: string,
|
|
text: string
|
|
): ChatMessage {
|
|
const timestampNumber = Math.floor(timestamp.valueOf() / 1000);
|
|
const payload = Buffer.from(text, 'utf-8');
|
|
|
|
return new ChatMessage({
|
|
timestamp: timestampNumber,
|
|
nick,
|
|
payload,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Decode a protobuf payload to a ChatMessage.
|
|
* @param bytes The payload to decode.
|
|
*/
|
|
static decode(bytes: Uint8Array): ChatMessage {
|
|
const protoMsg = proto.ChatMessage.decode(Reader.create(bytes));
|
|
return new ChatMessage(protoMsg);
|
|
}
|
|
|
|
/**
|
|
* Encode this ChatMessage to a byte array, to be used as a protobuf payload.
|
|
* @returns The encoded payload.
|
|
*/
|
|
encode(): Uint8Array {
|
|
return proto.ChatMessage.encode(this.proto).finish();
|
|
}
|
|
|
|
get timestamp(): Date {
|
|
return new Date(this.proto.timestamp * 1000);
|
|
}
|
|
|
|
get nick(): string {
|
|
return this.proto.nick;
|
|
}
|
|
|
|
get payloadAsUtf8(): string {
|
|
if (!this.proto.payload) {
|
|
return '';
|
|
}
|
|
|
|
return Buffer.from(this.proto.payload).toString('utf-8');
|
|
}
|
|
}
|