mirror of
https://github.com/logos-messaging/examples.waku.org.git
synced 2026-01-02 12:53:08 +00:00
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { MessageV0 } from "@waku/core/lib/waku_message/version_0";
|
|
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: MessageV0): 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", 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;
|
|
}
|
|
}
|