Franck Royer ad9d629daa
ChatMessage has been moved from js-waku to web-chat example
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.
2021-08-06 17:25:12 +10:00

49 lines
1.1 KiB
TypeScript

import { WakuMessage } from 'js-waku';
import { ChatMessage } from './chat_message';
export class Message {
public chatMessage: ChatMessage;
// WakuMessage timestamp
public sentTimestamp: Date | undefined;
constructor(chatMessage: ChatMessage, sentTimestamp: Date | undefined) {
this.chatMessage = chatMessage;
this.sentTimestamp = sentTimestamp;
}
static fromWakuMessage(wakuMsg: WakuMessage): Message | undefined {
if (wakuMsg.payload) {
try {
const chatMsg = ChatMessage.decode(wakuMsg.payload);
if (chatMsg) {
return new Message(chatMsg, wakuMsg.timestamp);
}
} catch (e) {
console.error(
'Failed to decode chat message',
wakuMsg.payloadAsUtf8,
e
);
}
}
return;
}
static fromUtf8String(nick: string, text: string): Message {
const now = new Date();
return new Message(ChatMessage.fromUtf8String(now, nick, text), now);
}
get nick() {
return this.chatMessage.nick;
}
get timestamp() {
return this.chatMessage.timestamp;
}
get payloadAsUtf8() {
return this.chatMessage.payloadAsUtf8;
}
}