diff --git a/src/chat/index.ts b/src/chat/index.ts index d50dfa2f40..b2aef93765 100644 --- a/src/chat/index.ts +++ b/src/chat/index.ts @@ -72,11 +72,12 @@ const ChatContentTopic = 'dingpu'; console.log( `Retrieving archived messages from ${storePeerId.toB58String()}` ); - const msg = await waku.store.queryHistory(storePeerId, [ChatContentTopic]); - msg?.messages.map((msg) => { - const wakuMsg = WakuMessage.fromProto(msg); - if (wakuMsg.payload) { - const chatMsg = ChatMessage.decode(wakuMsg.payload); + const messages = await waku.store.queryHistory(storePeerId, [ + ChatContentTopic, + ]); + messages?.map((msg) => { + if (msg.payload) { + const chatMsg = ChatMessage.decode(msg.payload); printMessage(chatMsg); } }); diff --git a/src/lib/waku_store/index.spec.ts b/src/lib/waku_store/index.spec.ts index a6e1ddaba5..2fe5d8c09b 100644 --- a/src/lib/waku_store/index.spec.ts +++ b/src/lib/waku_store/index.spec.ts @@ -54,17 +54,12 @@ describe('Waku Store', () => { const nimPeerId = await nimWaku.getPeerId(); - const response = await waku.store.queryHistory(nimPeerId); - const messages = response?.messages; + const messages = await waku.store.queryHistory(nimPeerId); expect(messages?.length).eq(2); - const result = messages - ?.map((protoMsg) => { - return WakuMessage.fromProto(protoMsg); - }) - .findIndex((msg) => { - return msg.utf8Payload() === 'Message 0'; - }); + const result = messages?.findIndex((msg) => { + return msg.utf8Payload() === 'Message 0'; + }); expect(result).to.not.eq(-1); }); @@ -82,27 +77,18 @@ describe('Waku Store', () => { const nimPeerId = await nimWaku.getPeerId(); - const response = await waku.store.queryHistory(nimPeerId); - const messages = response?.messages; + const messages = await waku.store.queryHistory(nimPeerId); expect(messages?.length).eq(20); expect( - messages - ?.map((protoMsg) => { - return WakuMessage.fromProto(protoMsg); - }) - .findIndex((msg) => { - return msg.utf8Payload() === 'Message 0'; - }) + messages?.findIndex((msg) => { + return msg.utf8Payload() === 'Message 0'; + }) ).to.not.eq(-1); expect( - messages - ?.map((protoMsg) => { - return WakuMessage.fromProto(protoMsg); - }) - .findIndex((msg) => { - return msg.utf8Payload() === 'Message 19'; - }) + messages?.findIndex((msg) => { + return msg.utf8Payload() === 'Message 19'; + }) ).to.not.eq(-1); }); }); diff --git a/src/lib/waku_store/index.ts b/src/lib/waku_store/index.ts index ce4341a758..a2dae137f6 100644 --- a/src/lib/waku_store/index.ts +++ b/src/lib/waku_store/index.ts @@ -4,6 +4,8 @@ import pipe from 'it-pipe'; import Libp2p from 'libp2p'; import PeerId from 'peer-id'; +import { WakuMessage } from '../waku_message'; + import { HistoryRPC } from './history_rpc'; export const StoreCodec = '/vac/waku/store/2.0.0-beta1'; @@ -17,7 +19,10 @@ export class WakuStore { * @param topics * @throws if not able to reach peer */ - async queryHistory(peerId: PeerId, topics?: string[]) { + async queryHistory( + peerId: PeerId, + topics?: string[] + ): Promise { const peer = this.libp2p.peerStore.get(peerId); if (!peer) throw 'Peer is unknown'; if (!peer.protocols.includes(StoreCodec)) @@ -40,7 +45,15 @@ export class WakuStore { const buf = res.slice(); try { const reply = HistoryRPC.decode(buf); - return reply.response; + + if (!reply.response) { + console.log('No response in HistoryRPC'); + return null; + } + + return reply.response.messages.map((protoMsg) => { + return WakuMessage.fromProto(protoMsg); + }); } catch (err) { console.log('Failed to decode store reply', err); }