Change API to directly return array of Waku Messages

This commit is contained in:
Franck Royer 2021-04-13 12:51:04 +10:00
parent 59df437490
commit 1e10eeb5f5
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 32 additions and 32 deletions

View File

@ -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);
}
});

View File

@ -54,15 +54,10 @@ 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) => {
const result = messages?.findIndex((msg) => {
return msg.utf8Payload() === 'Message 0';
});
expect(result).to.not.eq(-1);
@ -82,25 +77,16 @@ 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) => {
messages?.findIndex((msg) => {
return msg.utf8Payload() === 'Message 0';
})
).to.not.eq(-1);
expect(
messages
?.map((protoMsg) => {
return WakuMessage.fromProto(protoMsg);
})
.findIndex((msg) => {
messages?.findIndex((msg) => {
return msg.utf8Payload() === 'Message 19';
})
).to.not.eq(-1);

View File

@ -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<WakuMessage[] | null> {
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);
}