Move `contentTopics` out the `WakuStore.queryHistory`'s optional params

This commit is contained in:
Franck Royer 2021-08-04 11:59:53 +10:00
parent 55a36f2263
commit b422c9a10b
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
6 changed files with 36 additions and 40 deletions

View File

@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- **Breaking**: The `WakuMessage` APIs have been changed to move `contentTopic` out of the optional parameters. - **Breaking**: The `WakuMessage` APIs have been changed to move `contentTopic` out of the optional parameters.
- **Breaking**: Move `contentTopics` out the `WakuStore.queryHistory`'s optional parameters.
### Removed ### Removed
- Examples (web-chat): Remove broken `/fleet` command. - Examples (web-chat): Remove broken `/fleet` command.

View File

@ -118,20 +118,19 @@ Query a waku store peer to check historical messages:
```ts ```ts
// Process messages once they are all retrieved // Process messages once they are all retrieved
const messages = await waku.store.queryHistory({ contentTopics: ["/my-cool-app/1/my-use-case/proto"] }); const messages = await waku.store.queryHistory(['/my-cool-app/1/my-use-case/proto']);
messages.forEach((msg) => { messages.forEach((msg) => {
console.log("Message retrieved:", msg.payloadAsUtf8) console.log('Message retrieved:', msg.payloadAsUtf8);
}) });
// Or, pass a callback function to be executed as pages are received: // Or, pass a callback function to be executed as pages are received:
waku.store.queryHistory({ waku.store.queryHistory(['/my-cool-app/1/my-use-case/proto'], {
contentTopics: ["/my-cool-app/1/my-use-case/proto"], callback: (messages) => {
callback: (messages) => { messages.forEach((msg) => {
messages.forEach((msg) => { console.log('Message retrieved:', msg.payloadAsUtf8);
console.log("Message retrieved:", msg.payloadAsUtf8); });
}); }
} });
});
``` ```
### Encryption & Signature ### Encryption & Signature
@ -212,9 +211,8 @@ Keys can be removed using `WakuMessage.deleteDecryptionKey`.
##### Waku Store ##### Waku Store
```ts ```ts
const messages = await waku.store.queryHistory({ const messages = await waku.store.queryHistory([], {
contentTopics: [], decryptionKeys: [privateKey, symKey]
decryptionKeys: [privateKey, symKey],
}); });
``` ```
@ -228,20 +226,20 @@ In the case where your app does not need encryption then you could use symmetric
Signature keys can be generated the same way asymmetric keys for encryption are: Signature keys can be generated the same way asymmetric keys for encryption are:
```ts ```ts
import { generatePrivateKey, getPublicKey, WakuMessage } from "js-waku"; import { generatePrivateKey, getPublicKey, WakuMessage } from 'js-waku';
const signPrivateKey = generatePrivateKey(); const signPrivateKey = generatePrivateKey();
// Asymmetric Encryption // Asymmetric Encryption
const message1 = await WakuMessage.fromBytes(payload, myAppContentTopic, { const message1 = await WakuMessage.fromBytes(payload, myAppContentTopic, {
encPublicKey: recipientPublicKey, encPublicKey: recipientPublicKey,
sigPrivKey: signPrivateKey, sigPrivKey: signPrivateKey
}); });
// Symmetric Encryption // Symmetric Encryption
const message2 = await WakuMessage.fromBytes(payload, myAppContentTopic, { const message2 = await WakuMessage.fromBytes(payload, myAppContentTopic, {
encPublicKey: symKey, encPublicKey: symKey,
sigPrivKey: signPrivateKey, sigPrivKey: signPrivateKey
}); });
``` ```

View File

@ -62,8 +62,7 @@ async function retrieveStoreMessages(
setArchivedMessages(messages); setArchivedMessages(messages);
}; };
const res = await waku.store.queryHistory({ const res = await waku.store.queryHistory([ChatContentTopic], {
contentTopics: [ChatContentTopic],
pageSize: 5, pageSize: 5,
direction: Direction.FORWARD, direction: Direction.FORWARD,
callback, callback,

View File

@ -8,7 +8,7 @@ export enum Direction {
FORWARD = 'forward', FORWARD = 'forward',
} }
export interface Options { export interface Params {
contentTopics: string[]; contentTopics: string[];
cursor?: proto.Index; cursor?: proto.Index;
pubsubTopic: string; pubsubTopic: string;
@ -22,22 +22,22 @@ export class HistoryRPC {
/** /**
* Create History Query. * Create History Query.
*/ */
static createQuery(options: Options): HistoryRPC { static createQuery(params: Params): HistoryRPC {
const direction = directionToProto(options.direction); const direction = directionToProto(params.direction);
const pagingInfo = { const pagingInfo = {
pageSize: options.pageSize, pageSize: params.pageSize,
cursor: options.cursor, cursor: params.cursor,
direction, direction,
}; };
const contentFilters = options.contentTopics.map((contentTopic) => { const contentFilters = params.contentTopics.map((contentTopic) => {
return { contentTopic }; return { contentTopic };
}); });
return new HistoryRPC({ return new HistoryRPC({
requestId: uuid(), requestId: uuid(),
query: { query: {
pubsubTopic: options.pubsubTopic, pubsubTopic: params.pubsubTopic,
contentFilters, contentFilters,
pagingInfo, pagingInfo,
startTime: undefined, startTime: undefined,

View File

@ -55,9 +55,7 @@ describe('Waku Store', () => {
waku.libp2p.peerStore.once('change:protocols', resolve); waku.libp2p.peerStore.once('change:protocols', resolve);
}); });
const messages = await waku.store.queryHistory({ const messages = await waku.store.queryHistory([]);
contentTopics: [],
});
expect(messages?.length).eq(2); expect(messages?.length).eq(2);
const result = messages?.findIndex((msg) => { const result = messages?.findIndex((msg) => {
@ -91,8 +89,7 @@ describe('Waku Store', () => {
waku.libp2p.peerStore.once('change:protocols', resolve); waku.libp2p.peerStore.once('change:protocols', resolve);
}); });
const messages = await waku.store.queryHistory({ const messages = await waku.store.queryHistory([], {
contentTopics: [],
direction: Direction.FORWARD, direction: Direction.FORWARD,
}); });
@ -136,9 +133,8 @@ describe('Waku Store', () => {
const nimPeerId = await nimWaku.getPeerId(); const nimPeerId = await nimWaku.getPeerId();
const messages = await waku.store.queryHistory({ const messages = await waku.store.queryHistory([], {
peerId: nimPeerId, peerId: nimPeerId,
contentTopics: [],
}); });
expect(messages?.length).eq(2); expect(messages?.length).eq(2);
@ -237,8 +233,7 @@ describe('Waku Store', () => {
} }
dbg('Retrieve messages from store'); dbg('Retrieve messages from store');
const messages = await waku2.store.queryHistory({ const messages = await waku2.store.queryHistory([], {
contentTopics: [],
decryptionKeys: [privateKey, symKey], decryptionKeys: [privateKey, symKey],
}); });

View File

@ -33,7 +33,6 @@ export interface CreateOptions {
export interface QueryOptions { export interface QueryOptions {
peerId?: PeerId; peerId?: PeerId;
contentTopics: string[];
pubsubTopic?: string; pubsubTopic?: string;
direction?: Direction; direction?: Direction;
pageSize?: number; pageSize?: number;
@ -58,10 +57,10 @@ export class WakuStore {
/** /**
* Query given peer using Waku Store. * Query given peer using Waku Store.
* *
* @param contentTopics The content topics to pass to the query, leave empty to
* retrieve all messages.
* @param options * @param options
* @param options.peerId The peer to query.Options * @param options.peerId The peer to query.Options
* @param options.contentTopics The content topics to pass to the query, leave empty to
* retrieve all messages.
* @param options.pubsubTopic The pubsub topic to pass to the query. Defaults * @param options.pubsubTopic The pubsub topic to pass to the query. Defaults
* to the value set at creation. See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/). * to the value set at creation. See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/).
* @param options.callback Callback called on page of stored messages as they are retrieved * @param options.callback Callback called on page of stored messages as they are retrieved
@ -70,14 +69,18 @@ export class WakuStore {
* methods. * methods.
* @throws If not able to reach the peer to query. * @throws If not able to reach the peer to query.
*/ */
async queryHistory(options: QueryOptions): Promise<WakuMessage[] | null> { async queryHistory(
contentTopics: string[],
options?: QueryOptions
): Promise<WakuMessage[] | null> {
const opts = Object.assign( const opts = Object.assign(
{ {
pubsubTopic: this.pubsubTopic, pubsubTopic: this.pubsubTopic,
direction: Direction.BACKWARD, direction: Direction.BACKWARD,
pageSize: 10, pageSize: 10,
}, },
options options,
{ contentTopics }
); );
dbg('Querying history with the following options', options); dbg('Querying history with the following options', options);