mirror of https://github.com/status-im/js-waku.git
parent
330caa2525
commit
3b7fc44419
|
@ -2,7 +2,7 @@ syntax = "proto3";
|
|||
|
||||
package chat.v2;
|
||||
|
||||
message ChatMessageProto {
|
||||
message ChatMessage {
|
||||
uint64 timestamp = 1;
|
||||
string nick = 2;
|
||||
bytes payload = 3;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Reader } from 'protobufjs/minimal';
|
||||
|
||||
import { ChatMessageProto } from '../../proto/chat/v2/chat_message';
|
||||
import * as proto from '../../proto/chat/v2/chat_message';
|
||||
|
||||
// TODO: Move to waku library?
|
||||
export class ChatMessage {
|
||||
|
@ -11,7 +11,7 @@ export class ChatMessage {
|
|||
) {}
|
||||
|
||||
static decode(bytes: Uint8Array): ChatMessage {
|
||||
const protoMsg = ChatMessageProto.decode(Reader.create(bytes));
|
||||
const protoMsg = proto.ChatMessage.decode(Reader.create(bytes));
|
||||
const timestamp = new Date(protoMsg.timestamp * 1000);
|
||||
const message = protoMsg.payload
|
||||
? Array.from(protoMsg.payload)
|
||||
|
@ -27,7 +27,7 @@ export class ChatMessage {
|
|||
const timestamp = Math.floor(this.timestamp.valueOf() / 1000);
|
||||
const payload = Buffer.from(this.message, 'utf-8');
|
||||
|
||||
return ChatMessageProto.encode({
|
||||
return proto.ChatMessage.encode({
|
||||
timestamp,
|
||||
nick: this.nick,
|
||||
payload,
|
||||
|
|
|
@ -4,17 +4,17 @@ import _m0 from 'protobufjs/minimal';
|
|||
|
||||
export const protobufPackage = 'chat.v2';
|
||||
|
||||
export interface ChatMessageProto {
|
||||
export interface ChatMessage {
|
||||
timestamp: number;
|
||||
nick: string;
|
||||
payload: Uint8Array;
|
||||
}
|
||||
|
||||
const baseChatMessageProto: object = { timestamp: 0, nick: '' };
|
||||
const baseChatMessage: object = { timestamp: 0, nick: '' };
|
||||
|
||||
export const ChatMessageProto = {
|
||||
export const ChatMessage = {
|
||||
encode(
|
||||
message: ChatMessageProto,
|
||||
message: ChatMessage,
|
||||
writer: _m0.Writer = _m0.Writer.create()
|
||||
): _m0.Writer {
|
||||
if (message.timestamp !== 0) {
|
||||
|
@ -29,10 +29,10 @@ export const ChatMessageProto = {
|
|||
return writer;
|
||||
},
|
||||
|
||||
decode(input: _m0.Reader | Uint8Array, length?: number): ChatMessageProto {
|
||||
decode(input: _m0.Reader | Uint8Array, length?: number): ChatMessage {
|
||||
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
|
||||
let end = length === undefined ? reader.len : reader.pos + length;
|
||||
const message = { ...baseChatMessageProto } as ChatMessageProto;
|
||||
const message = { ...baseChatMessage } as ChatMessage;
|
||||
message.payload = new Uint8Array();
|
||||
while (reader.pos < end) {
|
||||
const tag = reader.uint32();
|
||||
|
@ -54,8 +54,8 @@ export const ChatMessageProto = {
|
|||
return message;
|
||||
},
|
||||
|
||||
fromJSON(object: any): ChatMessageProto {
|
||||
const message = { ...baseChatMessageProto } as ChatMessageProto;
|
||||
fromJSON(object: any): ChatMessage {
|
||||
const message = { ...baseChatMessage } as ChatMessage;
|
||||
message.payload = new Uint8Array();
|
||||
if (object.timestamp !== undefined && object.timestamp !== null) {
|
||||
message.timestamp = Number(object.timestamp);
|
||||
|
@ -73,7 +73,7 @@ export const ChatMessageProto = {
|
|||
return message;
|
||||
},
|
||||
|
||||
toJSON(message: ChatMessageProto): unknown {
|
||||
toJSON(message: ChatMessage): unknown {
|
||||
const obj: any = {};
|
||||
message.timestamp !== undefined && (obj.timestamp = message.timestamp);
|
||||
message.nick !== undefined && (obj.nick = message.nick);
|
||||
|
@ -84,8 +84,8 @@ export const ChatMessageProto = {
|
|||
return obj;
|
||||
},
|
||||
|
||||
fromPartial(object: DeepPartial<ChatMessageProto>): ChatMessageProto {
|
||||
const message = { ...baseChatMessageProto } as ChatMessageProto;
|
||||
fromPartial(object: DeepPartial<ChatMessage>): ChatMessage {
|
||||
const message = { ...baseChatMessage } as ChatMessage;
|
||||
if (object.timestamp !== undefined && object.timestamp !== null) {
|
||||
message.timestamp = object.timestamp;
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue